cee::PtrRef< T > Class Template Reference

Smart pointer class used for handling reference counted objects (that derive from Object). More...

Public Types

typedef T *PtrRef::* unspecified_bool_type
 Helper type to implement the safe-bool idiom. More...
 

Public Member Functions

 PtrRef (T *object=NULL)
 Constructs an object from naked pointer. More...
 
 PtrRef (const PtrRef &other)
 Construct and object as a copy of other. More...
 
template<typename T2 >
 PtrRef (const PtrRef< T2 > &other)
 Constructs from related. More...
 
PtrRefoperator= (T *rhs)
 Assigns from raw pointer. More...
 
PtrRefoperator= (PtrRef rhs)
 Assigns from rhs. More...
 
template<typename T2 >
PtrRefoperator= (const PtrRef< T2 > &rhs)
 Assigns from related. More...
 
T * operator-> ()
 Returns the naked pointer this object is associated with. More...
 
const T * operator-> () const
 Returns naked const pointer this object is associated with. More...
 
T & operator* ()
 Dereference operator returning a modifiable reference to the associated object. More...
 
const T & operator* () const
 Dereference operator returning a const reference to the associated object. More...
 
bool operator< (const PtrRef &rhs) const
 Does less than comparison of two PtrRef objects. More...
 
const T * get () const
 Returns naked const pointer. More...
 
T * get ()
 Returns the naked pointer. More...
 
void swap (PtrRef &other)
 Exchanges the contents of the two smart pointers. More...
 
T * detach ()
 Detach the PtrRef from the internal object WITHOUT calling release on the object. More...
 
 operator unspecified_bool_type () const
 Operator for supporting tests if the objects internal pointer is NULL or not. More...
 
template<typename T2 >
PtrRef< T > & operator= (const PtrRef< T2 > &rhs)
 

Detailed Description

template<typename T>
class cee::PtrRef< T >

Smart pointer class used for handling reference counted objects (that derive from Object).

The PtrRef<T> class encapsulates reference counting by calling Object::addRef() and Object::release() on the internally stored object pointer.

Member Typedef Documentation

template<typename T>
typedef T* PtrRef::* cee::PtrRef< T >::unspecified_bool_type

Helper type to implement the safe-bool idiom.

Constructor & Destructor Documentation

template<typename T>
cee::PtrRef< T >::PtrRef ( T *  object = NULL)

Constructs an object from naked pointer.

Will call addRef() on the passed object.

template<typename T>
cee::PtrRef< T >::PtrRef ( const PtrRef< T > &  other)

Construct and object as a copy of other.

Copies the internal pointer from the passed PtrRef object and calls addRef() on the object.

template<typename T >
template<typename T2 >
cee::PtrRef< T >::PtrRef ( const PtrRef< T2 > &  other)

Constructs from related.

Copies the internal pointer from the passed PtrRef object and calls addRef() on the object.

Member Function Documentation

template<typename T >
T * cee::PtrRef< T >::detach ( )

Detach the PtrRef from the internal object WITHOUT calling release on the object.

After calling, *this == NULL and associated object will have the same refCount as before the call.

Note
Use with caution! The associated object will not be release()'ed and it is the callers responsibility to ensure proper life cycle management of the object.
template<typename T >
const T * cee::PtrRef< T >::get ( ) const
inline

Returns naked const pointer.

template<typename T >
T * cee::PtrRef< T >::get ( )
inline

Returns the naked pointer.

template<typename T >
cee::PtrRef< T >::operator unspecified_bool_type ( ) const

Operator for supporting tests if the objects internal pointer is NULL or not.

Using the safe bool idiom, PtrRef allows for code like

PtrRef<cee::Image> myImage = new cee::Image;
...
if (myImage)
{
// Do something
}

without defining the operator bool(), which have many potential side effects.

template<typename T >
T & cee::PtrRef< T >::operator* ( )
inline

Dereference operator returning a modifiable reference to the associated object.

Added to be able to write the same code for smart pointers as for normal naked pointers.

PtrRef<MyObject> myObject = new MyObject;
*myObject.doSomething();
template<typename T >
const T & cee::PtrRef< T >::operator* ( ) const
inline

Dereference operator returning a const reference to the associated object.

template<typename T >
T * cee::PtrRef< T >::operator-> ( )
inline

Returns the naked pointer this object is associated with.

Added to be able to write the same code for smart pointers as for normal naked pointers.

template<typename T >
const T * cee::PtrRef< T >::operator-> ( ) const
inline

Returns naked const pointer this object is associated with.

template<typename T >
bool cee::PtrRef< T >::operator< ( const PtrRef< T > &  rhs) const

Does less than comparison of two PtrRef objects.

Returns true if this object's internal pointer is less than the internal pointer of rhs.

The comparison is done by comparing the internal pointer values ( this.p() < rhs.p() ) This operator is used in several STL collections (e.g. std::set) as well as in many STL algorithms (e.g. std::sort() ).

template<typename T>
PtrRef< T > & cee::PtrRef< T >::operator= ( T *  rhs)

Assigns from raw pointer.

template<typename T>
PtrRef< T > & cee::PtrRef< T >::operator= ( PtrRef< T >  rhs)

Assigns from rhs.

Copies the internal pointer from rhs and calls addRef(). If we're already storing an internal pointer, this pointer will be release()'ed.

template<typename T>
template<typename T2 >
cee::PtrRef< T >::operator= ( const PtrRef< T2 > &  rhs)

Assigns from related.

Copies the internal pointer from rhs and calls addRef(). If we're already storing an internal pointer, this pointer will be release()'ed.

template<typename T >
void cee::PtrRef< T >::swap ( PtrRef< T > &  other)

Exchanges the contents of the two smart pointers.

other is a modifiable reference to the PtrRef object that should have its contents swapped.

Swap the associated pointer in this and the passed PtrRef object. Will not modify the reference count of any of the associated objects.