31#ifndef DW_FRAMEWORK_ENUMDESCRIPTOR_HPP_
32#define DW_FRAMEWORK_ENUMDESCRIPTOR_HPP_
34#include <dw/core/base/Status.h>
35#include <dwshared/dwfoundation/dw/core/container/StringView.hpp>
43#define DW_ENUMERATOR_NAME_STRING_VIEW(NAME_STR) \
44 dw::core::StringView { NAME_STR }
54#define DW_DESCRIBE_ENUMERATOR(NAME) describeEnumerator(DW_ENUMERATOR_NAME_STRING_VIEW(#NAME), EnumT::NAME)
62#define DW_DESCRIBE_C_ENUMERATOR(NAME) describeEnumerator(DW_ENUMERATOR_NAME_STRING_VIEW(#NAME), (NAME))
71template <
typename EnumT,
size_t NumberOfEnumerators>
74template <
typename EnumT>
77 static_assert(std::is_enum<EnumT>::value,
"EnumT must be an enumeration type");
90 static_assert(
sizeof(EnumT) == 0,
"Needs specialization for specific enum type");
91 constexpr const size_t numberOfEnumerators = 0;
109template <
typename EnumT,
typename... Args>
113 return std::array<std::pair<dw::core::StringView, EnumT>,
sizeof...(Args)>{std::forward<const Args>(args)...};
128template <
typename EnumT>
130constexpr auto describeEnumerator(dw::core::StringView&& name, EnumT value) -> std::pair<dw::core::StringView, EnumT>
132 return std::make_pair(std::move(name), value);
142template <
typename EnumT>
148 for (
const auto& pair : pairs)
150 if (pair.first == name)
156 FixedString<4096> validNames{};
157 for (
const auto& pair : pairs)
159 validNames << dw::core::StringView{
" "} << pair.first;
161 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"Invalid enumerator name '", name,
"' for enum type ",
typeid(EnumT).name(),
", valid names:", validNames);
171template <
typename EnumT>
177 for (
const auto& pair : pairs)
179 if (pair.second == value)
184 throw ExceptionWithStatus(DW_INVALID_ARGUMENT,
"The enumerator value is invalid: ",
typeid(EnumT).name(),
" ",
static_cast<std::underlying_type_t<EnumT>
>(value));
dw::core::StringView mapEnumValueToName(EnumT value)
Get the enumerator name based on the value.
constexpr auto describeEnumeratorCollection(Args const &&... args) -> std::array< std::pair< dw::core::StringView, EnumT >, sizeof...(Args)>
Describe the enumerators.
constexpr auto describeEnumerator(dw::core::StringView &&name, EnumT value) -> std::pair< dw::core::StringView, EnumT >
Describe an enumerator.
auto mapEnumNameToValue(dw::core::StringView const &name) -> EnumT
Get the enumerator value based on the name.
std::array< std::pair< dw::core::StringView, EnumT >, NumberOfEnumerators > EnumDescriptionReturnType
static constexpr EnumDescriptionReturnType< EnumT, 0 > get()
Describe the enumerators.