c++-gtk-utils
Public Member Functions | Friends
Cgu::IntrusivePtr< T > Class Template Reference

This is a smart pointer for managing objects allocated on freestore which maintain their own reference count. More...

#include <c++-gtk-utils/intrusive_ptr.h>

List of all members.

Public Member Functions

 IntrusivePtr (T *ptr=0)
 IntrusivePtr (const IntrusivePtr &intr_ptr)
 IntrusivePtr (IntrusivePtr &&intr_ptr)
template<class U >
 IntrusivePtr (const IntrusivePtr< U > &intr_ptr)
template<class U >
 IntrusivePtr (IntrusivePtr< U > &&intr_ptr)
IntrusivePtroperator= (IntrusivePtr intr_ptr)
template<class U >
IntrusivePtroperator= (const IntrusivePtr< U > &intr_ptr)
template<class U >
IntrusivePtroperator= (IntrusivePtr< U > &&intr_ptr)
T * get () const
T & operator* () const
T * operator-> () const
void reset (T *ptr=0)
 ~IntrusivePtr ()

Friends

class IntrusivePtr

Detailed Description

template<class T>
class Cgu::IntrusivePtr< T >

This is a smart pointer for managing objects allocated on freestore which maintain their own reference count.

This is a class which manages objects which maintain their own reference count. It requires that the referenced object has two functions called ref() and unref(), which increment and decrement the reference count respectively. The IntrusiveCounter or IntrusiveLockCounter class can be inherited from to do this, but they do not have to be used. The IntrusiveLockCounter class is the same as the IntrusiveCounter class, except that it locks the reference count when it is incremented or decremented in order that IntrusivePtr objects in different threads can access the same object. (But only the reference count is locked, not the methods of the referenced object.)

All the constructors (including the constructor which takes a raw pointer) increment the reference count, and the destructor decrements it and expects the referenced object to be deleted when the last IntrusivePtr referencing a particular object is destroyed. The IntrusiveCounter and IntrusiveLockCounter classes behave in this way. (This is different from the behaviour of GobjHandle smart pointers, which are constrained by the GObject reference counting system which begins with a reference count of 1 rather than 0, and of course different from normal shared pointer implementations for the same reason. The advantage of the approach with IntrusivePtr is that an already-managed object may safely be passed to the constructor taking a raw pointer without any additional steps being necessary.)


Constructor & Destructor Documentation

template<class T>
Cgu::IntrusivePtr< T >::IntrusivePtr ( T *  ptr = 0)
inlineexplicit

This constructor does not throw.

Parameters:
ptrThe object which the IntrusivePtr is to manage (if any).
template<class T>
Cgu::IntrusivePtr< T >::IntrusivePtr ( const IntrusivePtr< T > &  intr_ptr)
inline

This copy constructor does not throw.

Parameters:
intr_ptrThe intrusive pointer to be copied.
template<class T>
Cgu::IntrusivePtr< T >::IntrusivePtr ( IntrusivePtr< T > &&  intr_ptr)
inline

The move constructor does not throw. It has move semantics.

Parameters:
intr_ptrThe instrusive pointer to be moved.
template<class T>
template<class U >
Cgu::IntrusivePtr< T >::IntrusivePtr ( const IntrusivePtr< U > &  intr_ptr)
inline

A version of the copy constructor which enables pointer type conversion (assuming the type passed is implicitly type convertible to the managed type, such as a derived type). This copy constructor does not throw.

Parameters:
intr_ptrThe intrusive pointer to be copied.
template<class T>
template<class U >
Cgu::IntrusivePtr< T >::IntrusivePtr ( IntrusivePtr< U > &&  intr_ptr)
inline

A version of the move constructor which enables pointer type conversion (assuming the type passed is implicitly type convertible to the managed type, such as a derived type). This move constructor does not throw.

Parameters:
intr_ptrThe intrusive pointer to be moved.
template<class T>
Cgu::IntrusivePtr< T >::~IntrusivePtr ( )
inline

The destructor does not throw unless the destructor of a managed object throws - that should never happen.


Member Function Documentation

template<class T>
T* Cgu::IntrusivePtr< T >::get ( ) const
inline

This method does not throw.

Returns:
A pointer to the managed object (or NULL if none is handled).
template<class T>
T& Cgu::IntrusivePtr< T >::operator* ( ) const
inline

This method does not throw.

Returns:
A reference to the managed object.
template<class T>
T* Cgu::IntrusivePtr< T >::operator-> ( ) const
inline

This method does not throw.

Returns:
A pointer to the managed object (or NULL if none is handled).
template<class T>
IntrusivePtr& Cgu::IntrusivePtr< T >::operator= ( IntrusivePtr< T >  intr_ptr)
inline

This method (and so copy or move assignment) does not throw unless the destructor of a managed object throws.

Parameters:
intr_ptrThe assignee.
Returns:
The IntrusivePtr object after assignment.
template<class T>
template<class U >
IntrusivePtr& Cgu::IntrusivePtr< T >::operator= ( const IntrusivePtr< U > &  intr_ptr)
inline

A version of the assignment operator which enables pointer type conversion (assuming the type passed is implicitly type convertible to the managed type, such as a derived type). This method does not throw unless the destructor of a managed object throws.

Parameters:
intr_ptrThe assignee.
Returns:
The IntrusivePtr object after assignment.
template<class T>
template<class U >
IntrusivePtr& Cgu::IntrusivePtr< T >::operator= ( IntrusivePtr< U > &&  intr_ptr)
inline

A version of the operator for move assignment which enables pointer type conversion (assuming the type passed is implicitly type convertible to the managed type, such as a derived type). This method does not throw unless the destructor of a managed object throws.

Parameters:
intr_ptrThe intrusive pointer to be moved.
Returns:
The IntrusivePtr object after the move operation.
template<class T>
void Cgu::IntrusivePtr< T >::reset ( T *  ptr = 0)
inline

Causes the intrusive pointer to cease to manage its managed object, deleting it if this is the last intrusive pointer managing it. If the argument passed is not NULL, the intrusive pointer will manage the new object passed. This method does not throw unless the destructor of a managed object throws.

Parameters:
ptrNULL (the default), or a new object to manage.

Friends And Related Function Documentation

template<class T>
friend class IntrusivePtr
friend

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