C12Adapter Opensource C++ Interface
MSharedPointer< C > Class Template Reference

Generic intrusive shared pointer. More...

Public Types

typedef C ClientType
 Static type of the client class or its parent as used during template definition.
 

Public Member Functions

 MSharedPointer ()
 Default constructor to initialize the pointer to NULL value.
 
 MSharedPointer (C *client)
 Constructor that takes a pointer to client as parameter. More...
 
 MSharedPointer (const MSharedPointer &other)
 Copy constructor that takes another shared pointer. More...
 
 ~MSharedPointer ()
 Shared pointer destructor. More...
 
int GetNumberOfReferences () const
 Get the current number of usage references in the object. More...
 
MSharedPointeroperator= (const MSharedPointer &other)
 Assignment operator that takes another shared pointer. More...
 
MSharedPointeroperator= (C *ptr)
 Assignment operator that takes a pointer. More...
 
 operator C * () const
 Return the pointer that is associated with this shared pointer class. More...
 
C & operator* ()
 Return the reference to client C object associated with this shared pointer class. More...
 
const C & operator* () const
 Return the constant reference to client C object associated with this shared pointer class. More...
 
C * operator-> ()
 Dereference the pointer that is associated with this shared pointer class. More...
 
const C * operator-> () const
 Dereference the constant pointer that is associated with this shared pointer class. More...
 
void DoAddRef ()
 Add the reference to the client object. More...
 
void DoReleaseRef ()
 Release the reference to the client object. More...
 

Friends

bool operator== (const MSharedPointer< C > &p1, const MSharedPointer< C > &p2)
 Equality comparison operator that takes two shared pointers.
 
bool operator== (const MSharedPointer< C > &p1, const C *p2)
 Equality comparison operator that takes a shared pointer and a pointer.
 
bool operator== (const C *p1, const MSharedPointer< C > &p2)
 Equality comparison operator that takes a pointer and a shared pointer.
 
bool operator!= (const MSharedPointer< C > &p1, const MSharedPointer< C > &p2)
 Inequality comparison operator that takes two shared pointers.
 
bool operator!= (const MSharedPointer< C > &p1, const C *p2)
 Inequality comparison operator that takes a shared pointer and a pointer.
 
bool operator!= (const C *p1, const MSharedPointer< C > &p2)
 Inequality comparison operator that takes a pointer and a shared pointer.
 

Detailed Description

template<class C>
class MSharedPointer< C >

Generic intrusive shared pointer.

Shared pointer should be used for those classes, where ownership is shared among objects that can have different and generally unpredictable life spans. An important restriction is that there should be no circular dependency in shared ownership of such objects, as in this case the objects will hold references to themselves forever. Also, shared pointer cannot hold an array of elements. These restrictions shall be watched at design and development time.

The reason why the intrusive shared pointer is implemented as a set of macros is to avoid multiple inheritance for types that have to be parts of the existing implementation hierarchy. While macros are somewhat uglier, they do what we want.

The class to which a shared pointer can point should include a macro M_SHARED_POINTER_CLASS, which will define a special typedef SharedPointer, and a set of private definitions to handle sharing. Here is an example:

// Definition:
class MSomeClass
{
public:
MSomeClass()
:
{
}
MSomeClass(const MStdString& name)
:
m_name(name)
{
}
....
};

Usage:

{
MSomeClass::SharedPointer ptr = new MSomeClass()
MSomeClass::SharedPointer otherPtr = ptr;
ptr = NULL;
otherPtr = NULL; // delete will be called on MSomeClass object here.
}

Constructor & Destructor Documentation

template<class C >
MSharedPointer< C >::MSharedPointer ( C *  client)
inline

Constructor that takes a pointer to client as parameter.

The shared pointer will be initialized and the usage count will be increased in client.

Parameters
clientClient object defined with M_SHARED_POINTER_CLASS macro
template<class C >
MSharedPointer< C >::MSharedPointer ( const MSharedPointer< C > &  other)
inline

Copy constructor that takes another shared pointer.

Parameters
otherOther object of the same type or its child
Precondition
The shared pointer given is a valid shared object. No explicit checks are done, but in many cases there will be a compile error otherwise.
template<class C >
MSharedPointer< C >::~MSharedPointer ( )
inline

Shared pointer destructor.

If the client object is not NULL, decrease the usage count, and when the usage count reaches zero, delete the client object.

Member Function Documentation

template<class C >
void MSharedPointer< C >::DoAddRef ( )
inline

Add the reference to the client object.

If the client is NULL, nothing is done by this call. This service is public, while direct manipulation of reference count should be very rare.

template<class C >
void MSharedPointer< C >::DoReleaseRef ( )
inline

Release the reference to the client object.

If the client is NULL, nothing is done by this call. If the count reaches zero, delete client object and nullify pointer. This service is public, while direct manipulation of reference count should be very rare.

template<class C >
int MSharedPointer< C >::GetNumberOfReferences ( ) const
inline

Get the current number of usage references in the object.

If the shared pointer is NULL, return zero by convention. If the shared pointer is not NULL, zero count is impossible due to implementation.

template<class C >
MSharedPointer< C >::operator C * ( ) const
inline

Return the pointer that is associated with this shared pointer class.

This makes possible the pointer-like behavior of this class.

template<class C >
C& MSharedPointer< C >::operator* ( )
inline

Return the reference to client C object associated with this shared pointer class.

This makes possible the pointer-like behavior of this class.

Precondition
On debug build, the assertion is done whether the pointer is not NULL.
template<class C >
const C& MSharedPointer< C >::operator* ( ) const
inline

Return the constant reference to client C object associated with this shared pointer class.

This makes possible the pointer-like behavior of this class.

Precondition
On debug build, the assertion is done whether the pointer is not NULL.
template<class C >
C* MSharedPointer< C >::operator-> ( )
inline

Dereference the pointer that is associated with this shared pointer class.

This makes possible the pointer-like behavior of this class.

Precondition
The C object has to be associated with this object. Otherwise there is an assertion hit in the debugging version.
template<class C >
const C* MSharedPointer< C >::operator-> ( ) const
inline

Dereference the constant pointer that is associated with this shared pointer class.

This makes possible the pointer-like behavior of this class.

Precondition
The C object has to be associated with this object. Otherwise there is an assertion hit in the debugging version.
template<class C >
MSharedPointer& MSharedPointer< C >::operator= ( const MSharedPointer< C > &  other)
inline

Assignment operator that takes another shared pointer.

Parameters
otherOther object of the same type or its child
Precondition
The shared pointer given is a valid shared object, not a NULL. No explicit checks are done, but in many cases there will be a compile error otherwise.
template<class C >
MSharedPointer& MSharedPointer< C >::operator= ( C *  ptr)
inline

Assignment operator that takes a pointer.

Precondition
The shared pointer given is a valid shared object, not a NULL. No explicit checks are done, but in many cases there will be a compile error otherwise.