10 #include <string_view>
11 #include <type_traits>
20 template <
typename T,
typename ET = amrex_enum_traits<T>,
21 std::enable_if_t<ET::value,
int> = 0>
24 auto tmp =
amrex::split(std::string(ET::enum_names),
",");
25 std::vector<std::pair<std::string,T>>
r;
26 r.reserve(tmp.size());
28 for (
auto const& item : tmp) {
31 r.emplace_back(
amrex::trim(kv[0]),
static_cast<T
>(next_value));
33 }
else if (kv.size() == 2) {
35 auto it = std::find_if(
r.begin(),
r.end(),
36 [&] (
auto const&
x) ->
bool
37 { return x.first == value_string; });
38 auto this_value = it->second;
40 next_value =
static_cast<int>(this_value) + 1;
42 std::string error_msg(
"amrex::getEnumNameIntPairs: AMREX_ENUM(");
43 error_msg.append(ET::class_name).append(
", ").append(ET::enum_names)
45 throw std::runtime_error(error_msg);
51 template <
typename T,
typename ET = amrex_enum_traits<T>,
52 std::enable_if_t<ET::value,
int> = 0>
55 auto const& kv = getEnumNameValuePairs<T>();
56 auto it = std::find_if(kv.begin(), kv.end(),
57 [&] (
auto const&
x) ->
bool
58 { return x.first == s; });
62 std::string error_msg(
"amrex::getEnum: Unknown enum: ");
63 error_msg.append(s).append(
" in AMREX_ENUM(").append(ET::class_name)
64 .append(
", ").append(ET::enum_names).append(
").");
65 throw std::runtime_error(error_msg);
70 template <
typename T,
typename ET = amrex_enum_traits<T>,
71 std::enable_if_t<ET::value,
int> = 0>
74 auto const& kv = getEnumNameValuePairs<T>();
76 auto it = std::find_if(kv.begin(), kv.end(),
77 [&] (
auto const&
x) ->
bool
78 { return amrex::toLower(x.first) == ls; });
82 std::string error_msg(
"amrex::getEnumCaseInsensitive: Unknown enum: ");
83 error_msg.append(s).append(
" in AMREX_ENUM(").append(ET::class_name)
84 .append(
", ").append(ET::enum_names).append(
").");
85 throw std::runtime_error(error_msg);
90 template <
typename T,
typename ET = amrex_enum_traits<T>,
91 std::enable_if_t<ET::value,
int> = 0>
94 auto const& kv = getEnumNameValuePairs<T>();
95 auto it = std::find_if(kv.begin(), kv.end(),
96 [&] (
auto const&
x) ->
bool
97 { return x.second == v; });
101 std::string error_msg(
"amrex::getEnum: Unknown enum value: ");
102 error_msg.append(std::to_string(
static_cast<int>(v)))
103 .append(
" in AMREX_ENUM(").append(ET::class_name).append(
", ")
104 .append(ET::enum_names).append(
").");
105 throw std::runtime_error(error_msg);
106 return std::string();
110 template <
typename T,
typename ET = amrex_enum_traits<T>,
111 std::enable_if_t<ET::value,
int> = 0>
116 auto found = s.find(
'=');
117 if (found != std::string::npos) {
125 template <
typename T,
typename ET = amrex_enum_traits<T>,
126 std::enable_if_t<ET::value,
int> = 0>
129 return std::string(ET::class_name);
133 #define AMREX_ENUM(CLASS, ...) \
134 enum class CLASS : int { __VA_ARGS__ }; \
135 struct CLASS##_EnumTraits { \
136 using enum_class_t = CLASS; \
137 static constexpr bool value = true; \
138 static constexpr std::string_view class_name{#CLASS}; \
139 static constexpr std::string_view enum_names{#__VA_ARGS__}; \
141 CLASS##_EnumTraits amrex_get_enum_traits(CLASS)
decltype(amrex_get_enum_traits(std::declval< T >())) amrex_enum_traits
Definition: AMReX_Enum.H:16
Definition: AMReX_Amr.cpp:49
std::string getEnumClassName()
Definition: AMReX_Enum.H:127
std::string getEnumNameString(T const &v)
Definition: AMReX_Enum.H:92
std::string trim(std::string s, std::string const &space)
Definition: AMReX_String.cpp:25
std::string toLower(std::string s)
Converts all characters of the string into lower case based on std::locale.
Definition: AMReX_String.cpp:11
T getEnum(std::string_view const &s)
Definition: AMReX_Enum.H:53
std::vector< std::string > getEnumNameStrings()
Definition: AMReX_Enum.H:112
T getEnumCaseInsensitive(std::string_view const &s)
Definition: AMReX_Enum.H:72
std::vector< std::string > split(std::string const &s, std::string const &sep)
Split a string using given tokens in sep.
Definition: AMReX_String.cpp:42
std::vector< std::pair< std::string, T > > getEnumNameValuePairs()
Definition: AMReX_Enum.H:22