Loading...
Searching...
No Matches
TfDelegatedCountPtr< ValueType > Class Template Reference

Stores a pointer to a ValueType which uses TfDelegatedCountIncrement and TfDelegatedCountDecrement to bookkeep. More...

#include <delegatedCountPtr.h>

Public Types

using RawPtrType = std::add_pointer_t< ValueType >
 
using ReferenceType = std::add_lvalue_reference_t< ValueType >
 
using element_type = ValueType
 
using IncrementIsNoExcept = std::integral_constant< bool, noexcept(TfDelegatedCountIncrement(std::declval< RawPtrType >()))>
 
using DecrementIsNoExcept = std::integral_constant< bool, noexcept(TfDelegatedCountDecrement(std::declval< RawPtrType >()))>
 
using IncrementAndDecrementAreNoExcept = std::integral_constant< bool, IncrementIsNoExcept() &&DecrementIsNoExcept()>
 
using DereferenceIsNoExcept = std::integral_constant< bool, noexcept(*std::declval< RawPtrType >())>
 

Public Member Functions

 TfDelegatedCountPtr () noexcept=default
 Create a pointer storing nullptr
 
 TfDelegatedCountPtr (TfDelegatedCountDoNotIncrementTagType, RawPtrType rawPointer) noexcept
 Create a new pointer storing rawPointer without calling TfDelegatedCountIncrement.
 
 TfDelegatedCountPtr (TfDelegatedCountIncrementTagType, RawPtrType rawPointer) noexcept(IncrementIsNoExcept())
 Create a new pointer storing rawPointer and call TfDelegatedCountIncrement on it if it is not nullptr.
 
 TfDelegatedCountPtr (const TfDelegatedCountPtr &ptr) noexcept(IncrementIsNoExcept())
 Copy construct from ptr, calling TfDelegatedCountIncrement on the held pointer if it is not nullptr.
 
template<typename OtherType >
 TfDelegatedCountPtr (const TfDelegatedCountPtr< OtherType > &ptr, std::enable_if_t< _IsPtrConvertible< OtherType >::value, int >=0) noexcept(IncrementIsNoExcept())
 Copy construct from ptr if it is convertible to this class's RawPtrType.
 
 TfDelegatedCountPtr (TfDelegatedCountPtr &&ptr) noexcept
 Construct by moving from ptr.
 
TfDelegatedCountPtroperator= (const TfDelegatedCountPtr &ptr) noexcept(IncrementAndDecrementAreNoExcept())
 Assign by copying from ptr.
 
template<typename OtherType >
TfDelegatedCountPtroperator= (const TfDelegatedCountPtr< OtherType > &ptr) noexcept(IncrementAndDecrementAreNoExcept())
 Assign by copying from ptr if it is convertible to this class's RawPtrType.
 
TfDelegatedCountPtroperator= (TfDelegatedCountPtr &&ptr) noexcept(DecrementIsNoExcept())
 Assign by moving from ptr.
 
