Vanetza
 
Loading...
Searching...
No Matches
bit_number.hpp
1#ifndef BIT_NUMBER_HPP_H3ODBQR7
2#define BIT_NUMBER_HPP_H3ODBQR7
3
4#include <boost/operators.hpp>
5#include <cstddef>
6#include <type_traits>
7
8namespace vanetza {
9
10/**
11 * BitNumber restricts the value range by the given width
12 *
13 * \tparam T underlying integral type
14 * \tparam WIDTH number of bits used to represent a number
15 */
16template<typename T, std::size_t WIDTH>
17class BitNumber : public boost::totally_ordered<BitNumber<T, WIDTH>>
18{
19 static_assert(std::is_integral<T>::value == true,
20 "only integral types are supported");
21 static_assert(sizeof(T) * 8 > WIDTH,
22 "width has to be less than size of underlying type");
23
24public:
25 static constexpr T mask = (1 << WIDTH) - 1; /**< excessive bits are masked */
26 static constexpr std::size_t bits = WIDTH; /**< number of bits used */
27 typedef T value_type; /**< underlying (integral) value type */
28
29 BitNumber() : mValue(0) {}
30 BitNumber(T value) : mValue(value & mask) {}
31 BitNumber& operator=(T value) { mValue = value & mask; return *this; }
32 T raw() const { return mValue; }
33
34 bool operator<(BitNumber other) const { return mValue < other.mValue; }
35 bool operator==(BitNumber other) const { return mValue == other.mValue; }
36
37private:
38 T mValue;
39};
40
41} // namespace vanetza
42
43#endif /* BIT_NUMBER_HPP_H3ODBQR7 */
44
static constexpr T mask
Definition: bit_number.hpp:25
static constexpr std::size_t bits
Definition: bit_number.hpp:26