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 
43 
51  template< typename XClassType > bool AddObject(XClassType* obj);
52 
59  template< typename XClassType > XClassType* GetObject(const MHO_UUID& obj_id);
60 
68  template< typename XClassType > XClassType* GetObject(const std::string& shortname);
69 
77  template< typename XClassType > XClassType* GetObject(std::size_t index);
78 
86  template< typename XClassType > bool DeleteObject(XClassType* obj_ptr);
87 
94  template< typename XClassType > MHO_UUID GetTypeUUID();
95 
102  template< typename XClassType > std::size_t GetNObjects() const;
103 
104  //check if any object with the given object id is in the store
112  bool IsObjectPresent(const MHO_UUID& obj_id) const;
113 
120  MHO_Serializable* GetObject(const MHO_UUID& obj_id);
121 
128  bool DeleteObject(const MHO_UUID& obj_id);
129 
135  void GetAllTypeUUIDs(std::vector< MHO_UUID >& type_ids);
136 
143  void GetAllObjectUUIDsOfType(MHO_UUID type_id, std::vector< MHO_UUID >& obj_ids);
144 
150  std::size_t GetNObjects() const { return fObjectsToIds.size(); }
151 
159  bool SetShortName(const MHO_UUID& obj_id, const std::string& shortname);
160 
167  MHO_UUID GetObjectUUID(const std::string& shortname);
168 
175  MHO_UUID GetObjectTypeUUID(const MHO_UUID& obj_id);
176 
183  std::string GetShortName(const MHO_UUID& obj_id);
184 
190  void GetAllShortNames(std::vector< std::string >& shortnames);
191 
192  //this is primarily here to provide a object look-up table for the python interface
193  //returns a list of (object_type_uuid, object_item_uuid, shortname)
194  std::vector< std::tuple< std::string, std::string, std::string > > GetAllObjectInfo();
195 
196 
203  void RenameObject(const std::string& current_shortname, const std::string& new_shortname);
204 
205  //debug
207  {
208  for(auto it = fShortNameToIds.begin(); it != fShortNameToIds.end(); it++)
209  {
210  std::cout << it->first << " : " << it->second.as_string() << std::endl;
211  }
212  }
213 
214  protected:
215  using key_pair = std::pair< MHO_UUID, MHO_UUID >;
216 
217  //object dictionary...currently we only have one dictionary implementation
218  //however, we may in the future want to allow the user to pass a custom dictionary implementation
219  //if they have additional classes they want to serialize which are not already supported
221 
222  //all objects are stored as pointers to the base-class MHO_Serializable
223  //they are cast to the underlying type specified by the type id upon retrieval
224 
225  //the key pair is <type_uuid, obj_id>, and the value is a pointer to the object
226  std::map< key_pair, MHO_Serializable* > fIdsToObjects;
227 
228  //the key is a pointer to an object, and the value is a pair of <type_uuid, obj_id>
229  std::map< MHO_Serializable*, key_pair > fObjectsToIds;
230 
231  //maps string names to object uuids
232  std::map< std::string, MHO_UUID > fShortNameToIds;
233  std::set< std::string > fShortNameSet;
234 };
235 
242 template< typename XClassType > bool MHO_ContainerStore::AddObject(XClassType* obj)
243 {
244  if(obj == nullptr)
245  {
246  return false;
247  }
248 
249  //attempt to cast to our storage/serialization type
250  auto ptr = static_cast< MHO_Serializable* >(obj);
251  if(ptr == nullptr)
252  {
253  return false;
254  }
255 
256  MHO_UUID obj_id = obj->GetObjectUUID();
257  MHO_UUID type_id = obj->GetTypeUUID();
258 
259  key_pair kp;
260  kp.first = type_id;
261  kp.second = obj_id;
262 
263  fIdsToObjects[kp] = ptr;
264  fObjectsToIds[ptr] = kp;
265  return true;
266 }
267 
274 template< typename XClassType > XClassType* MHO_ContainerStore::GetObject(const MHO_UUID& obj_id)
275 {
276  MHO_UUID type_id = fDictionary.GetUUIDFor< XClassType >();
277 
278  key_pair kp;
279  kp.first = type_id;
280  kp.second = obj_id;
281 
282  XClassType* ptr = nullptr;
283  auto it = fIdsToObjects.find(kp);
284  if(it != fIdsToObjects.end())
285  {
286  MHO_Serializable* obj = it->second;
287  ptr = dynamic_cast< XClassType* >(obj);
288  }
289  return ptr;
290 }
291 
298 template< typename XClassType > XClassType* MHO_ContainerStore::GetObject(const std::string& shortname)
299 {
300  XClassType* ptr = nullptr;
301  MHO_UUID obj_id = GetObjectUUID(shortname);
302  if(!obj_id.is_empty())
303  {
304  ptr = GetObject< XClassType >(obj_id);
305  }
306  return ptr;
307 }
308 
315 template< typename XClassType > XClassType* MHO_ContainerStore::GetObject(std::size_t index)
316 {
317  XClassType* ptr = nullptr;
318  std::size_t n_objects = GetNObjects< XClassType >();
319  std::size_t count = 0;
320  if(index < n_objects)
321  {
322  MHO_UUID type_id = fDictionary.GetUUIDFor< XClassType >();
323  for(auto it = fIdsToObjects.begin(); it != fIdsToObjects.end(); it++)
324  {
325  key_pair item_ids = it->first;
326  MHO_UUID item_type_id = item_ids.first;
327  if(type_id == item_type_id)
328  {
329  if(count == index)
330  {
331  MHO_Serializable* obj = it->second;
332  ptr = dynamic_cast< XClassType* >(obj);
333  break;
334  }
335  count++;
336  }
337  }
338  }
339 
340  return ptr;
341 }
342 
349 template< typename XClassType > bool MHO_ContainerStore::DeleteObject(XClassType* obj_ptr)
350 {
351  MHO_Serializable* ptr = static_cast< MHO_Serializable* >(obj_ptr);
352  if(ptr == nullptr)
353  {
354  return false;
355  }
356 
357  auto it = fObjectsToIds.find(ptr);
358  if(it == fObjectsToIds.end())
359  {
360  return false;
361  }
362 
363  key_pair kp = it->second;
364  MHO_UUID obj_id = kp.second;
365  auto it2 = fIdsToObjects.find(kp);
366  if(it2 == fIdsToObjects.end())
367  {
368  return false;
369  }
370 
371  //remove entries related to this object
372  fObjectsToIds.erase(it);
373  fIdsToObjects.erase(it2);
374  delete obj_ptr;
375 
376  //remove labels and short name associated with this object
377  std::string shortname = "";
378  for(auto it = fShortNameToIds.begin(); it != fShortNameToIds.end(); it++)
379  {
380  if(it->second == obj_id)
381  {
382  shortname = it->first;
383  fShortNameToIds.erase(it);
384  break;
385  }
386  }
387  fShortNameSet.erase(shortname);
388  return true;
389 }
390 
396 template< typename XClassType > MHO_UUID MHO_ContainerStore::GetTypeUUID()
397 {
398  MHO_UUID type_id = fDictionary.GetUUIDFor< XClassType >();
399  return type_id;
400 }
401 
407 template< typename XClassType > std::size_t MHO_ContainerStore::GetNObjects() const
408 {
409  MHO_UUID type_id = fDictionary.GetUUIDFor< XClassType >();
410  std::size_t count = 0;
411  for(auto it = fIdsToObjects.begin(); it != fIdsToObjects.end(); it++)
412  {
413  key_pair item_ids = it->first;
414  MHO_UUID item_type_id = item_ids.first;
415  if(type_id == item_type_id)
416  {
417  count++;
418  }
419  }
420  return count;
421 }
422 
429 inline void MHO_ContainerStore::RenameObject(const std::string& current_shortname, const std::string& new_shortname)
430 {
431  MHO_UUID obj_uuid = GetObjectUUID(current_shortname);
432  if(obj_uuid.as_long() != 0)
433  {
434  fShortNameSet.erase(current_shortname);
435  fShortNameToIds.erase(current_shortname);
436  SetShortName(obj_uuid, new_shortname);
437  }
438 }
439 
440 } // namespace hops
441 
442 #endif
MHO_UUID GetUUIDFor() const
Getter for uuid for a class type.
Definition: MHO_ClassIdentityMap.hh:155
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:206
std::set< std::string > fShortNameSet
Definition: MHO_ContainerStore.hh:233
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:349
MHO_ContainerDictionary fDictionary
Definition: MHO_ContainerStore.hh:220
std::map< MHO_Serializable *, key_pair > fObjectsToIds
Definition: MHO_ContainerStore.hh:229
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:232
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:274
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:226
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:150
std::pair< MHO_UUID, MHO_UUID > key_pair
Definition: MHO_ContainerStore.hh:215
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:429
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:396
std::size_t GetNObjects() const
get the number of objects of a specific type
Definition: MHO_ContainerStore.hh:407
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:242
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_ChannelLabeler.hh:17