HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TfRefPtr< T > Class Template Reference

#include <HUSD_DataHandle.h>

Classes

struct  Rebind
 

Public Types

typedef T DataType
 Convenience type accessor to underlying type T for template code. More...
 
using UnspecifiedBoolType = const TfRefBase *(TfRefPtr::*)
 

Public Member Functions

 TfRefPtr ()
 
 TfRefPtr (TfRefPtr< T > &&p)
 
 TfRefPtr (const TfRefPtr< T > &p)
 
template<template< class > class X, class U >
 TfRefPtr (const TfWeakPtrFacade< X, U > &p, typename std::enable_if< std::is_convertible< U *, T * >::value >::type *=0)
 
template<class U >
 TfRefPtr (U *ptr, typename std::enable_if< std::is_convertible< U *, T * >::value >::type *=nullptr)
 
 TfRefPtr (TfNullPtrType)
 Implicit conversion from TfNullPtr to TfRefPtr. More...
 
 TfRefPtr (std::nullptr_t)
 Implicit conversion from nullptr to TfRefPtr. More...
 
TfRefPtr< T > & operator= (const TfRefPtr< T > &p)
 
TfRefPtr< T > & operator= (TfRefPtr< T > &&p)
 
 ~TfRefPtr ()
 
template<class U >
 TfRefPtr (const TfRefPtr< U > &p)
 
template<class U >
 TfRefPtr (TfRefPtr< U > &&p)
 
template<class U >
TfRefPtr< T > & operator= (const TfRefPtr< U > &p)
 
template<class U >
TfRefPtr< T > & operator= (TfRefPtr< U > &&p)
 
template<class U >
auto operator== (const TfRefPtr< U > &p) const -> decltype(std::declval< T * >()==std::declval< U * >(), bool())
 
template<class U >
auto operator!= (const TfRefPtr< U > &p) const -> decltype(std::declval< T * >()!=std::declval< U * >(), bool())
 
template<class U >
auto operator< (const TfRefPtr< U > &p) const -> decltype(std::declval< T * >()< std::declval< U * >(), bool())
 
template<class U >
auto operator> (const TfRefPtr< U > &p) const -> decltype(std::declval< T * >() > std::declval< U * >(), bool())
 
template<class U >
auto operator<= (const TfRefPtr< U > &p) const -> decltype(std::declval< T * >()<=std::declval< U * >(), bool())
 
template<class U >
auto operator>= (const TfRefPtr< U > &p) const -> decltype(std::declval< T * >() >=std::declval< U * >(), bool())
 
Toperator-> () const
 Accessor to T's public members. More...
 
Toperator* () const
 Dereferences the stored pointer. More...
 
 operator UnspecifiedBoolType () const
 True if the pointer points to an object. More...
 
bool operator! () const
 True if the pointer points to NULL. More...
 
void swap (TfRefPtr &other)
 
void Reset ()
 

Friends

template<class U >
class TfRefPtr
 
template<class U >
class TfWeakPtr
 
class Tf_Remnant
 
class TfWeakBase
 
template<class U >
TfRefPtr< U > TfCreateRefPtr (U *)
 
template<class HashState , class U >
void TfHashAppend (HashState &, const TfRefPtr< U > &)
 
template<class U >
size_t hash_value (const TfRefPtr< U > &)
 
Tget_pointer (TfRefPtr const &p)
 
template<class D , class B >
TfRefPtr< typename D::DataType > TfDynamic_cast (const TfRefPtr< B > &)
 
template<class D , class B >
TfRefPtr< typename D::DataType > TfSafeDynamic_cast (const TfRefPtr< B > &)
 
template<class D , class B >
TfRefPtr< typename D::DataType > TfStatic_cast (const TfRefPtr< B > &)
 
template<class D >
TfRefPtr< typename D::DataType > TfConst_cast (const TfRefPtr< const typename D::DataType > &)
 
template<class U >
const std::type_info & TfTypeid (const TfRefPtr< U > &ptr)
 
template<class U >
TfRefPtr< U > TfCreateRefPtrFromProtectedWeakPtr (TfWeakPtr< U > const &)
 

Detailed Description

template<class T>
class TfRefPtr< T >

Reference-counted smart pointer utility class

The TfRefPtr class implements a reference counting on objects that inherit from TfRefBase.

For more information, see either the Quick Start example or read the detailed discussion.

Definition at line 70 of file HUSD_DataHandle.h.

Member Typedef Documentation

template<class T>
typedef T TfRefPtr< T >::DataType

Convenience type accessor to underlying type T for template code.

Definition at line 586 of file refPtr.h.

template<class T>
using TfRefPtr< T >::UnspecifiedBoolType = const TfRefBase * (TfRefPtr::*)

Definition at line 937 of file refPtr.h.

Constructor & Destructor Documentation

template<class T>
TfRefPtr< T >::TfRefPtr ( )
inline

Initialize pointer to nullptr.

The default constructor leaves the pointer initialized to point to the NULL object. Attempts to use the -> operator will cause an abort until the pointer is given a value.

Definition at line 598 of file refPtr.h.

template<class T>
TfRefPtr< T >::TfRefPtr ( TfRefPtr< T > &&  p)
inline

Moves the pointer managed by p to *this.

After construction, *this will point to the object p had been pointing at and p will be pointing at the NULL object. The reference count of the object being pointed at does not change.

Definition at line 608 of file refPtr.h.

template<class T>
TfRefPtr< T >::TfRefPtr ( const TfRefPtr< T > &  p)
inline

Initializes *this to point at p's object.

Increments p's object's reference count.

Definition at line 618 of file refPtr.h.

template<class T>
template<template< class > class X, class U >
TfRefPtr< T >::TfRefPtr ( const TfWeakPtrFacade< X, U > &  p,
typename std::enable_if< std::is_convertible< U *, T * >::value >::type = 0 
)
inline

Initializes *this to point at gp's object.

Increments gp's object's reference count.

Definition at line 362 of file weakPtrFacade.h.

template<class T>
template<class U >
TfRefPtr< T >::TfRefPtr ( U *  ptr,
typename std::enable_if< std::is_convertible< U *, T * >::value >::type = nullptr 
)
inlineexplicit

Initializes to point at *ptr.

Increments *ptr's reference count. Note that newly constructed objects start with a reference count of one. Therefore, you should NOT use this constructor (either implicitly or explicitly) from within a New() function. Use TfCreateRefPtr() instead.

Definition at line 678 of file refPtr.h.

template<class T>
TfRefPtr< T >::TfRefPtr ( TfNullPtrType  )
inline

Implicit conversion from TfNullPtr to TfRefPtr.

Definition at line 688 of file refPtr.h.

template<class T>
TfRefPtr< T >::TfRefPtr ( std::nullptr_t  )
inline

Implicit conversion from nullptr to TfRefPtr.

Definition at line 694 of file refPtr.h.

template<class T>
TfRefPtr< T >::~TfRefPtr ( )
inline

Decrements reference count of object being pointed to.

If the reference count of the object (if any) that was just pointed at reaches zero, the object will typically be destroyed at this point.

Definition at line 764 of file refPtr.h.

template<class T>
template<class U >
TfRefPtr< T >::TfRefPtr ( const TfRefPtr< U > &  p)
inline

Initializes to point at p's object, and increments reference count.

This initialization is legal only if

U* uPtr;
T* tPtr = uPtr;

is legal.

Definition at line 780 of file refPtr.h.

template<class T>
template<class U >
TfRefPtr< T >::TfRefPtr ( TfRefPtr< U > &&  p)
inline

Moves the pointer managed by p to *this and leaves p pointing at the NULL object. The reference count of the object being pointed to is not changed.

This initialization is legal only if

U* uPtr;
T* tPtr = uPtr;

is legal.

Definition at line 799 of file refPtr.h.

Member Function Documentation

template<class T>
TfRefPtr< T >::operator UnspecifiedBoolType ( ) const
inline

True if the pointer points to an object.

Definition at line 941 of file refPtr.h.

template<class T>
bool TfRefPtr< T >::operator! ( void  ) const
inline

True if the pointer points to NULL.

Definition at line 946 of file refPtr.h.

template<class T>
template<class U >
auto TfRefPtr< T >::operator!= ( const TfRefPtr< U > &  p) const -> decltype(std::declval<T *>() != std::declval<U *>(), bool())
inline

Returns true if *this and p do not point to the same object.

The comparison is legal only if a T* and a U* are comparable.

Definition at line 881 of file refPtr.h.

template<class T>
T& TfRefPtr< T >::operator* ( ) const
inline

Dereferences the stored pointer.

Definition at line 932 of file refPtr.h.

template<class T>
T* TfRefPtr< T >::operator-> ( ) const
inline

Accessor to T's public members.

Definition at line 923 of file refPtr.h.

template<class T>
template<class U >
auto TfRefPtr< T >::operator< ( const TfRefPtr< U > &  p) const -> decltype(std::declval<T *>() < std::declval<U *>(), bool())
inline

Returns true if the address of the object pointed to by *this compares less than the address of the object pointed to by p.

The comparison is legal only if a T* and a U* are comparable.

Definition at line 893 of file refPtr.h.

template<class T>
template<class U >
auto TfRefPtr< T >::operator<= ( const TfRefPtr< U > &  p) const -> decltype(std::declval<T *>() <= std::declval<U *>(), bool())
inline

Definition at line 909 of file refPtr.h.

template<class T>
TfRefPtr<T>& TfRefPtr< T >::operator= ( const TfRefPtr< T > &  p)
inline

Assigns pointer to point at p's object, and increments reference count.

The object (if any) pointed at before the assignment has its reference count decremented, while the object newly pointed at has its reference count incremented. If the object previously pointed to now has nobody left to point at it, the object will typically be destroyed at this point.

An assignment

can be used to make ptr "forget" where it is pointing; note however that this has an important side effect, since it decrements the reference count of the object previously pointed to by ptr, possibly triggering destruction of that object.

Definition at line 717 of file refPtr.h.

template<class T>
TfRefPtr<T>& TfRefPtr< T >::operator= ( TfRefPtr< T > &&  p)
inline

Moves the pointer managed by p to *this and leaves p pointing at the NULL object.

The object (if any) pointed at before the assignment has its reference count decremented, while the reference count of the object newly pointed at is not changed.

Definition at line 745 of file refPtr.h.

template<class T>
template<class U >
TfRefPtr<T>& TfRefPtr< T >::operator= ( const TfRefPtr< U > &  p)
inline

Assigns pointer to point at p's object, and increments reference count.

This assignment is legal only if

U* uPtr;
T* tPtr;
tPtr = uPtr;

is legal.

Definition at line 820 of file refPtr.h.

template<class T>
template<class U >
TfRefPtr<T>& TfRefPtr< T >::operator= ( TfRefPtr< U > &&  p)
inline

Moves the pointer managed by p to *this and leaves p pointing at the NULL object. The reference count of the object being pointed to is not changed.

This assignment is legal only if

U* uPtr;
T* tPtr;
tPtr = uPtr;

is legal.

Definition at line 847 of file refPtr.h.

template<class T>
template<class U >
auto TfRefPtr< T >::operator== ( const TfRefPtr< U > &  p) const -> decltype(std::declval<T *>() == std::declval<U *>(), bool())
inline

Returns true if *this and p point to the same object (or if they both point to NULL).

The comparison is legal only if a T* and a U* are comparable.

Definition at line 870 of file refPtr.h.

template<class T>
template<class U >
auto TfRefPtr< T >::operator> ( const TfRefPtr< U > &  p) const -> decltype(std::declval<T *>() > std::declval<U *>(), bool())
inline

Definition at line 901 of file refPtr.h.

template<class T>
template<class U >
auto TfRefPtr< T >::operator>= ( const TfRefPtr< U > &  p) const -> decltype(std::declval<T *>() >= std::declval<U *>(), bool())
inline

Definition at line 917 of file refPtr.h.

template<class T>
void TfRefPtr< T >::Reset ( )
inline

Set this pointer to point to no object. Equivalent to assignment with TfNullPtr.

Definition at line 964 of file refPtr.h.

template<class T>
void TfRefPtr< T >::swap ( TfRefPtr< T > &  other)
inline

Swap this pointer with other. After this operation, this pointer will point to what other formerly pointed to, and other will point to what this pointer formerly pointed to.

Definition at line 954 of file refPtr.h.

Friends And Related Function Documentation

template<class T>
T* get_pointer ( TfRefPtr< T > const p)
friend

Definition at line 976 of file refPtr.h.

template<class T>
template<class U >
size_t hash_value ( const TfRefPtr< U > &  )
friend
template<class T>
friend class Tf_Remnant
friend

Definition at line 1113 of file refPtr.h.

template<class T>
template<class D >
TfRefPtr<typename D::DataType> TfConst_cast ( const TfRefPtr< const typename D::DataType > &  )
friend

Allows const casting of a TfRefPtr.

The following is always legal:

TfRefPtr<const T> cPtr = ...;
tPtr = TfConst_cast< TfRefPtr<T> >(cPtr);

As with the C++ const_cast operator, use of this function is discouraged.

template<class T>
template<class U >
TfRefPtr<U> TfCreateRefPtr ( U *  )
friend

Transfer a raw pointer to a reference-counted pointer.

The TfCreateRefPtr() function should only be used from within a static New() function (or similarly, a Clone() function) of a reference-counted class. Reference-counted objects have their reference count initially set to one to account for the fact that a newly created object must always persist at least until its New() function returns. Therefore, the transfer of the pointer returned by new into a reference pointer must not increase the reference count. The transfer of the raw pointer returned by new into the object returned by New() is a "transfer of ownership" and does not represent an additional reference to the object.

In summary, this code is wrong, and will return an object that can never be destroyed:

SimpleRefPtr Simple::New() {
return SimpleRefPtr(new Simple); // legal, but leaks memory: beware!!
}

The correct form is

SimpleRefPtr Simple::New() {
return TfCreateRefPtr(new Simple);
}

Note also that a function which is essentially like New(), for example Clone(), would also want to use TfCreateRefPtr().

template<class T>
template<class U >
TfRefPtr<U> TfCreateRefPtrFromProtectedWeakPtr ( TfWeakPtr< U > const )
friend
template<class T>
template<class D , class B >
TfRefPtr<typename D::DataType> TfDynamic_cast ( const TfRefPtr< B > &  )
friend

Allows dynamic casting of a TfRefPtr.

If it is legal to dynamically cast a T* to a D* , then the following is also legal:

TfRefPtr<T> tPtr = ... ;
if (!(dPtr = TfDynamic_cast< TfRefPtr<D> >(tPtr)))
...; // cast failed

The runtime performance of this function is exactly the same as a dynamic_cast (i.e. one virtual function call). If the pointer being cast is NULL or does not point to an object of the requisite type, the result is a TfRefPtr pointing to NULL.

template<class T>
template<class HashState , class U >
void TfHashAppend ( HashState &  ,
const TfRefPtr< U > &   
)
friend
template<class T>
template<class U >
friend class TfRefPtr
friend

Definition at line 1111 of file refPtr.h.

template<class T>
template<class D , class B >
TfRefPtr<typename D::DataType> TfSafeDynamic_cast ( const TfRefPtr< B > &  )
friend
template<class T>
template<class D , class B >
TfRefPtr<typename D::DataType> TfStatic_cast ( const TfRefPtr< B > &  )
friend

Allows static casting of a TfRefPtr.

If it is legal to statically cast a T* to a D* , then the following is also legal:

TfRefPtr<T> tPtr = ... ;
dPtr = TfStatic_cast< TfRefPtr<D> >(tPtr);

The runtime performance of this function is exactly the same as a regular TfRefPtr initialization, since the cost of the underlying static_cast is zero. Of course, a TfDynamic_cast is preferred, assuming the underlying types are polymorphic (i.e. have virtual functions).

template<class T>
template<class U >
const std::type_info& TfTypeid ( const TfRefPtr< U > &  ptr)
friend

Call typeid on the object pointed to by a TfRefPtr.

If ptr is a TfRefPtr, typeid(ptr) will return type information about the TfRefPtr. To access type information about the object pointed to by a TfRefPtr, one can use TfTypeid.

template<class T>
friend class TfWeakBase
friend

Definition at line 1118 of file refPtr.h.

template<class T>
template<class U >
friend class TfWeakPtr
friend

Definition at line 1112 of file refPtr.h.


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