ALib C++ Framework
by
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
bitwise.inl
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header-file is part of the module \alib_enumops of the \aliblong.
4///
5/// \emoji :copyright: 2013-2025 A-Worx GmbH, Germany.
6/// Published under #"mainpage_license".
7//==================================================================================================
8ALIB_EXPORT namespace alib {
9
10/// This is the reference documentation of sub-namespace \c enumops of the \aliblink, which
11/// holds types of library module \alib_enumops.
12///
13/// Extensive documentation for this module is provided with
14/// #"alib_mod_enums;ALib Module Enums - Programmer's Manual".
15///
16/// \attention
17/// All operators are declared in the global namespace, other than this
18/// namespace documentation indicates!
19namespace enumops {
20
21//##################################################################################################
22// struct BitwiseTraits
23//##################################################################################################
24
25//==================================================================================================
26/// Simple type trait that inherits <c>std::false_type</c> by default.
27/// If specialized for an enumeration type \p{TEnum} to inherit <c>std::true_type</c>, the
28/// following operators set of \alib operators become applicable to elements of \p{TEnum}:
29///
30/// - #"bitwise::operator&"
31/// - #"bitwise::operator&="
32/// - #"bitwise::operator|"
33/// - #"bitwise::operator|="
34/// - #"bitwise::operator^"
35/// - #"bitwise::operator^="
36/// - #"bitwise::operator~"
37/// - #"bitwise::operator+" (Alias for #"bitwise::operator|")
38/// - #"bitwise::operator-" (Alias for a combination of operators
39/// #"bitwise::operator&" and #"bitwise::operator~")
40/// - #"bitwise::operator+=" (An alias for #"bitwise::operator|=")
41/// - #"bitwise::operator-=" (Removes given bit(s) )
42///
43/// \attention
44/// Likewise with the operators introduced with the type trait #"ArithmeticalTraits",
45/// this documentation "fakes" the operators into namespace <c>alib::enumops</c>,
46/// while in fact they are defined in the global namespace.<br>
47/// See #"alib_enums_arithmetic_standard;corresponding note" in the Programmer's Manual
48/// for details.
49///
50/// <b>Restrictions</b><br>
51/// For technical reasons, this concept is not applicable to enum types that are defined as
52/// \c private or \c protected inner types of structs or classes.
53///
54/// \see
55/// - Macro #"ALIB_ENUMS_MAKE_BITWISE", which specializes this type trait for a given
56/// enumeration type.
57/// - For details and a source code sample see chapter #"alib_enums_arithmetic_bitwise"
58/// of the Programmer's Manual of the module \alib_enumops.
59///
60/// \I{#############################################################################################}
61/// # Restrictions #
62/// For technical reasons, this concept is not applicable to enum types that are defined as
63/// \c private or \c protected inner types of structs or classes.
64///
65/// # Reference Documentation #
66///
67/// @tparam TEnum The enum type to enable bitwise operators for.
68//==================================================================================================
69template<typename TEnum>
70requires std::is_enum<TEnum>::value
71struct BitwiseTraits : std::false_type {};
72
74
75/// Concept that is satisfied if the type trait #"BitwiseTraits"
76/// is specialized for type \p{TEnum} to inherit <c>std::true_type</c>.
77/// @tparam TEnum The type to test.
78template<typename TEnum>
80
82
83} // namespace alib[::enumops]
84} // namespace [alib]
85
86//##################################################################################################
87// Bitwise Operators
88//##################################################################################################
89// For documentation, all operators and enum related template functions are faked into namespace
90// alib::enumops
91#if DOXYGEN
92namespace alib { namespace enumops {
93/// Operators available to elements of enumerations if #"BitwiseTraits" is specialized.
94///
95/// \note
96/// This namespace exits only in the documentation to collect the operators.
97/// When parsed by a C++ compiler, <b>the operators reside in the global namespace</b> and
98/// functions #"bitwise::HasBits", #"bitwise::CountElements",
99/// #"bitwise::ToBitwiseEnumeration" and #"bitwise::ToSequentialEnumeration"
100/// reside in namespace #"alib".
101///
102/// As required, when parsed by a C++ compiler, the operators reside in the global namespace.
104#endif
105
106
107/// Bitwise \b and operator usable with scoped enum types.<br>
108/// Selected by the compiler only if #"BitwiseTraits" is specialized for
109/// template enum type \p{TEnum} to inherit \c std::true_type.
110///
111/// @tparam TEnum Enumeration type.
112/// @param lhs First operand.
113/// @param rhs Second operand.
114/// @return The result of a bitwise and operation of the underlying enum values.
116template<typename TEnum>
118constexpr TEnum operator& (TEnum lhs, TEnum rhs) noexcept {
119 using TBits= typename std::underlying_type<TEnum>::type;
120 return TEnum(TBits(lhs) & TBits(rhs));
121}
122
123/// Bitwise assignment operator usable with scoped enum types.<br>
124/// Selected by the compiler only if #"BitwiseTraits" is specialized for
125/// template enum type \p{TEnum} to inherit \c std::true_type.
126///
127/// @tparam TEnum Enumeration type.
128/// @param[in,out] lhs Reference to the first operand. Receives the result.
129/// @param rhs Second operand.
130/// @return The new value of \p{lhs} which is set to <c>lhs & rhs</c>.
132template<typename TEnum>
134constexpr TEnum operator&= (TEnum& lhs, TEnum rhs) noexcept {
135 using TBits= typename std::underlying_type<TEnum>::type;
136 return lhs= TEnum( TBits(lhs) & TBits(rhs) );
137}
138
139/// Bitwise \b or operator usable with scoped enum types.<br>
140/// Selected by the compiler only if #"BitwiseTraits" is specialized for
141/// template enum type \p{TEnum} to inherit \c std::true_type.
142///
143/// @tparam TEnum Enumeration type.
144/// @param lhs First operand.
145/// @param rhs Second operand.
146/// @return The result of a bitwise or operation of the underlying enum values.
148template<typename TEnum>
150constexpr TEnum operator| (TEnum lhs, TEnum rhs) noexcept {
151 using TBits= typename std::underlying_type<TEnum>::type;
152 return TEnum(TBits(lhs) | TBits(rhs));
153}
154
155/// Bitwise <b>or assignment</b> operator usable with scoped enum types.<br>
156/// Selected by the compiler only if #"BitwiseTraits" is specialized for
157/// template enum type \p{TEnum} to inherit \c std::true_type.
158///
159/// @tparam TEnum Enumeration type.
160/// @param[in,out] lhs Reference to the first operand. Receives the result.
161/// @param rhs Second operand.
162/// @return The new value of \p{lhs} which is set to <c>lhs | rhs</c>.
164template<typename TEnum>
166constexpr TEnum operator|= (TEnum& lhs, TEnum rhs) noexcept {
167 using TBits= typename std::underlying_type<TEnum>::type;
168 return lhs= TEnum( TBits(lhs) | TBits(rhs) );
169}
170
171/// Bitwise \b xor operator usable with scoped enum types.<br>
172/// Selected by the compiler only if #"BitwiseTraits" is specialized for
173/// template enum type \p{TEnum} to inherit \c std::true_type.
174///
175/// @tparam TEnum Enumeration type.
176/// @param lhs First operand.
177/// @param rhs Second operand.
178/// @return The result of a bitwise xor operation of the underlying enum values.
180template<typename TEnum>
182constexpr TEnum operator^ (TEnum lhs, TEnum rhs) noexcept {
183 using TBits= typename std::underlying_type<TEnum>::type;
184 return TEnum(TBits(lhs) ^ TBits(rhs));
185}
186
187/// Bitwise <b>xor assignment</b> operator usable with scoped enum types.<br>
188/// Selected by the compiler only if #"BitwiseTraits" is specialized for
189/// template enum type \p{TEnum} to inherit \c std::true_type.
190///
191/// @tparam TEnum Enumeration type.
192/// @param[in,out] lhs Reference to the first operand. Receives the result.
193/// @param rhs Second operand.
194/// @return The new value of \p{lhs} which is set to <c>lhs ^ rhs</c>.
196template<typename TEnum>
198constexpr TEnum operator^= (TEnum& lhs, TEnum rhs) noexcept {
199 using TBits= typename std::underlying_type<TEnum>::type;
200 return lhs= TEnum( TBits(lhs) ^ TBits(rhs) );
201}
202
203/// Bitwise \b not operator usable with scoped enum types.<br>
204/// Selected by the compiler only if #"BitwiseTraits" is specialized for
205/// template enum type \p{TEnum} to inherit \c std::true_type.
206///
207/// \note To remove one or more bits from a scoped enum value, operator <b>&=</b> with this operator
208/// applied to \p{op} can be used.
209/// A shortcut to this is given with #"bitwise::operator-=(TEnum&, TEnum)".
210///
211/// @tparam TEnum Enumeration type.
212/// @param op The operand to be negated.
213/// @return The result of a bitwise negation of \p{op}.
215template<typename TEnum>
217constexpr TEnum operator~ (TEnum op) noexcept {
218 using TBits= typename std::underlying_type<TEnum>::type;
219 return TEnum( ~ TBits(op) );
220}
221
222
223/// Alias to bitwise \b or operator usable with scoped enum types.<br>
224/// Selected by the compiler only if #"BitwiseTraits" is specialized for
225/// template enum type \p{TEnum} to inherit \c std::true_type and if #"ArithmeticalTraits"
226/// is \b not specialized to inherit \c std::true_type. The latter is to avoid ambiguities in
227/// situations where an enum is both, arithmetical and bitwise.
228///
229/// @tparam TEnum Enumeration type.
230/// @param lhs First operand.
231/// @param rhs Second operand.
232/// @return The result of a bitwise or operation of the underlying enum values.
234template<typename TEnum>
235requires ( alib::enumops::IsBitwise <TEnum>
237constexpr TEnum operator+ (TEnum lhs, TEnum rhs) noexcept {
238 using TBits= typename std::underlying_type<TEnum>::type;
239 return TEnum(TBits(lhs) | TBits(rhs));
240}
241
242/// Alias for bitwise <b>or assignment</b> operator usable with scoped enum types.<br>
243/// Selected by the compiler only if #"BitwiseTraits" is specialized for
244/// template enum type \p{TEnum} to inherit \c std::true_type and if #"ArithmeticalTraits"
245/// is \b not specialized to inherit \c std::true_type. The latter is to avoid ambiguities in
246/// situations where an enum is both, arithmetical and bitwise.
247///
248/// @tparam TEnum Enumeration type.
249/// @param[in,out] lhs Reference to the first operand. Receives the result.
250/// @param rhs Second operand.
251/// @return The new value of \p{lhs} which is set to <c>lhs | rhs</c>.
253template<typename TEnum>
254requires ( alib::enumops::IsBitwise <TEnum>
256constexpr TEnum operator+= (TEnum& lhs, TEnum rhs) noexcept {
257 using TBits= typename std::underlying_type<TEnum>::type;
258 return lhs= TEnum( TBits(lhs) | TBits(rhs) );
259}
260
261/// Removes bit(s) found in \p{rhs} from \p{lhs} an returns result. This is a shortcut to:
262///
263/// lhs & !rhs
264///
265/// Selected by the compiler only if #"BitwiseTraits" is specialized for
266/// template enum type \p{TEnum} to inherit \c std::true_type and if #"ArithmeticalTraits"
267/// is \b not specialized to inherit \c std::true_type. The latter is to avoid ambiguities in
268/// situations where an enum is both, arithmetical and bitwise.
269///
270/// @tparam TEnum Enumeration type.
271/// @param lhs First operand.
272/// @param rhs Second operand.
273/// @return The result of <c>lhs & !rhs</c>.
275template<typename TEnum>
276requires ( alib::enumops::IsBitwise <TEnum>
278constexpr TEnum operator- (TEnum lhs, TEnum rhs) noexcept {
279 using TBits= typename std::underlying_type<TEnum>::type;
280 return TEnum( TBits(lhs) & (~TBits(rhs)) );
281}
282
283/// Removes bit(s) found in \p{rhs} from \p{lhs}. This is a shortcut to:
284///
285/// lhs &= !rhs
286///
287/// Selected by the compiler only if #"BitwiseTraits" is specialized for
288/// template enum type \p{TEnum} to inherit \c std::true_type and if #"ArithmeticalTraits"
289/// is \b not specialized to inherit \c std::true_type. The latter is to avoid ambiguities in
290/// situations where an enum is both, arithmetical and bitwise.
291///
292/// @tparam TEnum Enumeration type.
293/// @param[in,out] lhs Reference to the first operand. Receives the result.
294/// @param rhs Second operand.
295/// @return The new value of \p{lhs} which is set to <c>lhs & ( ~rhs )</c>.
297template<typename TEnum>
298requires ( alib::enumops::IsBitwise <TEnum>
300constexpr TEnum operator-= (TEnum& lhs, TEnum rhs) noexcept {
301 using TBits= typename std::underlying_type<TEnum>::type;
302 return lhs= TEnum( TBits(lhs) & (~TBits(rhs)) );
303}
304
305#if !DOXYGEN
306ALIB_EXPORT namespace alib {
307#endif
308
309/// Tests if the integral value of the given enum \p{element} contains all bits set in
310/// \p{selection}.
311/// In other words, returns result of:
312///
313/// ( element & selection ) == selection
314///
315/// Selected by the compiler only if #"BitwiseTraits" is specialized for
316/// template enum type \p{TEnum} to inherit \c std::true_type.
317///
318/// @tparam TEnum Enumeration type. Deduced by the compiler.
319/// @param element Bitset to be tested.
320/// @param selection Second operand.
321/// @return \c true if all bits of \p{selection} are set in \p{element}.
322template<typename TEnum>
324constexpr bool HasBits(TEnum element, TEnum selection) noexcept {
325 using TBits= typename std::underlying_type<TEnum>::type;
326 return ( TBits(element) & TBits(selection) ) == TBits(selection);
327}
328
329/// Tests if the integral value of the given enum \p{element} contains at least one of the bits
330/// set in \p{selection}.
331/// In other words, returns result of:
332///
333/// ( element & selection ) != 0
334///
335/// Selected by the compiler only if #"BitwiseTraits" is specialized for
336/// template enum type \p{TEnum} to inherit \c std::true_type.
337///
338/// @tparam TEnum Enumeration type. Deduced by the compiler.
339/// @param element Bitset to be tested.
340/// @param selection Second operand.
341/// @return \c true if one of the bits of \p{selection} are set in \p{element}.
342template<typename TEnum>
344constexpr bool HasOneOf(TEnum element, TEnum selection) noexcept {
345 using TBits= typename std::underlying_type<TEnum>::type;
346 return ( TBits(element) & TBits(selection) ) != TBits(0);
347}
348
349/// Returns the number of bitwise enumeration elements set in the given value.
350/// In other words, the bits given in \p{value} are counted and the number is returned.
351///
352/// Selected by the compiler only if #"BitwiseTraits" is specialized for
353/// template enum type \p{TEnum} to inherit \c std::true_type.
354///
355/// @param value A single or composite selection of bits.
356/// @return The result of a call to #"alib::lang::BitCount;2".
357template<typename TEnum>
359constexpr int CountElements( TEnum value ) { return lang::BitCount(UnderlyingIntegral(value)); }
360
361#if DOXYGEN
362}}} // doxygen namespace [alib::enumops::bitwise]
363#else
364} // namespace [alib]
365#endif
#define ALIB_ALLOW_DOCS
Definition alib.inl:662
#define ALIB_POP_ALLOWANCE
Definition alib.inl:673
#define ALIB_EXPORT
Definition alib.inl:562
ALIB_EXPORT constexpr TEnum operator|=(TEnum &lhs, TEnum rhs) noexcept
Definition bitwise.inl:166
ALIB_EXPORT constexpr TEnum operator~(TEnum op) noexcept
Definition bitwise.inl:217
ALIB_EXPORT constexpr TEnum operator&=(TEnum &lhs, TEnum rhs) noexcept
Definition bitwise.inl:134
ALIB_EXPORT constexpr TEnum operator|(TEnum lhs, TEnum rhs) noexcept
Definition bitwise.inl:150
constexpr bool HasOneOf(TEnum element, TEnum selection) noexcept
Definition bitwise.inl:344
ALIB_EXPORT constexpr TEnum operator^=(TEnum &lhs, TEnum rhs) noexcept
Definition bitwise.inl:198
ALIB_EXPORT constexpr TEnum operator^(TEnum lhs, TEnum rhs) noexcept
Definition bitwise.inl:182
constexpr int CountElements(TEnum value)
Definition bitwise.inl:359
ALIB_EXPORT constexpr TEnum operator&(TEnum lhs, TEnum rhs) noexcept
Definition bitwise.inl:118
constexpr bool HasBits(TEnum element, TEnum selection) noexcept
Definition bitwise.inl:324
std::underlying_type_t< TEnum > constexpr UnderlyingIntegral(TEnum element) noexcept
constexpr int BitCount(TIntegral value)
Definition bits.inl:210