Face Engine SDK  4.6.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 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 }
bool operator==(const Ref &other) const noexcept
Check if two refs are the same.
Definition: Ref.h:84
Ref & operator=(const Ref &other) noexcept
Assign a strong reference.
Definition: Ref.h:110
Ref & acquire(T *ptr) noexcept
Acquire ownership of the object.
Definition: Ref.h:157
Ref< T > make_ref(T *ptr) noexcept
Make smart reference to a IRefCounted based object.
Definition: Ref.h:193
Ref< S > as() const noexcept
Make smart reference with relative type.
Definition: Ref.h:182
Ref(const Ref &other) noexcept
Initializes object pointer with other and retains a reference.
Definition: Ref.h:41
Smart pointer for reference counted objects.
Definition: Ref.h:20
~Ref() noexcept
Releases reference being held (if any).
Definition: Ref.h:54
T * reset() noexcept
Reset reference counted object and assign nullptr to the pointer.
Definition: Ref.h:173
bool operator!=(const Ref &other) const noexcept
Check if two refs are not the same.
Definition: Ref.h:92
Generic base class of a pointer for reference counted objects.
Definition: RefBase.h:15
T * m_ptr
Raw pointer.
Definition: RefBase.h:104
Ref(T *ptr) noexcept
Initializes object pointer with ptr and retains a reference.
Definition: Ref.h:35
void set(T *ptr) noexcept
Replace object pointer without any checks or reference management.
Definition: RefBase.h:99
Ref(std::nullptr_t) noexcept
Initializes object pointer to nullptr.
Definition: Ref.h:29
bool isNull() const noexcept
Check for nullptr.
Definition: RefBase.h:68
Ref< S > acquire_as(T *ptr) noexcept
Acquire ownership of IRefCounted based object with a cast to a given type.
Definition: Ref.h:227
Ref & assign(T *ptr) noexcept
Assign an object. Presumes shared ownership, increases reference count.
Definition: Ref.h:136
T ** getInitReference() noexcept
Access pointer for initialization.
Definition: Ref.h:71
T * operator->() const noexcept
Access pointer.
Definition: Ref.h:62
Ref< S > make_ref_as(T *ptr) noexcept
Make smart reference to a IRefCounted based object.
Definition: Ref.h:204
Ref() noexcept
Constructor. Initializes object pointer to nullptr.
Definition: Ref.h:25
Ref< T > acquire(T *ptr) noexcept
Acquire ownership of IRefCounted based object.
Definition: Ref.h:214