/work/dcmtk-install/include/dcmtk/ofstd/ofglobal.h
Line | Count | Source |
1 | | /* |
2 | | * |
3 | | * Copyright (C) 1997-2024, OFFIS e.V. |
4 | | * All rights reserved. See COPYRIGHT file for details. |
5 | | * |
6 | | * This software and supporting documentation were developed by |
7 | | * |
8 | | * OFFIS e.V. |
9 | | * R&D Division Health |
10 | | * Escherweg 2 |
11 | | * D-26121 Oldenburg, Germany |
12 | | * |
13 | | * |
14 | | * Module: ofstd |
15 | | * |
16 | | * Author: Marco Eichelberg |
17 | | * |
18 | | * Purpose: OFGlobal is a template class intended for the declaration |
19 | | * of global objects, access to which is protected by a |
20 | | * Read/Write Lock for multi-thread applications. |
21 | | * class T must have copy constructor and assignment operator. |
22 | | * |
23 | | */ |
24 | | |
25 | | #ifndef OFGLOBAL_H |
26 | | #define OFGLOBAL_H |
27 | | |
28 | | #include "dcmtk/config/osconfig.h" |
29 | | #include "dcmtk/ofstd/ofthread.h" /* for class OFBool */ |
30 | | |
31 | | #if defined(HAVE_STL_ATOMIC) && defined(WITH_THREADS) |
32 | | #include <atomic> |
33 | | #endif |
34 | | |
35 | | /** Template class which allows to declare global objects that are |
36 | | * protected by a Read/Write Lock if used in multi-thread applications. |
37 | | * Must be compiled with -DWITH_THREADS for multi-thread-operation. |
38 | | * Template class T must have copy constructor and assignment operator. |
39 | | */ |
40 | | template <class T> class OFGlobal |
41 | | { |
42 | | public: |
43 | | |
44 | | /** constructor. |
45 | | * @param arg value to which this object is initialised |
46 | | */ |
47 | | OFGlobal(const T &arg) |
48 | | : val(arg) |
49 | | #ifdef WITH_THREADS |
50 | | , theRWLock() |
51 | | #endif |
52 | | { |
53 | | } |
54 | | |
55 | | /** destructor. |
56 | | */ |
57 | | virtual ~OFGlobal() { } |
58 | | |
59 | | /** assigns new value to this object. If compiled for multi-thread operation, |
60 | | * access to the value of the object is protected by a Read/Write Lock. |
61 | | * @param arg new value |
62 | | */ |
63 | | void set(const T& arg) |
64 | | { |
65 | | #ifdef WITH_THREADS |
66 | | theRWLock.wrlock(); |
67 | | #endif |
68 | | val = arg; |
69 | | #ifdef WITH_THREADS |
70 | | theRWLock.wrunlock(); |
71 | | #endif |
72 | | } |
73 | | |
74 | | /** gets the value of this object. If compiled for multi-thread operation, |
75 | | * access to the value of the object is protected by a Read/Write Lock. |
76 | | * @param arg return value is assigned to this parameter. |
77 | | */ |
78 | | void xget(T& arg) |
79 | | { |
80 | | #ifdef WITH_THREADS |
81 | | theRWLock.rdlock(); |
82 | | #endif |
83 | | arg = val; |
84 | | #ifdef WITH_THREADS |
85 | | theRWLock.rdunlock(); |
86 | | #endif |
87 | | } |
88 | | |
89 | | /** returns the value of this object. If compiled for multi-thread operation, |
90 | | * access to the value of the object is protected by a Read/Write Lock. The result |
91 | | * is returned by value, not by reference. |
92 | | * @return value of this object. |
93 | | */ |
94 | | T get() |
95 | | { |
96 | | #ifdef WITH_THREADS |
97 | | theRWLock.rdlock(); |
98 | | #endif |
99 | | T result(val); |
100 | | #ifdef WITH_THREADS |
101 | | theRWLock.rdunlock(); |
102 | | #endif |
103 | | return result; |
104 | | } |
105 | | |
106 | | private: |
107 | | |
108 | | /** value of this object |
109 | | */ |
110 | | T val; |
111 | | |
112 | | #ifdef WITH_THREADS |
113 | | /** if compiled for multi-thread operation, the Read/Write Lock protecting |
114 | | * access to the value of this object. |
115 | | */ |
116 | | OFReadWriteLock theRWLock; |
117 | | #endif |
118 | | |
119 | | /** unimplemented private default constructor */ |
120 | | OFGlobal(); |
121 | | |
122 | | /** unimplemented private copy constructor */ |
123 | | OFGlobal(const OFGlobal<T>& arg); |
124 | | |
125 | | /** unimplemented private assignment operator */ |
126 | | const OFGlobal<T>& operator=(const OFGlobal<T>& arg); |
127 | | |
128 | | }; |
129 | | |
130 | | #if defined(HAVE_STL_ATOMIC) && defined(WITH_THREADS) |
131 | | /** template specialization for OFGlobal<OFBool> that uses a std::atomic flag |
132 | | * without read-write lock and is lock-free (and thus much more efficient) on many platforms. |
133 | | */ |
134 | | template <> class OFGlobal<OFBool> |
135 | | { |
136 | | public: |
137 | | |
138 | | /** constructor. |
139 | | * @param arg value to which this object is initialised |
140 | | */ |
141 | | OFGlobal(const OFBool &arg) |
142 | | : val(arg) |
143 | | { |
144 | | } |
145 | | |
146 | | /** destructor. |
147 | | */ |
148 | | virtual ~OFGlobal() { } |
149 | | |
150 | | /** assigns new value to this object. |
151 | | * @param arg new value |
152 | | */ |
153 | | void set(const OFBool& arg) |
154 | | { |
155 | | val = arg; |
156 | | } |
157 | | |
158 | | /** gets the value of this object. |
159 | | * @param arg return value is assigned to this parameter. |
160 | | */ |
161 | | void xget(bool& arg) |
162 | 0 | { |
163 | 0 | arg = val; |
164 | 0 | } |
165 | | |
166 | | |
167 | | /** returns the value of this object. The result |
168 | | * is returned by value, not by reference. |
169 | | * @return value of this object. |
170 | | */ |
171 | | OFBool get() |
172 | | { |
173 | | OFBool result(val); |
174 | | return result; |
175 | | } |
176 | | |
177 | | private: |
178 | | |
179 | | /** value of this object |
180 | | */ |
181 | | std::atomic<OFBool> val; |
182 | | |
183 | | /** unimplemented private default constructor */ |
184 | | OFGlobal(); |
185 | | |
186 | | /** unimplemented private copy constructor */ |
187 | | OFGlobal(const OFGlobal<OFBool>& arg); |
188 | | |
189 | | /** unimplemented private assignment operator */ |
190 | | const OFGlobal<OFBool>& operator=(const OFGlobal<OFBool>& arg); |
191 | | |
192 | | }; |
193 | | #endif |
194 | | |
195 | | #endif |