GaSmartPtr
template class wraps C++ raw pointers, and takes over responsibility of managing the allocated memory. Smart pointer holds address of user data and reference to an object which is responsible for counting number of references to data, when there are no instances of GaSmartPtr
pointing to location of the data (reference count of the location reaches 0), object is destroyed and memory used by the object is freed. Memory management by GaSmartPtr
class is thread-safe, but after dereferencing smart pointer to access the data, it cannot be guaranteed that memory will not be freed if some other thread changes dereferenced pointer. Implemented smart pointers have some limitations:
#include <D:/Temp/vs/gal/source/SmartPtr.h>
Public Member Functions | |
GaSmartPtr (GaSmartStorage< T > *storage) | |
This constructor makes new reference to data that are managed by GaSmertStorage. Creation of smart pointer and counting of references is thread-safe. | |
GaSmartPtr (T *rawPtr) | |
This constructor make instance of GaSmartStorage and binds unmanaged memory to the smart storage. If the given memory is already managed by this mechanism, it can cause unexpected results. Creation of smart pointer and counting of references is thread-safe. | |
GaSmartPtr (const GaSmartPtr< T > &ptr) | |
Copy constructor makes new reference to data to which ptr points. Creation of smart pointer and counting of references is thread-safe. | |
GaSmartPtr () | |
Default constructor, initializes pointer as NULL pointer. Creation of smart pointer and counting of references is thread-safe. | |
~GaSmartPtr () | |
Decrements number of references to data. If there is no more references, memory used my data is freed and object is destroyed. Destruction of smart pointer is thread-safe. | |
T *GACALL | operator-> () const |
Operator provides access to data to which smart pointer points. This method is thread safe, but it cannot be guaranteed that memory will not be freed if some other thread changes pointer after address is returned. | |
T &GACALL | operator* () const |
Operator provides access to data to which smart pointer points. This method is thread safe, but it cannot be guaranteed that memory will not be freed if some other thread changes pointer after address is returned. | |
GaSmartPtr< T > &GACALL | operator= (const GaSmartPtr< T > &rhs) |
Sets smart pointer to points to same location as rhs pointer. It also decrements number of references of old smart locationa and increments number of new smart location. If number of references of old location reached zero, this operator frees used memory. This operator is thread-safe. | |
GaSmartPtr< T > &GACALL | operator= (GaSmartStorage< T > &rhs) |
Sets smart pointer to points to rhs smart location for storing data. It also decrements number of references of old smart locationa and increments number of new smart location. If number of references of old location reached zero, this operator frees used memory. This operator is thread-safe. | |
GaSmartPtr< T > &GACALL | operator= (T *rhs) |
This operator makes new instance of GaSmartStorage and binds unmanaged memory to the smart storage and sets pointer to it. It also decrements number of references of old smart locationa and increments number of new smart location. If number of references of old location reached zero, this operator frees used memory. This operator is thread-safe. | |
bool GACALL | operator== (const GaSmartPtr< T > &rhs) const |
Compares two smart pointers to see they points to same data. This operator is thread-safe. | |
T *GACALL | GetRawPtr () const |
This method is thread-safe. | |
bool GACALL | IsNULL () const |
Checks pointer against NULL value. This method is thread-safe. | |
Static Public Attributes | |
static const GaSmartPtr< T > | NullPtr |
NullPtr is global constant NULL pointer for T type. | |
Private Member Functions | |
void | Lock () const |
This method prevents concurrent changes of smart pointer by locking it. | |
void | Lock (const GaSmartPtr< T > &second) const |
This method prevents concurrent changes of this and second smart pointers and it is used when value of one pointer is assigning to another (operator = or copy constructor). Order of pointer locking is defined by order of addresses of pointers' objects to prevent deadlocks (first is locked pointer which object has lower address). | |
void | Unlock () const |
This method unlocks changes of smart pointer. | |
void | Unlock (const GaSmartPtr< T > &second) |
This method unlocks changes of this and second smart pointers, it is used after assigning of values from one smart pointer to another is done (operator = or copy constructor). | |
Private Attributes | |
volatile unsigned long | _lock |
Guards smart pointer against concurrent changes. | |
T * | _data |
Holds address of user data. | |
GaSmartStorage< T > * | _location |
Pointer to object which holds reference-count and address of data. |
GaSmartPtr
template class wraps C++ raw pointers, and takes over responsibility of managing the allocated memory. Smart pointer holds address of user data and reference to an object which is responsible for counting number of references to data, when there are no instances of GaSmartPtr
pointing to location of the data (reference count of the location reaches 0), object is destroyed and memory used by the object is freed. Memory management by GaSmartPtr
class is thread-safe, but after dereferencing smart pointer to access the data, it cannot be guaranteed that memory will not be freed if some other thread changes dereferenced pointer. Implemented smart pointers have some limitations:
This class has no built-in synchronizator, so LOCK_OBJECT
and LOCK_THIS_OBJECT
macros cannot be used with instances of this class, but all public method and operators are thread-safe.
T | type of data to which smart pointer references. |
Common::GaSmartPtr< T >::GaSmartPtr | ( | GaSmartStorage< T > * | storage | ) | [inline] |
This constructor makes new reference to data that are managed by GaSmertStorage. Creation of smart pointer and counting of references is thread-safe.
storage | reference to object responsible for reference-counting. |
Common::GaSmartPtr< T >::GaSmartPtr | ( | T * | rawPtr | ) | [inline] |
This constructor make instance of GaSmartStorage and binds unmanaged memory to the smart storage. If the given memory is already managed by this mechanism, it can cause unexpected results. Creation of smart pointer and counting of references is thread-safe.
rawPtr | raw pointer to data. |
Common::GaSmartPtr< T >::GaSmartPtr | ( | const GaSmartPtr< T > & | ptr | ) | [inline] |
Copy constructor makes new reference to data to which ptr
points. Creation of smart pointer and counting of references is thread-safe.
ptr | reference to smart pointer which should be copied. |
Common::GaSmartPtr< T >::GaSmartPtr | ( | ) | [inline] |
Default constructor, initializes pointer as NULL
pointer. Creation of smart pointer and counting of references is thread-safe.
Common::GaSmartPtr< T >::~GaSmartPtr | ( | ) | [inline] |
Decrements number of references to data. If there is no more references, memory used my data is freed and object is destroyed. Destruction of smart pointer is thread-safe.
T* GACALL Common::GaSmartPtr< T >::operator-> | ( | ) | const [inline] |
Operator provides access to data to which smart pointer points. This method is thread safe, but it cannot be guaranteed that memory will not be freed if some other thread changes pointer after address is returned.
T& GACALL Common::GaSmartPtr< T >::operator* | ( | ) | const [inline] |
Operator provides access to data to which smart pointer points. This method is thread safe, but it cannot be guaranteed that memory will not be freed if some other thread changes pointer after address is returned.
GaSmartPtr<T>& GACALL Common::GaSmartPtr< T >::operator= | ( | const GaSmartPtr< T > & | rhs | ) | [inline] |
Sets smart pointer to points to same location as rhs
pointer. It also decrements number of references of old smart locationa and increments number of new smart location. If number of references of old location reached zero, this operator frees used memory. This operator is thread-safe.
rhs | smart pointer which holds address to which this pointer should point. |
this
object.GaSmartPtr<T>& GACALL Common::GaSmartPtr< T >::operator= | ( | GaSmartStorage< T > & | rhs | ) | [inline] |
Sets smart pointer to points to rhs
smart location for storing data. It also decrements number of references of old smart locationa and increments number of new smart location. If number of references of old location reached zero, this operator frees used memory. This operator is thread-safe.
rhs | reference to smart location to which this pointer should point. |
this
object.GaSmartPtr<T>& GACALL Common::GaSmartPtr< T >::operator= | ( | T * | rhs | ) | [inline] |
This operator makes new instance of GaSmartStorage and binds unmanaged memory to the smart storage and sets pointer to it. It also decrements number of references of old smart locationa and increments number of new smart location. If number of references of old location reached zero, this operator frees used memory. This operator is thread-safe.
rhs | raw pointer to user data which should be managed. |
this
object.bool GACALL Common::GaSmartPtr< T >::operator== | ( | const GaSmartPtr< T > & | rhs | ) | const [inline] |
Compares two smart pointers to see they points to same data. This operator is thread-safe.
rhs | the second smart pointer in the expression. |
T* GACALL Common::GaSmartPtr< T >::GetRawPtr | ( | ) | const [inline] |
This method is thread-safe.
bool GACALL Common::GaSmartPtr< T >::IsNULL | ( | ) | const [inline] |
Checks pointer against NULL
value. This method is thread-safe.
true
if this is NULL
pointer.void Common::GaSmartPtr< T >::Lock | ( | ) | const [inline, private] |
This method prevents concurrent changes of smart pointer by locking it.
void Common::GaSmartPtr< T >::Lock | ( | const GaSmartPtr< T > & | second | ) | const [inline, private] |
This method prevents concurrent changes of this
and second
smart pointers and it is used when value of one pointer is assigning to another (operator =
or copy constructor). Order of pointer locking is defined by order of addresses of pointers' objects to prevent deadlocks (first is locked pointer which object has lower address).
second | the second pointer which is used in expression. |
void Common::GaSmartPtr< T >::Unlock | ( | ) | const [inline, private] |
This method unlocks changes of smart pointer.
void Common::GaSmartPtr< T >::Unlock | ( | const GaSmartPtr< T > & | second | ) | [inline, private] |
This method unlocks changes of this and second smart pointers, it is used after assigning of values from one smart pointer to another is done (operator =
or copy constructor).
second | the second pointer whic is used in expression. |
volatile unsigned long Common::GaSmartPtr< T >::_lock [mutable, private] |
Guards smart pointer against concurrent changes.
T* Common::GaSmartPtr< T >::_data [private] |
Holds address of user data.
GaSmartStorage<T>* Common::GaSmartPtr< T >::_location [private] |
Pointer to object which holds reference-count and address of data.
const GaSmartPtr< T > Common::GaSmartPtr< T >::NullPtr [inline, static] |
NullPtr
is global constant NULL
pointer for T
type.