Public Member Functions | Static Private Member Functions | Private Attributes
vbl_smart_ptr< T > Class Template Reference

A templated smart pointer class. More...

#include <vbl_smart_ptr.h>

List of all members.

Public Member Functions

 vbl_smart_ptr ()
 vbl_smart_ptr (vbl_smart_ptr< T > const &p)
 vbl_smart_ptr (T *p)
 ~vbl_smart_ptr ()
vbl_smart_ptr< T > & operator= (vbl_smart_ptr< T > const &r)
 Assignment.
vbl_smart_ptr< T > & operator= (T *r)
 operator safe_bool () const
 Cast to bool.
bool operator! () const
 Inverse boolean value.
T & operator* () const
 Dereferencing the pointer.
T * operator-> () const
 These methods all return the raw/dumb pointer.
T * ptr () const
 These methods all return the raw/dumb pointer.
T * as_pointer () const
 These methods all return the raw/dumb pointer.
void unprotect ()
 Used for breaking circular references (see above).
bool is_protected () const
 Is this smart pointer responsible for the object being pointed to.
bool operator== (T const *p) const
 Do a shallow equality.
bool operator!= (T const *p) const
 Do a shallow inequality.
bool operator== (vbl_smart_ptr< T >const &p) const
 Do a shallow equality.
bool operator!= (vbl_smart_ptr< T >const &p) const
 Do a shallow inequality.
bool operator< (vbl_smart_ptr< T >const &p) const
bool operator> (vbl_smart_ptr< T >const &p) const
bool operator<= (vbl_smart_ptr< T >const &p) const
bool operator>= (vbl_smart_ptr< T >const &p) const

Static Private Member Functions

static void ref (T *p)
static void unref (T *p)

Private Attributes

bool protected_
 The protected flag says whether or not the object held will be unref()fed when the smart pointer goes out of scope.
T * ptr_
 Pointer to object, or 0.
 VCL_SAFE_BOOL_DEFINE

Detailed Description

template<class T>
class vbl_smart_ptr< T >

A templated smart pointer class.

This class requires that the class being templated over has the following signatures (methods) :

   void T::ref();
   void T::unref();

By default, the vbl_smart_ptr<T> will ref() the object given to it upon construction and unref() it upon destruction. In some cases, however, it is useful to cause an unref() immediately and to avoid an unref() in the constructor. For example, in the cyclic data structure

 start -> A -> B -> C -> D -> E
          ^                   |
          |                   |
          +-------------------+

The refcounts on A, B, C, D, E are 2, 1, 1, 1, 1 so when 'start' goes out of scope, the refcount will be 1, 1, 1, 1, 1 and therefore the ring never gets deleted. Calling unprotect() on 'E' solves this as it effectively transfers ownership of 'A' from 'E' to 'start'.

Although unprotect() can be handy, it should be used with care. It can sometimes (but not always) be avoided by assigning 0 (null pointer) to one of the nodes in the ring.

See also vbl_ref_count

Definition at line 53 of file vbl_smart_ptr.h.


Constructor & Destructor Documentation

template<class T>
vbl_smart_ptr< T >::vbl_smart_ptr ( ) [inline]

Definition at line 63 of file vbl_smart_ptr.h.

template<class T>
vbl_smart_ptr< T >::vbl_smart_ptr ( vbl_smart_ptr< T > const &  p) [inline]

Definition at line 66 of file vbl_smart_ptr.h.

template<class T>
vbl_smart_ptr< T >::vbl_smart_ptr ( T *  p) [inline]

Definition at line 69 of file vbl_smart_ptr.h.

template<class T>
vbl_smart_ptr< T >::~vbl_smart_ptr ( ) [inline]

Definition at line 72 of file vbl_smart_ptr.h.


Member Function Documentation

template<class T>
T* vbl_smart_ptr< T >::as_pointer ( ) const [inline]

These methods all return the raw/dumb pointer.

WARNING : Do not add an automatic cast to T*. This is intrinsically incorrect as you loose the smartness! In cases where you really need the pointer, it is better to be explicit about it and use one of the methods.

Definition at line 136 of file vbl_smart_ptr.h.

template<class T>
bool vbl_smart_ptr< T >::is_protected ( ) const [inline]

