Common::GaSmartPtr< T > Class Template Reference

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:
1. Dynamically allocated arrays cannot be managed by GaSmartPtr class.
2. Circular references can cause memory leakage. More...

#include <D:/Temp/vs/gal/source/SmartPtr.h>

List of all members.

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.


Detailed Description

template<typename T>
class Common::GaSmartPtr< T >

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:
1. Dynamically allocated arrays cannot be managed by GaSmartPtr class.
2. Circular references can cause memory leakage.

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.

Parameters:
T type of data to which smart pointer references.

Constructor & Destructor Documentation

template<typename T>
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.

Parameters:
storage reference to object responsible for reference-counting.

template<typename T>
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.

Parameters:
rawPtr raw pointer to data.

template<typename T>
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.

Parameters:
ptr reference to smart pointer which should be copied.

template<typename T>
Common::GaSmartPtr< T >::GaSmartPtr (  )  [inline]

Default constructor, initializes pointer as NULL pointer. Creation of smart pointer and counting of references is thread-safe.

template<typename T>
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.


Member Function Documentation

template<typename T>
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.

Returns:
Operator returns pointer to user data.

template<typename T>
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.

Returns:
Operator returns reference to user data.

template<typename T>
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.

Parameters:
rhs smart pointer which holds address to which this pointer should point.
Returns:
Operator returns reference to this object.

template<typename T>
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.

Parameters:
rhs reference to smart location to which this pointer should point.
Returns:
Operator returns reference to this object.

template<typename T>
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.

Parameters:
rhs raw pointer to user data which should be managed.
Returns:
Operator returns reference to this object.

template<typename T>
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.

Parameters:
rhs the second smart pointer in the expression.
Returns:
Operator returns true if two pointers point to same location.

template<typename T>
T* GACALL Common::GaSmartPtr< T >::GetRawPtr (  )  const [inline]

This method is thread-safe.

Returns:
Method returns raw pointer to user data.

template<typename T>
bool GACALL Common::GaSmartPtr< T >::IsNULL (  )  const [inline]

Checks pointer against NULL value. This method is thread-safe.

Returns:
Returns true if this is NULL pointer.

template<typename T>
void Common::GaSmartPtr< T >::Lock (  )  const [inline, private]

This method prevents concurrent changes of smart pointer by locking it.

template<typename T>
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).

Parameters:
second the second pointer which is used in expression.

template<typename T>
void Common::GaSmartPtr< T >::Unlock (  )  const [inline, private]

This method unlocks changes of smart pointer.

template<typename T>
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).

Parameters:
second the second pointer whic is used in expression.


Member Data Documentation

template<typename T>
volatile unsigned long Common::GaSmartPtr< T >::_lock [mutable, private]

Guards smart pointer against concurrent changes.

template<typename T>
T* Common::GaSmartPtr< T >::_data [private]

Holds address of user data.

template<typename T>
GaSmartStorage<T>* Common::GaSmartPtr< T >::_location [private]

Pointer to object which holds reference-count and address of data.

template<typename T>
const GaSmartPtr< T > Common::GaSmartPtr< T >::NullPtr [inline, static]

NullPtr is global constant NULL pointer for T type.


The documentation for this class was generated from the following file:

Genetic Algorithm Library
Coolsoft Software Development