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

Ref.h
1 #pragma once
2
3 #include "RefBase.h"
4
5 namespace fsdk
6 {
7 #ifndef DOXYGEN_SHOULD_SKIP_THIS
8
9 /* Forward decalrations. */
10 template<typename T> struct WeakRef;
11
12 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
13
21 template<typename T>
22 struct Ref : RefBase<T> {
23
27 Ref() noexcept {}
28
31 Ref(std::nullptr_t) noexcept {}
32
37 explicit Ref(T* ptr) noexcept { *this = ptr; }
38
43 Ref(const Ref& other) : RefBase<T>() { *this = other; }
44
45 #ifndef DOXYGEN_SHOULD_SKIP_THIS
46
50 Ref(const WeakRef<T>& other) noexcept { *this = other; }
51
52 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
53
56 ~Ref() noexcept {
57 reset();
58 }
59
64 T* operator -> () const noexcept {
65 assert(!this->isNull());
66 return this->get();
67 }
68
73 T** getInitReference() noexcept {
74 if(this->get())
75 this->get()->release();
76
77 this->set(nullptr);
78
79 return &this->m_ptr;
80 }
81
86 bool operator == (const Ref& other) const noexcept {
87 return this->get() == other.get();
88 }
89
94 bool operator != (const Ref& other) const noexcept {
95 return !(*this == other);
96 }
97
98 #ifndef DOXYGEN_SHOULD_SKIP_THIS
99
104 Ref& operator = (const WeakRef<T>& other) noexcept;
105
106 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
107
112 Ref& operator = (const Ref& other) noexcept {
113 return assign(other.get());
114 }
115
121 Ref& operator = (T* ptr) noexcept {
122 return assign(ptr);
123 }
124
129 Ref& operator = (std::nullptr_t) noexcept {
130 reset();
131 return *this;
132 }
133
138 Ref& assign(T* ptr) noexcept {
139 if(this->get() != ptr)
140 {
141 if(this->get())
142 this->get()->release();
143
144 this->set(ptr);
145
146 // if retain returns 0 or less it means refcounted object was destroyed in some other
147 // thread, so you cant use it and the only solution is to reset Ref
148 // tbh it should never return negative numbers, but just to be save check it
149 if(this->get() && this->get()->retain() < 1)
150 this->set(nullptr);
151 }
152
153 return *this;
154 }
155
159 Ref& acquire(T* ptr) noexcept {
160 if(this->get() != ptr)
161 {
162 if(this->get())
163 this->get()->release();
164
165 this->set(ptr);
166 }
167
168 return *this;
169 }
170
174 //void reset() noexcept {
175 T* reset() noexcept {
176 T* ptr = this->get();
177 assign(nullptr);
178 return ptr;
179 }
180
184 template<typename S> Ref<S> as() const noexcept {
185 return Ref<S>(static_cast<S*>(this->get()));
186 }
187 };
188
189
194 template<typename T>
195 inline Ref<T> make_ref(T* ptr) noexcept {
196 return Ref<T>(ptr);
197 }
198
199
205 template<typename S, typename T>
206 inline Ref<S> make_ref_as(T* ptr) noexcept {
207 return Ref<S>(static_cast<S*>(ptr));
208 }
209
210
215 template<typename T>
216 inline Ref<T> acquire(T* ptr) noexcept {
217 Ref<T> ref;
218 ref.acquire(ptr);
219 return ref;
220 }
221
222
228 template<typename S, typename T>
229 inline Ref<S> acquire_as(T* ptr) noexcept {
230 Ref<S> ref;
231 ref.acquire(static_cast<S*>(ptr));
232 return ref;
233 }
234 }
fsdk::Ref::reset
T * reset() noexcept
Reset reference counted object and assign nullptr to the pointer.
Definition: Ref.h:175
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:229
fsdk::acquire
Ref< T > acquire(T *ptr) noexcept
Acquire ownership of IRefCounted based object.
Definition: Ref.h:216
fsdk::RefBase::set
void set(T *ptr) noexcept
Replace object pointer without any checks or reference management.
Definition: RefBase.h:99
fsdk::Ref::operator=
Ref & operator=(const Ref &other) noexcept
Assign a strong reference.
Definition: Ref.h:112
fsdk::Ref::Ref
Ref(std::nullptr_t) noexcept
Initializes object pointer to nullptr.
Definition: Ref.h:31
fsdk
SDK namespace.
Definition: IAGSEstimator.h:8
fsdk::Ref::operator==
bool operator==(const Ref &other) const noexcept
Check if two refs are the same.
Definition: Ref.h:86
fsdk::Ref::acquire
Ref & acquire(T *ptr) noexcept
Acquire ownership of the object.
Definition: Ref.h:159
fsdk::Ref::Ref
Ref() noexcept
Constructor. Initializes object pointer to nullptr.
Definition: Ref.h:27
fsdk::Ref::~Ref
~Ref() noexcept
Releases reference being held (if any).
Definition: Ref.h:56
fsdk::RefBase::isNull
bool isNull() const noexcept
Check for nullptr.
Definition: RefBase.h:68
fsdk::make_ref_as
Ref< S > make_ref_as(T *ptr) noexcept
Make smart reference to a IRefCounted based object.
Definition: Ref.h:206
fsdk::RefBase
Generic base class of a pointer for reference counted objects.
Definition: RefBase.h:15
fsdk::Ref::assign
Ref & assign(T *ptr) noexcept
Assign an object. Presumes shared ownership, increases reference count.
Definition: Ref.h:138
fsdk::RefBase::m_ptr
T * m_ptr
Raw pointer.
Definition: RefBase.h:104
fsdk::Ref::Ref
Ref(T *ptr) noexcept
Initializes object pointer with ptr and retains a reference.
Definition: Ref.h:37
fsdk::make_ref
Ref< T > make_ref(T *ptr) noexcept
Make smart reference to a IRefCounted based object.
Definition: Ref.h:195
fsdk::Ref::operator!=
bool operator!=(const Ref &other) const noexcept
Check if two refs are not the same.
Definition: Ref.h:94
fsdk::Ref
Smart pointer for reference counted objects.
Definition: Ref.h:22
fsdk::Ref::getInitReference
T ** getInitReference() noexcept
Access pointer for initialization.
Definition: Ref.h:73
fsdk::Ref::as
Ref< S > as() const noexcept
Make smart reference with relative type.
Definition: Ref.h:184
fsdk::Ref::Ref
Ref(const Ref &other)
Initializes object pointer with other and retains a reference.
Definition: Ref.h:43
fsdk::RefBase::get
T * get() const noexcept
Get current raw object pointer.
Definition: RefBase.h:91
fsdk::Ref::operator->
T * operator->() const noexcept
Access pointer.
Definition: Ref.h:64