c++-gtk-utils
Public Member Functions | Friends | List of all members
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>

Public Member Functions

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

Friends

template<class U >
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

◆ IntrusivePtr() [1/5]

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

This constructor does not throw unless the managed object's ref() function throws (which would not happen with any sane implementation, and does not happen with the IntrusiveCounter and IntrusiveLockCounter classes).

Parameters
ptrThe object which the IntrusivePtr is to manage (if any).

◆ IntrusivePtr() [2/5]

template<class T >
Cgu::IntrusivePtr< T >::IntrusivePtr ( const IntrusivePtr< T > &  intr_ptr)
inline

This copy constructor does not throw unless the managed object's ref() function throws (which would not happen with any sane implementation, and does not happen with the IntrusiveCounter and IntrusiveLockCounter classes).

Parameters
intr_ptrThe intrusive pointer to be copied.

◆ IntrusivePtr() [3/5]

template<class T >
Cgu::IntrusivePtr< T >::IntrusivePtr ( IntrusivePtr< T > &&  intr_ptr)
inlinenoexcept

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

Parameters
intr_ptrThe instrusive pointer to be moved.

◆ IntrusivePtr() [4/5]

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 unless the managed object's ref() function throws (which would not happen with any sane implementation, and does not happen with the IntrusiveCounter and IntrusiveLockCounter classes).

Parameters
intr_ptrThe intrusive pointer to be copied.

◆ IntrusivePtr() [5/5]

template<class T >
template<class U >
Cgu::IntrusivePtr< T >::IntrusivePtr ( IntrusivePtr< U > &&  intr_ptr)
inlinenoexcept

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.

◆ ~IntrusivePtr()

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

◆ get()

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

This method does not throw.

Returns
A pointer to the managed object (or NULL if none is handled).

◆ operator*()

template<class T >
T& Cgu::IntrusivePtr< T >::operator* ( ) const
inlinenoexcept

This method does not throw.

Returns
A reference to the managed object.

◆ operator->()

template<class T >
T* Cgu::IntrusivePtr< T >::operator-> ( ) const
inlinenoexcept

This method does not throw.

Returns
A pointer to the managed object (or NULL if none is handled).

◆ operator=() [1/3]

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 unref() function or destructor of a managed object throws.

Parameters
intr_ptrThe assignee.
Returns
The IntrusivePtr object after assignment.

◆ operator=() [2/3]

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 unref() function or destructor of a managed object throws.

Parameters
intr_ptrThe assignee.
Returns
The IntrusivePtr object after assignment.

◆ operator=() [3/3]

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 unref() function or destructor of a managed object throws.

Parameters
intr_ptrThe intrusive pointer to be moved.
Returns
The IntrusivePtr object after the move operation.

◆ reset()

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 the unref() function or destructor of a managed object throws.

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

Friends And Related Function Documentation

◆ IntrusivePtr

template<class T >
template<class U >
friend class IntrusivePtr
friend

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