Jpp test-rotations-new
the software that should make you happy
Loading...
Searching...
No Matches
JStatus.hh
Go to the documentation of this file.
1#ifndef __JEEP__JSTATUS__
2#define __JEEP__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#include "JLang/JVectorize.hh"
13
14#include "JIO/JSerialisable.hh"
15
16
17/**
18 * \author mdejong
19 */
20
21namespace JEEP {}
22namespace JPP { using namespace JEEP; }
23
24namespace JEEP {
25
26 using JLANG::JEquals;
27 using JIO::JReader;
28 using JIO::JWriter;
29
30
31 /**
32 * Auxiliary class for handling status.
33 *
34 * The various status are controlled using a bitwise data field.\n
35 * The corresponding bits can externally be defined (e.g.\ via an enumeration).
36 */
37 struct JStatus :
38 public JEquals<JStatus>
39 {
40 /**
41 * Default constructor.
42 */
44 status(0)
45 {}
46
47
48 /**
49 * Constructor.
50 *
51 * \param status status
52 */
53 JStatus(const int status) :
55 {}
56
57
58 /**
59 * Get status.
60 *
61 * \return status
62 */
63 int getStatus() const
64 {
65 return this->status;
66 }
67
68
69 /**
70 * Get status.
71 *
72 * \return status
73 */
75 {
76 return *this;
77 }
78
79
80 /**
81 * Get status.
82 *
83 * \param mask mask
84 * \return status
85 */
86 int getStatus(const int mask) const
87 {
88 return (this->status & mask);
89 }
90
91
92 /**
93 * Set status.
94 *
95 * \param status status
96 */
98 {
99 this->status = status.status;
100 }
101
102
103 /**
104 * Equal method.
105 *
106 * \param status status
107 * \result true if this status equal to given status; else false
108 */
109 inline bool equals(const JStatus& status) const
110 {
111 return this->status == status.status;
112 }
113
114
115 /**
116 * Test PMT status.
117 *
118 * \param bit bit
119 */
120 bool has(const int bit) const
121 {
122 return (this->status & (1<<bit)) != 0;
123 }
124
125
126 /**
127 * Set PMT status.
128 *
129 * \param bit bit
130 */
131 void set(const int bit)
132 {
133 this->status |= (1<<bit);
134 }
135
136
137 /**
138 * Reset PMT status.
139 *
140 * \param bit bit
141 */
142 void reset(const int bit)
143 {
144 this->status &= ~(1<<bit);
145 }
146
147
148 /**
149 * Read status from input.
150 *
151 * \param in input stream
152 * \param status status
153 * \return input stream
154 */
155 friend inline std::istream& operator>>(std::istream& in, JStatus& status)
156 {
157 return in >> status.status;
158 }
159
160
161 /**
162 * Write status to output.
163 *
164 * \param out output stream
165 * \param status status
166 * \return output stream
167 */
168 friend inline std::ostream& operator<<(std::ostream& out, const JStatus& status)
169 {
170 return out << status.status;
171 }
172
173
174 /**
175 * Read status from input.
176 *
177 * \param in reader
178 * \param status status
179 * \return reader
180 */
182 {
183 return in >> status.status;
184 }
185
186
187 /**
188 * Write status to output.
189 *
190 * \param out writer
191 * \param status status
192 * \return writer
193 */
194 friend inline JWriter& operator<<(JWriter& out, const JStatus& status)
195 {
196 return out << status.status;
197 }
198
199 protected:
201 };
202
203
204 /**
205 * Auxiliary class to map key to status bit.
206 */
208 public std::map<std::string, int>
209 {
210 /**
211 * Get status bit.
212 *
213 * \param key key
214 * \return bit
215 */
216 int operator()(const std::string& key) const
217 {
218 return this->at(key);
219 }
220 };
221
222
223 /**
224 * Auxiliary class to map status bit to key.
225 */
227 public std::map<int, std::string>
228 {
229 /**
230 * Constructor.
231 *
232 * \param input status bits
233 */
235 {
236 using namespace std;
237
238 for (JGetStatusBit::const_iterator i = input.begin(); i != input.end(); ++i) {
239 this->insert(make_pair(i->second, i->first));
240 }
241 }
242
243
244 /**
245 * Put status bit.
246 *
247 * \param bit bit
248 * \return key
249 */
250 const std::string& operator()(const int bit) const
251 {
252 return this->at(bit);
253 }
254 };
255
256
257 /**
258 * Print status.
259 *
260 * \param out output stream
261 * \param status status
262 * \param helper helper
263 */
264 inline void print(std::ostream& out, const JStatus& status, const JPutStatusBit& helper)
265 {
266 using namespace std;
267
268 for (const auto& i : helper) {
269 out << setw(24) << left << i.second << right << status.has(i.first) << endl;
270 }
271 }
272
273
274 /**
275 * Print status.
276 *
277 * \param out output stream
278 * \param status status
279 * \param helper helper
280 */
281 inline void print(std::ostream& out, const JStatus& status, const JGetStatusBit& helper)
282 {
283 using namespace std;
284
285 for (const auto& i : helper) {
286 out << setw(24) << left << i.first << right << status.has(i.second) << endl;
287 }
288 }
289}
290
291#endif
Auxiliary methods to convert data members or return values of member methods of a set of objects to a...
Interface for binary input.
Interface for binary output.
General puprpose classes and methods.
void print(std::ostream &out, const JStatus &status, const JPutStatusBit &helper)
Print status.
Definition JStatus.hh:264
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary class to map key to status bit.
Definition JStatus.hh:209
int operator()(const std::string &key) const
Get status bit.
Definition JStatus.hh:216
Auxiliary class to map status bit to key.
Definition JStatus.hh:228
const std::string & operator()(const int bit) const
Put status bit.
Definition JStatus.hh:250
JPutStatusBit(const JGetStatusBit &input)
Constructor.
Definition JStatus.hh:234
Auxiliary class for handling status.
Definition JStatus.hh:39
JStatus & getStatus()
Get status.
Definition JStatus.hh:74
int getStatus() const
Get status.
Definition JStatus.hh:63
friend JReader & operator>>(JReader &in, JStatus &status)
Read status from input.
Definition JStatus.hh:181
friend std::istream & operator>>(std::istream &in, JStatus &status)
Read status from input.
Definition JStatus.hh:155
void set(const int bit)
Set PMT status.
Definition JStatus.hh:131
bool equals(const JStatus &status) const
Equal method.
Definition JStatus.hh:109
friend std::ostream & operator<<(std::ostream &out, const JStatus &status)
Write status to output.
Definition JStatus.hh:168
bool has(const int bit) const
Test PMT status.
Definition JStatus.hh:120
int getStatus(const int mask) const
Get status.
Definition JStatus.hh:86
JStatus(const int status)
Constructor.
Definition JStatus.hh:53
friend JWriter & operator<<(JWriter &out, const JStatus &status)
Write status to output.
Definition JStatus.hh:194
void reset(const int bit)
Reset PMT status.
Definition JStatus.hh:142
JStatus()
Default constructor.
Definition JStatus.hh:43
void setStatus(const JStatus &status)
Set status.
Definition JStatus.hh:97
Template definition of auxiliary base class for comparison of data structures.
Definition JEquals.hh:84