Face Engine SDK  5.8.0
A face detection, recognition and tracking engine.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
WeakRef.h
1 #pragma once
2 
3 #include "RefBase.h"
4 #include "Ref.h"
5 
6 namespace fsdk
7 {
8 #ifndef DOXYGEN_SHOULD_SKIP_THIS
9 
17  template<typename T>
18  struct WeakRef : RefBase<T> {
19 
22  WeakRef() noexcept = default;
23 
26  WeakRef(std::nullptr_t) noexcept {}
27 
31  explicit WeakRef(T* ptr) noexcept { *this = ptr; }
32 
36  WeakRef(const WeakRef& other) noexcept { *this = other; }
37 
41  WeakRef(const Ref<T>& other) noexcept { *this = other; }
42 
45  ~WeakRef() {
46  reset();
47  }
48 
53  WeakRef& operator = (const Ref<T>& other) noexcept {
54  return assign(other.get());
55  }
56 
61  WeakRef& operator = (const WeakRef& other) noexcept {
62  return assign(other.get());
63  }
64 
70  WeakRef& operator = (T* ptr) noexcept {
71  return assign(ptr);
72  }
73 
78  WeakRef& operator = (std::nullptr_t) noexcept {
79  reset();
80  return *this;
81  }
82 
87  WeakRef& assign(T* ptr) noexcept {
88  if(this->get() != ptr)
89  {
90  if(this->get())
91  this->get()->releaseWeak();
92 
93  this->set(ptr);
94 
95  if(this->get())
96  this->get()->retainWeak();
97  }
98 
99  return *this;
100  }
101 
105  void reset() noexcept {
106  assign(nullptr);
107  }
108 
113  Ref<T> lock() const noexcept {
114  return (!this->get() || this->isExpired()) ? Ref<T>() : Ref<T>(*this);
115  }
116 
120  template<typename S> WeakRef<S> as() noexcept {
121  return WeakRef<S>(static_cast<S*>(this->get()));
122  }
123  };
124 
125 
130  template<typename T>
131  inline WeakRef<T> make_weak_ref(T* ptr) noexcept {
132  return WeakRef<T>(ptr);
133  }
134 
135 
141  template<typename S, typename T>
142  inline WeakRef<S> make_weak_ref_as(T* ptr) noexcept {
143  return WeakRef<S>(static_cast<S*>(ptr));
144  }
145 
146  /* Documented elsewhere. */
147  template<typename T>
148  inline Ref<T>&
149  Ref<T>::operator = (const WeakRef<T>& other) noexcept {
150  if(this->get() != other.get())
151  {
152  if(this->get())
153  this->get()->release();
154 
155  this->set(other.get());
156 
157  // if retainLocked returns 0 or less it means refcounted object was destroyed in some other thread
158  if(this->get() && this->get()->retainLocked() < 1)
159  this->set(nullptr);
160  }
161 
162  return *this;
163  }
164 
165 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
166 }
Ref & operator=(const Ref &other) noexcept
Assign a strong reference.
Definition: Ref.h:124