EventAnalysis  1.0.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 
113  std::string GetAlias(const std::string &name) const;
114 
124  template <class DataType> ObjPtr<DataType> GetObject(const std::string &name);
125 
134  ObjectWrapper GetObjectWrapper(const std::string &objName);
135 
142  const std::string &GetNameOfAliasedObject(const std::string &aliasName);
143 
153  bool RemoveObject(const std::string &name, bool removeAliased = false);
154 
162  std::vector<observer_ptr<const std::string>> &GetListOfObjects();
163 
170  bool Clean();
171 
178  bool CleanAliases();
179 
185  std::string GetObjectClass(const std::string &objName);
186 
192  bool IsAlias(const std::string &objName);
193 
194 protected:
204  InsertionResult AddObject(const std::string &name, ObjectWrapper &&wrapper);
205 
206 private:
207  using Map = std::unordered_map<std::string, ObjectWrapper>;
208  std::vector<observer_ptr<const std::string>> _objList;
210  struct Alias {
211  std::string objName, aliasName;
212  std::type_index typeIndex;
213  };
214  using Aliases = std::vector<Alias>;
216 
217  Aliases::iterator _FindAlias(const std::string &aliasName) {
218  return std::find_if(_aliases.begin(), _aliases.end(),
219  [&aliasName](const Alias alias) { return (aliasName == alias.aliasName); });
220  }
221 
222  std::vector<Aliases::iterator> _FindAliasesOf(const std::string &objName) {
223  std::vector<Aliases::iterator> retValue;
224  auto aliasIter = _aliases.begin();
225  while (aliasIter != _aliases.end()) {
226  aliasIter = std::find_if(_aliases.begin(), _aliases.end(),
227  [&objName](const Alias alias) { return (objName == alias.objName); });
228  if (aliasIter != _aliases.end()) {
229  retValue.push_back(aliasIter);
230  }
231  }
232  return retValue;
233  }
234 };
235 
236 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
237 
238 template <class DataType> InsertionResult ObjectMap::AddObject(const std::string &name, DataType &obj) {
239  if (_FindAlias(name) != _aliases.end()) {
241  }
242  std::pair<Map::iterator, bool> insertResult =
243  _map.insert(std::pair<const std::string &, ObjectWrapper>(name, ObjectWrapper(observer_ptr<DataType>(&obj))));
244  if (insertResult.second) {
245  _objList.push_back(&(insertResult.first->first));
247  } else {
249  }
250 }
251 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
252 
253 template <class DataType> InsertionResult ObjectMap::AddObject(const std::string &name, observer_ptr<DataType> obj) {
254  if (_FindAlias(name) != _aliases.end()) {
256  }
257  std::pair<Map::iterator, bool> insertResult =
258  _map.insert(std::pair<const std::string &, ObjectWrapper>(name, ObjectWrapper(obj)));
259  if (insertResult.second) {
260  _objList.push_back(&(insertResult.first->first));
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) {
275  _objList.push_back(&(insertResult.first->first));
277  } else {
279  }
280 }
281 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
282 
283 template <class DataType> InsertionResult ObjectMap::AddObject(const std::string &name, std::unique_ptr<DataType> obj) {
284  if (_FindAlias(name) != _aliases.end()) {
286  }
287  std::pair<Map::iterator, bool> insertResult =
288  _map.insert(std::pair<const std::string &, ObjectWrapper>(name, ObjectWrapper(std::move(obj))));
289  if (insertResult.second) {
290  _objList.push_back(&(insertResult.first->first));
292  } else {
294  }
295 }
296 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
297 
298 template <class DataType> ObjPtr<DataType> ObjectMap::GetObject(const std::string &name) {
299 
300  Map::iterator iter;
301  auto alias = _FindAlias(name);
302  if (alias != _aliases.end()) {
303  // "name" is an alias, so search for the aliased object
304  iter = _map.find(alias->objName);
305  } else {
306  // "name" is the name of the object
307  iter = _map.find(name);
308  }
309  if (iter == _map.end()) {
310  ObjPtr<DataType> objPtr;
312  return objPtr; //
313  }
314 
315  ObjPtr<DataType> objPtr(wrapper_cast<DataType>(iter->second));
316  if (!objPtr) {
318  } else {
320  }
321  return objPtr;
322 }
323 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
324 
325 } // namespace EA
326 
327 #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:121
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:135
std::vector< Aliases::iterator > _FindAliasesOf(const std::string &objName)
Definition: ObjectMap.h:222
bool Clean()
Removes all the objects and aliases from the map.
Definition: ObjectMap.cpp:114
const std::string & GetNameOfAliasedObject(const std::string &aliasName)
Gets the name of the object corresponding to an alias.
Definition: ObjectMap.cpp:62
ObjectWrapper GetObjectWrapper(const std::string &objName)
Get a wrapper for a given object.
Definition: ObjectMap.cpp:44
std::string aliasName
Definition: ObjectMap.h:211
Definition: ObjectMap.h:210
std::string objName
Definition: ObjectMap.h:211
bool IsAlias(const std::string &objName)
Checks if an object name is actually an alias.
Definition: ObjectMap.cpp:177
IncludeFileExc.h IncludeFileExc class declaration.
Definition: Algorithm.h:21
std::type_index typeIndex
Definition: ObjectMap.h:212
Aliases _aliases
Definition: ObjectMap.h:215
A map between strings and objects of ObjectWrapper kind.
Definition: ObjectMap.h:32
Aliases::iterator _FindAlias(const std::string &aliasName)
Definition: ObjectMap.h:217
std::string GetAlias(const std::string &name) const
Get the alias of an object.
Definition: ObjectMap.cpp:167
Generic wrapper class.
Definition: ObjectWrapper.h:28
std::vector< Alias > Aliases
Definition: ObjectMap.h:214
InsertionResult AddObject(const std::string &name, DataType &obj)
Adds an object to the map.
Definition: ObjectMap.h:238
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:71
ObjPtr< DataType > GetObject(const std::string &name)
Retrieves an object given its name.
Definition: ObjectMap.h:298
std::string GetObjectClass(const std::string &objName)
Get the class name of an object in the store.
Definition: ObjectMap.cpp:126
std::unordered_map< std::string, ObjectWrapper > Map
Definition: ObjectMap.h:207
std::vector< observer_ptr< const std::string > > & GetListOfObjects()
Returns a list of the objects in the map.
Definition: ObjectMap.cpp:112
Map _map
Definition: ObjectMap.h:209
std::vector< observer_ptr< const std::string > > _objList
Definition: ObjectMap.h:208
~ObjectMap()
Destructor.
Definition: ObjectMap.cpp:14
Object found but class does not match with requested.
Object already present.
RetrievalResult _result
Definition: ObjPtr.h:114