Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members  

mpcl::memory::TSmartPointer< TItem > Class Template Reference

#include <smart_pointer.hh>

Inheritance diagram for mpcl::memory::TSmartPointer< TItem >:

mpcl::memory::TBaseSmartPointer List of all members.

Public Types

typedef const TItem * const_pointer
typedef const TItem & const_reference
typedef TItem * pointer
typedef TItem & reference
typedef size_t size_type
typedef TItem value_type

Public Methods

 TSmartPointer (void)
 Builds a new instance.

 TSmartPointer (const TSmartPointer &rkqtITEM)
template<typename TDerivedItem>  TSmartPointer (const TSmartPointer< TDerivedItem > &rkqtITEM_DERIVED)
 TSmartPointer (const_pointer pktITEM)
 ~TSmartPointer (void)
 Destroys the instance.

TSmartPointer & operator= (const_pointer pktITEM)
void release (void)
template<typename TPointer> TPointer constCast (void) const throw ()
pointer get (void) const throw ()
bool operator== (const TSmartPointer &rkqtITEM) const throw ()
bool operator!= (const TSmartPointer &rkqtITEM) const throw ()
bool operator! (void) const throw ()
pointer operator-> (void) const throw (TConstraintException)
reference operator * (void) const throw (TConstraintException)

Protected Methods

void freeCell (void)
void reserveCell (const_pointer pktITEM)

Detailed Description

template<typename TItem>
class mpcl::memory::TSmartPointer< TItem >

Smart pointer for objects obtained via the operator new.

This class provides a semantics of shared ownership. After the initial construction, a TSmartPointer owns the object it holds a pointer to. Copying a TSmartPointer copies the pointer and shares ownership with the destination. If more than one TSmartPointer owns the same object at the same time the behaviour of the program is to delete the object when the last TSmartPointer is destroyed. This class instances should be initialized from a TItem class (or derived) instances created with new, or from other TSmartPointer instances. This is so, because when this instance is asigned to another pointer value, the former pointer value is deleted when there are no more references to it.

A common error when using smart pointers is assigning a TSmartPointer object to another TSmartPointer object type-casted to TItem*. This is one of worst things you can do, because both instances think that they do not share the object with the other smart pointer.

Until version 1.42, this class implemented sharing between derived smart pointers. This is an error prone 'feature' that let the smart pointer holding an address of a parent object and thinking that it holds an address of a derived object. Now let us see some examples of bad use of this old features:

qtFather = new TSon(); // qtFather == :TSon + x DynamicCast (qtSon, qtFather); // qtSon == :TSon + x => ERROR

qtSon = new TSon(); // qtSon == :TSon qtFather = qtSon; // qtFather == :TSon => ERROR

These are examples of bad use of current smart pointers:

class TParent {}; class TSon : public TParent {};

TParent tParent; TParent* ptParent1 (new TParent()); TParent* ptParent2 (new TParent()); TSmartPointer<TParent> qtParent (&tParent);

qtParent = new TSon(); // Error when releasing a static allocated object (&tParent). qtParent = ptParent1; qtParent = ptParent2; // Object pointed by ptParent1 is deleted. qtParent = ptParent1; // Error, ptParent1 points to a deleted object.

Precondition:
( !ptSharedCell )
( ptSharedCell != NULL ) => ( ptSharedCell->pvItem != NULL )
See also:
operator new().
Warning:
This class is not thread safe.

Definition at line 93 of file smart_pointer.hh.


Member Typedef Documentation

template<typename TItem>
typedef const TItem* mpcl::memory::TSmartPointer< TItem >::const_pointer
 

Item const pointer type.

See also:
lib.container.requirements.

Definition at line 104 of file smart_pointer.hh.

template<typename TItem>
typedef const TItem& mpcl::memory::TSmartPointer< TItem >::const_reference
 

Item const reference type.

See also:
lib.container.requirements.

Definition at line 112 of file smart_pointer.hh.

template<typename TItem>
typedef TItem* mpcl::memory::TSmartPointer< TItem >::pointer
 

Item pointer type.

See also:
lib.container.requirements.

Definition at line 120 of file smart_pointer.hh.

template<typename TItem>
typedef TItem& mpcl::memory::TSmartPointer< TItem >::reference
 

Item reference type.

See also:
lib.container.requirements.

Definition at line 128 of file smart_pointer.hh.

template<typename TItem>
typedef size_t mpcl::memory::TSmartPointer< TItem >::size_type
 

Item size type.

See also:
lib.container.requirements.

Definition at line 136 of file smart_pointer.hh.

template<typename TItem>
typedef TItem mpcl::memory::TSmartPointer< TItem >::value_type
 

Item value type.

See also:
lib.container.requirements.

Definition at line 144 of file smart_pointer.hh.


Constructor & Destructor Documentation

template<typename TItem>
mpcl::memory::TSmartPointer< TItem >::TSmartPointer const TSmartPointer< TItem > &    rkqtITEM [inline]
 

Builds a new instance from another instance.

Parameters:
rkqtITEM  Source instance.

Definition at line 191 of file smart_pointer.hh.

template<typename TItem>
template<typename TDerivedItem>
mpcl::memory::TSmartPointer< TItem >::TSmartPointer const TSmartPointer< TDerivedItem > &    rkqtITEM_DERIVED [inline, explicit]
 

