Simple Application Framework
1
|
Shared pointer class with reference counting. More...
#include <SharedPtr.h>
Public Member Functions | |
SharedPtr () | |
Default constructor. | |
SharedPtr (T *ptr) | |
Constructor that binds a given pointer. | |
SharedPtr (const SharedPtr< T > &p) | |
Copy constructor. | |
~SharedPtr () | |
Destructor. | |
SharedPtr< T > & | Bind (T *ptr) |
Bind a given object pointer. | |
SharedPtr< T > & | BindWithCounter (T *ptr, Size *counter) |
Bind a given object pointer and an external reference counter. | |
template<class C > | |
SharedPtr< C > | Cast () |
Dynamic cast to a different pointer type. | |
void | Free () |
Explicitly unbind and free the attached object. | |
T * | Get () |
Get a pointer to the binded object. | |
const T * | Get () const |
Get a pointer to the binded object. | |
bool | IsNull () const |
Returns true when there is no binded pointer. | |
T & | operator* () |
Get a reference to the binded object. | |
const T & | operator* () const |
Get a reference to the binded object. | |
T * | operator-> () |
Get a pointer to the binded object. | |
const T * | operator-> () const |
Get a pointer to the binded object. | |
SharedPtr< T > & | operator= (const SharedPtr< T > &p) |
Assignment operator. | |
void | Swap (SharedPtr< T > &p) |
Swap two shared pointers. |
Shared pointer class with reference counting.
Use this class instead of standard pointers to obtain an exception safe pointer class with automatic deallocation and reference counting. Examples:
// The memory will be freed automatically when p is destroyed Saf::SharedPtr<int> p(SAF_NEW int); // The memory will be freed automatically when BOTH p and q are destroyed Saf::SharedPtr<int> p(SAF_NEW int), q; q = p; // Another example with type casting Saf::SharedPtr<A> p(SAF_NEW A); Saf::SharedPtr<B> q = p.template Cast<B>(); // Type cast, works if class A is derived from class B p.Free(); // Unbind, the memory is not deallocated as it is still referenced by q q.Free(); // Unbind, the memory is deallocated now
Because of the reference counting the Saf::SharedPtr object can be safely returned from functions, used in collections, etc. However, be careful in the following situations:
func(Saf::SharedPtr<A>(SAF_NEW A()), SomeFunc());
SomeFunc
after the allocation of A()
and before the constructor of Saf::SharedPtr and thus creating a dangling pointer.Saf::Mem::SharedPtr< T >::SharedPtr | ( | ) | [inline] |
Default constructor.
Saf::Mem::SharedPtr< T >::SharedPtr | ( | T * | ptr | ) | [inline, explicit] |
Constructor that binds a given pointer.
This constructor is explicit to prevent dangerous automatic conversions of pointers to Saf::SharedPtr.
Saf::Mem::SharedPtr< T >::SharedPtr | ( | const SharedPtr< T > & | p | ) | [inline] |
Copy constructor.
Saf::Mem::SharedPtr< T >::~SharedPtr | ( | ) | [inline] |
Destructor.
SharedPtr<T>& Saf::Mem::SharedPtr< T >::Bind | ( | T * | ptr | ) | [inline] |
Bind a given object pointer.
When the pointer is binded it becomes reference counted and is freed when the last reference disappears. Do not bind the same pointer to several Saf::SharedPtr objects, use copy constructor or assignment operator instead. Example:
int *a = SAF_NEW int(5); Saf::SharedPtr<int> p(a), q(a); // Wrong, p and q do not know of each other, memory will be deallocated twice! Saf::SharedPtr<int> p(a), q(p); // Correct Saf::SharedPtr<int> p(a), q = p; // Correct
SharedPtr<T>& Saf::Mem::SharedPtr< T >::BindWithCounter | ( | T * | ptr, |
Size * | counter | ||
) | [inline] |
Bind a given object pointer and an external reference counter.
Dynamic cast to a different pointer type.
Can be used to safely cast the attached pointer to a different type (e.g., to a base class) without affecting the reference counting.
void Saf::Mem::SharedPtr< T >::Free | ( | ) | [inline] |
Explicitly unbind and free the attached object.
Keep in mind that the binded pointer is actually freed from the memory only when there are no other references to it.
T* Saf::Mem::SharedPtr< T >::Get | ( | ) | [inline] |
Get a pointer to the binded object.
const T* Saf::Mem::SharedPtr< T >::Get | ( | ) | const [inline] |
Get a pointer to the binded object.
bool Saf::Mem::SharedPtr< T >::IsNull | ( | ) | const [inline] |
Returns true when there is no binded pointer.
T& Saf::Mem::SharedPtr< T >::operator* | ( | ) | [inline] |
Get a reference to the binded object.
const T& Saf::Mem::SharedPtr< T >::operator* | ( | ) | const [inline] |
Get a reference to the binded object.
T* Saf::Mem::SharedPtr< T >::operator-> | ( | ) | [inline] |
Get a pointer to the binded object.
const T* Saf::Mem::SharedPtr< T >::operator-> | ( | ) | const [inline] |
Get a pointer to the binded object.
SharedPtr<T>& Saf::Mem::SharedPtr< T >::operator= | ( | const SharedPtr< T > & | p | ) | [inline] |
Assignment operator.
void Saf::Mem::SharedPtr< T >::Swap | ( | SharedPtr< T > & | p | ) | [inline] |
Swap two shared pointers.