Nugget
Public Types | Public Member Functions | Protected Types | Protected Attributes | List of all members
eastl::intrusive_ptr< T > Class Template Reference

#include <intrusive_ptr.h>

Public Types

typedef T element_type
 
typedef T *(this_type::* bool_) () const
 

Public Member Functions

 intrusive_ptr ()
 
 intrusive_ptr (T *p, bool bAddRef=true)
 
 intrusive_ptr (const intrusive_ptr &ip)
 
 intrusive_ptr (intrusive_ptr &&ip)
 
template<typename U >
 intrusive_ptr (const intrusive_ptr< U > &ip)
 
 ~intrusive_ptr ()
 
intrusive_ptroperator= (const intrusive_ptr &ip)
 
intrusive_ptroperator= (intrusive_ptr &&ip)
 
template<typename U >
intrusive_ptroperator= (const intrusive_ptr< U > &ip)
 
intrusive_ptroperator= (T *pObject)
 
T & operator* () const
 
T * operator-> () const
 
T * get () const
 
void reset ()
 
void swap (this_type &ip)
 
void attach (T *pObject)
 
T * detach ()
 
 operator bool_ () const
 
bool operator! () const
 

Protected Types

typedef intrusive_ptr< T > this_type
 

Protected Attributes

T * mpObject
 

Detailed Description

template<typename T>
class eastl::intrusive_ptr< T >

intrusive_ptr

This is a class that acts like the C++ auto_ptr class except that instead of deleting its member data when it goes out of scope, it releases its member data when it goes out of scope. This class thus requires that the templated data type have an AddRef and Release function (or whatever is configured to be the two refcount functions).

This class is useful for automatically releasing an object when this class goes out of scope. See below for some usage. You should be careful about putting instances of this class as members of another class. If you do so, then the intrusive_ptr destructor will only be called if the object that owns it is destructed. This creates a potential chicken-and-egg situation. What if the intrusive_ptr member contains a pointer to an object that has a reference on the object that owns the intrusive_ptr member? The answer is that the neither object can ever be destructed. The solution is to: 1) Be very careful about what objects you put into member intrusive_ptr objects. 2) Clear out your intrusive_ptr members in your shutdown function. 3) Simply don't use intrusive_ptr objects as class members.

Example usage: intrusive_ptr<IWidget> pWidget = new Widget; pWidget = new Widget; pWidget->Reset();

Member Typedef Documentation

◆ bool_

template<typename T >
typedef T*(this_type::* eastl::intrusive_ptr< T >::bool_) () const

Implicit operator bool Allows for using a intrusive_ptr as a boolean. Example usage: intrusive_ptr<Widget> ptr = new Widget; if(ptr) ++*ptr;

Note that below we do not use operator bool(). The reason for this is that booleans automatically convert up to short, int, float, etc. The result is that this: if(intrusivePtr == 1) would yield true (bad).

◆ element_type

template<typename T >
typedef T eastl::intrusive_ptr< T >::element_type

element_type This typedef is present for consistency with the C++ standard library auto_ptr template. It allows users to refer to the templated type via a typedef. This is sometimes useful to be able to do.

Example usage: intrusive_ptr<IWidget> ip; void DoSomething(intrusive_ptr<IWidget>::element_type someType);

Constructor & Destructor Documentation

◆ intrusive_ptr() [1/5]

template<typename T >
eastl::intrusive_ptr< T >::intrusive_ptr ( )
inline

intrusive_ptr Default constructor. The member object is set to NULL.

◆ intrusive_ptr() [2/5]

template<typename T >
eastl::intrusive_ptr< T >::intrusive_ptr ( T *  p,
bool  bAddRef = true 
)
inline

intrusive_ptr Provides a constructor which takes ownership of a pointer. The incoming pointer is AddRefd.

Example usage: intrusive_ptr<Widget> pWidget(new Widget);

◆ intrusive_ptr() [3/5]

template<typename T >
eastl::intrusive_ptr< T >::intrusive_ptr ( const intrusive_ptr< T > &  ip)
inline

intrusive_ptr Construction from self type.

◆ intrusive_ptr() [4/5]

template<typename T >
eastl::intrusive_ptr< T >::intrusive_ptr ( intrusive_ptr< T > &&  ip)
inline

