RESTinio
bitops.hpp
Go to the documentation of this file.
1 /*
2  * restinio
3  */
4 
5 /*!
6  * Helpers for extraction group of bits from numbers.
7  */
8 
9 #pragma once
10 
11 #include <cstdint>
12 
13 namespace restinio {
14 
15 namespace utils{
16 
17 namespace impl {
18 
19 namespace bitops {
20 
21 namespace details {
22 
23 template< typename T >
24 constexpr T mask( unsigned bits_to_extract )
25 {
26  return bits_to_extract <= 1u ? T{1} :
27  static_cast<T>((mask<T>(bits_to_extract-1) << 1) | T{1});
28 }
29 
30 template< typename T >
31 struct bits_count;
32 
33 template<>
34 struct bits_count<std::uint8_t> { static constexpr unsigned count = 8u; };
35 
36 template<>
37 struct bits_count<std::int8_t> { static constexpr unsigned count = 8u; };
38 
39 template<>
40 struct bits_count<char> { static constexpr unsigned count = 8u; };
41 
42 template<>
43 struct bits_count<std::uint16_t> { static constexpr unsigned count = 16u; };
44 
45 template<>
46 struct bits_count<std::int16_t> { static constexpr unsigned count = 16u; };
47 
48 template<>
49 struct bits_count<std::uint32_t> { static constexpr unsigned count = 32u; };
50 
51 template<>
52 struct bits_count<std::int32_t> { static constexpr unsigned count = 32u; };
53 
54 template<>
55 struct bits_count<std::uint64_t> { static constexpr unsigned count = 64u; };
56 
57 template<>
58 struct bits_count<std::int64_t> { static constexpr unsigned count = 64u; };
59 
60 } /* namespace details */
61 
62 /*!
63  * \brief Extract N bits from a bigger integer value.
64  *
65  * Usage example:
66  * \code
67  * // Extract 8 bits as unsigned char from bits 24..31 in uint32_t.
68  * const std::uint32_t v1 = some_uint_value();
69  * const auto u8 = n_bits_from<std::uint8_t, 24>(v1);
70  *
71  * // Extract 6 bits as char from bits 12..17 in uint32_t.
72  * const auto ch = n_bits_from<char, 12, 6>(v1);
73  *
74  * // Extract 4 bits as unsigned int from bits 32..35 in uint64_t.
75  * const std::uint64_t v2 = some_uint64_value();
76  * const auto ui = n_bits_from<unsigned int, 32, 4>(v2);
77  * \endcode
78  *
79  */
80 template<
81  typename T,
82  unsigned Shift,
83  unsigned Bits_To_Extract = details::bits_count<T>::count,
84  typename F = unsigned int >
85 T
86 n_bits_from( F value )
87 {
88  return static_cast<T>(value >> Shift) & details::mask<T>(Bits_To_Extract);
89 }
90 
91 } /* namespace bitops */
92 
93 } /* namespace impl */
94 
95 } /* namespace utils */
96 
97 } /* namespace restinio */
string_view_t from_string< string_view_t >(string_view_t s)
Get a value from string_view.
T n_bits_from(F value)
Extract N bits from a bigger integer value.
Definition: bitops.hpp:86
std::enable_if< std::is_same< Parameter_Container, query_string_params_t >::value||std::is_same< Parameter_Container, router::route_params_t >::value, std::optional< Value_Type > >::type opt_value(const Parameter_Container &params, string_view_t key)
Gets the value of a parameter specified by key wrapped in std::optional<Value_Type> if parameter exis...
Definition: value_or.hpp:64
constexpr T mask(unsigned bits_to_extract)
Definition: bitops.hpp:24