7#ifndef PXR_BASE_TF_ANY_WEAK_PTR_H
8#define PXR_BASE_TF_ANY_WEAK_PTR_H
15#include "pxr/base/tf/api.h"
17#include "pxr/base/tf/type.h"
20#ifdef PXR_PYTHON_SUPPORT_ENABLED
24#include "pxr/base/tf/pyObjWrapper.h"
30PXR_NAMESPACE_OPEN_SCOPE
46 template <
class Ptr,
class =
typename
47 std::enable_if<Tf_SupportsWeakPtr<
48 typename Ptr::DataType>::value>::type>
50 static_assert(
sizeof(_PointerHolder<Ptr>) <=
sizeof(_Data),
51 "Ptr is too big to fit in a TfAnyWeakPtr");
52 new (&_ptrStorage) _PointerHolder<Ptr>(ptr);
57 static_assert(
sizeof(_EmptyHolder) <=
sizeof(_Data),
58 "Ptr is too big to fit in a TfAnyWeakPtr");
59 new (&_ptrStorage) _EmptyHolder;
69 other._Get()->Clone(&_ptrStorage);
74 _Get()->~_PointerHolderBase();
75 other._Get()->Clone(&_ptrStorage);
81 _Get()->~_PointerHolderBase();
95 TF_API
operator bool()
const;
105 return !(*
this == rhs);
113 return !(rhs < *
this);
123 return !(*
this < rhs);
138#ifdef PXR_PYTHON_SUPPORT_ENABLED
143 friend pxr_boost::python::api::object
144 Tf_GetPythonObjectFromAnyWeakPtr(This
const &self);
147 pxr_boost::python::api::object _GetPythonObject()
const;
150 template <
class WeakPtr>
151 friend WeakPtr TfAnyWeakPtrDynamicCast(
const TfAnyWeakPtr &anyWeak, WeakPtr*);
154 struct _PointerHolderBase {
155 TF_API
virtual ~_PointerHolderBase();
156 virtual void Clone(_Data *target)
const = 0;
157 virtual bool IsInvalid()
const = 0;
158 virtual void const * GetUniqueIdentifier()
const = 0;
159 virtual TfWeakBase const *GetWeakBase()
const = 0;
160 virtual operator bool()
const = 0;
161 virtual bool _IsConst()
const = 0;
163 virtual const std::type_info & GetTypeInfo()
const = 0;
164 virtual TfType const& GetType()
const = 0;
165 virtual const void* _GetMostDerivedPtr()
const = 0;
166 virtual bool _IsPolymorphic()
const = 0;
169 struct _EmptyHolder : _PointerHolderBase {
170 TF_API
virtual ~_EmptyHolder();
171 TF_API
virtual void Clone(_Data *target)
const;
172 TF_API
virtual bool IsInvalid()
const;
173 TF_API
virtual void const * GetUniqueIdentifier()
const;
174 TF_API
virtual TfWeakBase const *GetWeakBase()
const;
175 TF_API
virtual operator bool()
const;
176 TF_API
virtual bool _IsConst()
const;
178 TF_API
virtual const std::type_info & GetTypeInfo()
const;
179 TF_API
virtual TfType const& GetType()
const;
180 TF_API
virtual const void* _GetMostDerivedPtr()
const;
181 TF_API
virtual bool _IsPolymorphic()
const;
184 template <
typename Ptr>
185 struct _PointerHolder : _PointerHolderBase {
186 _PointerHolder(Ptr
const &ptr) : _ptr(ptr) {
189 virtual ~_PointerHolder();
190 virtual void Clone(_Data *target)
const;
191 virtual bool IsInvalid()
const;
192 virtual void const *GetUniqueIdentifier()
const;
193 virtual TfWeakBase const *GetWeakBase()
const;
194 virtual operator bool()
const;
195 virtual bool _IsConst()
const;
197 virtual const std::type_info & GetTypeInfo()
const;
198 virtual TfType const& GetType()
const;
199 virtual const void* _GetMostDerivedPtr()
const;
200 virtual bool _IsPolymorphic()
const;
205 _PointerHolderBase* _Get()
const {
206 return (_PointerHolderBase*)(&_ptrStorage);
216template <
class HashState,
217 class T,
class =
typename std::enable_if<
218 std::is_same<T, TfAnyWeakPtr>::value>::type>
220TfHashAppend(HashState &h,
const T& ptr)
222 h.Append(ptr.GetUniqueIdentifier());
226TfAnyWeakPtr::_PointerHolder<Ptr>::~_PointerHolder() {}
230TfAnyWeakPtr::_PointerHolder<Ptr>::Clone(_Data *target)
const
232 new (target) _PointerHolder<Ptr>(_ptr);
237TfAnyWeakPtr::_PointerHolder<Ptr>::IsInvalid()
const
239 return _ptr.IsInvalid();
244TfAnyWeakPtr::_PointerHolder<Ptr>::GetUniqueIdentifier()
const
246 return _ptr.GetUniqueIdentifier();
251TfAnyWeakPtr::_PointerHolder<Ptr>::GetWeakBase()
const
253 return &(_ptr->__GetTfWeakBase__());
257TfAnyWeakPtr::_PointerHolder<Ptr>::operator bool()
const
264TfAnyWeakPtr::_PointerHolder<Ptr>::GetPythonObject()
const
266#ifdef PXR_PYTHON_SUPPORT_ENABLED
273const std::type_info &
274TfAnyWeakPtr::_PointerHolder<Ptr>::GetTypeInfo()
const
276 return TfTypeid(_ptr);
281TfAnyWeakPtr::_PointerHolder<Ptr>::GetType()
const
288TfAnyWeakPtr::_PointerHolder<Ptr>::_GetMostDerivedPtr()
const
294 typename Ptr::DataType
const *rawPtr = get_pointer(_ptr);
300TfAnyWeakPtr::_PointerHolder<Ptr>::_IsPolymorphic()
const
302 return std::is_polymorphic<typename Ptr::DataType>::value;
307TfAnyWeakPtr::_PointerHolder<Ptr>::_IsConst()
const
309 return std::is_const<typename Ptr::DataType>::value;
312PXR_NAMESPACE_CLOSE_SCOPE
Miscellaneous Utilities for dealing with script.
pxr_boost::python::object TfPyObject(T const &t, bool complainOnFailure=true)
Return a python object for the given C++ object, loading the appropriate wrapper code if necessary.
Provides the ability to hold an arbitrary TfWeakPtr in a non-type-specific manner in order to observe...
TF_API const std::type_info & GetTypeInfo() const
returns the type_info of the underlying WeakPtr
bool operator!=(const TfAnyWeakPtr &rhs) const
inequality operator
TF_API TfType const & GetType() const
Returns the TfType of the underlying WeakPtr.
bool operator>(const TfAnyWeakPtr &rhs) const
greater than operator
bool operator<=(const TfAnyWeakPtr &rhs) const
less than or equal operator
TF_API bool operator!() const
operator !
TF_API bool IsInvalid() const
Return true only if this expiry checker is watching a weak pointer which has expired.
TF_API void const * GetUniqueIdentifier() const
Return the unique identifier of the WeakPtr this AnyWeakPtr contains.
bool operator>=(const TfAnyWeakPtr &rhs) const
greater than or equal operator
TfAnyWeakPtr(Ptr const &ptr)
Construct an AnyWeakPtr watching ptr.
TF_API TfWeakBase const * GetWeakBase() const
Return the TfWeakBase object of the WeakPtr we are holding.
size_t GetHash() const
Return a hash value for this instance.
TF_API bool operator<(const TfAnyWeakPtr &rhs) const
comparison operator
TfAnyWeakPtr(TfNullPtrType)
Construct and implicitly convert from TfNullPtr.
TfAnyWeakPtr(std::nullptr_t)
Construct and implicitly convert from std::nullptr_t.
TF_API bool operator==(const TfAnyWeakPtr &rhs) const
equality operator
TfAnyWeakPtr()
Construct an AnyWeakPtr not watching any ptr.
Boost Python object wrapper.
TfType represents a dynamic runtime type.
static TfType const & Find()
Retrieve the TfType corresponding to type T.
Enable a concrete base class for use with TfWeakPtr.
std::enable_if< std::is_polymorphic< T >::value, Tf_CopyCV< T, void > * >::type TfCastToMostDerivedType(T *ptr)
Return a pointer to the most-derived object.
Pointer storage with deletion detection.