25 #include <type_traits>
31 namespace _unique_id_impl {
38 template<
typename ResourceTypePtr,
bool IsSharedPtr>
41 template<
typename ResourceTypePtr>
51 ResourceTypePtr as_pointer()
const {
56 template<
typename ResourceTypePtr>
58 typedef std::weak_ptr<typename ResourceTypePtr::element_type> weak_ptr_type;
69 ResourceTypePtr as_pointer()
const {
76 template<
typename ResourceTypePtr>
79 typedef ResourceTypePtr resource_pointer_type;
80 typedef typename ResourceTypePtr::element_type element_type;
82 explicit operator bool()
const {
96 members_(other.members_.ptr) {
101 void _bind(resource_pointer_type ptr) {
105 ResourceTypePtr fetch()
const {
106 auto ret = members_.as_pointer();
111 T* fetch_as()
const {
113 return dynamic_cast<T*
>(&(*fetch()));
116 bool is_bound()
const {
117 return bool(members_.ptr);
120 explicit UniqueID(uint32_t
id, ResourceTypePtr ptr):
127 return this->id_ == other.id_;
131 template<typename=std::enable_if<_unique_id_impl::is_shared_ptr<resource_pointer_type>::value>>
132 UniqueID(
const resource_pointer_type& pointer) {
133 this->id_ = pointer->id().id_;
137 template<typename=std::enable_if<_unique_id_impl::is_shared_ptr<resource_pointer_type>::value>>
138 UniqueID& operator=(
const resource_pointer_type& pointer) {
139 this->id_ = pointer->id().id_;
151 this->id_ = other.id_;
152 this->members_.ptr = other.members_.ptr;
157 return this->id_ < other.id_;
161 return !(*
this == other);
165 return o <<
typeid(ResourceTypePtr).name() <<
":" << instance.value();
168 uint32_t value()
const {
return id_; }
181 typedef std::pair<std::type_index, uint32_t> UniqueIDKey;
183 template<
typename ID>
184 UniqueIDKey make_unique_id_key(
const ID&
id) {
185 return std::make_pair(
186 std::type_index(
typeid(
typename ID::element_type)),
191 template<
typename UniqueID>
192 UniqueID make_unique_id_from_key(
const UniqueIDKey& key) {
193 assert(key.first == std::type_index(
typeid(
typename UniqueID::element_type)));
194 return UniqueID(key.second);
201 template<
typename ResourcePtrType>
202 struct hash<
smlt::UniqueID<ResourcePtrType> > {
204 hash<uint32_t> make_hash;
205 return make_hash(
id.value());