7#ifndef PXR_BASE_TF_WEAK_PTR_FACADE_H
8#define PXR_BASE_TF_WEAK_PTR_FACADE_H
21PXR_NAMESPACE_OPEN_SCOPE
25template <
template <
class>
class PtrTemplate,
class DataType>
35 template <
template <
class>
class PtrTemplate,
class DataType>
36 friend class TfWeakPtrFacade;
38 template <
class Facade>
39 static typename Facade::DataType *FetchPointer(Facade
const &f) {
40 return f._FetchPointer();
43 template <
class Facade>
44 static void const *GetUniqueIdentifier(Facade
const &f) {
45 return f._GetUniqueIdentifier();
48 template <
class Facade>
49 static void EnableExtraNotification(Facade
const &f) {
50 return f._EnableExtraNotification();
53 template <
class Facade>
54 static bool IsInvalid(Facade
const &f) {
55 return f._IsInvalid();
62template <
template <
class>
class X,
class Y>
63Y *get_pointer(TfWeakPtrFacade<X, Y>
const &p) {
64 return TfWeakPtrFacadeAccess::FetchPointer(p);
68class TfWeakPtrFacadeBase {};
70template <
template <
class>
class PtrTemplate,
class Type>
71class TfWeakPtrFacade :
public TfWeakPtrFacadeBase {
77 typedef Type DataType;
78 typedef PtrTemplate<DataType> Derived;
81 typedef Type element_type;
83 template <
class Other>
84 bool operator == (PtrTemplate<Other>
const &p)
const {
86 return _FetchPointer() == TfWeakPtrFacadeAccess::FetchPointer(p);
87 return GetUniqueIdentifier() == p.GetUniqueIdentifier();
90 template <
class Other>
91 bool operator != (PtrTemplate<Other>
const &p)
const {
97 if (!GetUniqueIdentifier())
99 DataType *ptr = _FetchPointer();
100 return ptr && ptr == get_pointer(p);
105 return !(*
this == p);
109 friend bool operator == (
const TfRefPtr<T>& p1, Derived
const &p2) {
114 friend bool operator != (
const TfRefPtr<T>& p1, Derived
const &p2) {
118 template <
class Other>
119 bool operator < (PtrTemplate<Other>
const &p)
const {
121 return _FetchPointer() < TfWeakPtrFacadeAccess::FetchPointer(p);
122 return GetUniqueIdentifier() < p.GetUniqueIdentifier();
125 template <
class Other>
126 bool operator > (PtrTemplate<Other>
const &p)
const {
127 return !(*
this < p) && !(*
this == p);
130 template <
class Other>
131 bool operator <= (PtrTemplate<Other>
const &p)
const {
132 return (*
this < p) || (*
this == p);
135 template <
class Other>
136 bool operator >= (PtrTemplate<Other>
const &p)
const {
140 using UnspecifiedBoolType = DataType * (TfWeakPtrFacade::*)(
void)
const;
142 operator UnspecifiedBoolType ()
const {
143 return _FetchPointer() ? &TfWeakPtrFacade::_FetchPointer :
nullptr;
146 bool operator ! ()
const {
147 return !(bool(*
this));
151 bool PointsTo(T
const &obj)
const {
152 return _FetchPointer() == &obj;
159 bool PointsToA()
const {
160 return dynamic_cast<T *
>(_FetchPointer());
163 bool IsInvalid()
const {
164 return Access::IsInvalid(_Derived());
167 void const *GetUniqueIdentifier()
const {
168 return Access::GetUniqueIdentifier(_Derived());
172 return &_Derived()->__GetTfWeakBase__();
175 void EnableExtraNotification()
const {
176 Access::EnableExtraNotification(_Derived());
179 DataType *operator -> ()
const {
180 DataType *ptr = _FetchPointer();
184 Tf_PostNullSmartPtrDereferenceFatalError(
185 TF_CALL_CONTEXT,
typeid(Derived).name());
188 DataType &operator * ()
const {
189 return * operator->();
195 _Derived() = TfNullPtr;
200 friend std::type_info
const &TfTypeid(Derived
const &p) {
201 if (ARCH_UNLIKELY(!p))
204 return typeid(*get_pointer(p));
207 DataType *_FetchPointer()
const {
208 return Access::FetchPointer(_Derived());
211 Derived &_Derived() {
212 return static_cast<Derived &
>(*this);
215 Derived
const &_Derived()
const {
216 return static_cast<Derived
const &
>(*this);
231template <
template <
class>
class X,
class Y>
232inline bool operator== (TfWeakPtrFacade<X, Y>
const &p, std::nullptr_t)
236template <
template <
class>
class X,
class Y>
237inline bool operator== (std::nullptr_t, TfWeakPtrFacade<X, Y>
const &p)
242template <
template <
class>
class X,
class Y>
243inline bool operator!= (TfWeakPtrFacade<X, Y>
const &p, std::nullptr_t)
245 return !(p ==
nullptr);
247template <
template <
class>
class X,
class Y>
248inline bool operator!= (std::nullptr_t, TfWeakPtrFacade<X, Y>
const &p)
250 return !(
nullptr == p);
253template <
template <
class>
class X,
class Y>
254inline bool operator< (TfWeakPtrFacade<X, Y>
const &p, std::nullptr_t)
256 return std::less<void const *>()(p.GetUniqueIdentifier(),
nullptr);
258template <
template <
class>
class X,
class Y>
259inline bool operator< (std::nullptr_t, TfWeakPtrFacade<X, Y>
const &p)
261 return std::less<void const *>()(
nullptr, p.GetUniqueIdentifier());
264template <
template <
class>
class X,
class Y>
265inline bool operator<= (TfWeakPtrFacade<X, Y>
const &p, std::nullptr_t)
267 return !(
nullptr < p);
269template <
template <
class>
class X,
class Y>
270inline bool operator<= (std::nullptr_t, TfWeakPtrFacade<X, Y>
const &p)
272 return !(p <
nullptr);
275template <
template <
class>
class X,
class Y>
276inline bool operator> (TfWeakPtrFacade<X, Y>
const &p, std::nullptr_t)
280template <
template <
class>
class X,
class Y>
281inline bool operator> (std::nullptr_t, TfWeakPtrFacade<X, Y>
const &p)
286template <
template <
class>
class X,
class Y>
287inline bool operator>= (TfWeakPtrFacade<X, Y>
const &p, std::nullptr_t)
289 return !(p <
nullptr);
291template <
template <
class>
class X,
class Y>
292inline bool operator>= (std::nullptr_t, TfWeakPtrFacade<X, Y>
const &p)
294 return !(
nullptr < p);
299template <
class ToPtr,
template <
class>
class X,
class Y>
300ToPtr TfDynamic_cast(TfWeakPtrFacade<X, Y>
const &p) {
301 return ToPtr(
dynamic_cast<typename ToPtr::DataType *
>
305template <
class ToPtr,
template <
class>
class X,
class Y>
307 return ToPtr(TfSafeDynamic_cast<typename ToPtr::DataType *>
311template <
class ToPtr,
template <
class>
class X,
class Y>
312ToPtr TfStatic_cast(TfWeakPtrFacade<X, Y>
const &p) {
313 return ToPtr(
static_cast<typename ToPtr::DataType *
>
317template <
class ToPtr,
template <
class>
class X,
class Y>
318ToPtr TfConst_cast(TfWeakPtrFacade<X, Y>
const &p) {
319 return ToPtr(
const_cast<typename ToPtr::DataType *
>
331template <
template <
class>
class X,
class U>
333 typename std::enable_if<
334 std::is_convertible<U*, T*>::value
336 : _refBase(get_pointer(p))
339 Tf_RefPtrTracker_New(
this, _refBase, _NullT);
345template <
template <
class>
class Ptr,
class T>
348 std::is_base_of<TfWeakPtrFacadeBase, Ptr<T>>::value
351 static T* GetRawPtr(
const Ptr<T>& t) {
352 return get_pointer(t);
355 static Ptr<T> ConstructFromRawPtr(T* ptr) {
359 static bool IsNull(
const Ptr<T>& t) {
363 static void Class_Object_MUST_Be_Passed_By_Address() { }
364 static void Class_Object_MUST_Not_Be_Const() { }
367template <
template <
class>
class Ptr,
class T>
370 std::is_base_of<TfWeakPtrFacadeBase, Ptr<const T>>::value
373 static const T* GetRawPtr(
const Ptr<const T>& t) {
374 return get_pointer(t);
377 static Ptr<const T> ConstructFromRawPtr(
const T* ptr) {
378 return Ptr<const T>(ptr);
381 static bool IsNull(
const Ptr<const T>& t) {
385 static void Class_Object_MUST_Be_Passed_By_Address() { }
389template <
class HashState,
template <
class>
class X,
class T>
391TfHashAppend(HashState &h, TfWeakPtrFacade<X, T>
const &ptr)
393 return h.Append(ptr.GetUniqueIdentifier());
397template <
template <
class>
class X,
class T>
404PXR_NAMESPACE_CLOSE_SCOPE
Low-level utilities for informing users of various internal and external diagnostic conditions.
A user-extensible hashing mechanism for use with runtime hash tables.
Reference-counted smart pointer utility class.
TfRefPtr()
Initialize pointer to nullptr.
T DataType
Convenience type accessor to underlying type T for template code.
Enable a concrete base class for use with TfWeakPtr.
This access class is befriended by TfWeakPtrFacade -derived classes to grant TfWeakPtrFacade access t...
Demangle C++ typenames generated by the typeid() facility.
std::string ArchGetDemangled()
Return demangled RTTI generated-type name.
#define TF_FATAL_ERROR(fmt, args)
Issue a fatal error and end the program.
size_t hash_value(const half h)
Overload hash_value for half.
TO TfSafeDynamic_cast(FROM *ptr)
Safely perform a dynamic cast.
Implements assorted functions based on compile-time type information.