Jpp 20.0.0-rc.8
the software that should make you happy
Loading...
Searching...
No Matches
JStatus.hh
Go to the documentation of this file.
1#ifndef __JLANG__JSTATUS__
2#define __JLANG__JSTATUS__
3
4#include <istream>
5#include <ostream>
6#include <iomanip>
7#include <string>
8#include <map>
9
10#include "JLang/JEquals.hh"
11#include "JLang/JType.hh"
12
13
14/**
15 * \author mdejong
16 */
17
18namespace JLANG {}
19namespace JPP { using namespace JLANG; }
20
21namespace JLANG {
22
23 /**
24 * Auxiliary class for handling status.
25 *
26 * The various status are controlled using a bitwise data field.\n
27 * The corresponding bits can externally be defined (e.g.\ via an enumeration).
28 */
29 struct JStatus :
30 public JEquals<JStatus>
31 {
32 typedef int status_type;
33
34 static const int NUMBER_OF_STATUS_BITS = sizeof(status_type) * 8; //!< number of status bits
35
36
37 /**
38 * Default constructor.
39 */
41 status(0)
42 {}
43
44
45 /**
46 * Constructor.
47 *
48 * \param status status
49 */
53
54
55 /**
56 * Get status.
57 *
58 * \return status
59 */
61 {
62 return this->status;
63 }
64
65
66 /**
67 * Get status.
68 *
69 * \return status
70 */
71 JStatus getStatus(const JType<JStatus>& type) const
72 {
73 return *this;
74 }
75
76
77 /**
78 * Get status.
79 *
80 * \return status
81 */
82 template<class T>
83 const T getStatus() const
84 {
85 return getStatus(JType<T>());
86 }
87
88
89 /**
90 * Get status.
91 *
92 * \return status
93 */
94 const JStatus& getStatus() const
95 {
96 return static_cast<const JStatus&>(*this);
97 }
98
99
100 /**
101 * Get status.
102 *
103 * \return status
104 */
106 {
107 return this->status;
108 }
109
110
111 /**
112 * Get status.
113 *
114 * \return status
115 */
117 {
118 return *this;
119 }
120
121
122 /**
123 * Get status.
124 *
125 * \return status
126 */
127 template<class T>
129 {
130 return getStatus(JType<T>());
131 }
132
133
134 /**
135 * Get status.
136 *
137 * \return status
138 */
140 {
141 return static_cast<JStatus&>(*this);
142 }
143
144
145 /**
146 * Set status.
147 *
148 * \param status status
149 */
151 {
152 this->status = status.status;
153 }
154
155
156 /**
157 * Equal method.
158 *
159 * \param status status
160 * \result true if this status equal to given status; else false
161 */
162 inline bool equals(const JStatus& status) const
163 {
164 return this->status == status.status;
165 }
166
167
168 /**
169 * Get combined status.
170 *
171 * \param status status
172 * \return status
173 */
175 {
176 return JStatus(this->status & status.status);
177 }
178
179
180 /**
181 * Get combined status.
182 *
183 * \param status status
184 * \return status
185 */
187 {
188 return JStatus(this->status | status.status);
189 }
190
191
192 /**
193 * Test PMT status.
194 *
195 * \param bit bit
196 * \return true if bit is set; else false
197 */
198 bool has(const int bit) const
199 {
200 return (this->status & (1<<bit)) != 0;
201 }
202
203
204 /**
205 * Set PMT status.
206 *
207 * \param bit bit
208 */
209 void set(const int bit)
210 {
211 this->status |= (1<<bit);
212 }
213
214
215 /**
216 * Reset PMT status.
217 *
218 * \param bit bit
219 */
220 void reset(const int bit)
221 {
222 this->status &= ~(1<<bit);
223 }
224
225
226 /**
227 * Read status from input.
228 *
229 * \param in input stream
230 * \param status status
231 * \return input stream
232 */
233 friend inline std::istream& operator>>(std::istream& in, JStatus& status)
234 {
235 return in >> status.status;
236 }
237
238
239 /**
240 * Write status to output.
241 *
242 * \param out output stream
243 * \param status status
244 * \return output stream
245 */
246 friend inline std::ostream& operator<<(std::ostream& out, const JStatus& status)
247 {
248 return out << status.status;
249 }
250
251 protected:
253 };
254
255
256 /**
257 * Auxiliary class to map key to status bit.
258 */
260 public std::map<std::string, int>
261 {
262 /**
263 * Get status bit.
264 *
265 * \param key key
266 * \return bit
267 */
268 int operator()(const std::string& key) const
269 {
270 return this->at(key);
271 }
272 };
273
274
275 /**
276 * Auxiliary class to map status bit to key.
277 */
279 public std::map<int, std::string>
280 {
281 /**
282 * Constructor.
283 *
284 * \param input status bits
285 */
287 {
288 using namespace std;
289
290 for (JGetStatusBit::const_iterator i = input.begin(); i != input.end(); ++i) {
291 this->insert(make_pair(i->second, i->first));
292 }
293 }
294
295
296 /**
297 * Put status bit.
298 *
299 * \param bit bit
300 * \return key
301 */
302 const std::string& operator()(const int bit) const
303 {
304 return this->at(bit);
305 }
306 };
307
308
309 /**
310 * Print status.
311 *
312 * \param out output stream
313 * \param status status
314 * \param helper helper
315 */
316 inline void print(std::ostream& out, const JStatus& status, const JPutStatusBit& helper)
317 {
318 using namespace std;
319
320 for (const auto& i : helper) {
321 out << setw(24) << left << i.second << right << status.has(i.first) << endl;
322 }
323 }
324
325
326 /**
327 * Print status.
328 *
329 * \param out output stream
330 * \param status status
331 * \param helper helper
332 */
333 inline void print(std::ostream& out, const JStatus& status, const JGetStatusBit& helper)
334 {
335 using namespace std;
336
337 for (const auto& i : helper) {
338 out << setw(24) << left << i.first << right << status.has(i.second) << endl;
339 }
340 }
341}
342
343#endif
Auxiliary classes and methods for language specific functionality.
void print(std::ostream &out, const JStatus &status, const JPutStatusBit &helper)
Print status.
Definition JStatus.hh:316
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Template definition of auxiliary base class for comparison of data structures.
Definition JEquals.hh:84
Auxiliary class to map key to status bit.
Definition JStatus.hh:261
int operator()(const std::string &key) const
Get status bit.
Definition JStatus.hh:268
Auxiliary class to map status bit to key.
Definition JStatus.hh:280
JPutStatusBit(const JGetStatusBit &input)
Constructor.
Definition JStatus.hh:286
const std::string & operator()(const int bit) const
Put status bit.
Definition JStatus.hh:302
Auxiliary class for handling status.
Definition JStatus.hh:31
JStatus getAND(const JStatus &status) const
Get combined status.
Definition JStatus.hh:174
status_type getStatus(const JType< status_type > &type) const
Get status.
Definition JStatus.hh:60
const JStatus & getStatus() const
Get status.
Definition JStatus.hh:94
friend std::istream & operator>>(std::istream &in, JStatus &status)
Read status from input.
Definition JStatus.hh:233
T & getStatus()
Get status.
Definition JStatus.hh:128
JStatus & getStatus()
Get status.
Definition JStatus.hh:139
bool has(const int bit) const
Test PMT status.
Definition JStatus.hh:198
void reset(const int bit)
Reset PMT status.
Definition JStatus.hh:220
status_type status
Definition JStatus.hh:252
status_type & getStatus(const JType< status_type > &type)
Get status.
Definition JStatus.hh:105
JStatus & getStatus(const JType< JStatus > &type)
Get status.
Definition JStatus.hh:116
JStatus getOR(const JStatus &status) const
Get combined status.
Definition JStatus.hh:186
JStatus getStatus(const JType< JStatus > &type) const
Get status.
Definition JStatus.hh:71
friend std::ostream & operator<<(std::ostream &out, const JStatus &status)
Write status to output.
Definition JStatus.hh:246
bool equals(const JStatus &status) const
Equal method.
Definition JStatus.hh:162
JStatus()
Default constructor.
Definition JStatus.hh:40
const T getStatus() const
Get status.
Definition JStatus.hh:83
static const int NUMBER_OF_STATUS_BITS
number of status bits
Definition JStatus.hh:34
JStatus(const status_type status)
Constructor.
Definition JStatus.hh:50
void set(const int bit)
Set PMT status.
Definition JStatus.hh:209
void setStatus(const JStatus &status)
Set status.
Definition JStatus.hh:150
Auxiliary class for a type holder.
Definition JType.hh:19