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

WeakRef.h
1 #pragma once
2 #include <fsdk/Def.h>
3
4 namespace fsdk
5 {
6 #ifndef DOXYGEN_SHOULD_SKIP_THIS
7
15 template<typename T>
16 struct WeakRef : RefBase<T> {
17
20 WeakRef() noexcept {}
21
24 WeakRef(std::nullptr_t) noexcept {}
25
29 explicit WeakRef(T* ptr) noexcept { *this = ptr; }
30
34 WeakRef(const WeakRef& other) noexcept { *this = other; }
35
39 WeakRef(const Ref<T>& other) noexcept { *this = other; }
40
43 ~WeakRef() {
44 reset();
45 }
46
51 WeakRef& operator = (const Ref<T>& other) noexcept {
52 return assign(other.get());
53 }
54
59 WeakRef& operator = (const WeakRef& other) noexcept {
60 return assign(other.get());
61 }
62
68 WeakRef& operator = (T* ptr) noexcept {
69 return assign(ptr);
70 }
71
76 WeakRef& operator = (std::nullptr_t) noexcept {
77 reset();
78 return *this;
79 }
80
85 WeakRef& assign(T* ptr) noexcept {
86 if(this->get() != ptr)
87 {
88 if(this->get())
89 this->get()->releaseWeak();
90
91 this->set(ptr);
92
93 // if retainWeak returns 0 or less it means refcounted object was deallocated in some other
94 // thread, so you cant use it and the only solution is to reset WeakRef
95 // tbh it should never return negative numbers, but just to be save check it
96 if(this->get() && this->get()->retainWeak() < 1)
97 this->set(nullptr);
98 }
99
100 return *this;
101 }
102
106 void reset() noexcept {
107 assign(nullptr);
108 }
109
114 Ref<T> lock() const noexcept {
115 return (!this->get() || this->isExpired()) ? Ref<T>() : Ref<T>(this->get());
116 }
117
121 template<typename S> WeakRef<S> as() noexcept {
122 return WeakRef<S>(static_cast<S*>(this->get()));
123 }
124 };
125
126
131 template<typename T>
132 inline WeakRef<T> make_weak_ref(T* ptr) noexcept {
133 return WeakRef<T>(ptr);
134 }
135
136
142 template<typename S, typename T>
143 inline WeakRef<S> make_weak_ref_as(T* ptr) noexcept {
144 return WeakRef<S>(static_cast<S*>(ptr));
145 }
146
147 /* Documented elsewhere. */
148 template<typename T>
149 inline 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 }
fsdk::Ref::operator=
Ref & operator=(const Ref &other) noexcept
Assign a strong reference.
Definition: Ref.h:112
fsdk
SDK namespace.
Definition: IAGSEstimator.h:8