Face Engine SDK  5.25.0
A face detection, recognition and tracking engine.
include/fsdk/Types/WeakRef.h
00001 #pragma once
00002 
00003 #include <fsdk/Types/Ref.h>
00004 #include <fsdk/Types/RefBase.h>
00005 
00006 namespace fsdk {
00007 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00008 
00016     template <typename T>
00017     struct WeakRef : RefBase<T> {
00018 
00021         WeakRef() noexcept = default;
00022 
00025         WeakRef(std::nullptr_t) noexcept {
00026         }
00027 
00031         explicit WeakRef(T* ptr) noexcept {
00032             *this = ptr;
00033         }
00034 
00038         WeakRef(const WeakRef& other) noexcept {
00039             *this = other;
00040         }
00041 
00045         WeakRef(const Ref<T>& other) noexcept {
00046             *this = other;
00047         }
00048 
00051         ~WeakRef() {
00052             reset();
00053         }
00054 
00059         WeakRef& operator=(const Ref<T>& other) noexcept {
00060             return assign(other.get());
00061         }
00062 
00067         WeakRef& operator=(const WeakRef& other) noexcept {
00068             return assign(other.get());
00069         }
00070 
00076         WeakRef& operator=(T* ptr) noexcept {
00077             return assign(ptr);
00078         }
00079 
00084         WeakRef& operator=(std::nullptr_t) noexcept {
00085             reset();
00086             return *this;
00087         }
00088 
00093         WeakRef& assign(T* ptr) noexcept {
00094             if(this->get() != ptr) {
00095                 if(this->get())
00096                     this->get()->releaseWeak();
00097 
00098                 this->set(ptr);
00099 
00100                 if(this->get())
00101                     this->get()->retainWeak();
00102             }
00103 
00104             return *this;
00105         }
00106 
00110         void reset() noexcept {
00111             assign(nullptr);
00112         }
00113 
00118         Ref<T> lock() const noexcept {
00119             return (!this->get() || this->isExpired()) ? Ref<T>() : Ref<T>(*this);
00120         }
00121 
00125         template <typename S>
00126         WeakRef<S> as() noexcept {
00127             return WeakRef<S>(static_cast<S*>(this->get()));
00128         }
00129     };
00130 
00135     template <typename T>
00136     inline WeakRef<T> make_weak_ref(T* ptr) noexcept {
00137         return WeakRef<T>(ptr);
00138     }
00139 
00145     template <typename S, typename T>
00146     inline WeakRef<S> make_weak_ref_as(T* ptr) noexcept {
00147         return WeakRef<S>(static_cast<S*>(ptr));
00148     }
00149 
00150     /* Documented elsewhere. */
00151     template <typename T>
00152     inline Ref<T>& Ref<T>::operator=(const WeakRef<T>& other) noexcept {
00153         if(this->get() != other.get()) {
00154             if(this->get())
00155                 this->get()->release();
00156 
00157             this->set(other.get());
00158 
00159             // if retainLocked returns 0 or less it means refcounted object was destroyed in some other thread
00160             if(this->get() && this->get()->retainLocked() < 1)
00161                 this->set(nullptr);
00162         }
00163 
00164         return *this;
00165     }
00166 
00167 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
00168 } // namespace fsdk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines