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