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  if (ptr) {
33  assert(ptr->get_uuid() == uuid);
34  }
35  return *ptr;
36  }
37 
38  T *operator->() const
39  {
40  if (ptr) {
41  assert(ptr->get_uuid() == uuid);
42  }
43  return ptr;
44  }
45 
46  operator T *() const
47  {
48  if (ptr) {
49  assert(ptr->get_uuid() == uuid);
50  }
51  return ptr;
52  }
53 
54  T *ptr;
55  UUID uuid;
56  void update(std::map<UUID, T> &map)
57  {
58  if (uuid) {
59  if (map.count(uuid)) {
60  ptr = &map.at(uuid);
61  }
62  else {
63  ptr = nullptr;
64  }
65  }
66  }
67  void update(const std::map<UUID, T_without_const> &map)
68  {
69  if (uuid) {
70  if (map.count(uuid)) {
71  ptr = &map.at(uuid);
72  }
73  else {
74  ptr = nullptr;
75  }
76  }
77  }
78 };
79 } // 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