Home C-Bits Package Documentation Project Page

Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members  

cbits::sync_ptr Class Template Reference

"Smart", synchronizing pointer. More...

#include <sync_ptr.h>

Collaboration diagram for cbits::sync_ptr:

[legend]
List of all members.

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...


Detailed Description

template<typename Lockable>
class cbits::sync_ptr< Lockable >

"Smart", synchronizing pointer.

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 & Destructor Documentation

template<typename Lockable>
cbits::sync_ptr< Lockable >::sync_ptr Lockable *    t,
const bool    autofree = false
[inline]
 

Constructor.

This locks the object. If the autofree is specified as true, then the underlying Lockable object will be deleted when this pointer destructs.

Parameters:
t  the object to synchronize
autofree  own the pointer

template<typename Lockable>
cbits::sync_ptr< Lockable >::sync_ptr const sync_ptr< Lockable > &    r [inline]
 

Copy constructor.

This transfers ownership of the Lockable object to the new instance.

template<typename Lockable>
cbits::sync_ptr< Lockable >::~sync_ptr   [inline]
 

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.


Member Function Documentation

template<typename Lockable>
const bool cbits::sync_ptr< Lockable >::isOwner   const [inline]
 

Test if this pointer owns the object.

template<typename Lockable>
Lockable& cbits::sync_ptr< Lockable >::operator *   [inline]
 

Access member of T.

template<typename Lockable>
Lockable* cbits::sync_ptr< Lockable >::operator->   [inline]
 

Access member of T.

template<typename Lockable>
sync_ptr<Lockable>& cbits::sync_ptr< Lockable >::operator= const sync_ptr< Lockable > &    r [inline]
 

Assignment.

This transfers ownership of the Lockable object to this instance.

template<typename Lockable>
bool cbits::sync_ptr< Lockable >::operator== const sync_ptr< Lockable > &    r [inline]
 

Test for equality.

template<typename Lockable>
void cbits::sync_ptr< Lockable >::release   const [inline, private]
 

Give up ownership of the object.

This doesn't change the locked status of the object.


Member Data Documentation

template<typename Lockable>
bool cbits::sync_ptr::_autofree [private]
 

if true, obj will be deleted.

template<typename Lockable>
bool cbits::sync_ptr::_owned [private]
 

true if locked.

template<typename Lockable>
Lockable* cbits::sync_ptr::_t [private]
 

The underlying object.


The documentation for this class was generated from the following file:
Generated by
doxygen
Hosted by
SourceForge