intrusive_ptr move constructor

◆ intrusive_ptr() [5/5]

template<typename T >
template<typename U >
eastl::intrusive_ptr< T >::intrusive_ptr ( const intrusive_ptr< U > &  ip)
inline

intrusive_ptr Provides a constructor which copies a pointer from another intrusive_ptr. The incoming pointer is AddRefd. The source intrusive_ptr object maintains its AddRef on the pointer.

Example usage: intrusive_ptr<Widget> pWidget1; intrusive_ptr<Widget> pWidget2(pWidget1);

◆ ~intrusive_ptr()

template<typename T >
eastl::intrusive_ptr< T >::~intrusive_ptr ( )
inline

intrusive_ptr Releases the owned pointer.

Member Function Documentation

◆ attach()

template<typename T >
void eastl::intrusive_ptr< T >::attach ( T *  pObject)
inline

attach Sets an intrusive_ptr pointer without calling AddRef() on the pointed object. The intrusive_ptr thus eventually only does a Release() on the object. This is useful for assuming a reference that someone else has handed you and making sure it is always released, even if you return in the middle of a function or an exception is thrown.

◆ detach()

template<typename T >
T* eastl::intrusive_ptr< T >::detach ( )
inline

detach Surrenders the reference held by an intrusive_ptr pointer – it returns the current reference and nulls the pointer. If the returned pointer is non-null it must be released. This is useful in functions that must return a reference while possibly being aborted by a return or thrown exception:

bool GetFoo(T** pp){ intrusive_ptr<T> p(PrivateGetFoo()); if(p->Method()) return false; *pp = p.detach(); return true; }

◆ get()

template<typename T >
T* eastl::intrusive_ptr< T >::get ( ) const
inline

get() Returns a pointer to the contained object.

◆ operator!()

template<typename T >
bool eastl::intrusive_ptr< T >::operator! ( ) const
inline

operator! This returns the opposite of operator bool; it returns true if the owned pointer is null. Some compilers require this and some don't. intrusive_ptr<Widget> ptr = new Widget; if(!ptr) assert(false);

◆ operator*()

template<typename T >
T& eastl::intrusive_ptr< T >::operator* ( ) const
inline

operator * Returns a reference to the contained object.

◆ operator->()

template<typename T >
T* eastl::intrusive_ptr< T >::operator-> ( ) const
inline

operator * Returns a pointer to the contained object, allowing the user to use this container as if it were contained pointer itself.

◆ operator=() [1/4]

template<typename T >
intrusive_ptr& eastl::intrusive_ptr< T >::operator= ( const intrusive_ptr< T > &  ip)
inline

operator= Assignment to self type.

◆ operator=() [2/4]

template<typename T >
template<typename U >
intrusive_ptr& eastl::intrusive_ptr< T >::operator= ( const intrusive_ptr< U > &  ip)
inline

operator = Assigns an intrusive_ptr object to this intrusive_ptr object. The incoming pointer is AddRefd. The source intrusive_ptr object maintains its AddRef on the pointer. If there is an existing member pointer, it is Released before the incoming pointer is assigned. If the incoming pointer is equal to the existing pointer, no
action is taken. The incoming pointer is AddRefd before any member pointer is Released.

◆ operator=() [3/4]

template<typename T >
intrusive_ptr& eastl::intrusive_ptr< T >::operator= ( intrusive_ptr< T > &&  ip)
inline

operator= Move assignment operator

◆ operator=() [4/4]

template<typename T >
intrusive_ptr& eastl::intrusive_ptr< T >::operator= ( T *  pObject)
inline

operator= Assigns an intrusive_ptr object to this intrusive_ptr object. The incoming pointer is AddRefd. If there is an existing member pointer, it is Released before the incoming pointer is assigned. If the incoming pointer is equal to the existing pointer, no
action is taken. The incoming pointer is AddRefd before any member pointer is Released.

◆ reset()

template<typename T >
void eastl::intrusive_ptr< T >::reset ( )
inline

reset Releases the owned object and clears our reference to it.

◆ swap()

template<typename T >
void eastl::intrusive_ptr< T >::swap ( this_type ip)
inline

swap Exchanges the owned pointer beween two intrusive_ptr objects.


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