header_utils
Loading...
Searching...
No Matches
flag_bits.h
1
4
5#pragma once
6
7#include "flag_bits_v.h"
8#include <bit>
9
10namespace ghassanpl
11{
15
19
20 namespace detail
21 {
22 template<typename T>
23 concept bit_integral = std::integral<T> && !std::same_as<std::decay_t<T>, bool>;
24
25 template<typename T>
26 concept valid_integral = bit_integral<T> || requires (T other, int t) {
27 { other &= t } -> bit_integral;
28 { other ^= t } -> bit_integral;
29 { other |= t } -> bit_integral;
30 { other & t } -> bit_integral;
31 { other ^ t } -> bit_integral;
32 { other | t } -> bit_integral;
33 };
34
35 template <typename RESULT_TYPE, typename... ENUM_TYPES>
36 concept valid_flag_bits_arguments = detail::valid_integral<RESULT_TYPE> && (integral_or_enum<ENUM_TYPES> && ...);
37
38 template <typename T>
39 requires std::is_integral_v<T>
40 [[nodiscard]] constexpr auto to_unsigned(T t) noexcept { return static_cast<std::make_unsigned_t<T>>(t); }
41 }
42
59 template <typename RESULT_TYPE = unsigned long long, typename... ARGS>
60 constexpr RESULT_TYPE flag_bits(ARGS... args) noexcept
61 requires detail::valid_flag_bits_arguments<RESULT_TYPE, ARGS...>
62 {
63 return ((RESULT_TYPE{ 1 } << detail::to_underlying_type(args)) | ... | 0);
64 }
65
66 template <typename RESULT_TYPE = unsigned long long, typename BIT_TYPE>
67 requires integral_or_enum<BIT_TYPE>
68 constexpr RESULT_TYPE bit_to_flag(BIT_TYPE bit) noexcept
69 {
70 if constexpr (std::is_enum_v<BIT_TYPE>)
71 return std::countr_zero(detail::to_unsigned(detail::to_underlying_type(bit)));
72 else
73 return std::countr_zero(detail::to_unsigned(bit));
74 }
75
77 template <typename INTEGRAL, typename ENUM_TYPE>
78 requires detail::valid_flag_bits_arguments<INTEGRAL, ENUM_TYPE>
79 constexpr bool is_flag_set(INTEGRAL const& bits, ENUM_TYPE flag) noexcept { return (bits & flag_bits<INTEGRAL>(flag)) != 0; }
80
82 template <typename INTEGRAL, typename... ARGS>
83 requires detail::valid_flag_bits_arguments<INTEGRAL, ARGS...>
84 constexpr bool are_any_flags_set(INTEGRAL const& bits, ARGS... args) noexcept
85 {
86 return (bits & flag_bits(args...)) != 0;
87 }
88
90 template <typename INTEGRAL, typename... ARGS>
91 requires detail::valid_flag_bits_arguments<INTEGRAL, ARGS...>
92 constexpr bool are_all_flags_set(INTEGRAL const& bits, ARGS... args) noexcept
93 {
94 return (bits & flag_bits(args...)) == flag_bits(args...);
95 }
96
98 template <typename INTEGRAL, typename... ARGS>
99 requires detail::valid_flag_bits_arguments<INTEGRAL, ARGS...>
100 constexpr void set_flags(INTEGRAL& bits, ARGS... args) noexcept { bits |= flag_bits<INTEGRAL>(args...); }
101
103 template <typename INTEGRAL, typename... ARGS>
104 requires detail::valid_flag_bits_arguments<INTEGRAL, ARGS...>
105 constexpr void unset_flags(INTEGRAL& bits, ARGS... args) noexcept { bits &= ~ flag_bits<INTEGRAL>(args...); }
106
108 template <typename INTEGRAL, typename... ARGS>
109 requires detail::valid_flag_bits_arguments<INTEGRAL, ARGS...>
110 constexpr void toggle_flags(INTEGRAL& bits, ARGS... args) noexcept { bits ^= flag_bits<INTEGRAL>(args...); }
111
113 template <typename INTEGRAL, typename... ARGS>
114 requires detail::valid_flag_bits_arguments<INTEGRAL, ARGS...>
115 constexpr void set_flags_to(INTEGRAL& bits, bool val, ARGS... args) noexcept
116 {
117 if (val)
118 bits |= flag_bits<INTEGRAL>(args...);
119 else
120 bits &= ~ flag_bits<INTEGRAL>(args...);
121 }
122
124}
constexpr bool is_flag_set(INTEGRAL const &bits, ENUM_TYPE flag) noexcept
Checks if an integral value has the bit at number represented by flag set.
Definition flag_bits.h:79
constexpr void set_flags_to(INTEGRAL &bits, bool val, ARGS... args) noexcept
Sets the bits at numbers represented by args in bits to either 0 or 1, depending on val.
Definition flag_bits.h:115
constexpr RESULT_TYPE flag_bits(ARGS... args) noexcept
Takes a list of enum values that represent bit numbers (not actual bit values) and returns a value wi...
Definition flag_bits.h:60
constexpr bool are_any_flags_set(INTEGRAL const &bits, ARGS... args) noexcept
Checks if an integral value has any of the bits at numbers represented by args set.
Definition flag_bits.h:84
constexpr void unset_flags(INTEGRAL &bits, ARGS... args) noexcept
Unsets the bits at numbers represented by args in bits.
Definition flag_bits.h:105
constexpr void set_flags(INTEGRAL &bits, ARGS... args) noexcept
Sets the bits at numbers represented by args in bits.
Definition flag_bits.h:100
constexpr bool are_all_flags_set(INTEGRAL const &bits, ARGS... args) noexcept
Checks if an integral value has all of the bits at numbers represented by args set.
Definition flag_bits.h:92
constexpr void toggle_flags(INTEGRAL &bits, ARGS... args) noexcept
Toggles the bits at numbers represented by args in bits.
Definition flag_bits.h:110
Primary namespace for everything in this library.
Definition align+rec2.h:10