ALib C++ Library
Library Version: 2412 R0
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#pragma once
11#if !defined(DOXYGEN)
12# include "alib/alib.hpp"
13#endif
14
16
17#include "alib/enums/iterable.hpp"
18#include "alib/lang/bits.hpp"
19
20#if DOXYGEN
21 namespace alib::enums::bitwise {
22#else
23 namespace alib {
24#endif
25
27#if DOXYGEN
28/// Returns the bitwise enumeration element of \p TEnum from a given sequential enumeration.
29///
30/// Selected by the compiler only if \alib{enums;T_EnumIsBitwise} is specialized for
31/// template enum type \p{TEnum} to inherit \c std::true_type.
32///
33/// \see The reverse function #ToSequentialEnumeration.
34///
35/// @tparam TEnum An enumeration type which is defined to be "bitwise".
36/// @param number A sequentially enumerated number, for which the corresponding bitwise
37/// enumeration element is requested.
38/// \ref alib_enums_arithmetic_bitwise "bitwise enumeration".
39/// @return Returns <c>1 << number</c>.
40template<typename TEnum>
41constexpr inline
42TEnum ToBitwiseEnumeration(typename std::underlying_type<TEnum>::type number );
43
44#else
45
46template<typename TEnum>
47constexpr
49ToBitwiseEnumeration(typename std::underlying_type<TEnum>::type number )
50{
51 #if !defined (HPP_ALIB_ENUMS_ITERABLE)
52 ALIB_ASSERT_ERROR( number >= 0
54 || enums::T_EnumIsIterable<TEnum>::End > TEnum( 1 << number ) ),
55 "ENUMS", "Number out of bounds." )
56 #endif
57 return TEnum( 1 << number );
58}
59#endif
60
61#if DOXYGEN
62
63/// Returns the sequentially enumerated number derived from the given bitwise enumeration
64/// value.
65/// In other words, the positon of the most significant bit set in the underlying integral of the
66/// given enum \p element is returned.<br>
67/// In debug-compilations an \alib assertion is raised in case that the given value is not a
68/// single enum element but a combination of bits.
69///
70/// Selected by the compiler only if \alib{enums;T_EnumIsBitwise} is specialized for
71/// template enum type \p{TEnum} to inherit \c std::true_type.
72///
73/// \see The reverse function #ToBitwiseEnumeration.
74/// @tparam TEnum A bitwise defined enumeration type. Deduced by the compiler.
75/// @param element An enumeration value.
76/// @return The sequential number of an element of an enum type which is defined bitwise.
77template<typename TEnum>
78static constexpr inline
79typename std::underlying_type<TEnum>::type
80ToSequentialEnumeration( TEnum element );
81#else
82template<typename TEnum>
83constexpr
84ATMP_T_IF(typename std::underlying_type<TEnum>::type, alib::enums::T_EnumIsBitwise<TEnum>::value)
85ToSequentialEnumeration( TEnum element )
86{
87 ALIB_ASSERT_ERROR( UnderlyingIntegral(element) != 0, "ENUMS",
88 "No bits set in given enum value" )
89 ALIB_ASSERT_ERROR( lang::BitCount(UnderlyingIntegral(element)) == 1, "ENUMS",
90 "Multiple bits given with enum value" )
91 return static_cast<typename std::underlying_type<TEnum>::type>(
92 lang::MSB( UnderlyingIntegral(element) ) - 1 );
93}
94#endif
95
96
97} // namespace [alib] (doxygen [alib::enums::bitwise])
98
100
101#endif // HPP_ALIB_ENUMS_BW_IT_CONVERSION
102
#define ALIB_ASSERT_MODULE(modulename)
Definition alib.hpp:223
#define ALIB_ASSERT_ERROR(cond,...)
Definition alib.hpp:1271
#define ATMP_T_IF(T, Cond)
Definition tmp.hpp:49
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
Definition alib.cpp:69