2#include <UnCompute/Memory/Object.h>
15 using ObjectType =
typename T::ObjectType;
17 inline explicit PtrRef(T* ptr)
22 inline operator ObjectType**()
24 return m_Ptr->ReleaseAndGetAddressOf();
27 inline ObjectType* operator*()
47 template<
class T1,
class =
void>
48 struct IsComplete : std::false_type
53 struct IsComplete<T1, decltype(void(sizeof(T1)))> : std::true_type
57 template<
class TBase,
class TDerived,
bool>
58 struct IsBaseOf : std::true_type
63 template<
class TBase,
class TDerived>
64 struct IsBaseOf<TBase, TDerived, true> : std::is_base_of<TBase, TDerived>
68 static_assert(IsBaseOf<IObject, T, IsComplete<T>::value>::value,
69 "The template parameter in Ptr<T> must be a class or an interface derived from IObject");
74 inline void InternalAddRef()
const
82 inline UInt32 InternalRelease()
87 result = m_pObject->Release();
109 inline Ptr(T* pObject)
noexcept
119 : m_pObject(other.m_pObject)
129 : m_pObject(other.Get())
138 : m_pObject(other.m_pObject)
140 other.m_pObject =
nullptr;
146 auto* t = other.m_pObject;
147 other.m_pObject = m_pObject;
165 Ptr(std::move(other)).
Swap(*
this);
225 template<
class TDest>
226 inline std::enable_if_t<std::is_base_of_v<TDest, T>, TDest*>
As()
const
228 return static_cast<TDest*
>(
Get());
232 template<
class TDest>
233 inline std::enable_if_t<std::is_base_of_v<T, TDest> && !std::is_same_v<T, TDest>, TDest*>
As()
const
235 return un_verify_cast<TDest*>(
Get());
243 inline T& operator*()
248 inline T* operator->()
253 inline const T& operator*()
const
258 inline const T* operator->()
const
263 inline explicit operator bool()
const
270 inline bool operator==(
const Ptr<T>& lhs, std::nullptr_t)
272 return lhs.Get() ==
nullptr;
276 inline bool operator!=(
const Ptr<T>& lhs, std::nullptr_t)
278 return !(lhs ==
nullptr);
282 inline bool operator==(std::nullptr_t,
const Ptr<T>& rhs)
284 return rhs.Get() ==
nullptr;
288 inline bool operator!=(std::nullptr_t,
const Ptr<T>& rhs)
290 return !(
nullptr == rhs);
293 template<
class T1,
class T2>
294 inline bool operator==(
const Ptr<T1>& lhs, T2* rhs)
296 return static_cast<IObject*
>(lhs.Get()) ==
static_cast<IObject*
>(rhs);
299 template<
class T1,
class T2>
300 inline bool operator!=(
const Ptr<T1>& lhs, T2* rhs)
302 return !(lhs == rhs);
305 template<
class T1,
class T2>
306 inline bool operator==(T1* lhs,
const Ptr<T2>& rhs)
311 template<
class T1,
class T2>
312 inline bool operator!=(T1* lhs,
const Ptr<T2>& rhs)
314 return !(lhs == rhs);
317 template<
class T1,
class T2>
318 inline bool operator==(
const Ptr<T1>& lhs,
const Ptr<T2>& rhs)
320 return static_cast<IObject*
>(lhs.Get()) ==
static_cast<IObject*
>(rhs.Get());
323 template<
class T1,
class T2>
324 inline bool operator!=(
const Ptr<T1>& lhs,
const Ptr<T2>& rhs)
326 return !(lhs == rhs);
329 template<
class TDest,
class TSrc>
330 inline std::enable_if_t<std::is_base_of_v<TSrc, TDest>, Ptr<TDest>> un_verify_cast(
const Ptr<TSrc>& pSourceObject)
332 return Ptr<TDest>(un_verify_cast<TDest*>(pSourceObject.Get()));
Shared smart pointer implementation that uses reference counting.
Definition: Ptr.h:44
void Attach(T *pObject)
Attach a pointer and do not add strong reference.
Definition: Ptr.h:202
Ptr(T *pObject) noexcept
Create a pointer that points to the specified object.
Definition: Ptr.h:109
std::enable_if_t< std::is_base_of_v< T, TDest > &&!std::is_same_v< T, TDest >, TDest * > As() const
Get underlying raw pointer and cast it.
Definition: Ptr.h:233
T ** GetAddressOf()
Get pointer to the underlying pointer.
Definition: Ptr.h:187
Ptr(const Ptr< T1 > &other) noexcept
Copy a pointer (adds a strong reference to underlying object).
Definition: Ptr.h:128
Ptr(Ptr &&other) noexcept
Move a pointer (doesn't add a strong reference to underlying object).
Definition: Ptr.h:137
void Swap(Ptr &other)
Swap raw pointers of two objects without incrementing and decrementing ref-counters.
Definition: Ptr.h:144
Ptr & operator=(Ptr &&other) noexcept
Move a pointer (doesn't add a strong reference to underlying object).
Definition: Ptr.h:163
T * Get() const
Get underlying raw pointer.
Definition: Ptr.h:219
T ** ReleaseAndGetAddressOf()
Release a strong reference and get pointer to the stored pointer.
Definition: Ptr.h:195
Ptr() noexcept
Create a null reference counted pointer.
Definition: Ptr.h:98
Ptr(const Ptr &other) noexcept
Copy a pointer (adds a strong reference to underlying object).
Definition: Ptr.h:118
T *const * GetAddressOf() const
Get pointer to the underlying pointer.
Definition: Ptr.h:181
T * Detach()
Forget object and don't free it automatically.
Definition: Ptr.h:211
Ptr & operator=(const Ptr &other)
Copy a pointer (adds a strong reference to underlying object).
Definition: Ptr.h:154
std::enable_if_t< std::is_base_of_v< TDest, T >, TDest * > As() const
Get underlying raw pointer and cast it.
Definition: Ptr.h:226
void Reset()
Set a pointer to null.
Definition: Ptr.h:170