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