The Micro C++ Library
|
The SharedPtr template is used to store a pointer of type T*
which must have been allocated by new and which is automatically deleted when the SharedPtr instance is deallocated.
More...
#include <micxx.hxx>
Public Member Functions | |
SharedPtr (T *pointer=0) | |
Create a new SharedPtr instance for the pointer pointer. | |
SharedPtr (const SharedPtr< T > &right) | |
Create a new SharedPtr instance to a shared pointer that is already stored in right. | |
virtual | ~SharedPtr () |
Deallocate this SharedPtr instance by decrementing the shared reference counter. If the reference counter is decremented to 0 , the stored pointer is deleted, otherwise nothing more is done. | |
virtual | operator T * () |
virtual | operator const T * () const |
virtual bool | operator! () |
Check, if the stored (shared) pointer is 0 . | |
virtual T * | operator-> () |
virtual const T * | operator-> () const |
virtual T & | operator* () |
virtual const T & | operator* () const |
virtual SharedPtr< T > & | operator= (T *pointer) |
Replace the stored (shared) pointer with pointer. | |
virtual SharedPtr< T > & | operator= (const SharedPtr< T > &right) |
Replace the stored (shared) pointer with the shared pointer that is already stored by right. | |
virtual bool | operator== (const SharedPtr< T > &right) |
Check, if this and right store the same shared pointer. | |
virtual T * | release () |
The stored (shared) pointer is replaced with 0 . After this call all copies of this SharedPtr instance will point to 0 . The old stored (shared) pointer is no more automatically deleted by any copy of the SharedPtr instances that shared this pointer. | |
Protected Attributes | |
RefCountPtr< T > * | sharedPointer |
The stored (shared) pointer instance. |
The SharedPtr template is used to store a pointer of type T*
which must have been allocated by new and which is automatically deleted when the SharedPtr instance is deallocated.
When a pointer is assigned to an SharedPtr instance, it must neither be deleted explicitly nor assigned to another SharedPtr instance anymore without calling release() before.
It is possible to have several copies of an SharedPtr instances to the same shared pointer. All SharedPtr copies share the same pointer and a shared reference counter keeps track of the number of copies. Each time an SharedPtr instance is deallocated, the shared reference counter is decremented by one. If the last SharedPtr instance for a shared pointer is deallocated and the reference counter is decremented to 0
, the shared pointer is automatically deleted.
{ SharedPtr<std::string> astring(new std::string("A dynamic string")); // reference counter for astring is 1 SharedPtr<std::string> astring_copy(astring); // refecence counter for astring and astring_copy is 2 // using astring in the same way as string* std::cout << *astring << " " << astring_copy->length() << std::endl; // when leaving the block astring_copy will be deallocated // decreasing the reference counter by one and // astring will be deallocated decreasing the reference counter to // zero, which will eventually delete the wrapped string*. }
micxx::SharedPtr< T >::SharedPtr | ( | T * | pointer = 0 | ) |
Create a new SharedPtr instance for the pointer pointer.
pointer | The pointer to be stored. This pointer must not be deleted explicitly anymore without calling release() before. |
: sharedPointer(0) { __MICXXTRY__ sharedPointer = new RefCountPtr<T>(pointer); __MICXXEND__ }
micxx::SharedPtr< T >::SharedPtr | ( | const SharedPtr< T > & | right | ) |
Create a new SharedPtr instance to a shared pointer that is already stored in right.
right | An already existing SharedPtr instance with a shared pointer. |
: sharedPointer(0) { __MICXXTRY__ sharedPointer = right.sharedPointer; ++(*sharedPointer); __MICXXEND__ }
micxx::SharedPtr< T >::~SharedPtr | ( | ) | [virtual] |
Deallocate this SharedPtr instance by decrementing the shared reference counter. If the reference counter is decremented to 0
, the stored pointer is deleted, otherwise nothing more is done.
{ __MICXXTRY__ --(*sharedPointer); if (!*sharedPointer) { delete sharedPointer; } __MICXXEND__ }
micxx::SharedPtr< T >::operator const T * | ( | ) | const [virtual] |
{ __MICXXTRY__ return static_cast<const T*>(*sharedPointer); __MICXXEND__ }
micxx::SharedPtr< T >::operator T * | ( | ) | [virtual] |
{ __MICXXTRY__ return static_cast<T*>(*sharedPointer); __MICXXEND__ }
bool micxx::SharedPtr< T >::operator! | ( | ) | [virtual] |
Check, if the stored (shared) pointer is 0
.
true
, if (T*)this
is 0
, orfalse
, otherwise. { __MICXXTRY__ return static_cast<T*>(*sharedPointer) == 0; __MICXXEND__ }
T & micxx::SharedPtr< T >::operator* | ( | ) | [virtual] |
{ __MICXXTRY__ return *static_cast<T*>(*sharedPointer); __MICXXEND__ }
const T & micxx::SharedPtr< T >::operator* | ( | ) | const [virtual] |
{ __MICXXTRY__ return *static_cast<const T*>(*sharedPointer); __MICXXEND__ }
T * micxx::SharedPtr< T >::operator-> | ( | ) | [virtual] |
{ __MICXXTRY__ return static_cast<T*>(*sharedPointer); __MICXXEND__ }
const T * micxx::SharedPtr< T >::operator-> | ( | ) | const [virtual] |
{ __MICXXTRY__ return static_cast<const T*>(*sharedPointer); __MICXXEND__ }
SharedPtr< T > & micxx::SharedPtr< T >::operator= | ( | T * | pointer | ) | [virtual] |
Replace the stored (shared) pointer with pointer.
The shared reference counter is decremented before and a pointer that is already stored is deleted, if the reference counter is decremented to 0
.
pointer | The pointer to be stored. This pointer must neither be deleted explicitly nor assigned to any other SharedPtr instance anymore without calling release() before. |
{ __MICXXTRY__ --(*sharedPointer); if (!*sharedPointer) { *sharedPointer = pointer; } else { sharedPointer = new RefCountPtr<T>(pointer); } return *this; __MICXXEND__ }
SharedPtr< T > & micxx::SharedPtr< T >::operator= | ( | const SharedPtr< T > & | right | ) | [virtual] |
Replace the stored (shared) pointer with the shared pointer that is already stored by right.
The shared reference counter is decremented before and a pointer that is already stored is deleted, if the reference counter is decremented to 0
. Finally the shared reference counter of right is incremented.
*this == right
will be true
.right | The SharedPtr instance for the shared pointer for which a copy should be created. |
{ __MICXXTRY__ --(*sharedPointer); if (!*sharedPointer) { delete sharedPointer; } sharedPointer = right.sharedPointer; ++(*sharedPointer); return *this; __MICXXEND__ }
bool micxx::SharedPtr< T >::operator== | ( | const SharedPtr< T > & | right | ) | [virtual] |
Check, if this and right store the same shared pointer.
true
, if this an right store the same shared pointer, orfalse
, otherwise. { return sharedPointer == right.sharedPointer; }
T * micxx::SharedPtr< T >::release | ( | ) | [virtual] |
The stored (shared) pointer is replaced with 0
. After this call all copies of this SharedPtr instance will point to 0
. The old stored (shared) pointer is no more automatically deleted by any copy of the SharedPtr instances that shared this pointer.
{ __MICXXTRY__ return sharedPointer->release(); __MICXXEND__ }
RefCountPtr<T>* micxx::SharedPtr< T >::sharedPointer [protected] |
The stored (shared) pointer instance.