Is this smart pointer responsible for the object being pointed to.

If this value is false, the object does not have to save it.

Definition at line 147 of file vbl_smart_ptr.h.

template<class T>
vbl_smart_ptr< T >::operator safe_bool ( ) const [inline]

Cast to bool.

Definition at line 116 of file vbl_smart_ptr.h.

template<class T>
bool vbl_smart_ptr< T >::operator! ( ) const [inline]

Inverse boolean value.

Definition at line 119 of file vbl_smart_ptr.h.

template<class T>
bool vbl_smart_ptr< T >::operator!= ( T const *  p) const [inline]

Do a shallow inequality.

Do the smart pointers not point to the same object.

Definition at line 168 of file vbl_smart_ptr.h.

template<class T>
bool vbl_smart_ptr< T >::operator!= ( vbl_smart_ptr< T >const &  p) const [inline]

Do a shallow inequality.

Do the smart pointers not point to the same object.

Definition at line 176 of file vbl_smart_ptr.h.

template<class T>
T& vbl_smart_ptr< T >::operator* ( ) const [inline]

Dereferencing the pointer.

Definition at line 122 of file vbl_smart_ptr.h.

template<class T>
T* vbl_smart_ptr< T >::operator-> ( ) const [inline]

These methods all return the raw/dumb pointer.

Definition at line 125 of file vbl_smart_ptr.h.

template<class T>
bool vbl_smart_ptr< T >::operator< ( vbl_smart_ptr< T >const &  p) const [inline]

Definition at line 177 of file vbl_smart_ptr.h.

template<class T>
bool vbl_smart_ptr< T >::operator<= ( vbl_smart_ptr< T >const &  p) const [inline]

Definition at line 179 of file vbl_smart_ptr.h.

template<class T>
vbl_smart_ptr<T>& vbl_smart_ptr< T >::operator= ( vbl_smart_ptr< T > const &  r) [inline]

Assignment.

Definition at line 88 of file vbl_smart_ptr.h.

template<class T>
vbl_smart_ptr<T>& vbl_smart_ptr< T >::operator= ( T *  r) [inline]

Definition at line 93 of file vbl_smart_ptr.h.

template<class T>
bool vbl_smart_ptr< T >::operator== ( T const *  p) const [inline]

Do a shallow equality.

Do they point to the same object.

Definition at line 164 of file vbl_smart_ptr.h.

template<class T>
bool vbl_smart_ptr< T >::operator== ( vbl_smart_ptr< T >const &  p) const [inline]

Do a shallow equality.

Do they point to the same object.

Definition at line 172 of file vbl_smart_ptr.h.

template<class T>
bool vbl_smart_ptr< T >::operator> ( vbl_smart_ptr< T >const &  p) const [inline]

Definition at line 178 of file vbl_smart_ptr.h.

template<class T>
bool vbl_smart_ptr< T >::operator>= ( vbl_smart_ptr< T >const &  p) const [inline]

Definition at line 180 of file vbl_smart_ptr.h.

template<class T>
T* vbl_smart_ptr< T >::ptr ( ) const [inline]

These methods all return the raw/dumb pointer.

Definition at line 128 of file vbl_smart_ptr.h.

template<class T >
void vbl_smart_ptr< T >::ref ( T *  p) [static, private]

Definition at line 12 of file vbl_smart_ptr.txx.

template<class T>
void vbl_smart_ptr< T >::unprotect ( ) [inline]

Used for breaking circular references (see above).

Definition at line 139 of file vbl_smart_ptr.h.

template<class T >
void vbl_smart_ptr< T >::unref ( T *  p) [static, private]

Definition at line 19 of file vbl_smart_ptr.txx.


Member Data Documentation

template<class T>
bool vbl_smart_ptr< T >::protected_ [private]

The protected flag says whether or not the object held will be unref()fed when the smart pointer goes out of scope.

Definition at line 56 of file vbl_smart_ptr.h.

template<class T>
T* vbl_smart_ptr< T >::ptr_ [private]

Pointer to object, or 0.

Definition at line 59 of file vbl_smart_ptr.h.

template<class T>
vbl_smart_ptr< T >::VCL_SAFE_BOOL_DEFINE [private]

Definition at line 61 of file vbl_smart_ptr.h.


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