ALib C++ Library
Library Version: 2510 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
11 ALIB_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 )
31{
32 ALIB_ASSERT_ERROR( number >= 0
34 || enumops::IterableTraits<TEnum>::End > TEnum( 1 << number ) ),
35 "ENUMS", "Negative enum element-number given." )
36 return TEnum( 1 << number );
37}
38
39
40/// Returns the sequentially enumerated number derived from the given bitwise enumeration
41/// value.
42/// In other words, the positon of the most significant bit set in the underlying integral of the
43/// given enum \p element is returned.<br>
44/// In debug-compilations an \alib assertion is raised in case that the given value is not a
45/// single enum element but a combination of bits.
46///
47/// Selected by the compiler only if \alib{enumops;BitwiseTraits} is specialized for
48/// template enum type \p{TEnum} to inherit \c std::true_type.
49///
50/// \see The reverse function #ToBitwiseEnumeration.
51/// @tparam TEnum A bitwise defined enumeration type. Deduced by the compiler.
52/// @param element An enumeration value.
53/// @return The sequential number of an element of an enum type which is defined bitwise.
54template<typename TEnum>
56constexpr typename std::underlying_type<TEnum>::type ToSequentialEnumeration( TEnum element )
57{
58 ALIB_ASSERT_ERROR( UnderlyingIntegral(element) != 0, "ENUMS",
59 "No bits set in given enum value" )
61 "Multiple bits given with enum value" )
62 return static_cast<typename std::underlying_type<TEnum>::type>(
63 lang::MSB( UnderlyingIntegral(element) ) - 1 );
64}
65
66
67} // namespace [alib] (doxygen [alib::enumops::bitwise])
68
69#include "ALib.Lang.CIMethods.H"
70
71
#define ALIB_EXPORT
Definition alib.inl:488
#define ALIB_ASSERT_ERROR(cond, domain,...)
Definition alib.inl:1049
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:325
constexpr int BitCount(TIntegral value)
Definition bits.inl:222
static constexpr TEnum End
Definition iterable.inl:67