BitsetParser.cti
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 
14 /** \file
15  \brief BitsetParser inline template implementation */
16 
17 // Custom includes
18 #include <senf/Utils/senfassert.hh>
19 
20 #define prefix_ inline
21 //-/////////////////////////////////////////////////////////////////////////////////////////////////
22 
23 //-/////////////////////////////////////////////////////////////////////////////////////////////////
24 // senf::BitsetParser::reference
25 
26 template <std::size_t N, typename Endianness>
27 prefix_ typename senf::BitsetParser<N,Endianness>::reference &
28 senf::BitsetParser<N,Endianness>::reference::operator=(bool v)
29 {
30  parser_.set(pos_, v);
31  return *this;
32 }
33 
34 template <std::size_t N, typename Endianness>
35 prefix_ typename senf::BitsetParser<N,Endianness>::reference &
36 senf::BitsetParser<N,Endianness>::reference::operator=(BitsetParser<N,Endianness>::reference const & other)
37 {
38  parser_.set(pos_, other);
39  return *this;
40 }
41 
42 template <std::size_t N, typename Endianness>
43 prefix_ bool senf::BitsetParser<N,Endianness>::reference::operator~()
44  const
45 {
46  return ! parser_.test(pos_);
47 }
48 
49 template <std::size_t N, typename Endianness>
50 prefix_ senf::BitsetParser<N,Endianness>::reference::operator bool()
51  const
52 {
53  return parser_.test(pos_);
54 }
55 
56 template <std::size_t N, typename Endianness>
57 prefix_ typename senf::BitsetParser<N,Endianness>::reference & senf::BitsetParser<N,Endianness>::reference::flip()
58 {
59  parser_.flip(pos_);
60  return *this;
61 }
62 
63 template <std::size_t N, typename Endianness>
64 prefix_ senf::BitsetParser<N,Endianness>::reference::reference(BitsetParser<N,Endianness> & parser, std::size_t pos)
65  : parser_(parser), pos_(pos)
66 {}
67 
68 //-/////////////////////////////////////////////////////////////////////////////////////////////////
69 // senf::BitsetParser
70 
71 #define validatePostion(pos) if (pos > N-1) \
72  throw make_WrapException(std::out_of_range("invalid position in bitset"))
73 
74 #define assertPostion(pos) SENF_ASSERT( pos <= N-1, "invalid position in bitset");
75 
76 template <std::size_t N, typename Endianness>
77 prefix_ void senf::BitsetParser<N,Endianness>::reset()
78 {
79  std::fill_n( i(), fixed_bytes, 0x00);
80 }
81 
82 template <std::size_t N, typename Endianness>
83 prefix_ void senf::BitsetParser<N,Endianness>::reset(std::size_t pos)
84 {
85  validatePostion(pos);
86  i()[Endianness::bytePosition(fixed_bytes, pos)] &= ~Endianness::bit(pos);
87 }
88 
89 template <std::size_t N, typename Endianness>
90 prefix_ void senf::BitsetParser<N,Endianness>::flip()
91 {
92  for (std::size_t j(0); j<fixed_bytes; j++)
93  i()[j] = ~(i()[j]);
94 }
95 
96 template <std::size_t N, typename Endianness>
97 prefix_ void senf::BitsetParser<N,Endianness>::flip(std::size_t pos)
98 {
99  validatePostion(pos);
100  i()[Endianness::bytePosition(fixed_bytes, pos)] ^= Endianness::bit(pos);
101 }
102 
103 template <std::size_t N, typename Endianness>
104 prefix_ void senf::BitsetParser<N,Endianness>::set()
105 {
106  std::fill_n( i(), fixed_bytes, 0xff);
107 }
108 
109 template <std::size_t N, typename Endianness>
110 prefix_ void senf::BitsetParser<N,Endianness>::set(std::size_t pos, bool v)
111 {
112  validatePostion(pos);
113  if (v)
114  i()[Endianness::bytePosition(fixed_bytes, pos)] |= Endianness::bit(pos);
115  else
116  i()[Endianness::bytePosition(fixed_bytes, pos)] &= ~Endianness::bit(pos);
117 }
118 
119 template <std::size_t N, typename Endianness>
120 prefix_ bool senf::BitsetParser<N,Endianness>::test(std::size_t pos)
121  const
122 {
123  validatePostion(pos);
124  return i()[Endianness::bytePosition(fixed_bytes, pos)] & Endianness::bit(pos);
125 }
126 
127 template <std::size_t N, typename Endianness>
128 prefix_ bool senf::BitsetParser<N,Endianness>::operator[](std::size_t pos)
129  const
130 {
131  assertPostion(pos);
132  return i()[Endianness::bytePosition(fixed_bytes, pos)] & Endianness::bit(pos);
133 }
134 
135 template <std::size_t N, typename Endianness>
136 prefix_ typename senf::BitsetParser<N,Endianness>::reference senf::BitsetParser<N,Endianness>::operator[](std::size_t pos)
137 {
138  return reference(*this, pos);
139 }
140 
141 template <std::size_t N, typename Endianness>
142 prefix_ std::size_t senf::BitsetParser<N,Endianness>::size()
143  const
144 {
145  return N;
146 }
147 
148 template <std::size_t N, typename Endianness>
149 prefix_ std::size_t senf::BitsetParser<N,Endianness>::count()
150  const
151 {
152  std::size_t result = 0;
153  for (std::size_t j(0); j<N; j++)
154  result += (*this)[j];
155  return result;
156 }
157 
158 template <std::size_t N, typename Endianness>
159 prefix_ bool senf::BitsetParser<N,Endianness>::all()
160  const
161 {
162  for (std::size_t j(0); j<fixed_bytes; j++)
163  if (i()[j] != 0xffu) return false;
164  return true;
165 }
166 
167 template <std::size_t N, typename Endianness>
168 prefix_ bool senf::BitsetParser<N,Endianness>::any()
169  const
170 {
171  for (std::size_t j(0); j<fixed_bytes; j++)
172  if (i()[j] != 0x00) return true;
173  return false;
174 }
175 
176 template <std::size_t N, typename Endianness>
177 prefix_ bool senf::BitsetParser<N,Endianness>::none()
178  const
179 {
180  return ! this->any();
181 }
182 
183 template <std::size_t N, typename Endianness>
184 prefix_ void senf::BitsetParser<N,Endianness>::value(value_type const & bset)
185 {
186  for (std::size_t j(0); j<N; j++)
187  set(j, bset[j]);
188 }
189 
190 template <std::size_t N, typename Endianness>
191 prefix_ typename senf::BitsetParser<N,Endianness>::value_type senf::BitsetParser<N,Endianness>::value()
192  const
193 {
194  value_type v;
195  for (std::size_t j(0); j<N; j++)
196  v[j] = (*this)[j];
197  return v;
198 }
199 
200 #undef validatePostion
201 #undef assertPostion
202 
203 
204 //-/////////////////////////////////////////////////////////////////////////////////////////////////
205 #undef prefix_