C12Adapter Opensource C++ Interface
|
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... | |
MSharedPointer & | operator= (const MSharedPointer &other) |
Assignment operator that takes another shared pointer. More... | |
MSharedPointer & | operator= (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. | |
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:
Usage:
|
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.
client | Client object defined with M_SHARED_POINTER_CLASS macro |
|
inline |
Copy constructor that takes another shared pointer.
other | Other object of the same type or its child |
|
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.
|
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.
|
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.
|
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.
|
inline |
Return the pointer that is associated with this shared pointer class.
This makes possible the pointer-like behavior of this class.
|
inline |
Return the reference to client C object associated with this shared pointer class.
This makes possible the pointer-like behavior of this class.
NULL
.
|
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.
NULL
.
|
inline |
Dereference the pointer that is associated with this shared pointer class.
This makes possible the pointer-like behavior of this class.
|
inline |
Dereference the constant pointer that is associated with this shared pointer class.
This makes possible the pointer-like behavior of this class.
|
inline |
Assignment operator that takes another shared pointer.
other | Other object of the same type or its child |
NULL
. No explicit checks are done, but in many cases there will be a compile error otherwise.
|
inline |
Assignment operator that takes a pointer.
NULL
. No explicit checks are done, but in many cases there will be a compile error otherwise.