UCommon
access.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
2 //
3 // This file is part of GNU uCommon C++.
4 //
5 // GNU uCommon C++ is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published
7 // by the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // GNU uCommon C++ is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
17 
31 // we do this twice because of some bizarre issue in just this file that
32 // otherwise breaks doxygen and lists all items outside the namespace...
33 #include <ucommon/platform.h>
34 
35 #ifndef _UCOMMON_ACCESS_H_
36 #define _UCOMMON_ACCESS_H_
37 
38 #ifndef _UCOMMON_CPR_H_
39 #include <ucommon/cpr.h>
40 #endif
41 
42 NAMESPACE_UCOMMON
43 
50 class __EXPORT ExclusiveProtocol
51 {
52 protected:
53  virtual ~ExclusiveProtocol();
54 
55 public:
59  virtual void Exlock(void) = 0;
60 
64  virtual void Unlock(void) = 0;
65 
69  inline void Lock(void)
70  {Exlock();};
71 };
72 
79 class __EXPORT SharedProtocol
80 {
81 protected:
82  virtual ~SharedProtocol();
83 
84 public:
88  virtual void Shlock(void) = 0;
89 
93  virtual void Unlock(void) = 0;
94 
101  virtual void Share(void);
102 
110  virtual void Exclusive(void);
111 
115  inline void Lock(void)
116  {Shlock();};
117 };
118 
126 class __EXPORT exclusive_lock
127 {
128 private:
130 
131 public:
137 
141  ~exclusive_lock();
142 
147  inline bool operator!() const
148  {return lock == NULL;};
149 
154  inline operator bool() const
155  {return lock != NULL;};
156 
162  void release(void);
163 };
164 
172 class __EXPORT shared_lock
173 {
174 private:
176  int state;
177  bool modify;
178 
179 public:
184  shared_lock(SharedProtocol *object);
185 
189  ~shared_lock();
190 
195  inline bool operator!() const
196  {return lock == NULL;};
197 
202  inline operator bool() const
203  {return lock != NULL;};
204 
210  void release(void);
211 
215  void exclusive(void);
216 
220  void share(void);
221 };
222 
227 inline void lock(ExclusiveProtocol *object)
228  {object->Exlock();}
229 
234 inline void unlock(ExclusiveProtocol *object)
235  {object->Unlock();}
236 
241 inline void access(SharedProtocol *object)
242  {object->Shlock();}
243 
248 inline void release(SharedProtocol *object)
249  {object->Unlock();}
250 
255 inline void exclusive(SharedProtocol *object)
256  {object->Exclusive();}
257 
262 inline void share(SharedProtocol *object)
263  {object->Share();}
264 
269 
274 
279 inline void release(exlock_t &reference)
280  {reference.release();}
281 
286 inline void release(shlock_t &reference)
287  {reference.release();}
288 
289 // Special macros to allow member functions of an object with a protocol
290 // to create self locking states while the member functions are called by
291 // placing an exclusive_lock or shared_lock smart object on their stack
292 // frame to reference their self.
293 
294 #define exclusive_object() exlock_t __autolock__ = this
295 #define protected_object() shlock_t __autolock__ = this
296 #define exclusive_access(x) exlock_t __autolock__ = &x
297 #define protected_access(x) shlock_t __autolock__ = &x
298 
299 END_NAMESPACE
300 
301 #endif