Face Engine SDK  5.8.0
A face detection, recognition and tracking engine.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Ref.h
1 #pragma once
2 
3 #include <cassert>
4 #include <type_traits>
5 
6 #include "RefBase.h"
7 #include <fsdk/Def.h>
8 
9 namespace fsdk
10 {
11 #ifndef DOXYGEN_SHOULD_SKIP_THIS
12 
13  /* Forward declarations. */
14  template<typename T> struct WeakRef;
15 
16 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
17 
25  template<typename T>
26  struct Ref : RefBase<T> {
27 
31  Ref() noexcept = default;
32 
35  Ref(std::nullptr_t) noexcept {}
36 
41  FSDK_DEPRECATED("construction from raw pointer is deprecated, use make_ref function instead")
42  explicit Ref(T* ptr) noexcept { *this = ptr; }
43 
48  Ref(const Ref& other) : RefBase<T>() { *this = other; }
49 
54  template<class D, typename = typename std::enable_if<std::is_convertible<D*, T*>::value>::type>
55  explicit Ref(const Ref<D>& other) : RefBase<T>() { *this = other; }
56 
57  #ifndef DOXYGEN_SHOULD_SKIP_THIS
58 
62  Ref(const WeakRef<T>& other) noexcept { *this = other; }
63 
64  #endif /* DOXYGEN_SHOULD_SKIP_THIS */
65 
68  ~Ref() noexcept {
69  reset();
70  }
71 
76  T* operator -> () const noexcept {
77  assert(!this->isNull());
78  return this->get();
79  }
80 
85  T** getInitReference() noexcept {
86  if(this->get())
87  this->get()->release();
88 
89  this->set(nullptr);
90 
91  return &this->m_ptr;
92  }
93 
98  bool operator == (const Ref& other) const noexcept {
99  return this->get() == other.get();
100  }
101 
106  bool operator != (const Ref& other) const noexcept {
107  return !(*this == other);
108  }
109 
110  #ifndef DOXYGEN_SHOULD_SKIP_THIS
111 
116  Ref& operator = (const WeakRef<T>& other) noexcept;
117 
118  #endif /* DOXYGEN_SHOULD_SKIP_THIS */
119 
124  Ref& operator = (const Ref& other) noexcept {
125  assign(other.get());
126  return *this;
127  }
128 
133  template<class D, typename = typename std::enable_if<std::is_convertible<D*, T*>::value>::type>
134  Ref& operator = (const Ref<D>& other) noexcept {
135  assign(other.get());
136  return *this;
137  }
138 
144  FSDK_DEPRECATED("Assignment from raw pointer is deprecated, use make_ref function instead")
145  Ref& operator = (T* ptr) noexcept {
146  assign(ptr);
147  return *this;
148  }
149 
154  Ref& operator = (std::nullptr_t) noexcept {
155  reset();
156  return *this;
157  }
158 
163  Ref& assign(T* ptr) noexcept {
164  if(this->get() != ptr)
165  {
166  if(this->get())
167  this->get()->release();
168 
169  this->set(ptr);
170 
171  if(this->get())
172  this->get()->retain();
173  }
174 
175  return *this;
176  }
177 
181  Ref& acquire(T* ptr) noexcept {
182  if(this->get() != ptr)
183  {
184  if(this->get())
185  this->get()->release();
186 
187  this->set(ptr);
188  }
189 
190  return *this;
191  }
192 
196  Ref& make_ref(T* ptr) noexcept {
197  return assign(ptr);
198  }
199 
203  T* reset() noexcept {
204  T* ptr = this->get();
205  assign(nullptr);
206  return ptr;
207  }
208 
212  template<typename S> Ref<S> as() const noexcept {
213  Ref<S> ref;
214  ref.make_ref(static_cast<S*>(this->get()));
215  return ref;
216  }
217  };
218 
219 
224  template<typename T>
225  inline Ref<T> make_ref(T* ptr) noexcept {
226  Ref<T> ref;
227  ref.make_ref(ptr);
228  return ref;
229  }
230 
231 
237  template<typename S, typename T>
238  inline Ref<S> make_ref_as(T* ptr) noexcept {
239  Ref<T> ref;
240  ref.make_ref(ptr);
241  return ref.template as<S>();
242  }
243 
244 
249  template<typename T>
250  inline Ref<T> acquire(T* ptr) noexcept {
251  Ref<T> ref;
252  ref.acquire(ptr);
253  return ref;
254  }
255 
256 
262  template<typename S, typename T>
263  inline Ref<S> acquire_as(T* ptr) noexcept {
264  Ref<S> ref;
265  ref.acquire(static_cast<S*>(ptr));
266  return ref;
267  }
268 }
bool operator==(const Ref &other) const noexcept
Check if two refs are the same.
Definition: Ref.h:98
Ref & operator=(const Ref &other) noexcept
Assign a strong reference.
Definition: Ref.h:124
Ref & acquire(T *ptr) noexcept
Acquire ownership of the object.
Definition: Ref.h:181
Ref< T > make_ref(T *ptr) noexcept
Make smart reference to a IRefCounted based object without acquiring ownership.
Definition: Ref.h:225
Ref< S > as() const noexcept
Make smart reference with relative type.
Definition: Ref.h:212
Smart pointer for reference counted objects.
Definition: Ref.h:26
~Ref() noexcept
Releases reference being held (if any).
Definition: Ref.h:68
T * reset() noexcept
Reset reference counted object and assign nullptr to the pointer.
Definition: Ref.h:203
bool operator!=(const Ref &other) const noexcept
Check if two refs are not the same.
Definition: Ref.h:106
Ref() noexcept=default
Constructor. Initializes object pointer to nullptr.
Generic base class of a pointer for reference counted objects.
Definition: RefBase.h:17
T * m_ptr
Raw pointer.
Definition: RefBase.h:106
Common SDK definitions.
void set(T *ptr) noexcept
Replace object pointer without any checks or reference management.
Definition: RefBase.h:101
Ref(std::nullptr_t) noexcept
Initializes object pointer to nullptr.
Definition: Ref.h:35
bool isNull() const noexcept
Check for nullptr.
Definition: RefBase.h:70
Ref & make_ref(T *ptr) noexcept
Create ref without acquiring ownership.
Definition: Ref.h:196
Ref(const Ref< D > &other)
Initializes object pointer with other and retains a reference.
Definition: Ref.h:55
Ref(const Ref &other)
Initializes object pointer with other and retains a reference.
Definition: Ref.h:48
Ref< S > acquire_as(T *ptr) noexcept
Acquire ownership of IRefCounted based object with a cast to a given type.
Definition: Ref.h:263
Ref & assign(T *ptr) noexcept
Assign an object. Presumes shared ownership, increases reference count.
Definition: Ref.h:163
T ** getInitReference() noexcept
Access pointer for initialization.
Definition: Ref.h:85
T * operator->() const noexcept
Access pointer.
Definition: Ref.h:76
Ref< S > make_ref_as(T *ptr) noexcept
Make smart reference to a IRefCounted based object without acquiring ownership.
Definition: Ref.h:238
Ref< T > acquire(T *ptr) noexcept
Acquire ownership of IRefCounted based object.
Definition: Ref.h:250