cel_lockable.h

Go to the documentation of this file.
00001 //----------------------------------------------------------------------------
00004 //
00005 // (C) 2003-2006 Celartem Technology Inc. All rights reserved.
00006 //----------------------------------------------------------------------------
00007 
00008 #ifndef _cel_lockable_h_
00009 #define _cel_lockable_h_
00010 
00011 #include "cel_types.h"
00012 #include "cel_datatraits.h"
00013 
00014 namespace Celartem
00015 {
00025     class Lockable
00026     {
00027     public:
00032         virtual void lock() const = 0;
00033 
00038         virtual void unlock() const = 0;
00039 
00040     protected:
00041         virtual ~Lockable() {}
00042     };
00043 
00102     class Locker
00103     {
00104     public:
00108         Locker() : m_lockable(NULL)
00109         {
00110         }
00111 
00117         Locker(const Lockable& inLockable) : m_lockable(NULL)
00118         {
00119             lock(&inLockable);
00120         }
00121 
00128         Locker(const Locker& inLocker) : m_lockable(NULL)
00129         {
00130             lock(inLocker.m_lockable);
00131         }
00132 
00136         ~Locker()
00137         {
00138             unlock();
00139         }
00140 
00149         Locker& operator=(const Lockable& inLockable)
00150         {
00151             lock(&inLockable);
00152             return *this;
00153         }
00154 
00163         Locker& operator=(const Locker& inLocker)
00164         {
00165             lock(inLocker.m_lockable);
00166             return *this;
00167         }
00168         
00169         /*
00170             This method locks the specified Lockable instance.
00171             \param inLockable
00172                 An instance to lock. You can also pass NULL to release
00173                 the currently locked Lockable instance.
00174         */
00175         void lock(const Lockable *inLockable)
00176         {
00177             unlock();
00178             m_lockable = inLockable;
00179             if(m_lockable)
00180                 m_lockable->lock();
00181         }
00182         
00189         void lock(const Lockable& inLockable)
00190         {
00191             lock(&inLockable);
00192         }
00193 
00197         void unlock()
00198         {
00199             if(m_lockable)
00200             {
00201                 m_lockable->unlock();
00202                 m_lockable = NULL;
00203             }
00204         }
00205 
00212         void swap(Locker& inLocker)
00213         {
00214             const Lockable *temp = inLocker.m_lockable;
00215             inLocker.m_lockable = m_lockable;
00216             m_lockable = temp;
00217         }
00218         
00219         const Lockable* detach()
00220         {
00221             const Lockable* l = m_lockable;
00222             m_lockable = NULL;
00223             return l;
00224         }
00225 
00226     private:
00227         const Lockable *m_lockable;
00228     };
00229 
00233     CEL_DEFINE_DATATRAITS(Locker, byConstructor, notToBeStored);
00234 }
00235 
00236 #endif // _cel_lockable_h_

This document is automatically generated using doxygen 1.5.4 at Fri Jun 27 18:21:54 2008.