ALib C++ Library
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
bitwise_iterable_conversion.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 \ref mainpage_license "Boost Software License".
7//==================================================================================================
8#if DOXYGEN
10#else
11ALIB_EXPORT namespace alib {
12#endif
13
15
16/// Returns the bitwise enumeration element of \p{TEnum} from a given sequential enumeration.
17///
18/// Selected by the compiler only if \alib{enumops;BitwiseTraits} is specialized for
19/// template enum type \p{TEnum} to inherit \c std::true_type.
20///
21/// \see The reverse function #ToSequentialEnumeration.
22///
23/// @tparam TEnum An enumeration type which is defined to be "bitwise".
24/// @param number A sequentially enumerated number, for which the corresponding bitwise
25/// enumeration element is requested.
26/// \ref alib_enums_arithmetic_bitwise "bitwise enumeration".
27/// @return Returns enum-element <c>1 << number</c>.
28template<typename TEnum>
29requires alib::enumops::IsBitwise<TEnum>
30constexpr TEnum ToBitwiseEnumeration(typename std::underlying_type<TEnum>::type number ) {
31ALIB_ASSERT_ERROR( number >= 0
33 || enumops::IterableTraits<TEnum>::End > TEnum( 1 << number ) ),
34 "ENUMS", "Negative enum element-number given." )
35return TEnum( 1 << number );
36}
37
38
39/// Returns the sequentially enumerated number derived from the given bitwise enumeration
40/// value.
41/// In other words, the positon of the most significant bit set in the underlying integral of the
42/// given enum \p{element} is returned.<br>
43/// In debug-compilations an \alib_assertion is raised in case that the given value is not a
44/// single enum element but a combination of bits.
45///
46/// Selected by the compiler only if \alib{enumops;BitwiseTraits} is specialized for
47/// template enum type \p{TEnum} to inherit \c std::true_type.
48///
49/// \see The reverse function #ToBitwiseEnumeration.
50/// @tparam TEnum A bitwise defined enumeration type. Deduced by the compiler.
51/// @param element An enumeration value.
52/// @return The sequential number of an element of an enum type which is defined bitwise.
53template<typename TEnum>
55constexpr typename std::underlying_type<TEnum>::type ToSequentialEnumeration( TEnum element ) {
56ALIB_ASSERT_ERROR( UnderlyingIntegral(element) != 0, "ENUMS",
57 "No bits set in given enum value" )
59 "Multiple bits given with enum value" )
60return static_cast<typename std::underlying_type<TEnum>::type>(
61 lang::MSB( UnderlyingIntegral(element) ) - 1 );
62}
63
64
65} // namespace [alib] (doxygen [alib::enumops::bitwise])
66
67#include "ALib.Lang.CIMethods.H"
#define ALIB_EXPORT
Definition alib.inl:497
#define ALIB_ASSERT_ERROR(cond, domain,...)
Definition alib.inl:1066
constexpr TEnum ToBitwiseEnumeration(typename std::underlying_type< TEnum >::type number)
constexpr std::underlying_type< TEnum >::type ToSequentialEnumeration(TEnum element)
std::underlying_type_t< TEnum > constexpr UnderlyingIntegral(TEnum element) noexcept
constexpr int MSB(TIntegral value)
Definition bits.inl:314
constexpr int BitCount(TIntegral value)
Definition bits.inl:215
static constexpr TEnum End
Definition iterable.inl:67