#include <smart_pointer.hh>
Inheritance diagram for mpcl::memory::TSmartPointer< TItem >:
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) |
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.
Definition at line 93 of file smart_pointer.hh.
|
Item const pointer type.
Definition at line 104 of file smart_pointer.hh. |
|
Item const reference type.
Definition at line 112 of file smart_pointer.hh. |
|
Item pointer type.
Definition at line 120 of file smart_pointer.hh. |
|
Item reference type.
Definition at line 128 of file smart_pointer.hh. |
|
Item size type.
Definition at line 136 of file smart_pointer.hh. |
|
Item value type.
Definition at line 144 of file smart_pointer.hh. |
|
Builds a new instance from another instance.
Definition at line 191 of file smart_pointer.hh. |
|
Builds a new instance from another TSmartPointer instantiation.
Definition at line 203 of file smart_pointer.hh. |
|
Builds a new instance from a pointer.
Definition at line 229 of file smart_pointer.hh. References mpcl::memory::TSmartPointer< TItem >::reserveCell(). |
|
Function to cast this instance to another non-const.
Definition at line 307 of file smart_pointer.hh. References mpcl::memory::TBaseSmartPointer::ptSharedCell. |
|
Frees space of the cell.
Definition at line 158 of file smart_pointer.hh. References mpcl::memory::TBaseSmartPointer::ptSharedCell. Referenced by mpcl::memory::TSmartPointer< TItem >::release(). |
|
Returns a pointer to the shared item.
Definition at line 345 of file smart_pointer.hh. References mpcl::memory::TBaseSmartPointer::ptSharedCell. |
|
Returns the shared item.
Definition at line 403 of file smart_pointer.hh. References mpcl::memory::TBaseSmartPointer::ptSharedCell. |
|
Returns true if this represents a NULL pointer.
Definition at line 380 of file smart_pointer.hh. References mpcl::memory::TBaseSmartPointer::ptSharedCell. |
|
Returns false if both instances are bound.
Definition at line 371 of file smart_pointer.hh. References mpcl::memory::TSmartPointer< TItem >::operator==(). |
|
Returns the shared item.
Definition at line 389 of file smart_pointer.hh. References mpcl::memory::TBaseSmartPointer::ptSharedCell. |
|
Creates a new cell holding the pointer pktITEM.
Definition at line 246 of file smart_pointer.hh. References mpcl::memory::TSmartPointer< TItem >::release(), and mpcl::memory::TSmartPointer< TItem >::reserveCell(). |
|
Returns true if both instances hold the same item or are both null.
Definition at line 361 of file smart_pointer.hh. Referenced by mpcl::memory::TSmartPointer< TItem >::operator!=(). |
|
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(). |
|
Reserves space for a new cell that will contains pktITEM.
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(). |