MxEngine
LifetimeManager.h
1 // Copyright(c) 2019 - 2020, #Momo
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met :
6 //
7 // 1. Redistributions of source code must retain the above copyright notice, this
8 // list of conditions and the following disclaimer.
9 //
10 // 2. Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and /or other materials provided with the distribution.
13 //
14 // 3. Neither the name of the copyright holder nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 // OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 
30 #pragma once
31 
32 #include "Utilities/STL/MxHashMap.h"
33 #include "Utilities/STL/MxVector.h"
34 #include "Utilities/STL/MxString.h"
35 
36 #include "Utilities/Logger/Logger.h"
37 #include "Utilities/Format/Format.h"
38 
39 namespace MxEngine
40 {
48  template<
49  typename ValueType,
50  typename Id = MxString,
51  typename ValueStorage = MxHashMap<Id, ValueType>,
52  typename IdStorage = MxVector<Id>
53  >
55  {
59  ValueStorage storage;
63  ValueStorage toAdd;
67  IdStorage toRemove;
68 
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())
78  {
79  container.emplace_back(std::forward<IdT>(key), std::forward<ValueT>(value));
80  }
81 
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())
91  {
92  container.emplace(std::forward<IdT>(key), std::forward<ValueT>(value));
93  }
94 
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())
104  {
105  return container.find(std::forward<IdT>(key)) != container.end();
106  }
107 
114  template<typename T, typename IdT>
115  auto ExistsImpl(T& container, IdT&& key, long) ->
116  decltype(bool())
117  {
118  auto it = std::find_if(container.begin(), container.end(),
119  [&key](const auto& p)
120  {
121  return p.first == key;
122  });
123  return it != container.end();
124  }
125 
131  template<typename T>
132  auto FreeRemoveQueueImpl(T& container, int) ->
133  decltype(std::declval<T>().erase(std::declval<Id>()), void())
134  {
135  for (const auto& id : this->toRemove)
136  {
137  Logger::Instance().Debug("MxEngine::LifetimeManager", MxFormat("deleting object: {0}", id));
138  this->storage.erase(id);
139  }
140  this->toRemove.clear();
141  }
142 
148  template<typename T>
149  auto FreeRemoveQueueImpl(T& container, long) ->
150  decltype(void())
151  {
152  for (const auto& id : this->toRemove)
153  {
154  auto it = std::find_if(this->storage.begin(), this->storage.end(),
155  [&id](const auto& p)
156  {
157  return p.first == id;
158  });
159  if (it != this->storage.end())
160  {
161  Logger::Instance().Debug("MxEngine::LifetimeManager", Format(FMT_STRING("deleting object: {0}"), id));
162  this->storage.erase(it);
163  }
164  }
165  this->toRemove.clear();
166  }
167 
173  template<typename T>
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())
179  ), void())
180  {
181  if (!this->toAdd.empty())
182  {
183  this->storage.insert(
184  this->storage.end(),
185  std::make_move_iterator(this->toAdd.begin()),
186  std::make_move_iterator(this->toAdd.end())
187  );
188  this->toAdd.clear();
189  }
190  }
191 
197  template<typename T>
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())
202  ), void())
203  {
204  if (!this->toAdd.empty())
205  {
206  this->storage.insert(
207  std::make_move_iterator(this->toAdd.begin()),
208  std::make_move_iterator(this->toAdd.end())
209  );
210  this->toAdd.clear();
211  }
212  }
213 
214  public:
215  using Value = ValueType;
216  using Key = Id;
217  using Storage = ValueStorage;
218 
225  template<typename IdT, typename ValueT>
226  void Add(IdT&& key, ValueT&& value)
227  {
228  this->AddImpl(this->toAdd, std::forward<IdT>(key), std::forward<ValueT>(value), 0);
229  }
230 
235  ValueStorage& GetElements()
236  {
237  return this->storage;
238  }
239 
244  const ValueStorage& GetElements() const
245  {
246  return this->storage;
247  }
248 
254  template<typename IdT>
255  void Remove(IdT&& key)
256  {
257  this->toRemove.emplace_back(std::forward<IdT>(key));
258  }
259 
265  template<typename IdT>
266  bool Exists(IdT&& key)
267  {
268  this->Update();
269  return this->ExistsImpl(this->storage, std::forward<IdT>(key), 0);
270  }
271 
276  void Update()
277  {
278  this->FreeRemoveQueueImpl(this->storage, 0);
279  this->FreeAddQueueImpl(this->storage, 0);
280  }
281  };
282 }
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