Home | C-Bits Package Documentation | Project Page |
#include <sync_ptr.h>
Collaboration diagram for cbits::sync_ptr:
Public Methods | |
sync_ptr (Lockable *t, const bool autofree=false) | |
Constructor. More... | |
sync_ptr (const sync_ptr< Lockable > &r) | |
Copy constructor. More... | |
sync_ptr< Lockable > & | operator= (const sync_ptr< Lockable > &r) |
Assignment. More... | |
bool | operator== (const sync_ptr< Lockable > &r) |
Test for equality. More... | |
Lockable * | operator-> () |
Access member of T. More... | |
Lockable & | operator * () |
Access member of T. More... | |
const bool | isOwner () const |
Test if this pointer owns the object. More... | |
~sync_ptr () | |
Destroy reference to T. More... | |
Private Methods | |
void | release () const |
Give up ownership of the object. More... | |
Private Attributes | |
Lockable * | _t |
The underlying object. More... | |
bool | _owned |
true if locked. More... | |
bool | _autofree |
if true, obj will be deleted. More... |
This pointer synchronizes access to the underlying object. During the lifetime of this pointer, the object wil remain locked. The object is unlocked when this pointer destroyed.
For instance:
class A { // A 'lockable' class public: void lock(); void unlock(); void someFunc(); // ... }; A* a1 = new A(...); { // subscope - sync_ptr<A> a_ptr(a1); // a1 becomes locked a_ptr->someFunc(); // use just like 'a1' // ... } // end of subscope // a1 is now unlocked.
sync_ptr can also manage the lifetime of the underlying object:
{ // start of some scope sync_ptr<A> a_ptr(new A(...),true); // a_ptr 'owns' the A object // the A instance is locked. a_ptr->someFunc(); // ... } // end of scope // the A instance is unlocked AND deleted.
|
Constructor. This locks the object. If the autofree is specified as true, then the underlying Lockable object will be deleted when this pointer destructs.
|
|
Copy constructor. This transfers ownership of the Lockable object to the new instance. |
|
Destroy reference to T. If the underlying object is still 'owned' by this pointer, then it is unlocked. If the pointer took ownership of the object with autofree set to true, then the object is deleted. |
|
Test if this pointer owns the object.
|
|
Access member of T.
|
|
Access member of T.
|
|
Assignment. This transfers ownership of the Lockable object to this instance. |
|
Test for equality.
|
|
Give up ownership of the object. This doesn't change the locked status of the object. |
|
if true, obj will be deleted.
|
|
true if locked.
|
|
The underlying object.
|
|
|