BitsetParser.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_BitsetParser_
18 #define HH_SENF_Packets_BitsetParser_ 1
19 
20 // Custom includes
21 #include <bitset>
22 #include <stdexcept>
23 #include <boost/integer/static_min_max.hpp>
24 #include "PacketParser.hh"
25 
26 //-/////////////////////////////////////////////////////////////////////////////////////////////////
27 
28 namespace senf {
29 
31  {
32  static std::size_t bytePosition(std::size_t totalBytes, std::size_t bitPos);
33  static std::size_t bit(std::size_t bitPos);
34  };
35 
37  {
38  static std::size_t bytePosition(std::size_t totalBytes, std::size_t bitPos);
39  static std::size_t bit(std::size_t bitPos);
40  };
41 
42 
43  template <std::size_t N, typename Endianness>
44  struct BitsetParser
45  : public PacketParserBase
46  {
48  : PacketParserBase(i,s,fixed_bytes) {}
49 
50  typedef std::bitset<N> value_type;
51  static size_type const fixed_bytes = N/8 + boost::static_unsigned_min<1u, N % 8>::value;
52 
53  class reference
54  {
55  public:
56  ~reference() {};
57  reference & operator=(bool v);
58  reference & operator=(reference const & other);
59  bool operator~() const;
60  operator bool() const;
61  reference & flip();
62  private:
63  reference(BitsetParser<N,Endianness> & parser, std::size_t pos);
65  std::size_t pos_;
66  friend struct BitsetParser<N,Endianness>;
67  };
68 
69  void set();
70  void set(std::size_t pos, bool v = true);
71 
72  void reset();
73  void reset(std::size_t pos);
74 
75  void flip();
76  void flip(std::size_t pos);
77 
78  bool any() const;
79  bool none() const;
80  bool all() const;
81 
82  bool test(std::size_t pos) const;
83 
84  bool operator[](std::size_t pos) const;
85  reference operator[](std::size_t pos);
86 
87  std::size_t size() const;
88  std::size_t count() const;
89 
90  void value(value_type const & bset);
91  value_type value() const;
92  };
93 
94 }
95 
96 //-/////////////////////////////////////////////////////////////////////////////////////////////////
97 #include "BitsetParser.cci"
98 //#include "BitsetParser.ct"
99 #include "BitsetParser.cti"
100 #endif
PacketParser public header.
unspecified_keyword_type parser
static std::size_t bit(std::size_t bitPos)
BitsetParser(data_iterator i, state_type s)
Definition: BitsetParser.hh:47
Packet data STL-sequence view.
Definition: PacketData.hh:61
detail::packet::size_type size_type
Unsigned integral type.
std::uint32_t count
static std::size_t bytePosition(std::size_t totalBytes, std::size_t bitPos)
std::bitset< N > value_type
Definition: BitsetParser.hh:50
Parser Base class.
detail::packet::iterator data_iterator
Raw data iterator type.