VSM C++ SDK
Vehicle Specific Modules SDK
reference_guard.h
Go to the documentation of this file.
1 // Copyright (c) 2014, Smart Projects Holdings Ltd
2 // All rights reserved.
3 // See LICENSE file for license details.
4 
11 #ifndef REFERENCE_GUARD_H_
12 #define REFERENCE_GUARD_H_
13 
14 #include <ugcs/vsm/defs.h>
15 
16 #include <algorithm>
17 #include <type_traits>
18 #include <functional>
19 
20 namespace ugcs {
21 namespace vsm {
22 
30 template <typename Class_ptr>
32 public:
34  Reference_guard(std::nullptr_t ptr = nullptr):
35  ptr(ptr)
36  {}
37 
39  Reference_guard(const Class_ptr &ptr):
40  ptr(ptr)
41  {
42  if (this->ptr) {
43  this->ptr->Add_ref();
44  }
45  }
46 
48  Reference_guard(Class_ptr &&ptr):
49  ptr(std::move(ptr))
50  {
51  if (this->ptr) {
52  this->ptr->Add_ref();
53  }
54  }
55 
58  ptr(ref.ptr)
59  {
60  if (ptr) {
61  ptr->Add_ref();
62  }
63  }
64 
67  ptr(std::move(ref.ptr))
68  {
69  ref.ptr = nullptr;
70  }
71 
73  template <typename Src_class_ptr>
75  ptr(ref.ptr)
76  {
77  if (ptr) {
78  ptr->Add_ref();
79  }
80  }
81 
83  template <typename Src_class_ptr>
85  ptr(std::move(ref.ptr))
86  {
87  ref.ptr = nullptr;
88  }
89 
91  {
92  if (ptr) {
93  ptr->Release_ref();
94  }
95  }
96 
100  {
101  if (ref.ptr) {
102  ref.ptr->Add_ref();
103  }
104  if (ptr) {
105  ptr->Release_ref();
106  }
107  ptr = ref.ptr;
108  return *this;
109  }
110 
114  {
115  if (ptr) {
116  ptr->Release_ref();
117  }
118  ptr = std::move(ref.ptr);
119  ref.ptr = nullptr;
120  return *this;
121  }
122 
125  operator =(std::nullptr_t ref __UNUSED)
126  {
127  if (ptr) {
128  ptr->Release_ref();
129  }
130  ptr = nullptr;
131  return *this;
132  }
133 
135  bool
137  {
138  return ptr == ref.ptr;
139  }
140 
142  bool
144  {
145  return ptr != ref.ptr;
146  }
147 
149  explicit operator bool() const
150  {
151  return ptr != nullptr;
152  }
153 
155  Class_ptr
157  {
158  ASSERT(ptr);
159  return ptr;
160  }
161 
163  const Class_ptr
164  operator ->() const
165  {
166  ASSERT(ptr);
167  return ptr;
168  }
169 
171  decltype(*std::declval<Class_ptr>())
172  operator *()
173  {
174  return *ptr;
175  }
176 
177 #ifndef NO_DOXYGEN
178 
179  decltype(*std::declval<Class_ptr>())
180  operator *() const
181  {
182  return *ptr;
183  }
184 #endif
185 
187  class Hasher {
188  public:
189 
191  size_t
192  operator ()(const Reference_guard& ref) const
193  {
194  static std::hash<Class_ptr> hasher;
195  return hasher(ref.ptr);
196  }
197  };
198 
199 private:
200  template <typename> friend class Reference_guard;
202  Class_ptr ptr;
203 };
204 
205 } /* namespace vsm */
206 } /* namespace ugcs */
207 
208 #endif /* REFERENCE_GUARD_H_ */
UGCS root namespace.
Definition: android-linux/ugcs/vsm/platform_sockets.h:27
Reference_guard(const Reference_guard< Src_class_ptr > &ref)
Copy guard object.
Definition: reference_guard.h:74
Hasher class for reference type.
Definition: reference_guard.h:187
Reference_guard(const Reference_guard< Class_ptr > &ref)
Copy guard object.
Definition: reference_guard.h:57
decltype(*std::declval< Class_ptr >() operator*)()
Dereference is transparent.
Definition: reference_guard.h:172
size_t operator()(const Reference_guard &ref) const
Calculate hash value.
Definition: reference_guard.h:192
Reference_guard(Reference_guard< Class_ptr > &&ref)
Move guard object.
Definition: reference_guard.h:66
bool operator==(const Reference_guard< Class_ptr > &ref) const
Guards are equal when managed object pointers are equal.
Definition: reference_guard.h:136
bool operator!=(const Reference_guard< Class_ptr > &ref) const
Guards are not equal when managed object pointers are not equal.
Definition: reference_guard.h:143
STL namespace.
Reference_guard(Reference_guard< Src_class_ptr > &&ref)
Move guard object.
Definition: reference_guard.h:84
Common preprocessor definitions.
Reference_guard(const Class_ptr &ptr)
Construct guard object.
Definition: reference_guard.h:39
Class_ptr operator->()
Member access is transparent.
Definition: reference_guard.h:156
#define ASSERT(x)
No action in release.
Definition: debug.h:68
#define __UNUSED
Use with unused arguments if you like to declare that it is not (yet) used a the function.
Definition: defs.h:30
Reference_guard(Class_ptr &&ptr)
Construct guard by moving managed object to the guard.
Definition: reference_guard.h:48
Reference_guard< Class_ptr > & operator=(const Reference_guard< Class_ptr > &ref)
Copy assignment.
Definition: reference_guard.h:99
Reference guard objects keep references for managed objects.
Definition: reference_guard.h:31
Reference_guard(std::nullptr_t ptr=nullptr)
Construct an empty guard which doesn&#39;t have managed object.
Definition: reference_guard.h:34