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

WeakRef.h
1 #pragma once
2
3 #include <fsdk/Types/Ref.h>
4 #include <fsdk/Types/RefBase.h>
5
6 namespace fsdk {
7 #ifndef DOXYGEN_SHOULD_SKIP_THIS
8
16 template <typename T>
17 struct WeakRef : RefBase<T> {
18
21 WeakRef() noexcept = default;
22
25 WeakRef(std::nullptr_t) noexcept {
26 }
27
31 explicit WeakRef(T* ptr) noexcept {
32 *this = ptr;
33 }
34
38 WeakRef(const WeakRef& other) noexcept {
39 *this = other;
40 }
41
45 WeakRef(const Ref<T>& other) noexcept {
46 *this = other;
47 }
48
51 ~WeakRef() {
52 reset();
53 }
54
59 WeakRef& operator=(const Ref<T>& other) noexcept {
60 return assign(other.get());
61 }
62
67 WeakRef& operator=(const WeakRef& other) noexcept {
68 return assign(other.get());
69 }
70
76 WeakRef& operator=(T* ptr) noexcept {
77 return assign(ptr);
78 }
79
84 WeakRef& operator=(std::nullptr_t) noexcept {
85 reset();
86 return *this;
87 }
88
93 WeakRef& assign(T* ptr) noexcept {
94 if(this->get() != ptr) {
95 if(this->get())
96 this->get()->releaseWeak();
97
98 this->set(ptr);
99
100 if(this->get())
101 this->get()->retainWeak();
102 }
103
104 return *this;
105 }
106
110 void reset() noexcept {
111 assign(nullptr);
112 }
113
118 Ref<T> lock() const noexcept {
119 return (!this->get() || this->isExpired()) ? Ref<T>() : Ref<T>(*this);
120 }
121
125 template <typename S>
126 WeakRef<S> as() noexcept {
127 return WeakRef<S>(static_cast<S*>(this->get()));
128 }
129 };
130
135 template <typename T>
136 inline WeakRef<T> make_weak_ref(T* ptr) noexcept {
137 return WeakRef<T>(ptr);
138 }
139
145 template <typename S, typename T>
146 inline WeakRef<S> make_weak_ref_as(T* ptr) noexcept {
147 return WeakRef<S>(static_cast<S*>(ptr));
148 }
149
150 /* Documented elsewhere. */
151 template <typename T>
152 inline Ref<T>& Ref<T>::operator=(const WeakRef<T>& other) noexcept {
153 if(this->get() != other.get()) {
154 if(this->get())
155 this->get()->release();
156
157 this->set(other.get());
158
159 // if retainLocked returns 0 or less it means refcounted object was destroyed in some other thread
160 if(this->get() && this->get()->retainLocked() < 1)
161 this->set(nullptr);
162 }
163
164 return *this;
165 }
166
167 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
168 } // namespace fsdk
fsdk::Ref::operator=
Ref & operator=(const Ref &other) noexcept
Assign a strong reference.
Definition: Ref.h:164
fsdk
SDK namespace.
Definition: IAGSEstimator.h:8