TfDelegatedCountPtroperator= (std::nullptr_t) noexcept(DecrementIsNoExcept())
 Reset this pointer to its default state (i.e. nullptr)
 
 ~TfDelegatedCountPtr () noexcept(DecrementIsNoExcept::value)
 Call TfDelegatedCountDecrement on the held pointer if it is not nullptr`.
 
ReferenceType operator* () const noexcept(DereferenceIsNoExcept())
 Dereference the underlying pointer.
 
RawPtrType operator-> () const noexcept
 Arrow operator dispatch for the underlying pointer.
 
 operator bool () const noexcept
 Return true if the underlying pointer is non-null, false otherwise.
 
template<typename OtherType >
bool operator== (const TfDelegatedCountPtr< OtherType > &other) const noexcept
 Return true if the underlying pointers are equivalent.
 
template<typename OtherType >
bool operator!= (const TfDelegatedCountPtr< OtherType > &other) const noexcept
 Returns false if the underlying pointers are equivalent.
 
template<typename OtherType >
bool operator< (const TfDelegatedCountPtr< OtherType > &other) const noexcept
 Orders based on the underlying pointer.
 
RawPtrType get () const noexcept
 Return the underlying pointer.
 
void reset () noexcept(DecrementIsNoExcept())
 Reset the pointer to its default state (nullptr), calling TfDelegatedCountDecrement if the held pointer is not null.
 
void swap (TfDelegatedCountPtr &other) noexcept
 Swap this object's held pointer with other's.
 

Detailed Description

template<typename ValueType>
class TfDelegatedCountPtr< ValueType >

Stores a pointer to a ValueType which uses TfDelegatedCountIncrement and TfDelegatedCountDecrement to bookkeep.

This class is most useful to adapt existing types that have their own bespoke reference counting logic to a common C++-style "smart pointer" interface.

The TfDelegatedCountPtr calls TfDelegatedCountIncrement and TfDelegatedCountDecrement as needed in construction, assignment, and destruction operations. These functions are expected to be provided by client code to do the specific resource management related to the pointed-to object. These functions must have the following signatures:

void TfDelegatedCountIncrement(MyObject *obj);
void TfDelegatedCountDecrement(MyObject *obj);

For example if MyObject has a reference count member variable, the overload TfDelegatedCountIncrement(MyObject *obj) could simply increment that count. The TfDelegatedCountDecrement(MyObject *obj) might decrement the count and check to see if it has gone to zero. If so, it can clean up resources related to the object such as deleting it or freeing memory.

These increment and decrement functions are never passed null pointers.

A TfDelegatedCountPtr can be created by construction with a raw pointer, or by TfMakeDelegatedCountPtr to create and manage an object on the heap.

Definition at line 80 of file delegatedCountPtr.h.

Member Typedef Documentation

◆ DecrementIsNoExcept

using DecrementIsNoExcept = std::integral_constant< bool, noexcept(TfDelegatedCountDecrement(std::declval<RawPtrType>()))>

Definition at line 99 of file delegatedCountPtr.h.

◆ DereferenceIsNoExcept

using DereferenceIsNoExcept = std::integral_constant<bool, noexcept(*std::declval<RawPtrType>())>

Definition at line 106 of file delegatedCountPtr.h.

◆ element_type

using element_type = ValueType

Definition at line 84 of file delegatedCountPtr.h.

◆ IncrementAndDecrementAreNoExcept

using IncrementAndDecrementAreNoExcept = std::integral_constant< bool, IncrementIsNoExcept() && DecrementIsNoExcept()>

Definition at line 103 of file delegatedCountPtr.h.

◆ IncrementIsNoExcept

using IncrementIsNoExcept = std::integral_constant< bool, noexcept(TfDelegatedCountIncrement(std::declval<RawPtrType>()))>

Definition at line 95 of file delegatedCountPtr.h.

◆ RawPtrType

using RawPtrType = std::add_pointer_t<ValueType>

Definition at line 82 of file delegatedCountPtr.h.

◆ ReferenceType

using ReferenceType = std::add_lvalue_reference_t<ValueType>

Definition at line 83 of file delegatedCountPtr.h.

Constructor & Destructor Documentation

◆ TfDelegatedCountPtr() [1/6]

TfDelegatedCountPtr ( )
defaultnoexcept

Create a pointer storing nullptr

◆ TfDelegatedCountPtr() [2/6]

TfDelegatedCountPtr ( TfDelegatedCountDoNotIncrementTagType  ,
RawPtrType  rawPointer 
)
inlinenoexcept

Create a new pointer storing rawPointer without calling TfDelegatedCountIncrement.

See also
TfDelegatedCountDoNotIncrementTag

Definition at line 121 of file delegatedCountPtr.h.

◆ TfDelegatedCountPtr() [3/6]

TfDelegatedCountPtr ( TfDelegatedCountIncrementTagType  ,
RawPtrType  rawPointer 
)
inlinenoexcept

Create a new pointer storing rawPointer and call TfDelegatedCountIncrement on it if it is not nullptr.

See also
TfDelegatedCountIncrementTag

Definition at line 129 of file delegatedCountPtr.h.

◆ TfDelegatedCountPtr() [4/6]

TfDelegatedCountPtr ( const TfDelegatedCountPtr< ValueType > &  ptr)
inlinenoexcept

Copy construct from ptr, calling TfDelegatedCountIncrement on the held pointer if it is not nullptr.

Definition at line 138 of file delegatedCountPtr.h.

◆ TfDelegatedCountPtr() [5/6]

TfDelegatedCountPtr ( const TfDelegatedCountPtr< OtherType > &  ptr,
std::enable_if_t< _IsPtrConvertible< OtherType >::value, int >  = 0 
)
inlineexplicitnoexcept

Copy construct from ptr if it is convertible to this class's RawPtrType.

Call TfDelegatedCountIncrement on the held pointer if it is not nullptr.

Definition at line 148 of file delegatedCountPtr.h.

◆ TfDelegatedCountPtr() [6/6]

TfDelegatedCountPtr ( TfDelegatedCountPtr< ValueType > &&  ptr)
inlinenoexcept

Construct by moving from ptr.

ptr is left in its default state (i.e. nullptr).

Definition at line 159 of file delegatedCountPtr.h.

◆ ~TfDelegatedCountPtr()

~TfDelegatedCountPtr ( )
inlinenoexcept

Call TfDelegatedCountDecrement on the held pointer if it is not nullptr`.

