Face Engine SDK  5.25.0
A face detection, recognition and tracking engine.
include/fsdk/Types/Ref.h
00001 #pragma once
00002 
00003 #include <cassert>
00004 #include <type_traits>
00005 
00006 #include <fsdk/Def.h>
00007 #include <fsdk/Types/RefBase.h>
00008 
00009 namespace fsdk {
00010 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00011 
00012     /* Forward declarations. */
00013     template <typename T>
00014     struct WeakRef;
00015 
00016 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
00017 
00025     template <typename T>
00026     struct Ref : RefBase<T> {
00027 
00031         Ref() noexcept = default;
00032 
00035         Ref(std::nullptr_t) noexcept {
00036         }
00037 
00042         FSDK_DEPRECATED("construction from raw pointer is deprecated, use make_ref function instead")
00043 
00044         explicit Ref(T* ptr) noexcept {
00045             *this = ptr;
00046         }
00047 
00052         Ref(const Ref& other)
00053             : RefBase<T>() {
00054             *this = other;
00055         }
00056 
00061         template <class D, typename = typename std::enable_if<std::is_convertible<D*, T*>::value>::type>
00062         explicit Ref(const Ref<D>& other)
00063             : RefBase<T>() {
00064             *this = other;
00065         }
00066 
00067 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00068 
00072         Ref(const WeakRef<T>& other) noexcept {
00073             *this = other;
00074         }
00075 
00076 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
00077 
00080         ~Ref() noexcept {
00081             reset();
00082         }
00083 
00088         T* operator->() const noexcept {
00089             assert(!this->isNull());
00090             return this->get();
00091         }
00092 
00097         T** getInitReference() noexcept {
00098             if(this->get())
00099                 this->get()->release();
00100 
00101             this->set(nullptr);
00102 
00103             return &this->m_ptr;
00104         }
00105 
00110         bool operator==(const Ref& other) const noexcept {
00111             return this->get() == other.get();
00112         }
00113 
00118         bool operator!=(const Ref& other) const noexcept {
00119             return !(*this == other);
00120         }
00121 
00122 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00123 
00128         Ref& operator=(const WeakRef<T>& other) noexcept;
00129 
00130 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
00131 
00136         Ref& operator=(const Ref& other) noexcept {
00137             assign(other.get());
00138             return *this;
00139         }
00140 
00145         template <class D, typename = typename std::enable_if<std::is_convertible<D*, T*>::value>::type>
00146         Ref& operator=(const Ref<D>& other) noexcept {
00147             assign(other.get());
00148             return *this;
00149         }
00150 
00156         FSDK_DEPRECATED("Assignment from raw pointer is deprecated, use make_ref function instead")
00157 
00158         Ref& operator=(T* ptr) noexcept {
00159             assign(ptr);
00160             return *this;
00161         }
00162 
00167         Ref& operator=(std::nullptr_t) noexcept {
00168             reset();
00169             return *this;
00170         }
00171 
00176         Ref& assign(T* ptr) noexcept {
00177             if(this->get() != ptr) {
00178                 if(this->get())
00179                     this->get()->release();
00180 
00181                 this->set(ptr);
00182 
00183                 if(this->get())
00184                     this->get()->retain();
00185             }
00186 
00187             return *this;
00188         }
00189 
00193         Ref& acquire(T* ptr) noexcept {
00194             if(this->get() != ptr) {
00195                 if(this->get())
00196                     this->get()->release();
00197 
00198                 this->set(ptr);
00199             }
00200 
00201             return *this;
00202         }
00203 
00207         Ref& make_ref(T* ptr) noexcept {
00208             return assign(ptr);
00209         }
00210 
00214         T* reset() noexcept {
00215             T* ptr = this->get();
00216             assign(nullptr);
00217             return ptr;
00218         }
00219 
00223         template <typename S>
00224         Ref<S> as() const noexcept {
00225             Ref<S> ref;
00226             ref.make_ref(static_cast<S*>(this->get()));
00227             return ref;
00228         }
00229     };
00230 
00235     template <typename T>
00236     inline Ref<T> make_ref(T* ptr) noexcept {
00237         Ref<T> ref;
00238         ref.make_ref(ptr);
00239         return ref;
00240     }
00241 
00247     template <typename S, typename T>
00248     inline Ref<S> make_ref_as(T* ptr) noexcept {
00249         Ref<T> ref;
00250         ref.make_ref(ptr);
00251         return ref.template as<S>();
00252     }
00253 
00258     template <typename T>
00259     inline Ref<T> acquire(T* ptr) noexcept {
00260         Ref<T> ref;
00261         ref.acquire(ptr);
00262         return ref;
00263     }
00264 
00270     template <typename S, typename T>
00271     inline Ref<S> acquire_as(T* ptr) noexcept {
00272         Ref<S> ref;
00273         ref.acquire(static_cast<S*>(ptr));
00274         return ref;
00275     }
00276 } // namespace fsdk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines