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
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
15 \brief BitsetParser inline template implementation */
18 #include <senf/Utils/senfassert.hh>
20 #define prefix_ inline
21 //-/////////////////////////////////////////////////////////////////////////////////////////////////
23 //-/////////////////////////////////////////////////////////////////////////////////////////////////
24 // senf::BitsetParser::reference
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)
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)
38 parser_.set(pos_, other);
42 template <std::size_t N, typename Endianness>
43 prefix_ bool senf::BitsetParser<N,Endianness>::reference::operator~()
46 return ! parser_.test(pos_);
49 template <std::size_t N, typename Endianness>
50 prefix_ senf::BitsetParser<N,Endianness>::reference::operator bool()
53 return parser_.test(pos_);
56 template <std::size_t N, typename Endianness>
57 prefix_ typename senf::BitsetParser<N,Endianness>::reference & senf::BitsetParser<N,Endianness>::reference::flip()
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)
68 //-/////////////////////////////////////////////////////////////////////////////////////////////////
71 #define validatePostion(pos) if (pos > N-1) \
72 throw make_WrapException(std::out_of_range("invalid position in bitset"))
74 #define assertPostion(pos) SENF_ASSERT( pos <= N-1, "invalid position in bitset");
76 template <std::size_t N, typename Endianness>
77 prefix_ void senf::BitsetParser<N,Endianness>::reset()
79 std::fill_n( i(), fixed_bytes, 0x00);
82 template <std::size_t N, typename Endianness>
83 prefix_ void senf::BitsetParser<N,Endianness>::reset(std::size_t pos)
86 i()[Endianness::bytePosition(fixed_bytes, pos)] &= ~Endianness::bit(pos);
89 template <std::size_t N, typename Endianness>
90 prefix_ void senf::BitsetParser<N,Endianness>::flip()
92 for (std::size_t j(0); j<fixed_bytes; j++)
96 template <std::size_t N, typename Endianness>
97 prefix_ void senf::BitsetParser<N,Endianness>::flip(std::size_t pos)
100 i()[Endianness::bytePosition(fixed_bytes, pos)] ^= Endianness::bit(pos);
103 template <std::size_t N, typename Endianness>
104 prefix_ void senf::BitsetParser<N,Endianness>::set()
106 std::fill_n( i(), fixed_bytes, 0xff);
109 template <std::size_t N, typename Endianness>
110 prefix_ void senf::BitsetParser<N,Endianness>::set(std::size_t pos, bool v)
112 validatePostion(pos);
114 i()[Endianness::bytePosition(fixed_bytes, pos)] |= Endianness::bit(pos);
116 i()[Endianness::bytePosition(fixed_bytes, pos)] &= ~Endianness::bit(pos);
119 template <std::size_t N, typename Endianness>
120 prefix_ bool senf::BitsetParser<N,Endianness>::test(std::size_t pos)
123 validatePostion(pos);
124 return i()[Endianness::bytePosition(fixed_bytes, pos)] & Endianness::bit(pos);
127 template <std::size_t N, typename Endianness>
128 prefix_ bool senf::BitsetParser<N,Endianness>::operator[](std::size_t pos)
132 return i()[Endianness::bytePosition(fixed_bytes, pos)] & Endianness::bit(pos);
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)
138 return reference(*this, pos);
141 template <std::size_t N, typename Endianness>
142 prefix_ std::size_t senf::BitsetParser<N,Endianness>::size()
148 template <std::size_t N, typename Endianness>
149 prefix_ std::size_t senf::BitsetParser<N,Endianness>::count()
152 std::size_t result = 0;
153 for (std::size_t j(0); j<N; j++)
154 result += (*this)[j];
158 template <std::size_t N, typename Endianness>
159 prefix_ bool senf::BitsetParser<N,Endianness>::all()
162 for (std::size_t j(0); j<fixed_bytes; j++)
163 if (i()[j] != 0xffu) return false;
167 template <std::size_t N, typename Endianness>
168 prefix_ bool senf::BitsetParser<N,Endianness>::any()
171 for (std::size_t j(0); j<fixed_bytes; j++)
172 if (i()[j] != 0x00) return true;
176 template <std::size_t N, typename Endianness>
177 prefix_ bool senf::BitsetParser<N,Endianness>::none()
180 return ! this->any();
183 template <std::size_t N, typename Endianness>
184 prefix_ void senf::BitsetParser<N,Endianness>::value(value_type const & bset)
186 for (std::size_t j(0); j<N; j++)
190 template <std::size_t N, typename Endianness>
191 prefix_ typename senf::BitsetParser<N,Endianness>::value_type senf::BitsetParser<N,Endianness>::value()
195 for (std::size_t j(0); j<N; j++)
200 #undef validatePostion
204 //-/////////////////////////////////////////////////////////////////////////////////////////////////