LockObject

Files CCore/inc/gadget/LockObject.h CCore/src/gadget/LockObject.cpp

LockObject is a simple class, implementing the scope lock pattern:


template <class T>
class LockObject : NoCopy
 {
   T &obj;
   
  public:
  
   explicit LockObject(T &obj_) : obj(obj_) { obj_.lock(); }
   
   ~LockObject() { obj.unlock(); }
 };

If you have an object of the type C with methods lock() and unlock(), you can use LockObject to "lock" this object for a scope duration:


class C
 {
  public:

   void lock();

   void unlock();
 };

....

C obj;

....

{
 LockObject<C> lock(obj); // obj.lock() is called
                          // obj.unlock() will be called whenever scope is exited

 ...
}