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
47namespace ucommon {
48
55class __EXPORT ExclusiveProtocol
56{
57protected:
58 virtual ~ExclusiveProtocol();
59
60 virtual void _lock(void) = 0;
61
62 virtual void _unlock(void) = 0;
63
64public:
72 class __EXPORT Locking
73 {
74 private:
76
77 __DELETE_COPY(Locking);
78
79 public:
85
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
122class __EXPORT SharedProtocol
123{
124protected:
125 virtual ~SharedProtocol();
126
130 virtual void _share(void) = 0;
131
132 virtual void _unshare(void) = 0;
133
134public:
142 class __EXPORT Locking
143 {
144 private:
145 SharedProtocol *lock;
146 int state;
147 bool modify;
148
149 public:
155
156 Locking(const Locking& copy);
157
158 Locking& operator=(const Locking& copy);
159
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
224class __EXPORT shared_access
225{
226private:
227 SharedProtocol *lock;
228 int state;
229 bool modify;
230
231public:
237
238 shared_access(const shared_access& copy);
239
240 shared_access& operator=(const shared_access& copy);
241
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
281template<class T>
282class autoexclusive : private ExclusiveProtocol::Locking
283{
284private:
285 __DELETE_DEFAULTS(autoexclusive);
286
287public:
288 inline autoexclusive(T *lock) :
289 Locking(polystatic_cast<ExclusiveProtocol *>(lock)) {};
290};
291
292template<class T>
293class autoshared : private SharedProtocol::Locking
294{
295private:
296 __DELETE_DEFAULTS(autoshared);
297
298public:
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
Runtime functions.
Various miscellaneous platform specific headers and defines.
Abstract interfaces and support.
Common namespace for all ucommon objects.
Definition access.h:47
An exclusive locking protocol interface base.
Definition access.h:56
A kind of smart pointer object to support exclusive locking protocol.
Definition access.h:73
~Locking()
Destroy reference to exclusively locked object, release lock.
void release(void)
Release a held lock programmatically.
bool operator!() const
Test if the reference holds an active lock.
Definition access.h:95
Locking(ExclusiveProtocol *object)
Create an instance of an exclusive object reference.
An exclusive locking access interface base.
Definition access.h:123
virtual void share(void)
Share the lock with other referencers.
virtual void _share(void)=0
Access interface to share lock the object.
virtual void exclusive(void)
Convert object to an exclusive lock.
A kind of smart pointer object to support shared locking protocol.
Definition access.h:143
Locking(SharedProtocol *object)
Create an instance of an exclusive object reference.
~Locking()
Destroy reference to shared locked object, release lock.
void share(void)
Restore shared access on referenced objects protocol.
void exclusive(void)
Call exclusive access on referenced objects protocol.
bool operator!() const
Test if the reference holds an active lock.
Definition access.h:169
void release(void)
Release a held lock programmatically.
A kind of smart pointer object to support shared locking protocol.
Definition access.h:225
void share(void)
Restore shared access on referenced objects protocol.
void exclusive(void)
Call exclusive access on referenced objects protocol.
bool operator!() const
Test if the reference holds an active lock.
Definition access.h:251
shared_access(SharedProtocol *object)
Create an instance of an exclusive object reference.
void release(void)
Release a held lock programmatically.
~shared_access()
Destroy reference to shared locked object, release lock.