Face Engine SDK  5.23.1
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 <fsdk/Def.h>
7 #include <fsdk/Types/RefBase.h>
8 
9 namespace fsdk {
10 #ifndef DOXYGEN_SHOULD_SKIP_THIS
11 
12  /* Forward declarations. */
13  template <typename T>
14  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  }
37 
42  FSDK_DEPRECATED("construction from raw pointer is deprecated, use make_ref function instead")
43 
44  explicit Ref(T* ptr) noexcept {
45  *this = ptr;
46  }
47 
52  Ref(const Ref& other)
53  : RefBase<T>() {
54  *this = other;
55  }
56 
61  template <class D, typename = typename std::enable_if<std::is_convertible<D*, T*>::value>::type>
62  explicit Ref(const Ref<D>& other)
63  : RefBase<T>() {
64  *this = other;
65  }
66 
67 #ifndef DOXYGEN_SHOULD_SKIP_THIS
68 
72  Ref(const WeakRef<T>& other) noexcept {
73  *this = other;
74  }
75 
76 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
77 
80  ~Ref() noexcept {
81  reset();
82  }
83 
88  T* operator->() const noexcept {
89  assert(!this->isNull());
90  return this->get();
91  }
92 
97  T** getInitReference() noexcept {
98  if(this->get())
99  this->get()->release();
100 
101  this->set(nullptr);
102 
103  return &this->m_ptr;
104  }
105 
110  bool operator==(const Ref& other) const noexcept {
111  return this->get() == other.get();
112  }
113 
118  bool operator!=(const Ref& other) const noexcept {
119  return !(*this == other);
120  }
121 
122 #ifndef DOXYGEN_SHOULD_SKIP_THIS
123 
128  Ref& operator=(const WeakRef<T>& other) noexcept;
129 
130 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
131 
136  Ref& operator=(const Ref& other) noexcept {
137  assign(other.get());
138  return *this;
139  }
140 
145  template <class D, typename = typename std::enable_if<std::is_convertible<D*, T*>::value>::type>
146  Ref& operator=(const Ref<D>& other) noexcept {
147  assign(other.get());
148  return *this;
149  }
150 
156  FSDK_DEPRECATED("Assignment from raw pointer is deprecated, use make_ref function instead")
157 
158  Ref& operator=(T* ptr) noexcept {
159  assign(ptr);
160  return *this;
161  }
162 
167  Ref& operator=(std::nullptr_t) noexcept {
168  reset();
169  return *this;
170  }
171 
176  Ref& assign(T* ptr) noexcept {
177  if(this->get() != ptr) {
178  if(this->get())
179  this->get()->release();
180 
181  this->set(ptr);
182 
183  if(this->get())
184  this->get()->retain();
185  }
186 
187  return *this;
188  }
189 
193  Ref& acquire(T* ptr) noexcept {
194  if(this->get() != ptr) {
195  if(this->get())
196  this->get()->release();
197 
198  this->set(ptr);
199  }
200 
201  return *this;
202  }
203 
207  Ref& make_ref(T* ptr) noexcept {
208  return assign(ptr);
209  }
210 
214  T* reset() noexcept {
215  T* ptr = this->get();
216  assign(nullptr);
217  return ptr;
218  }
219 
223  template <typename S>
224  Ref<S> as() const noexcept {
225  Ref<S> ref;
226  ref.make_ref(static_cast<S*>(this->get()));
227  return ref;
228  }
229  };
230 
235  template <typename T>
236  inline Ref<T> make_ref(T* ptr) noexcept {
237  Ref<T> ref;
238  ref.make_ref(ptr);
239  return ref;
240  }
241 
247  template <typename S, typename T>
248  inline Ref<S> make_ref_as(T* ptr) noexcept {
249  Ref<T> ref;
250  ref.make_ref(ptr);
251  return ref.template as<S>();
252  }
253 
258  template <typename T>
259  inline Ref<T> acquire(T* ptr) noexcept {
260  Ref<T> ref;
261  ref.acquire(ptr);
262  return ref;
263  }
264 
270  template <typename S, typename T>
271  inline Ref<S> acquire_as(T* ptr) noexcept {
272  Ref<S> ref;
273  ref.acquire(static_cast<S*>(ptr));
274  return ref;
275  }
276 } // namespace fsdk
bool operator==(const Ref &other) const noexcept
Check if two refs are the same.
Definition: Ref.h:110
Ref & operator=(std::nullptr_t) noexcept
Assign a nullptr_t.
Definition: Ref.h:167
Ref & operator=(const Ref< D > &other) noexcept
Assign a strong reference.
Definition: Ref.h:146
Ref & operator=(const Ref &other) noexcept
Assign a strong reference.
Definition: Ref.h:136
Ref & acquire(T *ptr) noexcept
Acquire ownership of the object.
Definition: Ref.h:193
Ref< T > make_ref(T *ptr) noexcept
Make smart reference to a IRefCounted based object without acquiring ownership.
Definition: Ref.h:236
Ref< S > as() const noexcept
Make smart reference with relative type.
Definition: Ref.h:224
Smart pointer for reference counted objects.
Definition: Ref.h:26
~Ref() noexcept
Releases reference being held (if any).
Definition: Ref.h:80
T * reset() noexcept
Reset reference counted object and assign nullptr to the pointer.
Definition: Ref.h:214
bool operator!=(const Ref &other) const noexcept
Check if two refs are not the same.
Definition: Ref.h:118
Ref() noexcept=default
Constructor. Initializes object pointer to nullptr.
Generic base class of a pointer for reference counted objects.
Definition: RefBase.h:16
T * m_ptr
Raw pointer.
Definition: RefBase.h:107
Common SDK definitions.
void set(T *ptr) noexcept
Replace object pointer without any checks or reference management.
Definition: RefBase.h:102
Ref(std::nullptr_t) noexcept
Initializes object pointer to nullptr.
Definition: Ref.h:35
bool isNull() const noexcept
Check for nullptr.
Definition: RefBase.h:71
Ref & make_ref(T *ptr) noexcept
Create ref without acquiring ownership.
Definition: Ref.h:207
Ref(const Ref< D > &other)
Initializes object pointer with other and retains a reference.
Definition: Ref.h:62
Ref(const Ref &other)
Initializes object pointer with other and retains a reference.
Definition: Ref.h:52
Ref< S > acquire_as(T *ptr) noexcept
Acquire ownership of IRefCounted based object with a cast to a given type.
Definition: Ref.h:271
Ref & assign(T *ptr) noexcept
Assign an object. Presumes shared ownership, increases reference count.
Definition: Ref.h:176
T ** getInitReference() noexcept
Access pointer for initialization.
Definition: Ref.h:97
T * operator->() const noexcept
Access pointer.
Definition: Ref.h:88
Ref< S > make_ref_as(T *ptr) noexcept
Make smart reference to a IRefCounted based object without acquiring ownership.
Definition: Ref.h:248
Ref< T > acquire(T *ptr) noexcept
Acquire ownership of IRefCounted based object.
Definition: Ref.h:259