Jpp  debug
the software that should make you happy
JTag.hh
Go to the documentation of this file.
1 #ifndef __JNET__JCONTROLHOSTTAG__
2 #define __JNET__JCONTROLHOSTTAG__
3 
4 #include <istream>
5 #include <ostream>
6 #include <string>
7 
8 #include "JLang/JException.hh"
9 #include "JLang/JType.hh"
10 #include "JLang/JNullType.hh"
11 
12 
13 /**
14  * \file
15  * ControlHost tag.
16  *
17  * \author mdejong
18  */
19 
20 namespace JNET {}
21 namespace JPP { using namespace JNET; }
22 
23 namespace JNET {
24 
26 
27 
28  /**
29  * Type definition of numerical ControlHost tag.
30  */
31  typedef long long int JTag_t;
32  static const size_t TAGSIZE = sizeof(JTag_t); //!< Size of ControlHost tag.
33 
34 
35  /**
36  * ControlHost tag.
37  */
38  class JTag {
39  public:
40  /**
41  * Default constructor.
42  */
43  JTag() :
44  id(0)
45  {}
46 
47 
48  /**
49  * Copy constructor.
50  *
51  * \param tag tag
52  */
53  JTag(const JTag& tag)
54  {
55  setTag(tag);
56  }
57 
58 
59  /**
60  * Constructor.
61  *
62  * \param tag tag
63  */
64  JTag(const std::string& tag)
65  {
66  setTag(tag);
67  }
68 
69 
70  /**
71  * Constructor.
72  *
73  * \param id identifier
74  */
75  JTag(const JTag_t id)
76  {
77  setTag(id);
78  }
79 
80 
81  /**
82  * Get tag.
83  *
84  * \return tag
85  */
86  const JTag& getTag() const
87  {
88  return static_cast<const JTag&>(*this);
89  }
90 
91 
92  /**
93  * Get tag.
94  *
95  * \return tag
96  */
98  {
99  return static_cast<JTag&>(*this);
100  }
101 
102 
103  /**
104  * Set tag.
105  *
106  * \param tag tag
107  */
108  void setTag(const JTag& tag)
109  {
110  id = tag.getID();
111  }
112 
113 
114  /**
115  * Set tag.
116  *
117  * This method may throw an exception.
118  *
119  * \param tag tag
120  */
121  void setTag(const std::string& tag)
122  {
123  if (!tag.empty() && tag.size() <= TAGSIZE) {
124 
125  char* __p = (char*) &(this->id);
126 
127  size_t i = 0;
128 
129  for ( ; i != tag.size(); ++i) {
130  __p[i] = tag[i];
131  }
132 
133  for ( ; i != TAGSIZE; ++i) {
134  __p[i] = '\0';
135  }
136 
137  } else {
138 
139  THROW(JControlHostException, "Invalid tag length <" << tag << "> " << tag.size());
140  }
141  }
142 
143 
144  /**
145  * Set tag.
146  *
147  * \param id identifier
148  */
149  void setTag(const JTag_t id)
150  {
151  this->id = id;
152  }
153 
154 
155  /**
156  * Get identifier.
157  *
158  * \return identifier
159  */
160  JTag_t getID() const
161  {
162  return id;
163  }
164 
165 
166  /**
167  * Convert tag to string.
168  *
169  * \return tag
170  */
171  std::string toString() const
172  {
173  int pos = TAGSIZE;
174 
175  const char* __p = (const char*) &(this->id);
176 
177  while (pos != 0 && __p[pos-1] == '\0') {
178  --pos;
179  }
180 
181  return std::string(__p, pos);
182  }
183 
184 
185  /**
186  * Type conversion operators.
187  *
188  * \return tag
189  */
190  inline operator std::string() const
191  {
192  return toString();
193  }
194 
195 
196  /**
197  * C-string.
198  *
199  * \return tag
200  */
201  const char* c_str() const
202  {
203  return (const char*) &(this->id);
204  }
205 
206 
207  /**
208  * Get character.
209  *
210  * \param i index
211  * \return character at index
212  */
213  const char operator[](int i) const
214  {
215  const char* __p = (const char*) &(this->id);
216 
217  return __p[i];
218  }
219 
220 
221  /**
222  * Read JTag from input stream.
223  *
224  * \param in input stream
225  * \param object JTag
226  * \return input stream
227  */
228  friend inline std::istream& operator>>(std::istream& in, JTag& object)
229  {
230  std::string buffer;
231 
232  if (in >> buffer) {
233  object.setTag(buffer);
234  }
235 
236  return in;
237  }
238 
239 
240  /**
241  * Write JTag to output stream.
242  *
243  * \param out output stream
244  * \param object JTag
245  * \return output stream
246  */
247  friend inline std::ostream& operator<<(std::ostream& out, const JTag& object)
248  {
249  return out << object.toString();
250  }
251 
252 
253  protected:
255  };
256 
257 
258  /**
259  * Less than operator for JTag
260  *
261  * \param first tag
262  * \param second tag
263  * \return
264  */
265  inline bool operator<(const JTag& first, const JTag& second)
266  {
267  return first.getID() < second.getID();
268  }
269 
270 
271  /**
272  * Equal operator for JTag
273  *
274  * \param first tag
275  * \param second tag
276  * \return
277  */
278  inline bool operator==(const JTag& first, const JTag& second)
279  {
280  return first.getID() == second.getID();
281  }
282 
283 
284  /**
285  * Not equal operator for JTag
286  *
287  * \param first tag
288  * \param second tag
289  * \return
290  */
291  inline bool operator!=(const JTag& first, const JTag& second)
292  {
293  return first.getID() != second.getID();
294  }
295 
296 
297  /**
298  * Special ControlHost tags
299  */
300  static const JTag DISPTAG_Subscribe ("_Subscri");
301  static const JTag DISPTAG_Gime ("_Gime");
302  static const JTag DISPTAG_Always ("_Always");
303  static const JTag DISPTAG_MyId ("_MyId");
304  static const JTag DISPTAG_Born ("Born");
305  static const JTag DISPTAG_Died ("Died");
306  static const JTag DISPTAG_ShowStat ("_ShowSta");
307  static const JTag DISPTAG_WhereIs ("_WhereIs");
308  static const JTag DISPTAG_Version ("_Version");
309  static const JTag DISPTAG_Debug ("_Debug");
310  static const JTag DISPTAG_UNDEFINED (0);
311 }
312 
313 
314 /**
315  * Template definition for method returning JNET::JTag value.
316  * The template argument refers to the data type for future ControlHost operations.
317  */
318 template<class T>
320 {
321  return getTag(JLANG::JType<T>());
322 }
323 
324 
325 /**
326  * Default argument definition for method returning JNET::JTag.
327  * The method argument refers to the data type for future I/O operations.
328  * This method should be overloaded for each desired class and
329  * return a corresponding JNET::JTag value.
330  *
331  * \param type data type
332  */
333 template<class T>
335 
336 
337 
338 
339 /**
340  * Test availability of JNET::JTag value for given class.
341  *
342  * The parameter result evaluates to true if for this class a JNET::JTag value is defined.
343  */
344 template<class T>
346 {
347 private:
348 
349  class JSmall { char buffer; };
350  class JBig { int buffer; };
351 
352  static JBig JTest(...);
353  static JSmall JTest(const JNET::JTag&);
354 
355 public:
356  static const bool result = sizeof(JTest(getTag(JLANG::JType<T>()))) == sizeof(JSmall); //!< true if defined tag; else false
357 };
358 
359 #endif
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
JNET::JTag getTag()
Template definition for method returning JNET::JTag value.
Definition: JTag.hh:319
Exception for ControlHost.
Definition: JException.hh:486
ControlHost tag.
Definition: JTag.hh:38
void setTag(const JTag &tag)
Set tag.
Definition: JTag.hh:108
friend std::istream & operator>>(std::istream &in, JTag &object)
Read JTag from input stream.
Definition: JTag.hh:228
const char * c_str() const
C-string.
Definition: JTag.hh:201
JTag(const std::string &tag)
Constructor.
Definition: JTag.hh:64
friend std::ostream & operator<<(std::ostream &out, const JTag &object)
Write JTag to output stream.
Definition: JTag.hh:247
const char operator[](int i) const
Get character.
Definition: JTag.hh:213
void setTag(const std::string &tag)
Set tag.
Definition: JTag.hh:121
JTag(const JTag &tag)
Copy constructor.
Definition: JTag.hh:53
JTag(const JTag_t id)
Constructor.
Definition: JTag.hh:75
JTag & getTag()
Get tag.
Definition: JTag.hh:97
JTag_t id
Definition: JTag.hh:254
void setTag(const JTag_t id)
Set tag.
Definition: JTag.hh:149
std::string toString() const
Convert tag to string.
Definition: JTag.hh:171
const JTag & getTag() const
Get tag.
Definition: JTag.hh:86
JTag_t getID() const
Get identifier.
Definition: JTag.hh:160
JTag()
Default constructor.
Definition: JTag.hh:43
Test availability of JNET::JTag value for given class.
Definition: JTag.hh:346
static JSmall JTest(const JNET::JTag &)
static JBig JTest(...)
bool operator!=(const JTag &first, const JTag &second)
Not equal operator for JTag.
Definition: JTag.hh:291
static const JTag DISPTAG_Always("_Always")
bool operator==(const JTag &first, const JTag &second)
Equal operator for JTag.
Definition: JTag.hh:278
static const JTag DISPTAG_Gime("_Gime")
static const JTag DISPTAG_Version("_Version")
static const JTag DISPTAG_WhereIs("_WhereIs")
static const JTag DISPTAG_Debug("_Debug")
static const JTag DISPTAG_MyId("_MyId")
static const JTag DISPTAG_Subscribe("_Subscri")
Special ControlHost tags.
static const size_t TAGSIZE
Size of ControlHost tag.
Definition: JTag.hh:32
static const JTag DISPTAG_Born("Born")
long long int JTag_t
Type definition of numerical ControlHost tag.
Definition: JTag.hh:31
bool operator<(const JTag &first, const JTag &second)
Less than operator for JTag.
Definition: JTag.hh:265
static const JTag DISPTAG_UNDEFINED(0)
static const JTag DISPTAG_ShowStat("_ShowSta")
static const JTag DISPTAG_Died("Died")
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary class for no type definition.
Definition: JNullType.hh:19
Auxiliary base class for compile time evaluation of test.
Definition: JTest.hh:21
Auxiliary class for a type holder.
Definition: JType.hh:19