| Face Engine SDK 5.32.0 A face detection, recognition and tracking engine.

Ref.h
1 #pragma once
2
3 #include <cassert>
4 #include <type_traits>
5 #include <utility>
6
7 #include <fsdk/Def.h>
8 #include <fsdk/Types/RefBase.h>
9
10 namespace fsdk {
11 #ifndef DOXYGEN_SHOULD_SKIP_THIS
12
13 /* Forward declarations. */
14 template <typename T>
15 struct WeakRef;
16
17 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
18
26 template <typename T>
27 struct Ref : RefBase<T> {
28
32 Ref() noexcept = default;
33
36 Ref(std::nullptr_t) noexcept {
37 }
38
43 FSDK_DEPRECATED("construction from raw pointer is deprecated, use make_ref function instead")
44
45 explicit Ref(T* ptr) noexcept {
46 *this = ptr;
47 }
48
53 Ref(const Ref& other)
54 : RefBase<T>() {
55 *this = other;
56 }
57
62 template <class D, typename = typename std::enable_if<std::is_convertible<D*, T*>::value>::type>
63 explicit Ref(const Ref<D>& other)
64 : RefBase<T>() {
65 *this = other;
66 }
67
76 Ref(Ref&& other) noexcept
77 : RefBase<T>() {
78 move(std::move(other));
79 }
80
89 template <class D, typename = typename std::enable_if<std::is_convertible<D*, T*>::value>::type>
90 explicit Ref(Ref<D>&& other) noexcept
91 : RefBase<T>() {
92 move(std::move(other));
93 }
94
95 #ifndef DOXYGEN_SHOULD_SKIP_THIS
96
100 Ref(const WeakRef<T>& other) noexcept {
101 *this = other;
102 }
103
104 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
105
108 ~Ref() noexcept {
109 reset();
110 }
111
116 T* operator->() const noexcept {
117 assert(!this->isNull());
118 return this->get();
119 }
120
125 T** getInitReference() noexcept {
126 if(this->get())
127 this->get()->release();
128
129 this->set(nullptr);
130
131 return &this->m_ptr;
132 }
133
138 bool operator==(const Ref& other) const noexcept {
139 return this->get() == other.get();
140 }
141
146 bool operator!=(const Ref& other) const noexcept {
147 return !(*this == other);
148 }
149
150 #ifndef DOXYGEN_SHOULD_SKIP_THIS
151
156 Ref& operator=(const WeakRef<T>& other) noexcept;
157
158 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
159
164 Ref& operator=(const Ref& other) noexcept {
165 assign(other.get());
166 return *this;
167 }
168
173 template <class D, typename = typename std::enable_if<std::is_convertible<D*, T*>::value>::type>
174 Ref& operator=(const Ref<D>& other) noexcept {
175 assign(other.get());
176 return *this;
177 }
178
188 Ref& operator=(Ref&& other) noexcept {
189 return move(std::move(other));
190 }
191
201 template <class D, typename = typename std::enable_if<std::is_convertible<D*, T*>::value>::type>
202 Ref& operator=(Ref<D>&& other) noexcept {
203 return move(std::move(other));
204 }
205
211 FSDK_DEPRECATED("Assignment from raw pointer is deprecated, use make_ref function instead")
212
213 Ref& operator=(T* ptr) noexcept {
214 assign(ptr);
215 return *this;
216 }
217
222 Ref& operator=(std::nullptr_t) noexcept {
223 reset();
224 return *this;
225 }
226
231 Ref& assign(T* ptr) noexcept {
232 if(this->get() != ptr) {
233 if(this->get())
234 this->get()->release();
235
236 this->set(ptr);
237
238 if(this->get())
239 this->get()->retain();
240 }
241
242 return *this;
243 }
244
248 Ref& acquire(T* ptr) noexcept {
249 if(this->get() != ptr) {
250 if(this->get())
251 this->get()->release();
252
253 this->set(ptr);
254 }
255
256 return *this;
257 }
258
268 template <class D, typename = typename std::enable_if<std::is_convertible<D*, T*>::value>::type>
269 Ref& move(Ref<D>&& other) noexcept {
270 auto* ptr = other.get();
271 if(this->get() != ptr) {
272 if(this->get())
273 this->get()->release();
274
275 this->set(ptr);
276
277 if(ptr) {
278 other.set(nullptr);
279 }
280 }
281
282 return *this;
283 }
284
288 Ref& make_ref(T* ptr) noexcept {
289 return assign(ptr);
290 }
291
295 T* reset() noexcept {
296 T* ptr = this->get();
297 assign(nullptr);
298 return ptr;
299 }
300
304 template <typename S>
305 Ref<S> as() const noexcept {
306 Ref<S> ref;
307 ref.make_ref(static_cast<S*>(this->get()));
308 return ref;
309 }
310 };
311
316 template <typename T>
317 inline Ref<T> make_ref(T* ptr) noexcept {
318 Ref<T> ref;
319 ref.make_ref(ptr);
320 return ref;
321 }
322
328 template <typename S, typename T>
329 inline Ref<S> make_ref_as(T* ptr) noexcept {
330 Ref<T> ref;
331 ref.make_ref(ptr);
332 return ref.template as<S>();
333 }
334
339 template <typename T>
340 inline Ref<T> acquire(T* ptr) noexcept {
341 Ref<T> ref;
342 ref.acquire(ptr);
343 return ref;
344 }
345
351 template <typename S, typename T>
352 inline Ref<S> acquire_as(T* ptr) noexcept {
353 Ref<S> ref;
354 ref.acquire(static_cast<S*>(ptr));
355 return ref;
356 }
357 } // namespace fsdk
fsdk::Ref::operator==
bool operator==(const Ref &other) const noexcept
Check if two refs are the same.
Definition: Ref.h:138
fsdk::Ref::operator=
Ref & operator=(std::nullptr_t) noexcept
Assign a nullptr_t.
Definition: Ref.h:222
fsdk::Ref::operator=
Ref & operator=(const Ref< D > &other) noexcept
Assign a strong reference.
Definition: Ref.h:174
fsdk::Ref::operator=
Ref & operator=(const Ref &other) noexcept
Assign a strong reference.
Definition: Ref.h:164
fsdk
SDK namespace.
Definition: IAGSEstimator.h:8
fsdk::Ref::move
Ref & move(Ref< D > &&other) noexcept
Moves other&#39;s resource to this Ref instance. Leaves other Ref empty. Reference count of the resource ...
Definition: Ref.h:269
fsdk::Ref::acquire
Ref & acquire(T *ptr) noexcept
Acquire ownership of the object.
Definition: Ref.h:248
fsdk::make_ref
Ref< T > make_ref(T *ptr) noexcept
Make smart reference to a IRefCounted based object without acquiring ownership.
Definition: Ref.h:317
fsdk::Ref::as
Ref< S > as() const noexcept
Make smart reference with relative type.
Definition: Ref.h:305
fsdk::Ref
Smart pointer for reference counted objects.
Definition: Ref.h:27
fsdk::Ref::~Ref
~Ref() noexcept
Releases reference being held (if any).
Definition: Ref.h:108
fsdk::RefBase::get
T * get() const noexcept
Get current raw object pointer.
Definition: RefBase.h:94
fsdk::Ref::reset
T * reset() noexcept
Reset reference counted object and assign nullptr to the pointer.
Definition: Ref.h:295
fsdk::Ref::operator!=
bool operator!=(const Ref &other) const noexcept
Check if two refs are not the same.
Definition: Ref.h:146
fsdk::Ref::Ref
Ref() noexcept=default
Constructor. Initializes object pointer to nullptr.
fsdk::RefBase
Generic base class of a pointer for reference counted objects.
Definition: RefBase.h:16
fsdk::RefBase::m_ptr
T * m_ptr
Raw pointer.
Definition: RefBase.h:107
Def.h
Common SDK definitions.
fsdk::Ref::Ref
Ref(Ref &&other) noexcept
Constructs Ref by moving other&#39;s resource to the new Ref instance. Leaves other Ref empty...
Definition: Ref.h:76
fsdk::Ref::Ref
Ref(std::nullptr_t) noexcept
Initializes object pointer to nullptr.
Definition: Ref.h:36
fsdk::RefBase::isNull
bool isNull() const noexcept
Check for nullptr.
Definition: RefBase.h:71
fsdk::Ref::make_ref
Ref & make_ref(T *ptr) noexcept
Create ref without acquiring ownership.
Definition: Ref.h:288
fsdk::Ref::Ref
Ref(const Ref< D > &other)
Initializes object pointer with other and retains a reference.
Definition: Ref.h:63
fsdk::Ref::Ref
Ref(const Ref &other)
Initializes object pointer with other and retains a reference.
Definition: Ref.h:53
fsdk::acquire_as
Ref< S > acquire_as(T *ptr) noexcept
Acquire ownership of IRefCounted based object with a cast to a given type.
Definition: Ref.h:352
fsdk::Ref::assign
Ref & assign(T *ptr) noexcept
Assign an object. Presumes shared ownership, increases reference count.
Definition: Ref.h:231
fsdk::Ref::getInitReference
T ** getInitReference() noexcept
Access pointer for initialization.
Definition: Ref.h:125
fsdk::Ref::operator->
T * operator->() const noexcept
Access pointer.
Definition: Ref.h:116
fsdk::make_ref_as
Ref< S > make_ref_as(T *ptr) noexcept
Make smart reference to a IRefCounted based object without acquiring ownership.
Definition: Ref.h:329
fsdk::Ref::Ref
Ref(Ref< D > &&other) noexcept
Constructs Ref by moving other&#39;s resource to the new Ref instance. Leaves other Ref empty...
Definition: Ref.h:90
fsdk::Ref::operator=
Ref & operator=(Ref &&other) noexcept
Moves other&#39;s resource to this Ref instance. Leaves other Ref empty. Reference count of the resource ...
Definition: Ref.h:188
fsdk::Ref::operator=
Ref & operator=(Ref< D > &&other) noexcept
Moves other&#39;s resource to this Ref instance. Leaves other Ref empty. Reference count of the resource ...
Definition: Ref.h:202
fsdk::acquire
Ref< T > acquire(T *ptr) noexcept
Acquire ownership of IRefCounted based object.
Definition: Ref.h:340