UCommon
access.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
2 // Copyright (C) 2015 Cherokees of Idaho.
3 //
4 // This file is part of GNU uCommon C++.
5 //
6 // GNU uCommon C++ is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Lesser General Public License as published
8 // by the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // GNU uCommon C++ is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
18 
32 // we do this twice because of some bizarre issue in just this file that
33 // otherwise breaks doxygen and lists all items outside the namespace...
34 #include <ucommon/platform.h>
35 
36 #ifndef _UCOMMON_ACCESS_H_
37 #define _UCOMMON_ACCESS_H_
38 
39 #ifndef _UCOMMON_CPR_H_
40 #include <ucommon/cpr.h>
41 #endif
42 
43 #ifndef _UCOMMON_PROTOCOLS_H_
44 #include <ucommon/protocols.h>
45 #endif
46 
47 namespace ucommon {
48 
55 class __EXPORT ExclusiveProtocol
56 {
57 protected:
58  virtual ~ExclusiveProtocol();
59 
60  virtual void _lock(void) = 0;
61 
62  virtual void _unlock(void) = 0;
63 
64 public:
72  class __EXPORT Locking
73  {
74  private:
75  ExclusiveProtocol *lock;
76 
77  __DELETE_COPY(Locking);
78 
79  public:
84  Locking(ExclusiveProtocol *object);
85 
89  ~Locking();
90 
95  inline bool operator!() const {
96  return lock == NULL;
97  }
98 
103  inline operator bool() const {
104  return lock != NULL;
105  }
106 
112  void release(void);
113  };
114 };
115 
122 class __EXPORT SharedProtocol
123 {
124 protected:
125  virtual ~SharedProtocol();
126 
130  virtual void _share(void) = 0;
131 
132  virtual void _unshare(void) = 0;
133 
134 public:
142  class __EXPORT Locking
143  {
144  private:
145  SharedProtocol *lock;
146  int state;
147  bool modify;
148 
149  public:
154  Locking(SharedProtocol *object);
155 
156  Locking(const Locking& copy);
157 
158  Locking& operator=(const Locking& copy);
159 
163  ~Locking();
164 
169  inline bool operator!() const {
170  return lock == NULL;
171  }
172 
177  inline operator bool() const {
178  return lock != NULL;
179  }
180 
186  void release(void);
187 
191  void exclusive(void);
192 
196  void share(void);
197  };
198 
205  virtual void share(void);
206 
214  virtual void exclusive(void);
215 };
216 
224 class __EXPORT shared_access
225 {
226 private:
227  SharedProtocol *lock;
228  int state;
229  bool modify;
230 
231 public:
236  shared_access(SharedProtocol *object);
237 
239 
240  shared_access& operator=(const shared_access& copy);
241 
245  ~shared_access();
246 
251  inline bool operator!() const {
252  return lock == NULL;
253  }
254 
259  inline operator bool() const {
260  return lock != NULL;
261  }
262 
268  void release(void);
269 
273  void exclusive(void);
274 
278  void share(void);
279 };
280 
281 template<class T>
282 class autoexclusive : private ExclusiveProtocol::Locking
283 {
284 private:
285  __DELETE_DEFAULTS(autoexclusive);
286 
287 public:
288  inline autoexclusive(T *lock) :
289  Locking(polystatic_cast<ExclusiveProtocol *>(lock)) {};
290 };
291 
292 template<class T>
293 class autoshared : private SharedProtocol::Locking
294 {
295 private:
296  __DELETE_DEFAULTS(autoshared);
297 
298 public:
299  inline autoshared(T *lock) :
300  Locking(polystatic_cast<SharedProtocol *>(lock)) {};
301 };
302 
303 // Special macros to allow member functions of an object with a protocol
304 // to create self locking states while the member functions are called by
305 // placing an exclusive_lock or shared_lock smart object on their stack
306 // frame to reference their self.
307 
308 #define __EXCLUSIVE(x) exclusive_access __autolock__ = x
309 #define __SHARE(x) shared_access __autolock__ = x
310 
311 } // namespace ucommon
312 
313 #endif
A kind of smart pointer object to support shared locking protocol.
Definition: access.h:142
Locking(SharedProtocol *object)
Create an instance of an exclusive object reference.
An exclusive locking protocol interface base.
Definition: access.h:55
bool operator!() const
Test if the reference holds an active lock.
Definition: access.h:95
bool operator!() const
Test if the reference holds an active lock.
Definition: access.h:169
bool operator!() const
Test if the reference holds an active lock.
Definition: access.h:251
Various miscellaneous platform specific headers and defines.
Common namespace for all ucommon objects.
Definition: access.h:47
An exclusive locking access interface base.
Definition: access.h:122
T copy(const T &src)
Convenience function to copy objects.
Definition: generics.h:395
Abstract interfaces and support.
A kind of smart pointer object to support shared locking protocol.
Definition: access.h:224
Runtime functions.
A kind of smart pointer object to support exclusive locking protocol.
Definition: access.h:72