32 #include "Utilities/STL/MxHashMap.h" 33 #include "Utilities/STL/MxVector.h" 34 #include "Utilities/STL/MxString.h" 36 #include "Utilities/Logger/Logger.h" 37 #include "Utilities/Format/Format.h" 50 typename Id = MxString,
51 typename ValueStorage = MxHashMap<Id, ValueType>,
52 typename IdStorage = MxVector<Id>
75 template<
typename T,
typename IdT,
typename ValueT>
76 auto AddImpl(T& container, IdT&& key, ValueT&& value,
int) ->
77 decltype(std::declval<T>().emplace_back(std::declval<IdT>(), std::declval<ValueT>()),
void())
79 container.emplace_back(std::forward<IdT>(key), std::forward<ValueT>(value));
88 template<
typename T,
typename IdT,
typename ValueT>
89 auto AddImpl(T& container, IdT&& key, ValueT&& value,
long) ->
90 decltype(std::declval<T>().emplace(std::declval<IdT>(), std::declval<ValueT>()),
void())
92 container.emplace(std::forward<IdT>(key), std::forward<ValueT>(value));
101 template<
typename T,
typename IdT>
102 auto ExistsImpl(T& container, IdT&& key,
int) ->
103 decltype(std::declval<T>().find(key) != std::declval<T>().end(),
bool())
105 return container.find(std::forward<IdT>(key)) != container.end();
114 template<
typename T,
typename IdT>
115 auto ExistsImpl(T& container, IdT&& key,
long) ->
118 auto it = std::find_if(container.begin(), container.end(),
119 [&key](
const auto& p)
121 return p.first == key;
123 return it != container.end();
132 auto FreeRemoveQueueImpl(T& container,
int) ->
133 decltype(std::declval<T>().erase(std::declval<Id>()),
void())
135 for (
const auto&
id : this->toRemove)
137 Logger::Instance().Debug(
"MxEngine::LifetimeManager", MxFormat(
"deleting object: {0}",
id));
138 this->storage.erase(
id);
140 this->toRemove.clear();
149 auto FreeRemoveQueueImpl(T& container,
long) ->
152 for (
const auto&
id : this->toRemove)
154 auto it = std::find_if(this->storage.begin(), this->storage.end(),
157 return p.first == id;
159 if (it != this->storage.end())
162 this->storage.erase(it);
165 this->toRemove.clear();
174 auto FreeAddQueueImpl(T& container,
int) ->
175 decltype(std::declval<T>().insert(
176 std::declval<T>().end(),
177 std::make_move_iterator(std::declval<ValueStorage>().begin()),
178 std::make_move_iterator(std::declval<ValueStorage>().end())
181 if (!this->toAdd.empty())
183 this->storage.insert(
185 std::make_move_iterator(this->toAdd.begin()),
186 std::make_move_iterator(this->toAdd.end())
198 auto FreeAddQueueImpl(T& container,
long) ->
199 decltype(std::declval<T>().insert(
200 std::make_move_iterator(std::declval<ValueStorage>().begin()),
201 std::make_move_iterator(std::declval<ValueStorage>().end())
204 if (!this->toAdd.empty())
206 this->storage.insert(
207 std::make_move_iterator(this->toAdd.begin()),
208 std::make_move_iterator(this->toAdd.end())
215 using Value = ValueType;
217 using Storage = ValueStorage;
225 template<
typename IdT,
typename ValueT>
226 void Add(IdT&& key, ValueT&& value)
228 this->AddImpl(this->toAdd, std::forward<IdT>(key), std::forward<ValueT>(value), 0);
237 return this->storage;
246 return this->storage;
254 template<
typename IdT>
257 this->toRemove.emplace_back(std::forward<IdT>(key));
265 template<
typename IdT>
269 return this->ExistsImpl(this->storage, std::forward<IdT>(key), 0);
278 this->FreeRemoveQueueImpl(this->storage, 0);
279 this->FreeAddQueueImpl(this->storage, 0);
Definition: LifetimeManager.h:54
ValueStorage & GetElements()
Definition: LifetimeManager.h:235
const ValueStorage & GetElements() const
Definition: LifetimeManager.h:244
void Remove(IdT &&key)
Definition: LifetimeManager.h:255
static T & Instance()
Definition: SingletonHolder.h:80
void Add(IdT &&key, ValueT &&value)
Definition: LifetimeManager.h:226
void Update()
Definition: LifetimeManager.h:276
std::basic_string< Char > Format(const S &formatStr, Args &&... args)
Definition: Format.h:43
bool Exists(IdT &&key)
Definition: LifetimeManager.h:266
Definition: Application.cpp:49