3DiVi Face SDK  3.24.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Groups
light_shared_ptr.h
1 #ifndef __FACE_SDK__PBIO__LIGHT_SHARED_PTR_H__56133c8ad934432e86a8b16f11bb18d7
2 #define __FACE_SDK__PBIO__LIGHT_SHARED_PTR_H__56133c8ad934432e86a8b16f11bb18d7
3 
4 #include "object_with_ref_counter.h"
5 
6 #ifdef __FACE_SDK_PBIO_LIGHT_SHARED_PTR_CHECK_NULL_USE__e8dbe74d9bf04bfa9d97e90c29e858f5
7 #include "../Error.h"
8 #endif
9 
11 
12 namespace pbio
13 {
14 
15 template<typename T>
16 class light_shared_ptr
17 {
18 public:
19 
20 
21  light_shared_ptr() : ptr(NULL) {}
22 
23 
24  light_shared_ptr(const light_shared_ptr &a) : ptr(a.ptr)
25  {
26  if(ptr)
27  {
28  ptr->increment_refcounter();
29  }
30  }
31 
32 
33  light_shared_ptr(light_shared_ptr&& a) noexcept :
34  light_shared_ptr()
35  {
36  (*this) = std::move(a);
37  }
38 
39 
40  ~light_shared_ptr()
41  {
42  if(ptr)
43  {
44  ptr->decrement_refcounter();
45  }
46  }
47 
48 
49  light_shared_ptr& operator=(const light_shared_ptr &a)
50  {
51  if(ptr == a.ptr)
52  {
53  return *this;
54  }
55 
56  if(ptr)
57  {
58  ptr->decrement_refcounter();
59  }
60 
61  ptr = a.ptr;
62 
63  if(ptr)
64  {
65  ptr->increment_refcounter();
66  }
67 
68  return *this;
69  }
70 
71 
72  light_shared_ptr& operator=(light_shared_ptr&& a) noexcept
73  {
74  if (ptr == a.ptr)
75  {
76  return *this;
77  }
78 
79  if (ptr)
80  {
81  ptr->decrement_refcounter();
82  }
83 
84  ptr = a.ptr;
85 
86  a.ptr = nullptr;
87 
88  return *this;
89  }
90 
91 
92  T* get () const
93  {
94  if(ptr)
95  {
96  return &ptr->object;
97  }
98 
99  return NULL;
100  }
101 
102 
103  operator T* () const
104  {
105  return get();
106  }
107 
108  bool empty () const
109  {
110  return !ptr;
111  }
112 
113  void release ()
114  {
115  if(ptr)
116  {
117  ptr->decrement_refcounter();
118  ptr = NULL;
119  }
120  }
121 
122  T* operator -> () const
123  {
124 #ifdef __FACE_SDK_PBIO_LIGHT_SHARED_PTR_CHECK_NULL_USE__e8dbe74d9bf04bfa9d97e90c29e858f5
125  if(!ptr)
126  {
127  throw pbio::Error(0xef621abe, "Error: Ptr::operator-> called with NULL pointer stored, error code: 0xef621abe.");
128  }
129 #endif
130  return &ptr->object;
131  }
132 
133 
134  T& operator * () const
135  {
136 #ifdef __FACE_SDK_PBIO_LIGHT_SHARED_PTR_CHECK_NULL_USE__e8dbe74d9bf04bfa9d97e90c29e858f5
137  if(!ptr)
138  {
139  throw pbio::Error(0xb3fe4d07, "Error: Ptr::operator* called with NULL pointer stored, error code: 0xb3fe4d07.");
140  }
141 #endif
142  return ptr->object;
143  }
144 
145 
146  static inline light_shared_ptr make()
147  { return light_shared_ptr(new object_with_ref_counter<T>()); }
148 
149  template<typename t1>
150  static inline light_shared_ptr make(t1 a1)
151  { return light_shared_ptr(new object_with_ref_counter<T>(a1)); }
152 
153  template<typename t2, typename t1>
154  static inline light_shared_ptr make(t2 a2, t1 a1)
155  { return light_shared_ptr(new object_with_ref_counter<T>(a2, a1)); }
156 
157  template<typename t3, typename t2, typename t1>
158  static inline light_shared_ptr make(t3 a3, t2 a2, t1 a1)
159  { return light_shared_ptr(new object_with_ref_counter<T>(a3, a2, a1)); }
160 
161  template<typename t4, typename t3, typename t2, typename t1>
162  static inline light_shared_ptr make(t4 a4, t3 a3, t2 a2, t1 a1)
163  { return light_shared_ptr(new object_with_ref_counter<T>(a4, a3, a2, a1)); }
164 
165  template<typename t5, typename t4, typename t3, typename t2, typename t1>
166  static inline light_shared_ptr make(t5 a5, t4 a4, t3 a3, t2 a2, t1 a1)
167  { return light_shared_ptr(new object_with_ref_counter<T>(a5, a4, a3, a2, a1)); }
168 
169  template<typename t6, typename t5, typename t4, typename t3, typename t2, typename t1>
170  static inline light_shared_ptr make(t6 a6, t5 a5, t4 a4, t3 a3, t2 a2, t1 a1)
171  { return light_shared_ptr(new object_with_ref_counter<T>(a6, a5, a4, a3, a2, a1)); }
172 
173  template<typename t7, typename t6, typename t5, typename t4, typename t3, typename t2, typename t1>
174  static inline light_shared_ptr make(t7 a7, t6 a6, t5 a5, t4 a4, t3 a3, t2 a2, t1 a1)
175  { return light_shared_ptr(new object_with_ref_counter<T>(a7, a6, a5, a4, a3, a2, a1)); }
176 
177  template<typename t8, typename t7, typename t6, typename t5, typename t4, typename t3, typename t2, typename t1>
178  static inline light_shared_ptr make(t8 a8, t7 a7, t6 a6, t5 a5, t4 a4, t3 a3, t2 a2, t1 a1)
179  { return light_shared_ptr(new object_with_ref_counter<T>(a8, a7, a6, a5, a4, a3, a2, a1)); }
180 
181  template<typename t9, typename t8, typename t7, typename t6, typename t5, typename t4, typename t3, typename t2, typename t1>
182  static inline light_shared_ptr make(t9 a9, t8 a8, t7 a7, t6 a6, t5 a5, t4 a4, t3 a3, t2 a2, t1 a1)
183  { return light_shared_ptr(new object_with_ref_counter<T>(a9, a8, a7, a6, a5, a4, a3, a2, a1)); }
184 
185  template<typename t10, typename t9, typename t8, typename t7, typename t6, typename t5, typename t4, typename t3, typename t2, typename t1>
186  static inline light_shared_ptr make(t10 a10, t9 a9, t8 a8, t7 a7, t6 a6, t5 a5, t4 a4, t3 a3, t2 a2, t1 a1)
187  { return light_shared_ptr(new object_with_ref_counter<T>(a10, a9, a8, a7, a6, a5, a4, a3, a2, a1)); }
188 
189  template<typename t11, typename t10, typename t9, typename t8, typename t7, typename t6, typename t5, typename t4, typename t3, typename t2, typename t1>
190  static inline light_shared_ptr make(t11 a11, t10 a10, t9 a9, t8 a8, t7 a7, t6 a6, t5 a5, t4 a4, t3 a3, t2 a2, t1 a1)
191  { return light_shared_ptr(new object_with_ref_counter<T>(a11, a10, a9, a8, a7, a6, a5, a4, a3, a2, a1)); }
192 
193  template<typename t12, typename t11, typename t10, typename t9, typename t8, typename t7, typename t6, typename t5, typename t4, typename t3, typename t2, typename t1>
194  static inline light_shared_ptr make(t12 a12, t11 a11, t10 a10, t9 a9, t8 a8, t7 a7, t6 a6, t5 a5, t4 a4, t3 a3, t2 a2, t1 a1)
195  { return light_shared_ptr(new object_with_ref_counter<T>(a12, a11, a10, a9, a8, a7, a6, a5, a4, a3, a2, a1)); }
196 
197 private:
198 
199  light_shared_ptr(object_with_ref_counter<T>* ptr) :
200  ptr(ptr)
201  {
202  if(ptr)
203  {
204  ptr->increment_refcounter();
205  }
206  }
207 
208  object_with_ref_counter<T> *ptr;
209 
210 };
211 
212 
213 template<typename T>
214 inline
215 bool operator == (const light_shared_ptr<T> &a, const light_shared_ptr<T> &b)
216 {
217  return a.get() == b.get();
218 }
219 
220 template<typename T>
221 inline
222 bool operator != (const light_shared_ptr<T> &a, const light_shared_ptr<T> &b)
223 {
224  return a.get() != b.get();
225 }
226 
227 } // pbio namespace
228 
230 
231 #endif // __FACE_SDK__PBIO__LIGHT_SHARED_PTR_H__56133c8ad934432e86a8b16f11bb18d7
The class of exceptions thrown when errors occur.
Definition: Error.h:26