HOPS
HOPS class reference
MHO_ClassIdentityMap.hh
Go to the documentation of this file.
1 #ifndef MHO_ClassIdentityMap_HH__
2 #define MHO_ClassIdentityMap_HH__
3 
5 #include "MHO_ClassIdentity.hh"
9 #include "MHO_UUID.hh"
10 
11 #include <iostream>
12 #include <map>
13 #include <string>
14 
15 namespace hops
16 {
17 
30 {
31  public:
33 
35  {
36  for(auto it = fFactoryMap.begin(); it != fFactoryMap.end(); it++)
37  {
38  if(it->second)
39  {
40  delete it->second;
41  it->second = nullptr;
42  }
43  }
44 
45  for(auto it = fJSONConverterMap.begin(); it != fJSONConverterMap.end(); it++)
46  {
47  if(it->second)
48  {
49  delete it->second;
50  it->second = nullptr;
51  }
52  }
53  };
54 
60  template< typename XClassType > void AddClassType()
61  {
63  std::string name = MHO_ClassIdentity::ClassName< XClassType >();
64  fMD5Generator << name;
67  AddToMap(type_uuid, name);
68  //add a factory for these types of objects
69  auto it = fFactoryMap.find(type_uuid);
70  if(it == fFactoryMap.end())
71  {
73  }
74 
75  auto it2 = fJSONConverterMap.find(type_uuid);
76  if(it2 == fJSONConverterMap.end())
77  {
79  }
80  };
81 
88  template< typename XClassType > void AddClassType(const XClassType& obj)
89  {
91  std::string name = MHO_ClassIdentity::ClassName(obj);
92  fMD5Generator << name;
95  AddToMap(type_uuid, name);
96  //add a factory for these types of objects
97  auto it = fFactoryMap.find(type_uuid);
98  if(it == fFactoryMap.end())
99  {
101  }
102 
103  auto it2 = fJSONConverterMap.find(type_uuid);
104  if(it2 == fJSONConverterMap.end())
105  {
107  }
108  };
109 
116  template< typename XClassType > std::string GetClassNameFromObject(const XClassType& obj)
117  {
118  std::string name = MHO_ClassIdentity::ClassName(obj);
119  return name;
120  };
121 
128  std::string GetClassNameFromUUID(const MHO_UUID& uuid) const
129  {
130  std::map< MHO_UUID, std::string >::const_iterator it = fUUID2ClassName.find(uuid);
131  if(it != fUUID2ClassName.end())
132  {
133  return std::string(it->second);
134  }
135  else
136  {
137  return std::string("unknown");
138  }
139  };
140 
147  MHO_UUID GetUUIDFromClassName(const std::string& name) const
148  {
149  MHO_UUID tmp;
150  const auto it = fClassName2UUID.find(name);
151  if(it != fClassName2UUID.end())
152  {
153  tmp = it->second;
154  }
155  return tmp;
156  };
157 
163  template< typename XClassType > MHO_UUID GetUUIDFor() const
164  {
165  std::string name = MHO_ClassIdentity::ClassName< XClassType >();
166  return GetUUIDFromClassName(name);
167  }
168 
175  bool IsTypePresent(const MHO_UUID& uuid) const
176  {
177  auto it = fUUID2ClassName.find(uuid);
178  if(it != fUUID2ClassName.end())
179  {
180  return true;
181  }
182  return false;
183  }
184 
195  {
196  auto it = fFactoryMap.find(uuid);
197  if(it != fFactoryMap.end())
198  {
199  return fFactoryMap[uuid]->Build();
200  }
201  else
202  {
203  return nullptr;
204  }
205  }
206 
207  protected:
214  void AddToMap(const MHO_UUID& type_uuid, const std::string& name)
215  {
216  fUUID2ClassName[type_uuid] = name;
217  fClassName2UUID[name] = type_uuid;
218  };
219 
221  std::map< MHO_UUID, std::string > fUUID2ClassName;
222  std::map< std::string, MHO_UUID > fClassName2UUID;
223  std::map< MHO_UUID, MHO_SerializableObjectFactory* > fFactoryMap;
224  std::map< MHO_UUID, MHO_JSONConverter* > fJSONConverterMap;
225 };
226 
227 } // namespace hops
228 
229 #endif
Class MHO_ClassIdentityMap.
Definition: MHO_ClassIdentityMap.hh:30
void AddClassType(const XClassType &obj)
Function AddClassType, overload provided for passing object reference.
Definition: MHO_ClassIdentityMap.hh:88
std::string GetClassNameFromObject(const XClassType &obj)
Getter for class name from object.
Definition: MHO_ClassIdentityMap.hh:116
std::map< MHO_UUID, MHO_JSONConverter * > fJSONConverterMap
Definition: MHO_ClassIdentityMap.hh:224
MHO_UUID GetUUIDFromClassName(const std::string &name) const
Getter for uuid from class name.
Definition: MHO_ClassIdentityMap.hh:147
MHO_UUID GetUUIDFor() const
Getter for uuid for a class type.
Definition: MHO_ClassIdentityMap.hh:163
std::map< MHO_UUID, std::string > fUUID2ClassName
Definition: MHO_ClassIdentityMap.hh:221
MHO_MD5HashGenerator fMD5Generator
Definition: MHO_ClassIdentityMap.hh:218
std::map< MHO_UUID, MHO_SerializableObjectFactory * > fFactoryMap
Definition: MHO_ClassIdentityMap.hh:223
void AddToMap(const MHO_UUID &type_uuid, const std::string &name)
Adds an entry to the UUID-name map and its reverse map.
Definition: MHO_ClassIdentityMap.hh:214
std::map< std::string, MHO_UUID > fClassName2UUID
Definition: MHO_ClassIdentityMap.hh:222
void AddClassType()
Function AddClassType, adds a class of a particular type to the identity map.
Definition: MHO_ClassIdentityMap.hh:60
std::string GetClassNameFromUUID(const MHO_UUID &uuid) const
Getter for class name from uuid.
Definition: MHO_ClassIdentityMap.hh:128
virtual ~MHO_ClassIdentityMap()
Definition: MHO_ClassIdentityMap.hh:34
bool IsTypePresent(const MHO_UUID &uuid) const
Checks if a UUID is present in the class name map.
Definition: MHO_ClassIdentityMap.hh:175
MHO_ClassIdentityMap()
Definition: MHO_ClassIdentityMap.hh:32
MHO_Serializable * GenerateContainerFromUUID(const MHO_UUID &uuid)
Generates a container from a UUID and returns a ptr to MHO_Serializable if found in factory map.
Definition: MHO_ClassIdentityMap.hh:194
Class MHO_ContainerJSONConverter.
Definition: MHO_ContainerJSONConverter.hh:195
Class MHO_MD5HashGenerator declares the concrete class which does the work for file streams.
Definition: MHO_MD5HashGenerator.hh:134
void Finalize()
Finalizes the MD5 hash calculation and stores the result in fDigest.
Definition: MHO_MD5HashGenerator.hh:155
MHO_UUID GetDigestAsUUID()
Getter for digest as uuid.
Definition: MHO_MD5HashGenerator.hh:181
void Initialize()
Recursive function to initialize a game state.
Definition: MHO_MD5HashGenerator.hh:143
Class MHO_SerializableObjectFactorySpecific.
Definition: MHO_SerializableObjectFactory.hh:68
Class MHO_Serializable.
Definition: MHO_Serializable.hh:26
Class MHO_UUID - a class for a 16 byte UUID (for object and type identification)
Definition: MHO_UUID.hh:27
Definition: MHO_AdhocFlagging.hh:18
static std::string ClassName()
Returns the class name as a string.
Definition: MHO_ClassIdentity.hh:51