EventAnalysis  1.2.2
ObjectMap.h
Go to the documentation of this file.
1 /*
2  * ObjectMap.h
3  *
4  * Created on: 06 Feb 2017
5  * Author: Nicola Mori
6  */
7 
10 #ifndef OBJECTMAP_H_
11 #define OBJECTMAP_H_
12 
13 // EventAnalysis headers
14 #include "core/InsertionResult.h"
15 #include "core/ObjPtr.h"
16 #include "core/ObjectWrapper.h"
17 #include "core/ObserverPtr.h"
18 
19 // C/C++ headers
20 #include <algorithm>
21 #include <typeindex>
22 #include <unordered_map>
23 #include <vector>
24 
25 namespace EA {
26 
32 class ObjectMap {
33 
34 public:
36  ~ObjectMap();
37 
46  template <class DataType> InsertionResult AddObject(const std::string &name, DataType &obj);
47 
56  template <class DataType> InsertionResult AddObject(const std::string &name, observer_ptr<DataType> obj);
57 
67  InsertionResult AddObject(const std::string &name, const char *obj);
68 
77  template <class DataType> InsertionResult AddObject(const std::string &name, std::shared_ptr<DataType> obj);
78 
87  template <class DataType> InsertionResult AddObject(const std::string &name, std::unique_ptr<DataType> obj);
88 
106  InsertionResult SetAlias(const std::string &objName, const std::string &objAlias);
107 
117  template <class DataType> ObjPtr<DataType> GetObject(const std::string &name);
118 
127  ObjectWrapper GetObjectWrapper(const std::string &objName);
128 
135  const std::string &GetNameOfAliasedObject(const std::string &aliasName);
136 
146  bool RemoveObject(const std::string &name, bool removeAliased = false);
147 
152  std::vector<std::string> GetObjects() const;
153 
159  bool IsPresent(const std::string &nameOrAlias) const;
160 
166  std::string GetAlias(const std::string &name) const;
167 
172  std::vector<std::string> GetAliases() const;
173 
180  bool Clean();
181 
188  bool CleanAliases();
189 
195  std::string GetObjectClass(const std::string &objName);
196 
202  bool IsAlias(const std::string &objName);
203 
204 protected:
214  InsertionResult AddObject(const std::string &name, ObjectWrapper &&wrapper);
215 
216 private:
217  using Map = std::unordered_map<std::string, ObjectWrapper>;
219  struct Alias {
220  std::string objName, aliasName;
221  std::type_index typeIndex;
222  };
223  using Aliases = std::vector<Alias>;
225 
226  Aliases::iterator _FindAlias(const std::string &aliasName) {
227  return std::find_if(_aliases.begin(), _aliases.end(),
228  [&aliasName](const Alias alias) { return (aliasName == alias.aliasName); });
229  }
230 
231  std::vector<Aliases::iterator> _FindAliasesOf(const std::string &objName) {
232  std::vector<Aliases::iterator> retValue;
233  auto aliasIter = _aliases.begin();
234  while (aliasIter != _aliases.end()) {
235  aliasIter = std::find_if(_aliases.begin(), _aliases.end(),
236  [&objName](const Alias alias) { return (objName == alias.objName); });
237  if (aliasIter != _aliases.end()) {
238  retValue.push_back(aliasIter);
239  }
240  }
241  return retValue;
242  }
243 };
244 
245 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
246 
247 template <class DataType> InsertionResult ObjectMap::AddObject(const std::string &name, DataType &obj) {
248  if (_FindAlias(name) != _aliases.end()) {
250  }
251 
252  // Avoid move insertion to be able to use the wrapper if the insertion fails
253  // TODO: use try_emplace when C++17 will be available
254  ObjectWrapper wrapper{observer_ptr<DataType>(&obj)};
255  std::pair<Map::iterator, bool> insertResult =
256  _map.insert(std::pair<const std::string &, ObjectWrapper>(name, wrapper));
257  if (insertResult.second) {
259  } else {
260  if (insertResult.first->second == wrapper) {
262  } else {
264  }
265  }
266 }
267 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
268 
269 template <class DataType> InsertionResult ObjectMap::AddObject(const std::string &name, observer_ptr<DataType> obj) {
270  if (_FindAlias(name) != _aliases.end()) {
272  }
273 
274  // Avoid move insertion to be able to use the wrapper if the insertion fails
275  // TODO: use try_emplace when C++17 will be available
276  ObjectWrapper wrapper{obj};
277  std::pair<Map::iterator, bool> insertResult =
278  _map.insert(std::pair<const std::string &, ObjectWrapper>(name, wrapper));
279  if (insertResult.second) {
281  } else {
282  // The inserted object is already present. Check if it is exactly the same
283  if (insertResult.first->second == wrapper) {
285  } else {
287  }
288  }
289 }
290 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
291 
292 template <class DataType> InsertionResult ObjectMap::AddObject(const std::string &name, std::shared_ptr<DataType> obj) {
293  if (_FindAlias(name) != _aliases.end()) {
295  }
296 
297  // Avoid move insertion to be able to use the wrapper if the insertion fails
298  // TODO: use try_emplace when C++17 will be available
299  ObjectWrapper wrapper{obj};
300  std::pair<Map::iterator, bool> insertResult =
301  _map.insert(std::pair<const std::string &, ObjectWrapper>(name, wrapper));
302  if (insertResult.second) {
304  } else {
305  // The inserted object is already present. Check if it is exactly the same
306  if (insertResult.first->second == wrapper) {
308  } else {
310  }
311  }
312 }
313 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
314 
315 template <class DataType> InsertionResult ObjectMap::AddObject(const std::string &name, std::unique_ptr<DataType> obj) {
316  if (_FindAlias(name) != _aliases.end()) {
318  }
319 
320  // Avoid move insertion to be able to use the wrapper if the insertion fails
321  // TODO: use try_emplace when C++17 will be available
322  ObjectWrapper wrapper{std::move(obj)};
323  std::pair<Map::iterator, bool> insertResult =
324  _map.insert(std::pair<const std::string &, ObjectWrapper>(name, wrapper));
325  if (insertResult.second) {
327  } else {
328  // The inserted object is already present. Check if it is exactly the same
329  if (insertResult.first->second == wrapper) {
331  } else {
333  }
334  }
335 }
336 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
337 
338 template <class DataType> ObjPtr<DataType> ObjectMap::GetObject(const std::string &name) {
339 
340  Map::iterator iter;
341  auto alias = _FindAlias(name);
342  if (alias != _aliases.end()) {
343  // "name" is an alias, so search for the aliased object
344  iter = _map.find(alias->objName);
345  } else {
346  // "name" is the name of the object
347  iter = _map.find(name);
348  }
349  if (iter == _map.end()) {
350  ObjPtr<DataType> objPtr;
352  return objPtr; //
353  }
354 
355  ObjPtr<DataType> objPtr(wrapper_cast<DataType>(iter->second));
356  if (!objPtr) {
358  } else {
360  }
361  return objPtr;
362 }
363 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
364 
365 } // namespace EA
366 
367 #endif /* OBJECTMAP_H_ */
Class describing a pointer to an object retrieved from an ObjectMap.
Definition: ObjPtr.h:31
bool IsPresent(const std::string &nameOrAlias) const
Checks if an object is present in the map.
Definition: ObjectMap.cpp:123
bool CleanAliases()
Removes all the aliases from the map.
Definition: ObjectMap.cpp:138
Known object but not available at the moment.
InsertionResult SetAlias(const std::string &objName, const std::string &objAlias)
Set an alias for the given object.
Definition: ObjectMap.cpp:152
std::vector< Aliases::iterator > _FindAliasesOf(const std::string &objName)
Definition: ObjectMap.h:231
bool Clean()
Removes all the objects and aliases from the map.
Definition: ObjectMap.cpp:133
const std::string & GetNameOfAliasedObject(const std::string &aliasName)
Gets the name of the object corresponding to an alias.
Definition: ObjectMap.cpp:70
ObjectWrapper GetObjectWrapper(const std::string &objName)
Get a wrapper for a given object.
Definition: ObjectMap.cpp:52
std::string aliasName
Definition: ObjectMap.h:220
Definition: ObjectMap.h:219
std::string objName
Definition: ObjectMap.h:220
bool IsAlias(const std::string &objName)
Checks if an object name is actually an alias.
Definition: ObjectMap.cpp:203
IncludeFileExc.h IncludeFileExc class declaration.
Definition: Algorithm.h:21
std::type_index typeIndex
Definition: ObjectMap.h:221
Aliases _aliases
Definition: ObjectMap.h:224
A map between strings and objects of ObjectWrapper kind.
Definition: ObjectMap.h:32
Aliases::iterator _FindAlias(const std::string &aliasName)
Definition: ObjectMap.h:226
std::string GetAlias(const std::string &name) const
Get the alias of an object.
Definition: ObjectMap.cpp:184
Generic wrapper class.
Definition: ObjectWrapper.h:28
std::vector< std::string > GetObjects() const
Returns the names of the objects in the map.
Definition: ObjectMap.cpp:114
std::vector< Alias > Aliases
Definition: ObjectMap.h:223
InsertionResult AddObject(const std::string &name, DataType &obj)
Adds an object to the map.
Definition: ObjectMap.h:247
InsertionResult
Definition: InsertionResult.h:16
bool RemoveObject(const std::string &name, bool removeAliased=false)
Removes an object or an alias from the map.
Definition: ObjectMap.cpp:79
std::vector< std::string > GetAliases() const
Returns the aliases of the objects in the map.
Definition: ObjectMap.cpp:194
Object successfully inserted or already present.
ObjPtr< DataType > GetObject(const std::string &name)
Retrieves an object given its name.
Definition: ObjectMap.h:338
std::string GetObjectClass(const std::string &objName)
Get the class name of an object in the store.
Definition: ObjectMap.cpp:143
std::unordered_map< std::string, ObjectWrapper > Map
Definition: ObjectMap.h:217
Map _map
Definition: ObjectMap.h:218
~ObjectMap()
Destructor.
Definition: ObjectMap.cpp:14
Object found but class does not match with requested.
Another object with the same name already present.
RetrievalResult _result
Definition: ObjPtr.h:114