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