Builds a new instance from another TSmartPointer instantiation.

Parameters:
rkqtITEM_DERIVED  Source instance.

Definition at line 203 of file smart_pointer.hh.

template<typename TItem>
mpcl::memory::TSmartPointer< TItem >::TSmartPointer const_pointer    pktITEM [inline, explicit]
 

Builds a new instance from a pointer.

Parameters:
pktITEM  Source item.
Exceptions:
std::bad_alloc  If there is no memory available.

Definition at line 229 of file smart_pointer.hh.

References mpcl::memory::TSmartPointer< TItem >::reserveCell().


Member Function Documentation

template<typename TItem>
template<typename TPointer>
TPointer mpcl::memory::TSmartPointer< TItem >::constCast void    const throw () [inline]
 

Function to cast this instance to another non-const.

Returns:
The constant instance copy.

Definition at line 307 of file smart_pointer.hh.

References mpcl::memory::TBaseSmartPointer::ptSharedCell.

template<typename TItem>
void mpcl::memory::TSmartPointer< TItem >::freeCell void    [inline, protected]
 

Frees space of the cell.

Precondition:
( ptSharedCell != NULL )
( ptSharedCell->pvItem != NULL )

Definition at line 158 of file smart_pointer.hh.

References mpcl::memory::TBaseSmartPointer::ptSharedCell.

Referenced by mpcl::memory::TSmartPointer< TItem >::release().

template<typename TItem>
pointer mpcl::memory::TSmartPointer< TItem >::get void    const throw () [inline]
 

Returns a pointer to the shared item.

Returns:
A pointer to the shared item.

Definition at line 345 of file smart_pointer.hh.

References mpcl::memory::TBaseSmartPointer::ptSharedCell.

template<typename TItem>
reference mpcl::memory::TSmartPointer< TItem >::operator * void    const throw (TConstraintException) [inline]
 

Returns the shared item.

Returns:
The shared item.

Definition at line 403 of file smart_pointer.hh.

References mpcl::memory::TBaseSmartPointer::ptSharedCell.

template<typename TItem>
bool mpcl::memory::TSmartPointer< TItem >::operator! void    const throw () [inline]
 

Returns true if this represents a NULL pointer.

Returns:
True if this represents a NULL pointer.

Definition at line 380 of file smart_pointer.hh.

References mpcl::memory::TBaseSmartPointer::ptSharedCell.

template<typename TItem>
bool mpcl::memory::TSmartPointer< TItem >::operator!= const TSmartPointer< TItem > &    rkqtITEM const throw () [inline]
 

Returns false if both instances are bound.

Parameters:
rkqtITEM  Source instance.
Returns:
False if both instances are bound.

Definition at line 371 of file smart_pointer.hh.

References mpcl::memory::TSmartPointer< TItem >::operator==().

template<typename TItem>
pointer mpcl::memory::TSmartPointer< TItem >::operator-> void    const throw (TConstraintException) [inline]
 

Returns the shared item.

Returns:
The shared item.

Definition at line 389 of file smart_pointer.hh.

References mpcl::memory::TBaseSmartPointer::ptSharedCell.

template<typename TItem>
TSmartPointer& mpcl::memory::TSmartPointer< TItem >::operator= const_pointer    pktITEM [inline]
 

Creates a new cell holding the pointer pktITEM.

Parameters:
pktITEM  Source pointer.
Returns:
This instance.

Definition at line 246 of file smart_pointer.hh.

References mpcl::memory::TSmartPointer< TItem >::release(), and mpcl::memory::TSmartPointer< TItem >::reserveCell().

template<typename TItem>
bool mpcl::memory::TSmartPointer< TItem >::operator== const TSmartPointer< TItem > &    rkqtITEM const throw () [inline]
 

Returns true if both instances hold the same item or are both null.

Parameters:
rkqtITEM  Source instance.
Returns:
True if both instances are bound.

Definition at line 361 of file smart_pointer.hh.

Referenced by mpcl::memory::TSmartPointer< TItem >::operator!=().

template<typename TItem>
void mpcl::memory::TSmartPointer< TItem >::release void    [inline]
 

Decrements reference count and releases the shared cell and the bound item if this is the last owner.

Definition at line 267 of file smart_pointer.hh.

References mpcl::memory::TSmartPointer< TItem >::freeCell(), and mpcl::memory::TBaseSmartPointer::ptSharedCell.

Referenced by mpcl::memory::TSmartPointer< TItem >::operator=(), and mpcl::memory::TSmartPointer< TItem >::~TSmartPointer().

template<typename TItem>
void mpcl::memory::TSmartPointer< TItem >::reserveCell const_pointer    pktITEM [inline, protected]
 

Reserves space for a new cell that will contains pktITEM.

Parameters:
pktITEM  Item pointer to contain.

Definition at line 168 of file smart_pointer.hh.

References mpcl::memory::TBaseSmartPointer::ptSharedCell.

Referenced by mpcl::memory::TSmartPointer< TItem >::operator=(), and mpcl::memory::TSmartPointer< TItem >::TSmartPointer().


The documentation for this class was generated from the following file:
Generated on Mon Oct 13 02:35:25 2003 for MPCL by doxygen1.2.18