EventAnalysis  1.3.0
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 
170  std::vector<std::string> GetAliases(const std::string &name = "") const;
171 
178  bool Clean();
179 
186  bool CleanAliases();
187 
193  std::string GetObjectClass(const std::string &objName);
194 
200  bool IsAlias(const std::string &objName);
201 
202 protected:
212  InsertionResult AddObject(const std::string &name, ObjectWrapper &&wrapper);
213 
214 private:
215  using Map = std::unordered_map<std::string, ObjectWrapper>;
217  struct Alias {
218  std::string objName, aliasName;
219  std::type_index typeIndex;
220  };
221  using Aliases = std::vector<Alias>;
223 
224  Aliases::iterator _FindAlias(const std::string &aliasName) {
225  return std::find_if(_aliases.begin(), _aliases.end(),
226  [&aliasName](const Alias alias) { return (aliasName == alias.aliasName); });
227  }
228 
229  std::vector<Aliases::iterator> _FindAliasesOf(const std::string &objName) {
230  std::vector<Aliases::iterator> retValue;
231  auto aliasIter = _aliases.begin();
232  while (aliasIter != _aliases.end()) {
233  aliasIter = std::find_if(_aliases.begin(), _aliases.end(),
234  [&objName](const Alias alias) { return (objName == alias.objName); });
235  if (aliasIter != _aliases.end()) {
236  retValue.push_back(aliasIter);
237  }
238  }
239  return retValue;
240  }
241 };
242 
243 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
244 
245 template <class DataType> InsertionResult ObjectMap::AddObject(const std::string &name, DataType &obj) {
246  if (_FindAlias(name) != _aliases.end()) {
248  }
249 
250  // Avoid move insertion to be able to use the wrapper if the insertion fails
251  // TODO: use try_emplace when C++17 will be available
252  ObjectWrapper wrapper{observer_ptr<DataType>(&obj)};
253  std::pair<Map::iterator, bool> insertResult =
254  _map.insert(std::pair<const std::string &, ObjectWrapper>(name, wrapper));
255  if (insertResult.second) {
257  } else {
258  if (insertResult.first->second == wrapper) {
260  } else {
262  }
263  }
264 }
265 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
266 
267 template <class DataType> InsertionResult ObjectMap::AddObject(const std::string &name, observer_ptr<DataType> obj) {
268  if (_FindAlias(name) != _aliases.end()) {
270  }
271 
272  // Avoid move insertion to be able to use the wrapper if the insertion fails
273  // TODO: use try_emplace when C++17 will be available
274  ObjectWrapper wrapper{obj};
275  std::pair<Map::iterator, bool> insertResult =
276  _map.insert(std::pair<const std::string &, ObjectWrapper>(name, wrapper));
277  if (insertResult.second) {
279  } else {
280  // The inserted object is already present. Check if it is exactly the same
281  if (insertResult.first->second == wrapper) {
283  } else {
285  }
286  }
287 }
288 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
289 
290 template <class DataType> InsertionResult ObjectMap::AddObject(const std::string &name, std::shared_ptr<DataType> obj) {
291  if (_FindAlias(name) != _aliases.end()) {
293  }
294 
295  // Avoid move insertion to be able to use the wrapper if the insertion fails
296  // TODO: use try_emplace when C++17 will be available
297  ObjectWrapper wrapper{obj};
298  std::pair<Map::iterator, bool> insertResult =
299  _map.insert(std::pair<const std::string &, ObjectWrapper>(name, wrapper));
300  if (insertResult.second) {
302  } else {
303  // The inserted object is already present. Check if it is exactly the same
304  if (insertResult.first->second == wrapper) {
306  } else {
308  }
309  }
310 }
311 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
312 
313 template <class DataType> InsertionResult ObjectMap::AddObject(const std::string &name, std::unique_ptr<DataType> obj) {
314  if (_FindAlias(name) != _aliases.end()) {
316  }
317 
318  // Avoid move insertion to be able to use the wrapper if the insertion fails
319  // TODO: use try_emplace when C++17 will be available
320  ObjectWrapper wrapper{std::move(obj)};
321  std::pair<Map::iterator, bool> insertResult =
322  _map.insert(std::pair<const std::string &, ObjectWrapper>(name, wrapper));
323  if (insertResult.second) {
325  } else {
326  // The inserted object is already present. Check if it is exactly the same
327  if (insertResult.first->second == wrapper) {
329  } else {
331  }
332  }
333 }
334 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
335 
336 template <class DataType> ObjPtr<DataType> ObjectMap::GetObject(const std::string &name) {
337 
338  Map::iterator iter;
339  auto alias = _FindAlias(name);
340  if (alias != _aliases.end()) {
341  // "name" is an alias, so search for the aliased object
342  iter = _map.find(alias->objName);
343  } else {
344  // "name" is the name of the object
345  iter = _map.find(name);
346  }
347  if (iter == _map.end()) {
348  ObjPtr<DataType> objPtr;
350  return objPtr; //
351  }
352 
353  ObjPtr<DataType> objPtr(wrapper_cast<DataType>(iter->second));
354  if (!objPtr) {
356  } else {
358  }
359  return objPtr;
360 }
361 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
362 
363 } // namespace EA
364 
365 #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:229
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:218
Definition: ObjectMap.h:217
std::string objName
Definition: ObjectMap.h:218
bool IsAlias(const std::string &objName)
Checks if an object name is actually an alias.
Definition: ObjectMap.cpp:195
IncludeFileExc.h IncludeFileExc class declaration.
Definition: Algorithm.h:21
std::type_index typeIndex
Definition: ObjectMap.h:219
Aliases _aliases
Definition: ObjectMap.h:222
A map between strings and objects of ObjectWrapper kind.
Definition: ObjectMap.h:32
Aliases::iterator _FindAlias(const std::string &aliasName)
Definition: ObjectMap.h:224
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:221
InsertionResult AddObject(const std::string &name, DataType &obj)
Adds an object to the map.
Definition: ObjectMap.h:245
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 std::string &name="") const
Returns the aliases of the objects in the map.
Definition: ObjectMap.cpp:184
Object successfully inserted or already present.
ObjPtr< DataType > GetObject(const std::string &name)
Retrieves an object given its name.
Definition: ObjectMap.h:336
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:215
Map _map
Definition: ObjectMap.h:216
~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