EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Lock.h
Go to the documentation of this file.
1 /*
2  ------------------------------------------------------------------------------------
3  LICENSE:
4  ------------------------------------------------------------------------------------
5  This file is part of EVEmu: EVE Online Server Emulator
6  Copyright 2006 - 2008 The EVEmu Team
7  For the latest information visit http://evemu.mmoforge.org
8  ------------------------------------------------------------------------------------
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU Lesser General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  version.
13 
14  This program is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License along with
19  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20  Place - Suite 330, Boston, MA 02111-1307, USA, or go to
21  http://www.gnu.org/copyleft/lesser.txt.
22  ------------------------------------------------------------------------------------
23  Author: Bloody.Rabbit
24 */
25 
26 #ifndef __UTILS__LOCK_H__INCL__
27 #define __UTILS__LOCK_H__INCL__
28 
34 class Lockable
35 {
36 public:
42  virtual void Lock() = 0;
51  virtual bool TryLock() = 0;
52 
56  virtual void Unlock() = 0;
57 };
58 
69 template< typename T >
70 class Lock
71 {
72 public:
79  Lock( T& object, bool lock = true )
80  : mObject( object ),
81  mLocked( false )
82  {
83  if( lock )
84  Relock();
85  }
90  {
91  Unlock();
92  }
93 
100  bool isLocked() const { return mLocked; }
101 
105  void Relock()
106  {
107  if( !isLocked() )
108  mObject.Lock();
109 
110  mLocked = true;
111  }
115  void Unlock()
116  {
117  if( isLocked() )
118  mObject.Unlock();
119 
120  mLocked = false;
121  }
122 
123 protected:
127  bool mLocked;
128 };
129 
130 #endif /* !__UTILS__LOCK_H__INCL__ */
void Relock()
Locks the object.
Definition: Lock.h:105
Basic interface of a lockable object.
Definition: Lock.h:34
A lock for a Lockable object.
Definition: Lock.h:70
virtual void Unlock()=0
Unlocks a locked object.
Lock(T &object, bool lock=true)
Primary contructor, locks the object.
Definition: Lock.h:79
~Lock()
Destructor, unlocks the object.
Definition: Lock.h:89
virtual bool TryLock()=0
Attempts to lock the object.
T & mObject
The object this lock is bound to.
Definition: Lock.h:125
void Unlock()
Unlocks the object.
Definition: Lock.h:115
bool mLocked
True the mObject is locked, false if not.
Definition: Lock.h:127
bool isLocked() const
Obtains the lock state of the object.
Definition: Lock.h:100
virtual void Lock()=0
Locks the object.