7#ifndef PXR_BASE_TF_REF_PTR_H
8#define PXR_BASE_TF_REF_PTR_H
414#include "pxr/base/tf/nullPtr.h"
418#include "pxr/base/tf/api.h"
424#include <type_traits>
427PXR_NAMESPACE_OPEN_SCOPE
433struct Tf_SupportsUniqueChanged {
434 static const bool Value =
true;
440struct Tf_SupportsUniqueChanged<Tf_Remnant> {
441 static const bool Value =
false;
447template <
template <
class>
class X,
class Y>
448class TfWeakPtrFacade;
452Tf_RefPtrTracker_FirstRef(
const void*,
const TfRefBase *,
const void*) { }
454Tf_RefPtrTracker_LastRef(
const void*,
const TfRefBase *,
const void*) { }
456Tf_RefPtrTracker_New(
const void*,
const TfRefBase *,
const void*) { }
458Tf_RefPtrTracker_Delete(
const void*,
const TfRefBase *,
const void*) { }
460Tf_RefPtrTracker_Assign(
const void*,
const TfRefBase *,
466struct Tf_RefPtr_UniqueChangedCounter {
469 if (ARCH_UNLIKELY(!refBase)) {
472 const auto relaxed = std::memory_order_relaxed;
474 std::atomic_int &counter = refBase->_GetRefCount();
475 int prevCount = counter.load(relaxed);
476 if (ARCH_UNLIKELY(prevCount < 0)) {
482 if (prevCount != -1 && counter.
483 compare_exchange_weak(prevCount, prevCount-1, relaxed)) {
486 _AddRefMaybeLocked(refBase, prevCount);
490 counter.fetch_add(1, relaxed);
496 if (ARCH_UNLIKELY(!refBase)) {
499 const auto relaxed = std::memory_order_relaxed;
500 const auto release = std::memory_order_release;
502 std::atomic_int &counter = refBase->_GetRefCount();
503 int prevCount = counter.load(relaxed);
504 if (ARCH_UNLIKELY(prevCount < 0)) {
510 if (prevCount != -2 && counter.
511 compare_exchange_weak(prevCount, prevCount+1, release)) {
512 return prevCount == -1;
514 return _RemoveRefMaybeLocked(refBase, prevCount);
518 return counter.fetch_sub(1, release) == 1;
525 AddRefIfNonzero(
TfRefBase const *refBase);
528 _AddRefMaybeLocked(
TfRefBase const *refBase,
int prevCount);
531 _RemoveRefMaybeLocked(
TfRefBase const *refBase,
int prevCount);
537struct Tf_RefPtr_Counter {
540 if (ARCH_UNLIKELY(!refBase)) {
543 refBase->_GetRefCount().fetch_add(1, std::memory_order_relaxed);
548 if (ARCH_UNLIKELY(!refBase)) {
551 return refBase->_GetRefCount()
552 .fetch_sub(1, std::memory_order_release) == 1;
558 AddRefIfNonzero(
TfRefBase const *refBase) {
559 if (ARCH_UNLIKELY(!refBase)) {
562 auto &counter = refBase->_GetRefCount();
563 int prevCount = counter.load(std::memory_order_relaxed);
565 if (counter.compare_exchange_weak(prevCount, prevCount+1)) {
576Tf_PostNullSmartPtrDereferenceFatalError(
const TfCallContext &,
const char *);
592 using _Counter =
typename std::conditional<
593 Tf_SupportsUniqueChanged<T>::Value &&
594 !std::is_convertible<T*, TfSimpleRefBase*>::value,
595 Tf_RefPtr_UniqueChangedCounter,
596 Tf_RefPtr_Counter>::type;
598 static constexpr T *_NullT =
nullptr;
604 template <
class U>
struct Rebind {
614 Tf_RefPtrTracker_New(
this, _refBase, _NullT);
624 p._refBase =
nullptr;
625 Tf_RefPtrTracker_New(
this, _refBase, _NullT);
626 Tf_RefPtrTracker_Assign(&p, p._refBase, _refBase, _NullT);
634 Tf_RefPtrTracker_New(
this, _refBase, _NullT);
640 template <
template <
class>
class X,
class U>
641 inline TfRefPtr(
const TfWeakPtrFacade<X, U>& p,
642 typename std::enable_if<
643 std::is_convertible<U*, T*>::value
693 U* ptr,
typename std::enable_if<
694 std::is_convertible<U*, T*>::value>::type * =
nullptr) :
698 Tf_RefPtrTracker_New(
this, _refBase, _NullT);
704 Tf_RefPtrTracker_New(
this, _refBase, _NullT);
710 Tf_RefPtrTracker_New(
this, _refBase, _NullT);
742 Tf_RefPtrTracker_Assign(
this, p._refBase, _refBase, _NullT);
745 _refBase = p._refBase;
760 Tf_RefPtrTracker_Assign(
this, p._refBase, _refBase, _NullT);
761 Tf_RefPtrTracker_Assign(&p,
nullptr, p._refBase, _NullT);
764 _refBase = p._refBase;
765 p._refBase =
nullptr;
776 Tf_RefPtrTracker_Delete(
this, _refBase, _NullT);
777 _RemoveRef(_refBase);
792 static_assert(std::is_convertible<U*, T*>::value,
"");
794 Tf_RefPtrTracker_New(
this, _refBase, _NullT);
811 static_assert(std::is_convertible<U*, T*>::value,
"");
812 p._refBase =
nullptr;
813 Tf_RefPtrTracker_New(
this, _refBase, _NullT);
814 Tf_RefPtrTracker_Assign(&p, p._refBase, _refBase, _NullT);
831 static_assert(std::is_convertible<U*, T*>::value,
"");
833 Tf_RefPtrTracker_Assign(
this, p._refBase, _refBase, _NullT);
835 _refBase = p._GetData();
856 static_assert(std::is_convertible<U*, T*>::value,
"");
858 Tf_RefPtrTracker_Assign(
this, p._refBase, _refBase, _NullT);
859 Tf_RefPtrTracker_Assign(&p,
nullptr, p._refBase, _NullT);
861 _refBase = p._GetData();
862 p._refBase =
nullptr;
875 ->
decltype(std::declval<T *>() == std::declval<U *>(), bool()) {
876 return _refBase == p._refBase;
886 ->
decltype(std::declval<T *>() != std::declval<U *>(), bool()) {
887 return _refBase != p._refBase;
898 ->
decltype(std::declval<T *>() < std::declval<U *>(), bool()) {
899 return _refBase < p._refBase;
906 ->
decltype(std::declval<T *>() > std::declval<U *>(), bool()) {
907 return _refBase > p._refBase;
914 ->
decltype(std::declval<T *>() <= std::declval<U *>(), bool()) {
915 return _refBase <= p._refBase;
922 ->
decltype(std::declval<T *>() >= std::declval<U *>(), bool()) {
923 return _refBase >= p._refBase;
929 return static_cast<T*
>(
const_cast<TfRefBase*
>(_refBase));
931 Tf_PostNullSmartPtrDereferenceFatalError(
932 TF_CALL_CONTEXT,
typeid(
TfRefPtr).name());
945 operator UnspecifiedBoolType()
const {
946 return _refBase ? &TfRefPtr::_refBase :
nullptr;
951 return _refBase ==
nullptr;
959 Tf_RefPtrTracker_Assign(
this, other._refBase, _refBase, _NullT);
960 Tf_RefPtrTracker_Assign(&other, _refBase, other._refBase, _NullT);
961 std::swap(_refBase, other._refBase);
973 template <
class HashState,
class U>
974 friend inline void TfHashAppend(HashState &,
const TfRefPtr<U>&);
976 friend inline size_t hash_value(
const TfRefPtr<U>&);
978 friend T *get_pointer(
TfRefPtr const &p) {
979 return static_cast<T *
>(
const_cast<TfRefBase *
>(p._refBase));
983 class _CreateRefPtr { };
990 Tf_RefPtrTracker_FirstRef(
this, _refBase, _NullT);
991 Tf_RefPtrTracker_New(
this, _refBase, _NullT);
1017 template <
class D,
class B>
1021 template <
class D,
class B>
1047 template <
class D,
class B>
1074 T* _GetData()
const {
1075 return static_cast<T*
>(
const_cast<TfRefBase*
>(_refBase));
1088 void _AddRef()
const {
1089 _Counter::AddRef(_refBase);
1092 void _RemoveRef(
const TfRefBase* ptr)
const {
1093 if (_Counter::RemoveRef(ptr)) {
1096 std::atomic_thread_fence(std::memory_order_acquire);
1097 Tf_RefPtrTracker_LastRef(
this, ptr, _NullT);
1102#if ! defined(doxygen)
1105 template <
class U>
friend class TfRefPtr;
1106 template <
class U>
friend class TfWeakPtr;
1107 friend class Tf_Remnant;
1115#if !defined(doxygen)
1125inline bool operator== (
const TfRefPtr<T> &p, std::nullptr_t)
1130inline bool operator== (std::nullptr_t,
const TfRefPtr<T> &p)
1136inline bool operator!= (
const TfRefPtr<T> &p, std::nullptr_t)
1138 return !(p ==
nullptr);
1141inline bool operator!= (std::nullptr_t,
const TfRefPtr<T> &p)
1143 return !(
nullptr == p);
1147inline bool operator< (
const TfRefPtr<T> &p, std::nullptr_t)
1149 return std::less<const TfRefBase *>()(get_pointer(p),
nullptr);
1152inline bool operator< (std::nullptr_t,
const TfRefPtr<T> &p)
1154 return std::less<const TfRefBase *>()(
nullptr, get_pointer(p));
1158inline bool operator<= (
const TfRefPtr<T> &p, std::nullptr_t)
1160 return !(
nullptr < p);
1163inline bool operator<= (std::nullptr_t,
const TfRefPtr<T> &p)
1165 return !(p <
nullptr);
1169inline bool operator> (
const TfRefPtr<T> &p, std::nullptr_t)
1174inline bool operator> (std::nullptr_t,
const TfRefPtr<T> &p)
1180inline bool operator>= (
const TfRefPtr<T> &p, std::nullptr_t)
1182 return !(p <
nullptr);
1185inline bool operator>= (std::nullptr_t,
const TfRefPtr<T> &p)
1187 return !(
nullptr < p);
1191template <
typename T>
1193 return TfRefPtr<T>(ptr,
typename TfRefPtr<T>::_CreateRefPtr());
1197const std::type_info&
1200 if (ARCH_UNLIKELY(!ptr._refBase))
1203 return typeid(*ptr._GetData());
1206template <
class D,
class T>
1212 return RefPtr(
dynamic_cast<typename D::DataType*
>(ptr._GetData()));
1215template <
class D,
class T>
1224template <
class D,
class T>
1230 return RefPtr(
static_cast<typename D::DataType*
>(ptr._GetData()));
1241 return *((NonConstRefPtr*)(&ptr));
1261 return t.operator-> ();
1272 static void Class_Object_MUST_Be_Passed_By_Address() { }
1273 static void Class_Object_MUST_Not_Be_Const() { }
1279 return t.operator-> ();
1290 static void Class_Object_MUST_Be_Passed_By_Address() { }
1295#if !defined(doxygen)
1311template <
class HashState,
class T>
1315 h.Append(get_pointer(ptr));
1320#define TF_SUPPORTS_REFPTR(T) std::is_base_of_v<TfRefBase, T>
1322PXR_NAMESPACE_CLOSE_SCOPE
A user-extensible hashing mechanism for use with runtime hash tables.
Enable a concrete base class for use with TfRefPtr.
Reference-counted smart pointer utility class.
TfRefPtr< T > & operator=(const TfRefPtr< T > &p)
Assigns pointer to point at p's object, and increments reference count.
T & operator*() const
Dereferences the stored pointer.
T DataType
Convenience type accessor to underlying type T for template code.
TfRefPtr< T > & operator=(TfRefPtr< T > &&p)
Moves the pointer managed by p to *this and leaves p pointing at the NULL object.
auto operator==(const TfRefPtr< U > &p) const -> decltype(std::declval< T * >()==std::declval< U * >(), bool())
Returns true if *this and p point to the same object (or if they both point to NULL).
friend TfRefPtr< D > TfConst_cast(const TfRefPtr< const D > &)
Allows const casting of a TfRefPtr.
TfRefPtr< T > & operator=(const TfRefPtr< U > &p)
Assigns pointer to point at p's object, and increments reference count.
void Reset()
Set this pointer to point to no object.
TfRefPtr(TfRefPtr< T > &&p)
Moves the pointer managed by p to *this.
friend const std::type_info & TfTypeid(const TfRefPtr< U > &ptr)
Call typeid on the object pointed to by a TfRefPtr.
~TfRefPtr()
Decrements reference count of object being pointed to.
bool operator!() const
True if the pointer points to NULL.
void swap(TfRefPtr &other)
Swap this pointer with other.
TfRefPtr(U *ptr, typename std::enable_if< std::is_convertible< U *, T * >::value >::type *=nullptr)
Initializes to point at *ptr.
T * operator->() const
Accessor to T's public members.
friend TfRefPtr TfCreateRefPtr(T *)
Transfer a raw pointer to a reference-counted pointer.
friend TfRefPtr< D > TfStatic_cast(const TfRefPtr< T > &)
Allows static casting of a TfRefPtr.
TfRefPtr(TfNullPtrType)
Implicit conversion from TfNullPtr to TfRefPtr.
TfRefPtr(const TfRefPtr< T > &p)
Initializes *this to point at p's object.
TfRefPtr< T > & operator=(TfRefPtr< U > &&p)
Moves the pointer managed by p to *this and leaves p pointing at the NULL object.
auto operator!=(const TfRefPtr< U > &p) const -> decltype(std::declval< T * >() !=std::declval< U * >(), bool())
Returns true if *this and p do not point to the same object.
friend TfRefPtr< D > TfDynamic_cast(const TfRefPtr< T > &)
Allows dynamic casting of a TfRefPtr.
TfRefPtr()
Initialize pointer to nullptr.
TfRefPtr(TfRefPtr< U > &&p)
Moves the pointer managed by p to *this and leaves p pointing at the NULL object.
TfRefPtr(const TfRefPtr< U > &p)
Initializes to point at p's object, and increments reference count.
auto operator<(const TfRefPtr< U > &p) const -> decltype(std::declval< T * >()< std::declval< U * >(), bool())
Returns true if the address of the object pointed to by *this compares less than the address of the o...
TfRefPtr(std::nullptr_t)
Implicit conversion from nullptr to TfRefPtr.
Enable a concrete base class for use with TfWeakPtr.
Pointer storage with deletion detection.
Stripped down version of diagnostic.h that doesn't define std::string.
#define TF_FATAL_ERROR(fmt, args)
Issue a fatal error and end the program.
Safely compare C++ RTTI type structures.
TO TfSafeDynamic_cast(FROM *ptr)
Safely perform a dynamic cast.
Implements assorted functions based on compile-time type information.
size_t hash_value(const TfToken &x)
Overload hash_value for TfToken.
TfRefPtr< T > TfCreateRefPtrFromProtectedWeakPtr(TfWeakPtr< T > const &p)
Thread-safe creation of a Tf ref pointer from a Tf weak pointer.