ALib C++ Library
Library Version: 2402 R1
Documentation generated by doxygen
Loading...
Searching...
No Matches
bitwise_iterable_conversion.hpp
Go to the documentation of this file.
1/** ************************************************************************************************
2 * \file
3 * This header file is part of module \alib_enums of the \aliblong.
4 *
5 * \emoji :copyright: 2013-2024 A-Worx GmbH, Germany.
6 * Published under \ref mainpage_license "Boost Software License".
7 **************************************************************************************************/
8#ifndef HPP_ALIB_ENUMS_BW_IT_CONVERSION
9#define HPP_ALIB_ENUMS_BW_IT_CONVERSION 1
10
11#if !defined(HPP_ALIB) && !defined(ALIB_DOX)
12# include "alib/alib.hpp"
13#endif
14
16
17#if !defined (HPP_ALIB_ENUMS_ITERABLE)
19#endif
20
21#if !defined(HPP_ALIB_LANG_BITS)
22# include "alib/lang/bits.hpp"
23#endif
24
25#if defined(ALIB_DOX)
26namespace alib::enums::bitwise {
27#else
28namespace alib {
29#endif
30
31#if defined(ALIB_DOX)
32/**
33 * Returns the bitwise enumeration element of \p TEnum from a given sequential enumeration.
34 *
35 * Selected by the compiler only if \alib{enums;T_EnumIsBitwise} is specialized for
36 * template enum type \p{TEnum} to inherit \c std::true_type.
37 *
38 * \see The reverse function #ToSequentialEnumeration.
39 *
40 * @tparam TEnum An enumeration type which is defined to be "bitwise".
41 * @param number A sequentially enumerated number, for which the corresponding bitwise
42 * enumeration element is requested.
43 * \ref alib_enums_arithmetic_bitwise "bitwise enumeration".
44 * @return Returns <c>1 << number</c>.
45 */
46template<typename TEnum>
47constexpr inline
48TEnum ToBitwiseEnumeration(typename std::underlying_type<TEnum>::type number );
49
50#else
51
52template<typename TEnum>
53constexpr
55ToBitwiseEnumeration(typename std::underlying_type<TEnum>::type number )
56{
57 #if !defined (HPP_ALIB_ENUMS_ITERABLE)
58 ALIB_ASSERT_ERROR( number >= 0
60 || enums::T_EnumIsIterable<TEnum>::End > TEnum( 1 << number ) ),
61 "ENUMS", "Number out of bounds." )
62 #endif
63 return TEnum( 1 << number );
64}
65#endif
66
67#if defined(ALIB_DOX)
68
69/**
70 * Returns the sequentially enumerated number derived from the given bitwise enumeration
71 * value.
72 * In other words, the positon of the most significant bit set in the underlying integral of the
73 * given enum \p element is returned.<br>
74 * In debug-compilations an \alib assertion is raised in case that the given value is not a
75 * single enum element but a combination of bits.
76 *
77 * Selected by the compiler only if \alib{enums;T_EnumIsBitwise} is specialized for
78 * template enum type \p{TEnum} to inherit \c std::true_type.
79 *
80 * \see The reverse function #ToBitwiseEnumeration.
81 * @tparam TEnum A bitwise defined enumeration type. Deduced by the compiler.
82 * @param element An enumeration value.
83 * @return The sequential number of an element of an enum type which is defined bitwise.
84 */
85template<typename TEnum>
86static constexpr inline
87typename std::underlying_type<TEnum>::type
88ToSequentialEnumeration( TEnum element );
89#else
90template<typename TEnum>
91constexpr
92ATMP_T_IF(typename std::underlying_type<TEnum>::type, alib::enums::T_EnumIsBitwise<TEnum>::value)
93ToSequentialEnumeration( TEnum element )
94{
95 ALIB_ASSERT_ERROR( UnderlyingIntegral(element) != 0, "ENUMS",
96 "No bits set in given enum value" )
97 ALIB_ASSERT_ERROR( lang::BitCount(UnderlyingIntegral(element)) == 1, "ENUMS",
98 "Multiple bits given with enum value" )
99 return static_cast<typename std::underlying_type<TEnum>::type>(
100 lang::MSB( UnderlyingIntegral(element) ) - 1 );
101}
102#endif
103
104
105} // namespace [alib] (doxygen [alib::enums::bitwise])
106
107#endif // HPP_ALIB_ENUMS_BW_IT_CONVERSION
#define ALIB_ASSERT_MODULE(modulename)
Definition alib.hpp:190
#define ALIB_ASSERT_ERROR(cond,...)
Definition alib.hpp:984
#define ATMP_T_IF(T, Cond)
Definition tmp.hpp:53
constexpr TEnum ToBitwiseEnumeration(typename std::underlying_type< TEnum >::type number)
static constexpr std::underlying_type< TEnum >::type ToSequentialEnumeration(TEnum element)
constexpr std::underlying_type< TEnum >::type UnderlyingIntegral(TEnum element) noexcept(true)
Definition alib.cpp:57