IntParser.hh
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
17 #ifndef HH_SENF_Packets_IntParser_
18 #define HH_SENF_Packets_IntParser_ 1
19 
20 // Custom includes
21 #include <ostream>
22 #include <boost/cstdint.hpp>
23 #include <boost/static_assert.hpp>
24 #include <boost/integer/integer_mask.hpp>
25 #include "PacketParser.hh"
26 
27 //#include "IntParser.mpp"
28 //-/////////////////////////////////////////////////////////////////////////////////////////////////
29 #include "IntParser.ih"
30 
31 namespace senf {
32 
64  struct Int8Parser
65  : public detail::packet::IntParserOps<Int8Parser,boost::int8_t>,
66  public PacketParserBase
67  {
69 
70  //-////////////////////////////////////////////////////////////////////////
71 
72  typedef boost::int8_t value_type;
73  static size_type const fixed_bytes = 1;
74  static value_type const min_value = -128;
75  static value_type const max_value = 127;
76 
77  value_type value() const { return i()[0]; }
78  void value(value_type v) { i()[0] = v; }
79  Int8Parser const & operator= (value_type other) { value(other); return *this; }
80  };
84  inline std::ostream & operator<<(std::ostream & os, Int8Parser const & i)
85  { os << i.value(); return os; }
86 
91  struct UInt8Parser
92  : public detail::packet::IntParserOps<UInt8Parser,boost::uint8_t>,
93  public PacketParserBase
94  {
96 
97  //-////////////////////////////////////////////////////////////////////////
98 
99  typedef boost::uint8_t value_type;
100  static size_type const fixed_bytes = 1;
101  static value_type const min_value = 0u;
102  static value_type const max_value = 255u;
103 
104  value_type value() const { return i()[0]; }
105  void value(value_type v) { i()[0] = v; }
106  UInt8Parser const & operator= (value_type other) { value(other); return *this; }
107  };
111  inline std::ostream & operator<<(std::ostream & os, UInt8Parser const & i)
112  { os << i.value(); return os; }
113 
118  struct Int16Parser
119  : public detail::packet::IntParserOps<Int16Parser,boost::int16_t>,
120  public PacketParserBase
121  {
123 
124  //-////////////////////////////////////////////////////////////////////////
125 
126  typedef boost::int16_t value_type;
127  static size_type const fixed_bytes = 2;
128  static value_type const min_value = -32768;
129  static value_type const max_value = 32767;
130 
131 
132  value_type value() const { return detail::packet::parse_uint16(i()); }
133  void value(value_type v) { detail::packet::write_uint16(i(),v); }
134  Int16Parser const & operator= (value_type other) { value(other); return *this; }
135  };
139  inline std::ostream & operator<<(std::ostream & os, Int16Parser const & i)
140  { os << i.value(); return os; }
141 
147  : public detail::packet::IntParserOps<Int16LSBParser,boost::int16_t>,
148  public PacketParserBase
149  {
151 
152  //-////////////////////////////////////////////////////////////////////////
153 
154  typedef boost::int16_t value_type;
155  static size_type const fixed_bytes = 2;
156  static value_type const min_value = -32768;
157  static value_type const max_value = 32767;
158 
159 
160  value_type value() const { return detail::packet::parse_uint16LSB(i()); }
161  void value(value_type v) { detail::packet::write_uint16LSB(i(),v); }
162  Int16LSBParser const & operator= (value_type other) { value(other); return *this; }
163  };
167  inline std::ostream & operator<<(std::ostream & os, Int16LSBParser const & i)
168  { os << i.value(); return os; }
169 
175  : public detail::packet::IntParserOps<UInt16Parser,boost::uint16_t>,
176  public PacketParserBase
177  {
179 
180  //-////////////////////////////////////////////////////////////////////////
181 
182  typedef boost::uint16_t value_type;
183  static size_type const fixed_bytes = 2;
184  static value_type const min_value = 0u;
185  static value_type const max_value = 65535u;
186 
187  value_type value() const { return detail::packet::parse_uint16(i()); }
188  void value(value_type v) { detail::packet::write_uint16(i(),v); }
189  UInt16Parser const & operator= (value_type other) { value(other); return *this; }
190  };
194  inline std::ostream & operator<<(std::ostream & os, UInt16Parser const & i)
195  { os << i.value(); return os; }
196 
202  : public detail::packet::IntParserOps<UInt16LSBParser,boost::uint16_t>,
203  public PacketParserBase
204  {
206 
207  //-////////////////////////////////////////////////////////////////////////
208 
209  typedef boost::uint16_t value_type;
210  static size_type const fixed_bytes = 2;
211  static value_type const min_value = 0u;
212  static value_type const max_value = 65535u;
213 
214  value_type value() const { return detail::packet::parse_uint16LSB(i()); }
215  void value(value_type v) { detail::packet::write_uint16LSB(i(),v); }
216  UInt16LSBParser const & operator= (value_type other) { value(other); return *this; }
217  };
221  inline std::ostream & operator<<(std::ostream & os, UInt16LSBParser const & i)
222  { os << i.value(); return os; }
223 
228  struct Int24Parser
229  : public detail::packet::IntParserOps<Int24Parser,boost::int32_t>,
230  public PacketParserBase
231  {
233 
234  //-////////////////////////////////////////////////////////////////////////
235 
236  typedef boost::int32_t value_type;
237  static size_type const fixed_bytes = 3;
238  static value_type const min_value = -8388608;
239  static value_type const max_value = 8388607;
240 
241  value_type value() const {
242  value_type v (detail::packet::parse_uint24(i())); return v&0x800000 ? v|0xff000000 : v; }
243  void value(value_type v) { detail::packet::write_uint24(i(),v); }
244  Int24Parser const & operator= (value_type other) { value(other); return *this; }
245  };
249  inline std::ostream & operator<<(std::ostream & os, Int24Parser const & i)
250  { os << i.value(); return os; }
251 
257  : public detail::packet::IntParserOps<UInt24Parser,boost::uint32_t>,
258  public PacketParserBase
259  {
261 
262  //-////////////////////////////////////////////////////////////////////////
263 
264  typedef boost::uint32_t value_type;
265  static size_type const fixed_bytes = 3;
266  static value_type const min_value = 0u;
267  static value_type const max_value = 16777215u;
268 
269  value_type value() const { return detail::packet::parse_uint24(i()); }
270  void value(value_type v) { detail::packet::write_uint24(i(),v); }
271  UInt24Parser const & operator= (value_type other) { value(other); return *this; }
272  };
276  inline std::ostream & operator<<(std::ostream & os, UInt24Parser const & i)
277  { os << i.value(); return os; }
278 
283  struct Int32Parser
284  : public detail::packet::IntParserOps<Int32Parser,boost::int32_t>,
285  public PacketParserBase
286  {
288 
289  //-////////////////////////////////////////////////////////////////////////
290 
291  typedef boost::int32_t value_type;
292  static size_type const fixed_bytes = 4;
293  static value_type const min_value = -2147483647 - 1;
294  static value_type const max_value = 2147483647;
295 
296  value_type value() const { return detail::packet::parse_uint32(i()); }
297  void value(value_type v) { detail::packet::write_uint32(i(),v); }
298  Int32Parser const & operator= (value_type other) { value(other); return *this; }
299  };
303  inline std::ostream & operator<<(std::ostream & os, Int32Parser const & i)
304  { os << i.value(); return os; }
305 
311  : public detail::packet::IntParserOps<UInt32Parser,boost::uint32_t>,
312  public PacketParserBase
313  {
315 
316  //-////////////////////////////////////////////////////////////////////////
317 
318  typedef boost::uint32_t value_type;
319  static size_type const fixed_bytes = 4;
320  static value_type const min_value = 0u;
321  static value_type const max_value = 4294967295u;
322 
323  value_type value() const { return detail::packet::parse_uint32(i()); }
324  void value(value_type v) { detail::packet::write_uint32(i(),v); }
325  UInt32Parser const & operator= (value_type other) { value(other); return *this; }
326  };
330  inline std::ostream & operator<<(std::ostream & os, UInt32Parser const & i)
331  { os << i.value(); return os; }
332 
334  : public detail::packet::IntParserOps<UInt32LSBParser,boost::uint32_t>,
335  public PacketParserBase
336  {
338 
339  //-////////////////////////////////////////////////////////////////////////
340 
341  typedef boost::uint32_t value_type;
342  static size_type const fixed_bytes = 4;
343  static value_type const min_value = 0u;
344  static value_type const max_value = 4294967295u;
345 
346  value_type value() const { return detail::packet::parse_uint32LSB(i()); }
347  void value(value_type v) { detail::packet::write_uint32LSB(i(),v); }
348  UInt32LSBParser const & operator= (value_type other) { value(other); return *this; }
349  };
353  inline std::ostream & operator<<(std::ostream & os, UInt32LSBParser const & i)
354  { os << i.value(); return os; }
355 
356 
357 
358 
363  struct Int64Parser
364  : public detail::packet::IntParserOps<Int64Parser,boost::int64_t>,
365  public PacketParserBase
366  {
368 
369  //-////////////////////////////////////////////////////////////////////////
370 
371  typedef boost::int64_t value_type;
372  static size_type const fixed_bytes = 8;
373 
374  value_type value() const { return detail::packet::parse_uint64(i()); }
375  void value(value_type v) { detail::packet::write_uint64(i(),v); }
376  Int64Parser const & operator= (value_type other) { value(other); return *this; }
377  };
381  inline std::ostream & operator<<(std::ostream & os, Int64Parser const & i)
382  { os << i.value(); return os; }
383 
384 
390  : public detail::packet::IntParserOps<UInt64Parser,boost::uint64_t>,
391  public PacketParserBase
392  {
394 
395  //-////////////////////////////////////////////////////////////////////////
396 
397  typedef boost::uint64_t value_type;
398  static size_type const fixed_bytes = 8;
399 
400  value_type value() const { return detail::packet::parse_uint64(i()); }
401  void value(value_type v) { detail::packet::write_uint64(i(),v); }
402  UInt64Parser const & operator= (value_type other) { value(other); return *this; }
403  };
407  inline std::ostream & operator<<(std::ostream & os, UInt64Parser const & i)
408  { os << i.value(); return os; }
409 
415  : public detail::packet::IntParserOps<UInt64LSBParser,boost::uint64_t>,
416  public PacketParserBase
417  {
419 
420  //-////////////////////////////////////////////////////////////////////////
421 
422  typedef boost::uint64_t value_type;
423  static size_type const fixed_bytes = 8;
424 
425  value_type value() const { return detail::packet::parse_uint64LSB(i()); }
426  void value(value_type v) { detail::packet::write_uint64LSB(i(),v); }
427  UInt64LSBParser const & operator= (value_type other) { value(other); return *this; }
428  };
432  inline std::ostream & operator<<(std::ostream & os, UInt64LSBParser const & i)
433  { os << i.value(); return os; }
434 
457  template <unsigned Start, unsigned End>
459  : public detail::packet::IntParserOps<IntFieldParser<Start,End>,boost::int32_t>,
460  public PacketParserBase
461  {
463 
464  //-////////////////////////////////////////////////////////////////////////
465 
466  typedef boost::int32_t value_type;
467  static size_type const start_bit = Start;
468  static size_type const end_bit = End;
469  static size_type const fixed_bytes = (End-1)/8+1;
470  static value_type const max_value = boost::low_bits_mask_t<End-Start-1>::sig_bits;
471  static value_type const min_value = - max_value - 1;
472 
473  value_type value() const {
474  value_type v (detail::packet::parse_bitfield<Start,End>::parse(i()));
475  return v&boost::high_bit_mask_t<End-Start-1>::high_bit ?
476  v | ~boost::low_bits_mask_t<End-Start>::sig_bits : v;
477  }
478  void value(value_type v) { detail::packet::parse_bitfield<Start,End>::write(i(),v); }
479  IntFieldParser const & operator= (value_type other) { value(other); return *this; }
480 
481  private:
482  BOOST_STATIC_ASSERT( Start<End );
483  BOOST_STATIC_ASSERT( End-Start<=32 );
484  };
488  template <unsigned Start, unsigned End>
489  inline std::ostream & operator<<(std::ostream & os, IntFieldParser<Start,End> const & i)
490  { os << i.value(); return os; }
491 
514  template <unsigned Start, unsigned End>
516  : public detail::packet::IntParserOps<UIntFieldParser<Start,End>,boost::uint32_t>,
517  public PacketParserBase
518  {
520 
521  //-////////////////////////////////////////////////////////////////////////
522 
523  typedef boost::uint32_t value_type;
524  static size_type const start_bit = Start;
525  static size_type const end_bit = End;
526  static size_type const fixed_bytes = (End-1)/8+1;
527  static value_type const min_value = 0u;
528  static value_type const max_value = boost::low_bits_mask_t<End-Start>::sig_bits;
529 
530  value_type value() const { return detail::packet::parse_bitfield<Start,End>::parse(i()); }
531  void value(value_type v) { detail::packet::parse_bitfield<Start,End>::write(i(),v); }
532  UIntFieldParser const & operator= (value_type other) { value(other); return *this; }
533 
534  private:
535  BOOST_STATIC_ASSERT( Start<End );
536  BOOST_STATIC_ASSERT( End-Start<=32 );
537  };
541  template <unsigned Start, unsigned End>
542  inline std::ostream & operator<<(std::ostream & os, UIntFieldParser<Start,End> const & i)
543  { os << i.value(); return os; }
544 
559  template <unsigned Bit>
560  struct FlagParser
561  : public detail::packet::IntParserOps<FlagParser<Bit>,bool>,
562  public PacketParserBase
563  {
565 
566  //-////////////////////////////////////////////////////////////////////////
567 
568  typedef bool value_type;
569  static size_type const bit = Bit;
570  static size_type const fixed_bytes = Bit/8+1;
571  static value_type const min_value = 0;
572  static value_type const max_value = 1;
573 
574  value_type value() const { return i()[Bit/8] & (1<<(7-(Bit%8))); }
575  void value(value_type v) {
576  if (v) i()[Bit/8] |= 1<<(7-(Bit%8));
577  else i()[Bit/8] &= ~(1<<(7-(Bit%8)));
578  }
579  FlagParser const & operator= (value_type other) { value(other); return *this; }
580  };
584  template <unsigned Bit>
585  inline std::ostream & operator<<(std::ostream & os, FlagParser<Bit> const & i)
586  { os << i.value(); return os; }
587 
588 }
589 
590 //-/////////////////////////////////////////////////////////////////////////////////////////////////
591 #endif
592 #if !defined(HH_SENF_Packets_Packets__decls_) && !defined(HH_SENF_Packets_IntParser_i_)
593 #define HH_SENF_Packets_IntParser_i_
594 //#include "IntParser.cci"
595 //#include "IntParser.ct"
596 //#include "IntParser.cti"
597 #endif
598 
599 
600 // Local Variables:
601 // mode: c++
602 // fill-column: 100
603 // c-file-style: "senf"
604 // indent-tabs-mode: nil
605 // ispell-local-dictionary: "american"
606 // compile-command: "scons -u test"
607 // comment-column: 40
608 // End:
value_type value() const
Definition: IntParser.hh:187
void value(value_type v)
Definition: IntParser.hh:375
PacketParser public header.
boost::int16_t value_type
Definition: IntParser.hh:126
Parse unsigned bitfield with up to 32bit&#39;s.
Definition: IntParser.hh:515
boost::uint32_t value_type
Definition: IntParser.hh:523
void value(value_type v)
Definition: IntParser.hh:243
void value(value_type v)
Definition: IntParser.hh:297
value_type value() const
Definition: IntParser.hh:323
std::ostream & operator<<(std::ostream &os, UInt8Parser const &i)
Write parsed value to stream.
Definition: IntParser.hh:111
void value(value_type v)
Definition: IntParser.hh:426
Parse 8bit unsigned byte aligned integer.
Definition: IntParser.hh:91
void value(value_type v)
Definition: IntParser.hh:347
std::ostream & operator<<(std::ostream &os, UInt32Parser const &i)
Write parsed value to stream.
Definition: IntParser.hh:330
boost::int32_t value_type
Definition: IntParser.hh:466
value_type value() const
Definition: IntParser.hh:296
value_type value() const
Definition: IntParser.hh:400
Parse 64bit unsigned byte aligned integer LSB.
Definition: IntParser.hh:414
UInt24Parser(data_iterator i, state_type s)
Definition: IntParser.hh:260
value_type value() const
Definition: IntParser.hh:530
value_type value() const
Definition: IntParser.hh:132
value_type value() const
Definition: IntParser.hh:374
Parse 16bit signed byte aligned integer.
Definition: IntParser.hh:118
value_type value() const
Definition: IntParser.hh:214
Parse 24bit signed byte aligned integer.
Definition: IntParser.hh:228
void value(value_type v)
Definition: IntParser.hh:105
boost::uint64_t value_type
Definition: IntParser.hh:422
value_type value() const
Definition: IntParser.hh:574
Int8Parser(data_iterator i, state_type s)
Definition: IntParser.hh:68
static size_type const fixed_bytes
Definition: IntParser.hh:73
UInt8Parser(data_iterator i, state_type s)
Definition: IntParser.hh:95
UInt64LSBParser(data_iterator i, state_type s)
Definition: IntParser.hh:418
Packet data STL-sequence view.
Definition: PacketData.hh:61
std::ostream & operator<<(std::ostream &os, UInt16LSBParser const &i)
Write parsed value to stream.
Definition: IntParser.hh:221
boost::uint8_t value_type
Definition: IntParser.hh:99
UInt16LSBParser(data_iterator i, state_type s)
Definition: IntParser.hh:205
data_iterator i() const
Return beginning of data to parse.
std::ostream & operator<<(std::ostream &os, UInt64LSBParser const &i)
Write parsed value to stream.
Definition: IntParser.hh:432
boost::int32_t value_type
Definition: IntParser.hh:291
Int16Parser(data_iterator i, state_type s)
Definition: IntParser.hh:122
void value(value_type v)
Definition: IntParser.hh:133
boost::int16_t value_type
Definition: IntParser.hh:154
Parse 64bit unsigned byte aligned integer.
Definition: IntParser.hh:389
void value(value_type v)
Definition: IntParser.hh:161
Int24Parser(data_iterator i, state_type s)
Definition: IntParser.hh:232
value_type value() const
Definition: IntParser.hh:473
FlagParser(data_iterator i, state_type s)
Definition: IntParser.hh:564
std::ostream & operator<<(std::ostream &os, Int8Parser const &i)
Write parsed value to stream.
Definition: IntParser.hh:84
Parse 8bit signed byte aligned integer.
Definition: IntParser.hh:64
std::ostream & operator<<(std::ostream &os, Int24Parser const &i)
Write parsed value to stream.
Definition: IntParser.hh:249
std::ostream & operator<<(std::ostream &os, UInt64Parser const &i)
Write parsed value to stream.
Definition: IntParser.hh:407
Parse 16bit signed byte aligned integer LSB.
Definition: IntParser.hh:146
static value_type const min_value
Definition: IntParser.hh:74
Parse 16bit unsigned byte aligned integer LSB.
Definition: IntParser.hh:201
detail::packet::size_type size_type
Unsigned integral type.
void value(value_type v)
Definition: IntParser.hh:478
std::ostream & operator<<(std::ostream &os, Int16Parser const &i)
Write parsed value to stream.
Definition: IntParser.hh:139
void value(value_type v)
Definition: IntParser.hh:188
void value(value_type v)
Definition: IntParser.hh:575
boost::uint32_t value_type
Definition: IntParser.hh:341
Int16LSBParser(data_iterator i, state_type s)
Definition: IntParser.hh:150
std::ostream & operator<<(std::ostream &os, Int16LSBParser const &i)
Write parsed value to stream.
Definition: IntParser.hh:167
UInt32Parser(data_iterator i, state_type s)
Definition: IntParser.hh:314
Int64Parser(data_iterator i, state_type s)
Definition: IntParser.hh:367
std::ostream & operator<<(std::ostream &os, Int32Parser const &i)
Write parsed value to stream.
Definition: IntParser.hh:303
boost::int64_t value_type
Definition: IntParser.hh:371
Parse 24bit unsigned byte aligned integer.
Definition: IntParser.hh:256
value_type value() const
Definition: IntParser.hh:346
Parse signed bitfield with up to 32bit&#39;s.
Definition: IntParser.hh:458
void value(value_type v)
Definition: IntParser.hh:531
boost::uint64_t value_type
Definition: IntParser.hh:397
void value(value_type v)
Definition: IntParser.hh:215
boost::uint32_t value_type
Definition: IntParser.hh:264
Parser Base class.
Int8Parser const & operator=(value_type other)
Definition: IntParser.hh:79
Parse 32bit signed byte aligned integer.
Definition: IntParser.hh:283
Parse 32bit unsigned byte aligned integer.
Definition: IntParser.hh:310
void value(value_type v)
Definition: IntParser.hh:78
IntFieldParser(data_iterator i, state_type s)
Definition: IntParser.hh:462
value_type value() const
Definition: IntParser.hh:241
Parse single-bit flag.
Definition: IntParser.hh:560
static value_type const max_value
Definition: IntParser.hh:75
std::ostream & operator<<(std::ostream &os, UInt16Parser const &i)
Write parsed value to stream.
Definition: IntParser.hh:194
boost::int8_t value_type
Definition: IntParser.hh:72
Int32Parser(data_iterator i, state_type s)
Definition: IntParser.hh:287
void value(value_type v)
Definition: IntParser.hh:324
UIntFieldParser(data_iterator i, state_type s)
Definition: IntParser.hh:519
Parse 16bit unsigned byte aligned integer.
Definition: IntParser.hh:174
value_type value() const
Definition: IntParser.hh:269
std::ostream & operator<<(std::ostream &os, UInt24Parser const &i)
Write parsed value to stream.
Definition: IntParser.hh:276
value_type value() const
Definition: IntParser.hh:104
UInt32LSBParser(data_iterator i, state_type s)
Definition: IntParser.hh:337
boost::uint16_t value_type
Definition: IntParser.hh:209
void value(value_type v)
Definition: IntParser.hh:401
boost::uint32_t value_type
Definition: IntParser.hh:318
Parse 64bit signed byte aligned integer.
Definition: IntParser.hh:363
value_type value() const
Definition: IntParser.hh:425
detail::packet::iterator data_iterator
Raw data iterator type.
UInt64Parser(data_iterator i, state_type s)
Definition: IntParser.hh:393
UInt16Parser(data_iterator i, state_type s)
Definition: IntParser.hh:178
value_type value() const
Definition: IntParser.hh:77
void value(value_type v)
Definition: IntParser.hh:270
boost::uint16_t value_type
Definition: IntParser.hh:182
std::ostream & operator<<(std::ostream &os, UInt32LSBParser const &i)
Write parsed value to stream.
Definition: IntParser.hh:353
boost::int32_t value_type
Definition: IntParser.hh:236
value_type value() const
Definition: IntParser.hh:160
std::ostream & operator<<(std::ostream &os, Int64Parser const &i)
Write parsed value to stream.
Definition: IntParser.hh:381