A bug occurs in VS2017 where calling DecrementIsNoExcept() may return void. The bug is possibly related to an issue with using noexcept expressions in destructors.

Definition at line 208 of file delegatedCountPtr.h.

Member Function Documentation

◆ get()

RawPtrType get ( ) const
inlinenoexcept

Return the underlying pointer.

Definition at line 247 of file delegatedCountPtr.h.

◆ operator bool()

operator bool ( ) const
inlineexplicitnoexcept

Return true if the underlying pointer is non-null, false otherwise.

Definition at line 223 of file delegatedCountPtr.h.

◆ operator!=()

bool operator!= ( const TfDelegatedCountPtr< OtherType > &  other) const
inlinenoexcept

Returns false if the underlying pointers are equivalent.

Definition at line 234 of file delegatedCountPtr.h.

◆ operator*()

ReferenceType operator* ( ) const
inlinenoexcept

Dereference the underlying pointer.

Definition at line 213 of file delegatedCountPtr.h.

◆ operator->()

RawPtrType operator-> ( ) const
inlinenoexcept

Arrow operator dispatch for the underlying pointer.

Definition at line 218 of file delegatedCountPtr.h.

◆ operator<()

bool operator< ( const TfDelegatedCountPtr< OtherType > &  other) const
inlinenoexcept

Orders based on the underlying pointer.

Definition at line 241 of file delegatedCountPtr.h.

◆ operator=() [1/4]

TfDelegatedCountPtr & operator= ( const TfDelegatedCountPtr< ValueType > &  ptr)
inlinenoexcept

Assign by copying from ptr.

Call TfDelegatedCountIncrement on the held pointer if it is not nullptr.

Definition at line 168 of file delegatedCountPtr.h.

◆ operator=() [2/4]

TfDelegatedCountPtr & operator= ( const TfDelegatedCountPtr< OtherType > &  ptr)
inlinenoexcept

Assign by copying from ptr if it is convertible to this class's RawPtrType.

Call TfDelegatedCountIncrement on the held pointer if it is not nullptr.

Definition at line 178 of file delegatedCountPtr.h.

◆ operator=() [3/4]

TfDelegatedCountPtr & operator= ( std::nullptr_t  )
inlinenoexcept

Reset this pointer to its default state (i.e. nullptr)

Definition at line 198 of file delegatedCountPtr.h.

◆ operator=() [4/4]

TfDelegatedCountPtr & operator= ( TfDelegatedCountPtr< ValueType > &&  ptr)
inlinenoexcept

Assign by moving from ptr.

ptr is left in its default state (i.e. nullptr).

Definition at line 189 of file delegatedCountPtr.h.

◆ operator==()

bool operator== ( const TfDelegatedCountPtr< OtherType > &  other) const
inlinenoexcept

Return true if the underlying pointers are equivalent.

Definition at line 227 of file delegatedCountPtr.h.

◆ reset()

void reset ( )
inlinenoexcept

Reset the pointer to its default state (nullptr), calling TfDelegatedCountDecrement if the held pointer is not null.

Definition at line 251 of file delegatedCountPtr.h.

◆ swap()

void swap ( TfDelegatedCountPtr< ValueType > &  other)
inlinenoexcept

Swap this object's held pointer with other's.

Definition at line 257 of file delegatedCountPtr.h.


The documentation for this class was generated from the following file: