Public Member Functions

Celartem::Locker Class Reference

#include <cel_lockable.h>

List of all members.

Public Member Functions

 Locker ()
 Locker (const Lockable &inLockable)
 Locker (const Locker &inLocker)
 ~Locker ()
Lockeroperator= (const Lockable &inLockable)
Lockeroperator= (const Locker &inLocker)
void lock (const Lockable &inLockable)
void unlock ()
void swap (Locker &inLocker)

Detailed Description

This class is to lock Lockable instances.
This class is introduced to make the code exception-safe. Since the scheme of calling lock method in the first of a function and unlock on return is usually not easy to be exception-safe and the programmers may happen to introduce dirty try-catch hack to recover from it. The Locker object is to get rid of such dirty temporary hacks; all you have to do is to initialize the instance of Locker class directly in the local scope. (This class is intended to be initialized as auto variable or class member variable.) The following code shows how to use Locker class.

            void someOperation(Mutex& mutex)
            {
                // Locking the mutex; Locker is initialized as an auto object.
                Locker lock(mutex);

                // do something
                ....

                if(err)
                {
                    // At the time the exception is thrown, the instance of
                    // the Locker class is automatically deleted and the mutex
                    // is released.
                    celThrow(errOperationFailed);
                }
            }

The code which does same operation but in the old syntax:

            void someOperation(Mutex& mutex)
            {
                mutex->lock(); // lock the mutex

                try
                {
                    // do something; it may throw some exception...
                    ....
                }
                catch(...)
                {
                    mutex->unlock();
                    throw; // re-throw
                }

                // place unlock here to deal with both of error and non-error
                // cases.
                mutex->unlock();

                if(err)
                    celThrow(errOperationFailed);
            }
See also:
Lockable, Mutex, Semaphore

Constructor & Destructor Documentation

Celartem::Locker::Locker (  )  [inline]

Initializes the instance without any lockable instances.

Celartem::Locker::Locker ( const Lockable inLockable  )  [inline]

This constructor it to lock the specified lockable instance.

Parameters:
inLockable An instance to lock.
Celartem::Locker::Locker ( const Locker inLocker  )  [inline]

This constructor duplicates the lock; it is dengerous when you are deal with non-recursive Mutex.

Parameters:
inLocker An instance of Locker.
Celartem::Locker::~Locker (  )  [inline]

Unlocks the lockable.


Member Function Documentation

void Celartem::Locker::lock ( const Lockable inLockable  )  [inline]

This method locks the specified Lockable instance.

Parameters:
inLockable An instance to lock. You can also pass NULL to release the currently locked Lockable instance.
Locker& Celartem::Locker::operator= ( const Lockable inLockable  )  [inline]

This method sets the lockable instance to lock.

Parameters:
inLockable An instance to lock. You can also pass NULL to release the currently locked Lockable instance.
Returns:
This instance (*this ).
Locker& Celartem::Locker::operator= ( const Locker inLocker  )  [inline]

This method duplicates the lock; it is dengerous when you are deal with non-recursive Mutex.

Parameters:
inLocker An instance of Locker.
Returns:
This instance (*this ).
void Celartem::Locker::swap ( Locker inLocker  )  [inline]

This method swaps the locked instances of the two Locker instances.

Parameters:
inLocker The locker to exchange the locked instance with.
void Celartem::Locker::unlock (  )  [inline]

This method unlocks the currently locked instance.

Referenced by ~Locker().


The documentation for this class was generated from the following file:

This document is made with doxygen 1.7.1 at Sun Sep 5 2010 13:03:38.
Caminova Logo