HOPS
HOPS class reference
MHO_ContainerStore.hh
Go to the documentation of this file.
1 #ifndef MHO_ContainerStore_HH__
2 #define MHO_ContainerStore_HH__
3 
4 #include <map>
5 #include <set>
6 #include <utility>
7 #include <vector>
8 
10 #include "MHO_Message.hh"
11 #include "MHO_Serializable.hh"
12 #include "MHO_Types.hh"
13 #include "MHO_UUID.hh"
14 #include "MHO_UUIDGenerator.hh"
15 
16 namespace hops
17 {
18 
32 {
33  public:
35 
36  virtual ~MHO_ContainerStore() { Clear(); };
37 
41  void Clear();
42 
50  template< typename XClassType > bool AddObject(XClassType* obj);
51 
58  template< typename XClassType > XClassType* GetObject(const MHO_UUID& obj_id);
59 
67  template< typename XClassType > XClassType* GetObject(const std::string& shortname);
68 
76  template< typename XClassType > XClassType* GetObject(std::size_t index);
77 
85  template< typename XClassType > bool DeleteObject(XClassType* obj_ptr);
86 
93  template< typename XClassType > MHO_UUID GetTypeUUID();
94 
101  template< typename XClassType > std::size_t GetNObjects() const;
102 
103  //check if any object with the given object id is in the store
111  bool IsObjectPresent(const MHO_UUID& obj_id) const;
112 
119  MHO_Serializable* GetObject(const MHO_UUID& obj_id);
120 
127  bool DeleteObject(const MHO_UUID& obj_id);
128 
134  void GetAllTypeUUIDs(std::vector< MHO_UUID >& type_ids);
135 
142  void GetAllObjectUUIDsOfType(MHO_UUID type_id, std::vector< MHO_UUID >& obj_ids);
143 
149  std::size_t GetNObjects() const { return fObjectsToIds.size(); }
150 
158  bool SetShortName(const MHO_UUID& obj_id, const std::string& shortname);
159 
166  MHO_UUID GetObjectUUID(const std::string& shortname);
167 
174  MHO_UUID GetObjectTypeUUID(const MHO_UUID& obj_id);
175 
182  std::string GetShortName(const MHO_UUID& obj_id);
183 
189  void GetAllShortNames(std::vector< std::string >& shortnames);
190 
191  //this is primarily here to provide a object look-up table for the python interface
192  //returns a list of (object_type_uuid, object_item_uuid, shortname)
193  std::vector< std::tuple< std::string, std::string, std::string > > GetAllObjectInfo();
194 
201  void RenameObject(const std::string& current_shortname, const std::string& new_shortname);
202 
203  //debug
205  {
206  for(auto it = fShortNameToIds.begin(); it != fShortNameToIds.end(); it++)
207  {
208  std::cout << it->first << " : " << it->second.as_string() << std::endl;
209  }
210  }
211 
212  protected:
213  using key_pair = std::pair< MHO_UUID, MHO_UUID >;
214 
215  //object dictionary...currently we only have one dictionary implementation
216  //however, we may in the future want to allow the user to pass a custom dictionary implementation
217  //if they have additional classes they want to serialize which are not already supported
219 
220  //all objects are stored as pointers to the base-class MHO_Serializable
221  //they are cast to the underlying type specified by the type id upon retrieval
222 
223  //the key pair is <type_uuid, obj_id>, and the value is a pointer to the object
224  std::map< key_pair, MHO_Serializable* > fIdsToObjects;
225 
226  //the key is a pointer to an object, and the value is a pair of <type_uuid, obj_id>
227  std::map< MHO_Serializable*, key_pair > fObjectsToIds;
228 
229  //maps string names to object uuids
230  std::map< std::string, MHO_UUID > fShortNameToIds;
231  std::set< std::string > fShortNameSet;
232 };
233 
240 template< typename XClassType > bool MHO_ContainerStore::AddObject(XClassType* obj)
241 {
242  if(obj == nullptr)
243  {
244  return false;
245  }
246 
247  //attempt to cast to our storage/serialization type
248  auto ptr = static_cast< MHO_Serializable* >(obj);
249  if(ptr == nullptr)
250  {
251  return false;
252  }
253 
254  MHO_UUID obj_id = obj->GetObjectUUID();
255  MHO_UUID type_id = obj->GetTypeUUID();
256 
257  key_pair kp;
258  kp.first = type_id;
259  kp.second = obj_id;
260 
261  fIdsToObjects[kp] = ptr;
262  fObjectsToIds[ptr] = kp;
263  return true;
264 }
265 
272 template< typename XClassType > XClassType* MHO_ContainerStore::GetObject(const MHO_UUID& obj_id)
273 {
274  MHO_UUID type_id = fDictionary.GetUUIDFor< XClassType >();
275 
276  key_pair kp;
277  kp.first = type_id;
278  kp.second = obj_id;
279 
280  XClassType* ptr = nullptr;
281  auto it = fIdsToObjects.find(kp);
282  if(it != fIdsToObjects.end())
283  {
284  MHO_Serializable* obj = it->second;
285  ptr = dynamic_cast< XClassType* >(obj);
286  }
287  return ptr;
288 }
289 
296 template< typename XClassType > XClassType* MHO_ContainerStore::GetObject(const std::string& shortname)
297 {
298  XClassType* ptr = nullptr;
299  MHO_UUID obj_id = GetObjectUUID(shortname);
300  if(!obj_id.is_empty())
301  {
302  ptr = GetObject< XClassType >(obj_id);
303  }
304  return ptr;
305 }
306 
313 template< typename XClassType > XClassType* MHO_ContainerStore::GetObject(std::size_t index)
314 {
315  XClassType* ptr = nullptr;
316  std::size_t n_objects = GetNObjects< XClassType >();
317  std::size_t count = 0;
318  if(index < n_objects)
319  {
320  MHO_UUID type_id = fDictionary.GetUUIDFor< XClassType >();
321  for(auto it = fIdsToObjects.begin(); it != fIdsToObjects.end(); it++)
322  {
323  key_pair item_ids = it->first;
324  MHO_UUID item_type_id = item_ids.first;
325  if(type_id == item_type_id)
326  {
327  if(count == index)
328  {
329  MHO_Serializable* obj = it->second;
330  ptr = dynamic_cast< XClassType* >(obj);
331  break;
332  }
333  count++;
334  }
335  }
336  }
337 
338  return ptr;
339 }
340 
347 template< typename XClassType > bool MHO_ContainerStore::DeleteObject(XClassType* obj_ptr)
348 {
349  MHO_Serializable* ptr = static_cast< MHO_Serializable* >(obj_ptr);
350  if(ptr == nullptr)
351  {
352  return false;
353  }
354 
355  auto it = fObjectsToIds.find(ptr);
356  if(it == fObjectsToIds.end())
357  {
358  return false;
359  }
360 
361  key_pair kp = it->second;
362  MHO_UUID obj_id = kp.second;
363  auto it2 = fIdsToObjects.find(kp);
364  if(it2 == fIdsToObjects.end())
365  {
366  return false;
367  }
368 
369  //remove entries related to this object
370  fObjectsToIds.erase(it);
371  fIdsToObjects.erase(it2);
372  delete obj_ptr;
373 
374  //remove labels and short name associated with this object
375  std::string shortname = "";
376  for(auto it = fShortNameToIds.begin(); it != fShortNameToIds.end(); it++)
377  {
378  if(it->second == obj_id)
379  {
380  shortname = it->first;
381  fShortNameToIds.erase(it);
382  break;
383  }
384  }
385  fShortNameSet.erase(shortname);
386  return true;
387 }
388 
394 template< typename XClassType > MHO_UUID MHO_ContainerStore::GetTypeUUID()
395 {
396  MHO_UUID type_id = fDictionary.GetUUIDFor< XClassType >();
397  return type_id;
398 }
399 
405 template< typename XClassType > std::size_t MHO_ContainerStore::GetNObjects() const
406 {
407  MHO_UUID type_id = fDictionary.GetUUIDFor< XClassType >();
408  std::size_t count = 0;
409  for(auto it = fIdsToObjects.begin(); it != fIdsToObjects.end(); it++)
410  {
411  key_pair item_ids = it->first;
412  MHO_UUID item_type_id = item_ids.first;
413  if(type_id == item_type_id)
414  {
415  count++;
416  }
417  }
418  return count;
419 }
420 
427 inline void MHO_ContainerStore::RenameObject(const std::string& current_shortname, const std::string& new_shortname)
428 {
429  MHO_UUID obj_uuid = GetObjectUUID(current_shortname);
430  if(obj_uuid.as_long() != 0)
431  {
432  fShortNameSet.erase(current_shortname);
433  fShortNameToIds.erase(current_shortname);
434  SetShortName(obj_uuid, new_shortname);
435  }
436 }
437 
438 } // namespace hops
439 
440 #endif
MHO_UUID GetUUIDFor() const
Getter for uuid for a class type.
Definition: MHO_ClassIdentityMap.hh:163
Class MHO_ContainerDictionary.
Definition: MHO_ContainerDictionary.hh:28
Class MHO_ContainerStore.
Definition: MHO_ContainerStore.hh:32
MHO_ContainerStore()
Definition: MHO_ContainerStore.hh:34
bool SetShortName(const MHO_UUID &obj_id, const std::string &shortname)
provide the ability to attach a nicknames to object uuids, all nicknames must be unique returns false...
Definition: MHO_ContainerStore.cc:93
void GetAllShortNames(std::vector< std::string > &shortnames)
get all short names currently in use
Definition: MHO_ContainerStore.cc:135
void DumpShortNamesToIds()
Definition: MHO_ContainerStore.hh:204
std::set< std::string > fShortNameSet
Definition: MHO_ContainerStore.hh:231
MHO_UUID GetObjectTypeUUID(const MHO_UUID &obj_id)
returns the type uuid of the object with obj_id (if it exists)
Definition: MHO_ContainerStore.cc:145
bool DeleteObject(XClassType *obj_ptr)
Deletes an object and removes associated entries from containers, returns true if successful.
Definition: MHO_ContainerStore.hh:347
MHO_ContainerDictionary fDictionary
Definition: MHO_ContainerStore.hh:218
std::map< MHO_Serializable *, key_pair > fObjectsToIds
Definition: MHO_ContainerStore.hh:227
std::string GetShortName(const MHO_UUID &obj_id)
provide retrival of object short name from uuid
Definition: MHO_ContainerStore.cc:120
virtual ~MHO_ContainerStore()
Definition: MHO_ContainerStore.hh:36
std::map< std::string, MHO_UUID > fShortNameToIds
Definition: MHO_ContainerStore.hh:230
XClassType * GetObject(const MHO_UUID &obj_id)
get an object of a specific type via object uuid (returns nullptr if not present)
Definition: MHO_ContainerStore.hh:272
void GetAllTypeUUIDs(std::vector< MHO_UUID > &type_ids)
get every type uuid that is present in store
Definition: MHO_ContainerStore.cc:61
void Clear()
Deletes all objects in the store.
Definition: MHO_ContainerStore.cc:6
MHO_UUID GetObjectUUID(const std::string &shortname)
provide retrieval of an object uuid via shortname/nickname, returns zero'd uuid if none exist
Definition: MHO_ContainerStore.cc:108
void GetAllObjectUUIDsOfType(MHO_UUID type_id, std::vector< MHO_UUID > &obj_ids)
get every object uuid in store associated with the specified type UUID
Definition: MHO_ContainerStore.cc:78
bool IsObjectPresent(const MHO_UUID &obj_id) const
Checks if an object with a given UUID is present in the container store.
Definition: MHO_ContainerStore.cc:18
std::map< key_pair, MHO_Serializable * > fIdsToObjects
Definition: MHO_ContainerStore.hh:224
std::vector< std::tuple< std::string, std::string, std::string > > GetAllObjectInfo()
Definition: MHO_ContainerStore.cc:162
std::size_t GetNObjects() const
get total number of objects in store
Definition: MHO_ContainerStore.hh:149
std::pair< MHO_UUID, MHO_UUID > key_pair
Definition: MHO_ContainerStore.hh:213
void RenameObject(const std::string &current_shortname, const std::string &new_shortname)
provides a way in which we can replace the shortname/nickname of an object
Definition: MHO_ContainerStore.hh:427
MHO_UUID GetTypeUUID()
get the type uuid for a specific type (if it is supported) - if unsupported uuid will be zero
Definition: MHO_ContainerStore.hh:394
std::size_t GetNObjects() const
get the number of objects of a specific type
Definition: MHO_ContainerStore.hh:405
bool AddObject(XClassType *obj)
Adds an object (with specific type) to the container store if it's non-null and can be cast to MHO_Se...
Definition: MHO_ContainerStore.hh:240
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
uint64_t as_long() const
Definition: MHO_UUID.hh:108
bool is_empty() const
Definition: MHO_UUID.hh:168
Definition: MHO_AdhocFlagging.hh:18