29 #ifndef THOR_OWNERSHIPMODELS_HPP
30 #define THOR_OWNERSHIPMODELS_HPP
34 #include <Aurora/Meta/Templates.hpp>
76 template <
typename Map>
80 typename Map::iterator itr;
83 template <
typename Map>
84 ElementRef<Map> makeElementRef(Map& map,
typename Map::iterator itr)
87 ElementRef<Map> ref = {&map, itr};
91 template <
typename R,
typename Map>
92 struct TrackingDeleter
94 void operator() (R* pointer)
97 if (!tracker.expired())
100 assert(element.map->find(element.itr->first) != element.map->end());
101 element.map->erase(element.itr);
105 AURORA_REQUIRE_COMPLETE_TYPE(R);
109 ElementRef<Map> element;
110 std::weak_ptr<char> tracker;
114 template <
typename Model,
typename R>
115 struct OwnershipModel;
118 template <
typename R>
119 struct OwnershipModel<Resources::CentralOwner, R>
122 typedef const R& ConstReturned;
123 typedef std::unique_ptr<R> Loaded;
124 typedef std::unique_ptr<R> Stored;
126 static Returned makeReturned(
const std::unique_ptr<R>& initialOrStorage)
128 return *initialOrStorage;
131 template <
typename Map>
132 static std::unique_ptr<R> makeLoaded(std::unique_ptr<R>&& resource, ElementRef<Map>&&)
134 return std::move(resource);
137 static std::unique_ptr<R> makeStored(std::unique_ptr<R>&& loaded)
139 return std::move(loaded);
144 template <
typename R>
145 struct OwnershipModel<Resources::RefCounted, R>
147 typedef std::shared_ptr<R> Returned;
148 typedef std::shared_ptr<const R> ConstReturned;
154 std::shared_ptr<char> tracked;
155 std::shared_ptr<R> resource;
162 std::shared_ptr<char> tracked;
163 std::weak_ptr<R> resource;
166 static std::shared_ptr<R> makeReturned(
const Loaded& loaded)
168 return loaded.resource;
171 static std::shared_ptr<R> makeReturned(
const Stored& stored)
173 assert(!stored.resource.expired());
174 return std::shared_ptr<R>(stored.resource);
177 template <
typename Map>
178 static Loaded makeLoaded(std::unique_ptr<R>&& resource, ElementRef<Map>&& element)
182 auto tracked = std::make_shared<char>();
186 TrackingDeleter<R, Map> deleter;
187 deleter.tracker = tracked;
188 deleter.element = element;
192 loaded.tracked = std::move(tracked);
193 loaded.resource = std::shared_ptr<R>(resource.release(), deleter);
197 static Stored makeStored(Loaded&& loaded)
200 stored.tracked = std::move(loaded.tracked);
201 stored.resource = std::weak_ptr<R>(loaded.resource);
209 #endif // THOR_OWNERSHIPMODELS_HPP
Definition: Animator.hpp:42
Configuration header of the library.
Centralized ownership policy.
Definition: OwnershipModels.hpp:56
Reference-counted ownership policy.
Definition: OwnershipModels.hpp:64