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
WeakRef.h
1 #pragma once
2 
3 namespace fsdk
4 {
5 #ifndef DOXYGEN_SHOULD_SKIP_THIS
6 
14  template<typename T>
15  struct WeakRef : RefBase<T> {
16 
19  WeakRef() noexcept {}
20 
23  WeakRef(std::nullptr_t) noexcept {}
24 
28  explicit WeakRef(T* ptr) noexcept { *this = ptr; }
29 
33  WeakRef(const WeakRef& other) noexcept { *this = other; }
34 
38  WeakRef(const Ref<T>& other) noexcept { *this = other; }
39 
42  ~WeakRef() {
43  reset();
44  }
45 
50  WeakRef& operator = (const Ref<T>& other) noexcept {
51  return assign(other.get());
52  }
53 
58  WeakRef& operator = (const WeakRef& other) noexcept {
59  return assign(other.get());
60  }
61 
67  WeakRef& operator = (T* ptr) noexcept {
68  return assign(ptr);
69  }
70 
75  WeakRef& operator = (std::nullptr_t) noexcept {
76  reset();
77  return *this;
78  }
79 
84  WeakRef& assign(T* ptr) noexcept {
85  if(this->get() != ptr)
86  {
87  if(this->get())
88  this->get()->releaseWeak();
89 
90  this->set(ptr);
91 
92  // if retainWeak returns 0 or less it means refcounted object was deallocated in some other
93  // thread, so you cant use it and the only solution is to reset WeakRef
94  // tbh it should never return negative numbers, but just to be save check it
95  if(this->get() && this->get()->retainWeak() < 1)
96  this->set(nullptr);
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->get());
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<
148  typename T>
149  Ref<T>&
150  Ref<T>::operator = (const WeakRef<T>& other) noexcept {
151  *this = other.lock();
152  return *this;
153  }
154 
155 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
156 }
Ref & operator=(const Ref &other) noexcept
Assign a strong reference.
Definition: Ref.h:110