Horizon
uuid_ptr.hpp
1 #pragma once
2 #include "uuid.hpp"
3 #include "uuid_provider.hpp"
4 #include <assert.h>
5 #include <map>
6 #include <type_traits>
7 
8 namespace horizon {
9 template <typename T> class uuid_ptr {
10 private:
11  typedef typename std::remove_const<T>::type T_without_const;
12 
13 public:
14  uuid_ptr() : ptr(nullptr)
15  {
16  }
17  uuid_ptr(const UUID &uu) : ptr(nullptr), uuid(uu)
18  {
19  }
20  uuid_ptr(T *p, const UUID &uu) : ptr(p), uuid(uu)
21  {
22  }
23  uuid_ptr(T *p) : ptr(p), uuid(p ? p->get_uuid() : UUID())
24  {
25  /* static_assert(
26  std::is_base_of<T, decltype(*p)>::value,
27  "T must be a descendant of MyBase"
28  );*/
29  }
30  T &operator*()
31  {
32 #ifdef UUID_PTR_CHECK
33  if (ptr) {
34  assert(ptr->get_uuid() == uuid);
35  }
36 #endif
37  return *ptr;
38  }
39 
40  T *operator->() const
41  {
42 #ifdef UUID_PTR_CHECK
43  if (ptr) {
44  assert(ptr->get_uuid() == uuid);
45  }
46 #endif
47  return ptr;
48  }
49 
50  operator T *() const
51  {
52 #ifdef UUID_PTR_CHECK
53  if (ptr) {
54  assert(ptr->get_uuid() == uuid);
55  }
56 #endif
57  return ptr;
58  }
59 
60  T *ptr;
61  UUID uuid;
62  void update(std::map<UUID, T> &map)
63  {
64  if (uuid) {
65  if (map.count(uuid)) {
66  ptr = &map.at(uuid);
67  }
68  else {
69  ptr = nullptr;
70  }
71  }
72  }
73  void update(const std::map<UUID, T_without_const> &map)
74  {
75  if (uuid) {
76  if (map.count(uuid)) {
77  ptr = &map.at(uuid);
78  }
79  else {
80  ptr = nullptr;
81  }
82  }
83  }
84 };
85 } // namespace horizon
horizon::uuid_ptr
Definition: uuid_ptr.hpp:9
horizon::UUID
This class encapsulates a UUID and allows it to be uses as a value type.
Definition: uuid.hpp:16