Jpp  15.0.1-rc.1-highqe
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
21 namespace JEEP {}
22 namespace JPP { using namespace JEEP; }
23 
24 namespace 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  */
43  JStatus() :
44  status(0)
45  {}
46 
47 
48  /**
49  * Constructor.
50  *
51  * \param status status
52  */
53  JStatus(const int status) :
54  status(status)
55  {}
56 
57 
58  /**
59  * Get status.
60  *
61  * \return status
62  */
63  const JStatus& getStatus() const
64  {
65  return *this;
66  }
67 
68 
69  /**
70  * Get status.
71  *
72  * \return status
73  */
75  {
76  return *this;
77  }
78 
79 
80  /**
81  * Set status.
82  *
83  * \param status status
84  */
85  void setStatus(const JStatus& status)
86  {
87  this->status = status.status;
88  }
89 
90 
91  /**
92  * Equal method.
93  *
94  * \param status status
95  * \result true if this status equal to given status; else false
96  */
97  inline bool equals(const JStatus& status) const
98  {
99  return this->status == status.status;
100  }
101 
102 
103  /**
104  * Test PMT status.
105  *
106  * \param bit bit
107  */
108  bool has(const int bit) const
109  {
110  return (this->status & (1<<bit)) != 0;
111  }
112 
113 
114  /**
115  * Set PMT status.
116  *
117  * \param bit bit
118  */
119  void set(const int bit)
120  {
121  this->status |= (1<<bit);
122  }
123 
124 
125  /**
126  * Reset PMT status.
127  *
128  * \param bit bit
129  */
130  void reset(const int bit)
131  {
132  this->status &= ~(1<<bit);
133  }
134 
135 
136  /**
137  * Read status from input.
138  *
139  * \param in input stream
140  * \param status status
141  * \return input stream
142  */
143  friend inline std::istream& operator>>(std::istream& in, JStatus& status)
144  {
145  return in >> status.status;
146  }
147 
148 
149  /**
150  * Write status to output.
151  *
152  * \param out output stream
153  * \param status status
154  * \return output stream
155  */
156  friend inline std::ostream& operator<<(std::ostream& out, const JStatus& status)
157  {
158  return out << status.status;
159  }
160 
161 
162  /**
163  * Read status from input.
164  *
165  * \param in reader
166  * \param status status
167  * \return reader
168  */
170  {
171  return in >> status.status;
172  }
173 
174 
175  /**
176  * Write status to output.
177  *
178  * \param out writer
179  * \param status status
180  * \return writer
181  */
182  friend inline JWriter& operator<<(JWriter& out, const JStatus& status)
183  {
184  return out << status.status;
185  }
186 
187  protected:
188  int status;
189  };
190 
191 
192  /**
193  * Auxiliary class to map key to status bit.
194  */
195  struct JGetStatusBit :
196  public std::map<std::string, int>
197  {
198  /**
199  * Get status bit.
200  *
201  * \param key key
202  * \return bit
203  */
204  int operator()(const std::string& key) const
205  {
206  return this->at(key);
207  }
208  };
209 
210 
211  /**
212  * Auxiliary class to map status bit to key.
213  */
214  struct JPutStatusBit :
215  public std::map<int, std::string>
216  {
217  /**
218  * Constructor.
219  *
220  * \param input status bits
221  */
223  {
224  using namespace std;
225 
226  for (JGetStatusBit::const_iterator i = input.begin(); i != input.end(); ++i) {
227  this->insert(make_pair(i->second, i->first));
228  }
229  }
230 
231 
232  /**
233  * Put status bit.
234  *
235  * \param bit bit
236  * \return key
237  */
238  const std::string& operator()(const int bit) const
239  {
240  return this->at(bit);
241  }
242  };
243 
244 
245  /**
246  * Print status.
247  *
248  * \param out output stream
249  * \param status status
250  * \param helper helper
251  */
252  inline void print(std::ostream& out, const JStatus& status, const JPutStatusBit& helper)
253  {
254  using namespace std;
255 
256  for (const auto& i : helper) {
257  out << setw(24) << left << i.second << right << status.has(i.first) << endl;
258  }
259  }
260 
261 
262  /**
263  * Print status.
264  *
265  * \param out output stream
266  * \param status status
267  * \param helper helper
268  */
269  inline void print(std::ostream& out, const JStatus& status, const JGetStatusBit& helper)
270  {
271  using namespace std;
272 
273  for (const auto& i : helper) {
274  out << setw(24) << left << i.first << right << status.has(i.second) << endl;
275  }
276  }
277 }
278 
279 #endif
friend std::ostream & operator<<(std::ostream &out, const JStatus &status)
Write status to output.
Definition: JStatus.hh:156
JPutStatusBit(const JGetStatusBit &input)
Constructor.
Definition: JStatus.hh:222
bool equals(const JStatus &status) const
Equal method.
Definition: JStatus.hh:97
Interface for binary output.
int operator()(const std::string &key) const
Get status bit.
Definition: JStatus.hh:204
Auxiliary class to map key to status bit.
Definition: JStatus.hh:195
JStatus()
Default constructor.
Definition: JStatus.hh:43
void reset(const int bit)
Reset PMT status.
Definition: JStatus.hh:130
const std::string & operator()(const int bit) const
Put status bit.
Definition: JStatus.hh:238
bool has(const int bit) const
Test PMT status.
Definition: JStatus.hh:108
void set(const int bit)
Set PMT status.
Definition: JStatus.hh:119
Auxiliary class for handling status.
Definition: JStatus.hh:37
Template definition of auxiliary base class for comparison of data structures.
Definition: JEquals.hh:24
friend JReader & operator>>(JReader &in, JStatus &status)
Read status from input.
Definition: JStatus.hh:169
Interface for binary input.
Auxiliary methods to convert data members or return values of member methods of a set of objects to a...
print
Definition: JConvertDusj.sh:44
void setStatus(const JStatus &status)
Set status.
Definition: JStatus.hh:85
Auxiliary class to map status bit to key.
Definition: JStatus.hh:214
const JStatus & getStatus() const
Get status.
Definition: JStatus.hh:63
friend std::istream & operator>>(std::istream &in, JStatus &status)
Read status from input.
Definition: JStatus.hh:143
friend JWriter & operator<<(JWriter &out, const JStatus &status)
Write status to output.
Definition: JStatus.hh:182
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY source JAcoustics sh $DETECTOR_ID CHECK_EXIT_CODE typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:41
JStatus(const int status)
Constructor.
Definition: JStatus.hh:53
JStatus & getStatus()
Get status.
Definition: JStatus.hh:74