* Copyright 2003-2004, Waldemar Kornewald <wkornew@gmx.net>
* Distributed under the terms of the MIT License.
*/
\brief Helper class for automatically releasing a lock.
The LockerHelper acquires a lock on construction and releases it on destruction.
This is a very useful class because you do not have to worry about
releasing the lock at every possible point. It is done automatically.
*/
#ifndef _LOCKER_HELPER__H
#define _LOCKER_HELPER__H
#include <Locker.h>
class LockerHelper {
private:
LockerHelper(const LockerHelper& copy);
LockerHelper& operator= (const LockerHelper& copy);
public:
LockerHelper(BLocker& lock) : fLock(&lock)
{
if(!fLock->Lock())
fLock = NULL;
}
If you called \c UnlockNow() the BLocker will not be unlocked.
*/
~LockerHelper()
{
if(fLock)
fLock->Unlock();
}
The destructor will not unlock the BLocker anymore and any subsequent
calls to \c UnlockNow() will do \e nothing.
*/
void UnlockNow()
{
if(fLock)
fLock->Unlock();
fLock = NULL;
}
private:
BLocker *fLock;
};
#endif