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

Ref.h
1 #pragma once
2
3 namespace fsdk
4 {
5 #ifndef DOXYGEN_SHOULD_SKIP_THIS
6
7 /* Forward decalrations. */
8 template<typename T> struct WeakRef;
9
10 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
11
19 template<typename T>
20 struct Ref : RefBase<T> {
21
25 Ref() noexcept {}
26
29 Ref(std::nullptr_t) noexcept {}
30
35 explicit Ref(T* ptr) noexcept { *this = ptr; }
36
41 Ref(const Ref& other) noexcept { *this = other; }
42
43 #ifndef DOXYGEN_SHOULD_SKIP_THIS
44
48 Ref(const WeakRef<T>& other) noexcept { *this = other; }
49
50 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
51
54 ~Ref() noexcept {
55 reset();
56 }
57
62 T* operator -> () const noexcept {
63 assert(!this->isNull());
64 return this->get();
65 }
66
71 T** getInitReference() noexcept {
72 if(this->get())
73 this->get()->release();
74
75 this->set(nullptr);
76
77 return &this->m_ptr;
78 }
79
84 bool operator == (const Ref& other) const noexcept {
85 return this->get() == other.get();
86 }
87
92 bool operator != (const Ref& other) const noexcept {
93 return !(*this == other);
94 }
95
96 #ifndef DOXYGEN_SHOULD_SKIP_THIS
97
102 Ref& operator = (const WeakRef<T>& other) noexcept;
103
104 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
105
110 Ref& operator = (const Ref& other) noexcept {
111 return assign(other.get());
112 }
113
119 Ref& operator = (T* ptr) noexcept {
120 return assign(ptr);
121 }
122
127 Ref& operator = (std::nullptr_t) noexcept {
128 reset();
129 return *this;
130 }
131
136 Ref& assign(T* ptr) noexcept {
137 if(this->get() != ptr)
138 {
139 if(this->get())
140 this->get()->release();
141
142 this->set(ptr);
143
144 // if retain returns 0 or less it means refcounted object was destroyed in some other
145 // thread, so you cant use it and the only solution is to reset Ref
146 // tbh it should never return negative numbers, but just to be save check it
147 if(this->get() && this->get()->retain() < 1)
148 this->set(nullptr);
149 }
150
151 return *this;
152 }
153
157 Ref& acquire(T* ptr) noexcept {
158 if(this->get() != ptr)
159 {
160 if(this->get())
161 this->get()->release();
162
163 this->set(ptr);
164 }
165
166 return *this;
167 }
168
172 //void reset() noexcept {
173 T* reset() noexcept {
174 T* ptr = this->get();
175 assign(nullptr);
176 return ptr;
177 }
178
182 template<typename S> Ref<S> as() const noexcept {
183 return Ref<S>(static_cast<S*>(this->get()));
184 }
185 };
186
187
192 template<typename T>
193 inline Ref<T> make_ref(T* ptr) noexcept {
194 return Ref<T>(ptr);
195 }
196
197
203 template<typename S, typename T>
204 inline Ref<S> make_ref_as(T* ptr) noexcept {
205 return Ref<S>(static_cast<S*>(ptr));
206 }
207
208
213 template<typename T>
214 inline Ref<T> acquire(T* ptr) noexcept {
215 Ref<T> ref;
216 ref.acquire(ptr);
217 return ref;
218 }
219
220
226 template<typename S, typename T>
227 inline Ref<S> acquire_as(T* ptr) noexcept {
228 Ref<S> ref;
229 ref.acquire(static_cast<S*>(ptr));
230 return ref;
231 }
232 }
fsdk::Ref::operator==
bool operator==(const Ref &other) const noexcept
Check if two refs are the same.
Definition: Ref.h:84
fsdk::Ref::operator=
Ref & operator=(const Ref &other) noexcept
Assign a strong reference.
Definition: Ref.h:110
fsdk
SDK namespace.
Definition: IAGSEstimator.h:8
fsdk::Ref::acquire
Ref & acquire(T *ptr) noexcept
Acquire ownership of the object.
Definition: Ref.h:157
fsdk::make_ref
Ref< T > make_ref(T *ptr) noexcept
Make smart reference to a IRefCounted based object.
Definition: Ref.h:193
fsdk::Ref::as
Ref< S > as() const noexcept
Make smart reference with relative type.
Definition: Ref.h:182
fsdk::Ref::Ref
Ref(const Ref &other) noexcept
Initializes object pointer with other and retains a reference.
Definition: Ref.h:41
fsdk::Ref
Smart pointer for reference counted objects.
Definition: Ref.h:20
fsdk::Ref::~Ref
~Ref() noexcept
Releases reference being held (if any).
Definition: Ref.h:54
fsdk::Ref::reset
T * reset() noexcept
Reset reference counted object and assign nullptr to the pointer.
Definition: Ref.h:173
fsdk::Ref::operator!=
bool operator!=(const Ref &other) const noexcept
Check if two refs are not the same.
Definition: Ref.h:92
fsdk::RefBase
Generic base class of a pointer for reference counted objects.
Definition: RefBase.h:15
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:35
fsdk::Ref::Ref
Ref(std::nullptr_t) noexcept
Initializes object pointer to nullptr.
Definition: Ref.h:29
fsdk::RefBase::isNull
bool isNull() const noexcept
Check for nullptr.
Definition: RefBase.h:68
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:227
fsdk::Ref::assign
Ref & assign(T *ptr) noexcept
Assign an object. Presumes shared ownership, increases reference count.
Definition: Ref.h:136
fsdk::Ref::getInitReference
T ** getInitReference() noexcept
Access pointer for initialization.
Definition: Ref.h:71
fsdk::Ref::operator->
T * operator->() const noexcept
Access pointer.
Definition: Ref.h:62
fsdk::make_ref_as
Ref< S > make_ref_as(T *ptr) noexcept
Make smart reference to a IRefCounted based object.
Definition: Ref.h:204
fsdk::Ref::Ref
Ref() noexcept
Constructor. Initializes object pointer to nullptr.
Definition: Ref.h:25
fsdk::acquire
Ref< T > acquire(T *ptr) noexcept
Acquire ownership of IRefCounted based object.
Definition: Ref.h:214