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;
605 template <
class U>
struct Rebind {
615 Tf_RefPtrTracker_New(
this, _refBase, _NullT);
625 p._refBase =
nullptr;
626 Tf_RefPtrTracker_New(
this, _refBase, _NullT);
627 Tf_RefPtrTracker_Assign(&p, p._refBase, _refBase, _NullT);
635 Tf_RefPtrTracker_New(
this, _refBase, _NullT);
641 template <
template <
class>
class X,
class U>
642 inline TfRefPtr(
const TfWeakPtrFacade<X, U>& p,
643 typename std::enable_if<
644 std::is_convertible<U*, T*>::value
694 U* ptr,
typename std::enable_if<
695 std::is_convertible<U*, T*>::value>::type * =
nullptr) :
699 Tf_RefPtrTracker_New(
this, _refBase, _NullT);
705 Tf_RefPtrTracker_New(
this, _refBase, _NullT);
711 Tf_RefPtrTracker_New(
this, _refBase, _NullT);
743 Tf_RefPtrTracker_Assign(
this, p._refBase, _refBase, _NullT);
746 _refBase = p._refBase;
761 Tf_RefPtrTracker_Assign(
this, p._refBase, _refBase, _NullT);
762 Tf_RefPtrTracker_Assign(&p,
nullptr, p._refBase, _NullT);
765 _refBase = p._refBase;
766 p._refBase =
nullptr;
777 Tf_RefPtrTracker_Delete(
this, _refBase, _NullT);
778 _RemoveRef(_refBase);
793 static_assert(std::is_convertible<U*, T*>::value,
"");
795 Tf_RefPtrTracker_New(
this, _refBase, _NullT);
812 static_assert(std::is_convertible<U*, T*>::value,
"");
813 p._refBase =
nullptr;
814 Tf_RefPtrTracker_New(
this, _refBase, _NullT);
815 Tf_RefPtrTracker_Assign(&p, p._refBase, _refBase, _NullT);
832 static_assert(std::is_convertible<U*, T*>::value,
"");
834 Tf_RefPtrTracker_Assign(
this, p._refBase, _refBase, _NullT);
836 _refBase = p._GetData();
857 static_assert(std::is_convertible<U*, T*>::value,
"");
859 Tf_RefPtrTracker_Assign(
this, p._refBase, _refBase, _NullT);
860 Tf_RefPtrTracker_Assign(&p,
nullptr, p._refBase, _NullT);
862 _refBase = p._GetData();
863 p._refBase =
nullptr;
876 ->
decltype(std::declval<T *>() == std::declval<U *>(), bool()) {
877 return _refBase == p._refBase;
887 ->
decltype(std::declval<T *>() != std::declval<U *>(), bool()) {
888 return _refBase != p._refBase;
899 ->
decltype(std::declval<T *>() < std::declval<U *>(), bool()) {
900 return _refBase < p._refBase;
907 ->
decltype(std::declval<T *>() > std::declval<U *>(), bool()) {
908 return _refBase > p._refBase;
915 ->
decltype(std::declval<T *>() <= std::declval<U *>(), bool()) {
916 return _refBase <= p._refBase;
923 ->
decltype(std::declval<T *>() >= std::declval<U *>(), bool()) {
924 return _refBase >= p._refBase;
930 return static_cast<T*
>(
const_cast<TfRefBase*
>(_refBase));
932 Tf_PostNullSmartPtrDereferenceFatalError(
933 TF_CALL_CONTEXT,
typeid(
TfRefPtr).name());
946 operator UnspecifiedBoolType()
const {
947 return _refBase ? &TfRefPtr::_refBase :
nullptr;
952 return _refBase ==
nullptr;
960 Tf_RefPtrTracker_Assign(
this, other._refBase, _refBase, _NullT);
961 Tf_RefPtrTracker_Assign(&other, _refBase, other._refBase, _NullT);
962 std::swap(_refBase, other._refBase);
974 template <
class HashState,
class U>
975 friend inline void TfHashAppend(HashState &,
const TfRefPtr<U>&);
977 friend inline size_t hash_value(
const TfRefPtr<U>&);
979 friend T *get_pointer(
TfRefPtr const &p) {
980 return static_cast<T *
>(
const_cast<TfRefBase *
>(p._refBase));
984 class _CreateRefPtr { };
991 Tf_RefPtrTracker_FirstRef(
this, _refBase, _NullT);
992 Tf_RefPtrTracker_New(
this, _refBase, _NullT);
1018 template <
class D,
class B>
1022 template <
class D,
class B>
1048 template <
class D,
class B>
1075 T* _GetData()
const {
1076 return static_cast<T*
>(
const_cast<TfRefBase*
>(_refBase));
1089 void _AddRef()
const {
1090 _Counter::AddRef(_refBase);
1093 void _RemoveRef(
const TfRefBase* ptr)
const {
1094 if (_Counter::RemoveRef(ptr)) {
1095 Tf_RefPtrTracker_LastRef(
this, ptr, _NullT);
1100#if ! defined(doxygen)
1103 template <
class U>
friend class TfRefPtr;
1104 template <
class U>
friend class TfWeakPtr;
1105 friend class Tf_Remnant;
1113#if !defined(doxygen)
1123inline bool operator== (
const TfRefPtr<T> &p, std::nullptr_t)
1128inline bool operator== (std::nullptr_t,
const TfRefPtr<T> &p)
1134inline bool operator!= (
const TfRefPtr<T> &p, std::nullptr_t)
1136 return !(p ==
nullptr);
1139inline bool operator!= (std::nullptr_t,
const TfRefPtr<T> &p)
1141 return !(
nullptr == p);
1145inline bool operator< (
const TfRefPtr<T> &p, std::nullptr_t)
1147 return std::less<const TfRefBase *>()(get_pointer(p),
nullptr);
1150inline bool operator< (std::nullptr_t,
const TfRefPtr<T> &p)
1152 return std::less<const TfRefBase *>()(
nullptr, get_pointer(p));
1156inline bool operator<= (
const TfRefPtr<T> &p, std::nullptr_t)
1158 return !(
nullptr < p);
1161inline bool operator<= (std::nullptr_t,
const TfRefPtr<T> &p)
1163 return !(p <
nullptr);
1167inline bool operator> (
const TfRefPtr<T> &p, std::nullptr_t)
1172inline bool operator> (std::nullptr_t,
const TfRefPtr<T> &p)
1178inline bool operator>= (
const TfRefPtr<T> &p, std::nullptr_t)
1180 return !(p <
nullptr);
1183inline bool operator>= (std::nullptr_t,
const TfRefPtr<T> &p)
1185 return !(
nullptr < p);
1189template <
typename T>
1195const std::type_info&
1198 if (ARCH_UNLIKELY(!ptr._refBase))
1201 return typeid(*ptr._GetData());
1204template <
class D,
class T>
1210 return RefPtr(
dynamic_cast<typename D::DataType*
>(ptr._GetData()));
1213template <
class D,
class T>
1219 return RefPtr(TfSafeDynamic_cast<typename D::DataType*>(ptr._GetData()));
1222template <
class D,
class T>
1228 return RefPtr(
static_cast<typename D::DataType*
>(ptr._GetData()));
1239 return *((NonConstRefPtr*)(&ptr));
1259 return t.operator-> ();
1270 static void Class_Object_MUST_Be_Passed_By_Address() { }
1271 static void Class_Object_MUST_Not_Be_Const() { }
1277 return t.operator-> ();
1288 static void Class_Object_MUST_Be_Passed_By_Address() { }
1293#if !defined(doxygen)
1309template <
class HashState,
class T>
1313 h.Append(get_pointer(ptr));
1318#define TF_SUPPORTS_REFPTR(T) std::is_base_of_v<TfRefBase, T>
1320PXR_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.
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.
T DataType
Convenience type accessor to underlying type T for template code.
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.