EventAnalysis  1.1.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  std::string GetAlias(const std::string &name) const;
160 
165  std::vector<std::string> GetAliases() const;
166 
173  bool Clean();
174 
181  bool CleanAliases();
182 
188  std::string GetObjectClass(const std::string &objName);
189 
195  bool IsAlias(const std::string &objName);
196 
197 protected:
207  InsertionResult AddObject(const std::string &name, ObjectWrapper &&wrapper);
208 
209 private:
210  using Map = std::unordered_map<std::string, ObjectWrapper>;
212  struct Alias {
213  std::string objName, aliasName;
214  std::type_index typeIndex;
215  };
216  using Aliases = std::vector<Alias>;
218 
219  Aliases::iterator _FindAlias(const std::string &aliasName) {
220  return std::find_if(_aliases.begin(), _aliases.end(),
221  [&aliasName](const Alias alias) { return (aliasName == alias.aliasName); });
222  }
223 
224  std::vector<Aliases::iterator> _FindAliasesOf(const std::string &objName) {
225  std::vector<Aliases::iterator> retValue;
226  auto aliasIter = _aliases.begin();
227  while (aliasIter != _aliases.end()) {
228  aliasIter = std::find_if(_aliases.begin(), _aliases.end(),
229  [&objName](const Alias alias) { return (objName == alias.objName); });
230  if (aliasIter != _aliases.end()) {
231  retValue.push_back(aliasIter);
232  }
233  }
234  return retValue;
235  }
236 };
237 
238 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
239 
240 template <class DataType> InsertionResult ObjectMap::AddObject(const std::string &name, DataType &obj) {
241  if (_FindAlias(name) != _aliases.end()) {
243  }
244  std::pair<Map::iterator, bool> insertResult =
245  _map.insert(std::pair<const std::string &, ObjectWrapper>(name, ObjectWrapper(observer_ptr<DataType>(&obj))));
246  if (insertResult.second) {
248  } else {
250  }
251 }
252 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
253 
254 template <class DataType> InsertionResult ObjectMap::AddObject(const std::string &name, observer_ptr<DataType> obj) {
255  if (_FindAlias(name) != _aliases.end()) {
257  }
258  std::pair<Map::iterator, bool> insertResult =
259  _map.insert(std::pair<const std::string &, ObjectWrapper>(name, ObjectWrapper(obj)));
260  if (insertResult.second) {
262  } else {
264  }
265 }
266 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
267 
268 template <class DataType> InsertionResult ObjectMap::AddObject(const std::string &name, std::shared_ptr<DataType> obj) {
269  if (_FindAlias(name) != _aliases.end()) {
271  }
272  std::pair<Map::iterator, bool> insertResult =
273  _map.insert(std::pair<const std::string &, ObjectWrapper>(name, ObjectWrapper(obj)));
274  if (insertResult.second) {
276  } else {
278  }
279 }
280 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
281 
282 template <class DataType> InsertionResult ObjectMap::AddObject(const std::string &name, std::unique_ptr<DataType> obj) {
283  if (_FindAlias(name) != _aliases.end()) {
285  }
286  std::pair<Map::iterator, bool> insertResult =
287  _map.insert(std::pair<const std::string &, ObjectWrapper>(name, ObjectWrapper(std::move(obj))));
288  if (insertResult.second) {
290  } else {
292  }
293 }
294 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
295 
296 template <class DataType> ObjPtr<DataType> ObjectMap::GetObject(const std::string &name) {
297 
298  Map::iterator iter;
299  auto alias = _FindAlias(name);
300  if (alias != _aliases.end()) {
301  // "name" is an alias, so search for the aliased object
302  iter = _map.find(alias->objName);
303  } else {
304  // "name" is the name of the object
305  iter = _map.find(name);
306  }
307  if (iter == _map.end()) {
308  ObjPtr<DataType> objPtr;
310  return objPtr; //
311  }
312 
313  ObjPtr<DataType> objPtr(wrapper_cast<DataType>(iter->second));
314  if (!objPtr) {
316  } else {
318  }
319  return objPtr;
320 }
321 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
322 
323 } // namespace EA
324 
325 #endif /* OBJECTMAP_H_ */
Class describing a pointer to an object retrieved from an ObjectMap.
Definition: ObjPtr.h:31
bool CleanAliases()
Removes all the aliases from the map.
Definition: ObjectMap.cpp:118
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:132
std::vector< Aliases::iterator > _FindAliasesOf(const std::string &objName)
Definition: ObjectMap.h:224
bool Clean()
Removes all the objects and aliases from the map.
Definition: ObjectMap.cpp:113
const std::string & GetNameOfAliasedObject(const std::string &aliasName)
Gets the name of the object corresponding to an alias.
Definition: ObjectMap.cpp:60
ObjectWrapper GetObjectWrapper(const std::string &objName)
Get a wrapper for a given object.
Definition: ObjectMap.cpp:42
std::string aliasName
Definition: ObjectMap.h:213
Definition: ObjectMap.h:212
std::string objName
Definition: ObjectMap.h:213
bool IsAlias(const std::string &objName)
Checks if an object name is actually an alias.
Definition: ObjectMap.cpp:183
IncludeFileExc.h IncludeFileExc class declaration.
Definition: Algorithm.h:21
std::type_index typeIndex
Definition: ObjectMap.h:214
Aliases _aliases
Definition: ObjectMap.h:217
A map between strings and objects of ObjectWrapper kind.
Definition: ObjectMap.h:32
Aliases::iterator _FindAlias(const std::string &aliasName)
Definition: ObjectMap.h:219
std::string GetAlias(const std::string &name) const
Get the alias of an object.
Definition: ObjectMap.cpp:164
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:104
std::vector< Alias > Aliases
Definition: ObjectMap.h:216
InsertionResult AddObject(const std::string &name, DataType &obj)
Adds an object to the map.
Definition: ObjectMap.h:240
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:69
std::vector< std::string > GetAliases() const
Returns the aliases of the objects in the map.
Definition: ObjectMap.cpp:174
ObjPtr< DataType > GetObject(const std::string &name)
Retrieves an object given its name.
Definition: ObjectMap.h:296
std::string GetObjectClass(const std::string &objName)
Get the class name of an object in the store.
Definition: ObjectMap.cpp:123
std::unordered_map< std::string, ObjectWrapper > Map
Definition: ObjectMap.h:210
Map _map
Definition: ObjectMap.h:211
~ObjectMap()
Destructor.
Definition: ObjectMap.cpp:14
Object found but class does not match with requested.
Object already present.
RetrievalResult _result
Definition: ObjPtr.h:114