xorp

cref_ptr< _Tp > Class Template Reference

Compact Reference Counted Pointer Class. More...

#include <ref_ptr.hh>

List of all members.

Public Member Functions

 cref_ptr (_Tp *__p=0)
 Construct a compact reference pointer for object.
 cref_ptr (const cref_ptr &__r)
 Copy Constructor.
cref_ptroperator= (const cref_ptr &__r)
 Assignment Operator.
 ~cref_ptr ()
 Destruct reference pointer instance and lower reference count on object being tracked.
_Tp * get () const
 Dereference pointer to reference counted object.
_Tp & operator* () const
 Dereference reference counted object.
_Tp * operator-> () const
 Dereference pointer to reference counted object.
bool operator== (const cref_ptr &rp) const
 Equality Operator.
bool is_empty () const
 Check if reference pointer refers to an object or whether it has been assigned a null object.
bool is_only () const
bool at_least (int32_t n) const
void release () const
 Release reference on object.

Private Member Functions

void ref (const cref_ptr *__r) const
 Add reference.
void unref () const
 Remove reference.

Private Attributes

int32_t _M_index

Detailed Description

template<class _Tp>
class cref_ptr< _Tp >

Compact Reference Counted Pointer Class.

The cref_ptr class is a strong reference class. It maintains a count of how many references to an object exist and releases the memory associated with the object when the reference count reaches zero. The reference pointer can be dereferenced like an ordinary pointer to call methods on the reference counted object.

In contrast to the ref_ptr class, accessing the pointer requires two levels of indirection as the pointer is stored in the same table as the counter. This is a performance hit, but means each cref_ptr only consumes 4-bytes.

At the time of writing the only supported memory management is through the new and delete operators. At a future date, this class should support the STL allocator classes or an equivalent to provide greater flexibility.


Constructor & Destructor Documentation

template<class _Tp >
cref_ptr< _Tp >::cref_ptr ( _Tp *  __p = 0) [inline]

Construct a compact reference pointer for object.

Parameters:
ppointer to object to be reference counted. p must be allocated using operator new as it will be destructed using delete when the reference count reaches zero.
template<class _Tp >
cref_ptr< _Tp >::cref_ptr ( const cref_ptr< _Tp > &  __r) [inline]

Copy Constructor.

Constructs a reference pointer for object. Raises reference count associated with object by 1.

template<class _Tp >
cref_ptr< _Tp >::~cref_ptr ( ) [inline]

Destruct reference pointer instance and lower reference count on object being tracked.

The object being tracked will be deleted if the reference count falls to zero because of the destruction of the reference pointer.


Member Function Documentation

template<class _Tp >
bool cref_ptr< _Tp >::at_least ( int32_t  n) const [inline]
Parameters:
nminimum count.
Returns:
true if there are at least n references to object.
template<class _Tp >
_Tp* cref_ptr< _Tp >::get ( ) const [inline]

Dereference pointer to reference counted object.

Returns:
pointer to object.
template<class _Tp >
bool cref_ptr< _Tp >::is_empty ( ) const [inline]

Check if reference pointer refers to an object or whether it has been assigned a null object.

Returns:
true if reference pointer refers to a null object.
template<class _Tp >
bool cref_ptr< _Tp >::is_only ( ) const [inline]
Returns:
true if reference pointer represents only reference to object.
template<class _Tp >
_Tp& cref_ptr< _Tp >::operator* ( ) const [inline]

Dereference reference counted object.

Returns:
reference to object.
template<class _Tp >
_Tp* cref_ptr< _Tp >::operator-> ( ) const [inline]

Dereference pointer to reference counted object.

Returns:
pointer to object.
template<class _Tp >
cref_ptr& cref_ptr< _Tp >::operator= ( const cref_ptr< _Tp > &  __r) [inline]

Assignment Operator.

Assigns reference pointer to new object.

template<class _Tp >
bool cref_ptr< _Tp >::operator== ( const cref_ptr< _Tp > &  rp) const [inline]

Equality Operator.

Returns:
true if reference pointers refer to same object.
template<class _Tp >
void cref_ptr< _Tp >::release ( ) const [inline]

Release reference on object.

The reference pointers underlying object is set to null, and the former object is destructed if necessary.


The documentation for this class was generated from the following file:
 All Classes Namespaces Functions Variables Typedefs Enumerations