header_utils
Loading...
Searching...
No Matches
flag_bits_v.h
1
4
5#pragma once
6
7#include <type_traits>
8#include <climits>
9
10#if !__has_cpp_attribute(nodiscard)
11#error "This library requires [[nodiscard]]"
12#endif
13
14namespace ghassanpl
15{
16 template<typename T>
17 concept integral_or_enum = (std::is_integral_v<T> && !std::is_same_v<std::decay_t<T>, bool>) || std::is_enum_v<T>;
18
19 namespace detail
20 {
21 template <typename T>
22 requires integral_or_enum<T>
23 constexpr auto to_underlying_type(T t) noexcept
24 {
25 if constexpr (std::is_enum_v<T>)
26 return static_cast<std::underlying_type_t<T>>(t);
27 else
28 return t;
29 }
30
31 template <typename INT_TYPE, auto BIT_NUM>
32 concept allowed_bit_num = BIT_NUM >= 0 && BIT_NUM < CHAR_BIT * sizeof(INT_TYPE);
33
34 template <typename RESULT_TYPE, auto... VALUES>
35 concept valid_flag_bits_v_arguments =
36 std::is_integral_v<RESULT_TYPE> && !std::is_same_v<std::decay_t<RESULT_TYPE>, bool> &&
37 (integral_or_enum<decltype(VALUES)> && ...) &&
38 (detail::allowed_bit_num<RESULT_TYPE, static_cast<decltype(detail::to_underlying_type(VALUES))>(VALUES)> && ...);
39 }
40
41 template <typename RESULT_TYPE, auto... VALUES>
42 requires detail::valid_flag_bits_v_arguments<RESULT_TYPE, VALUES...>
43 constexpr inline RESULT_TYPE flag_bits_v = ((RESULT_TYPE(1) << (detail::to_underlying_type(VALUES))) | ... | 0);
44
45 template <auto INT_VALUE, auto VALUE>
46 requires detail::valid_flag_bits_v_arguments<decltype(INT_VALUE), VALUE>
47 constexpr inline auto is_flag_set_v = (INT_VALUE & flag_bits_v<decltype(INT_VALUE), VALUE>) != 0;
48
49 template <auto INT_VALUE, auto... VALUES>
50 requires detail::valid_flag_bits_v_arguments<decltype(INT_VALUE), VALUES...>
51 constexpr inline auto are_any_flags_set_v = (INT_VALUE & flag_bits_v<decltype(INT_VALUE), VALUES...>) != decltype(INT_VALUE){};//((is_flag_set_v<INT_VALUE, VALUES>) || ...);
52
53 template <auto INT_VALUE, auto... VALUES>
54 requires detail::valid_flag_bits_v_arguments<decltype(INT_VALUE), VALUES...>
55 constexpr inline auto are_all_flags_set_v = (INT_VALUE & flag_bits_v<decltype(INT_VALUE), VALUES...>) == flag_bits_v<decltype(INT_VALUE), VALUES...>;//((is_flag_set_v<INT_VALUE, VALUES>) && ...);
56
57 template <auto INT_VALUE, auto... VALUES>
58 requires detail::valid_flag_bits_v_arguments<decltype(INT_VALUE), VALUES...>
59 constexpr inline auto set_flag_v = INT_VALUE | (flag_bits_v<decltype(INT_VALUE), VALUES...>);
60
61 template <auto INT_VALUE, auto... VALUES>
62 requires detail::valid_flag_bits_v_arguments<decltype(INT_VALUE), VALUES...>
63 constexpr inline auto unset_flag_v = INT_VALUE & ~(flag_bits_v<decltype(INT_VALUE), VALUES...>);
64
65 template <auto INT_VALUE, auto... VALUES>
66 requires detail::valid_flag_bits_v_arguments<decltype(INT_VALUE), VALUES...>
67 constexpr inline auto toggle_flag_v = INT_VALUE ^ (flag_bits_v<decltype(INT_VALUE), VALUES...>);
68
69 template <auto INT_VALUE, bool TO, auto... VALUES>
70 requires detail::valid_flag_bits_v_arguments<decltype(INT_VALUE), VALUES...>
71 constexpr inline auto set_flag_to_v = TO ?
72 (INT_VALUE | (flag_bits_v<decltype(INT_VALUE), VALUES...>)) :
73 (INT_VALUE & ~(flag_bits_v<decltype(INT_VALUE), VALUES...>));
74}
constexpr auto bit_count
Equal to the number of bits in the type.
Definition bits.h:33
The below code is based on Sun's libm library code, which is licensed under the following license:
Primary namespace for everything in this library.
Definition align+rec2.h:10