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 #ifndef _UCOMMON_PROTOCOLS_H_
43 #include <ucommon/protocols.h>
44 #endif
45 
46 NAMESPACE_UCOMMON
47 
53 class __EXPORT UnlockAccess
54 {
55 public:
56  virtual ~UnlockAccess();
57 
58 protected:
59  virtual void _unlock(void) = 0;
60 };
61 
68 class __EXPORT ExclusiveAccess : public UnlockAccess
69 {
70 protected:
71  virtual ~ExclusiveAccess();
72 
73  virtual void _lock(void) = 0;
74 
75 public:
79  inline void exclusive_lock(void)
80  {return _lock();}
81 
85  inline void release_exclusive(void)
86  {return _unlock();}
87 };
88 
95 class __EXPORT SharedAccess : protected UnlockAccess
96 {
97 protected:
98  virtual ~SharedAccess();
99 
100 protected:
104  virtual void _share(void) = 0;
105 
106 public:
113  virtual void share(void);
114 
122  virtual void exclusive(void);
123 
124  inline void shared_lock(void)
125  {return _share();}
126 
127  inline void release_share(void)
128  {return _unlock();}
129 };
130 
138 class __EXPORT exclusive_access
139 {
140 private:
142 
143 public:
149 
153  ~exclusive_access();
154 
159  inline bool operator!() const
160  {return lock == NULL;};
161 
166  inline operator bool() const
167  {return lock != NULL;};
168 
174  void release(void);
175 };
176 
184 class __EXPORT shared_access
185 {
186 private:
188  int state;
189  bool modify;
190 
191 public:
196  shared_access(SharedAccess *object);
197 
201  ~shared_access();
202 
207  inline bool operator!() const
208  {return lock == NULL;};
209 
214  inline operator bool() const
215  {return lock != NULL;};
216 
222  void release(void);
223 
227  void exclusive(void);
228 
232  void share(void);
233 };
234 
239 inline void lock(ExclusiveAccess& object)
240  {object.exclusive_lock();}
241 
246 inline void unlock(ExclusiveAccess& object)
247  {object.release_exclusive();}
248 
253 inline void access(SharedAccess& object)
254  {object.shared_lock();}
255 
260 inline void release(SharedAccess& object)
261  {object.release_share();}
262 
267 inline void exclusive(SharedAccess& object)
268  {object.exclusive();}
269 
274 inline void share(SharedAccess& object)
275  {object.share();}
276 
281 
286 
291 inline void release(exlock_t &reference)
292  {reference.release();}
293 
298 inline void release(shlock_t &reference)
299  {reference.release();}
300 
301 // Special macros to allow member functions of an object with a protocol
302 // to create self locking states while the member functions are called by
303 // placing an exclusive_lock or shared_lock smart object on their stack
304 // frame to reference their self.
305 
306 #define exclusive_object() exlock_t __autolock__ = this
307 #define protected_object() shlock_t __autolock__ = this
308 #define exclusive_locking(x) exlock_t __autolock__ = &x
309 #define protected_locking(x) shlock_t __autolock__ = &x
310 
311 END_NAMESPACE
312 
313 #endif