header_utils
Loading...
Searching...
No Matches
flag_bits.h
1
4
5#pragma once
6
7#include "flag_bits_v.h"
8
9namespace ghassanpl
10{
14
18
19 namespace detail
20 {
21 template<typename T>
22 concept bit_integral = std::is_integral_v<T> && !std::is_same_v<std::decay_t<T>, bool>;
23
24 template<typename T>
25 concept valid_integral = bit_integral<T> || requires (T other, int t) {
26 { other &= t } -> bit_integral;
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 };
33
34 template <typename RESULT_TYPE, typename... ENUM_TYPES>
35 concept valid_flag_bits_arguments = detail::valid_integral<RESULT_TYPE> && (integral_or_enum<ENUM_TYPES> && ...);
36 }
37
54 template <typename RESULT_TYPE = unsigned long long, typename... ARGS>
55 constexpr RESULT_TYPE flag_bits(ARGS... args) noexcept
56 requires detail::valid_flag_bits_arguments<RESULT_TYPE, ARGS...>
57 {
58 return ((RESULT_TYPE{ 1 } << detail::to_underlying_type(args)) | ... | 0);
59 }
60
62 template <typename INTEGRAL, typename ENUM_TYPE>
63 requires detail::valid_flag_bits_arguments<INTEGRAL, ENUM_TYPE>
64 constexpr bool is_flag_set(INTEGRAL const& bits, ENUM_TYPE flag) noexcept { return (bits & flag_bits<INTEGRAL>(flag)) != 0; }
65
67 template <typename INTEGRAL, typename... ARGS>
68 requires detail::valid_flag_bits_arguments<INTEGRAL, ARGS...>
69 constexpr bool are_any_flags_set(INTEGRAL const& bits, ARGS... args) noexcept
70 {
71 return (bits & flag_bits(args...)) != 0;
72 }
73
75 template <typename INTEGRAL, typename... ARGS>
76 requires detail::valid_flag_bits_arguments<INTEGRAL, ARGS...>
77 constexpr bool are_all_flags_set(INTEGRAL const& bits, ARGS... args) noexcept
78 {
79 return (bits & flag_bits(args...)) == flag_bits(args...);
80 }
81
83 template <typename INTEGRAL, typename... ARGS>
84 requires detail::valid_flag_bits_arguments<INTEGRAL, ARGS...>
85 constexpr void set_flags(INTEGRAL& bits, ARGS... args) noexcept { bits |= flag_bits<INTEGRAL>(args...); }
86
88 template <typename INTEGRAL, typename... ARGS>
89 requires detail::valid_flag_bits_arguments<INTEGRAL, ARGS...>
90 constexpr void unset_flags(INTEGRAL& bits, ARGS... args) noexcept { bits &= ~ flag_bits<INTEGRAL>(args...); }
91
93 template <typename INTEGRAL, typename... ARGS>
94 requires detail::valid_flag_bits_arguments<INTEGRAL, ARGS...>
95 constexpr void toggle_flags(INTEGRAL& bits, ARGS... args) noexcept { bits ^= flag_bits<INTEGRAL>(args...); }
96
98 template <typename INTEGRAL, typename... ARGS>
99 requires detail::valid_flag_bits_arguments<INTEGRAL, ARGS...>
100 constexpr void set_flags_to(INTEGRAL& bits, bool val, ARGS... args) noexcept
101 {
102 if (val)
103 bits |= flag_bits<INTEGRAL>(args...);
104 else
105 bits &= ~ flag_bits<INTEGRAL>(args...);
106 }
107
109}
constexpr auto bit_count
Equal to the number of bits in the type.
Definition bits.h:33
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:55
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: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:77
constexpr void toggle_flags(INTEGRAL &bits, ARGS... args) noexcept
Toggles the bits at numbers represented by args in bits
Definition flag_bits.h:95
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:64
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:69
constexpr void set_flags(INTEGRAL &bits, ARGS... args) noexcept
Sets the bits at numbers represented by args in bits
Definition flag_bits.h:85
constexpr void unset_flags(INTEGRAL &bits, ARGS... args) noexcept
Unsets the bits at numbers represented by args in bits
Definition flag_bits.h:90
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