forked from AMReX-Codes/amrex
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AMREX_ENUM and ParmParse support for enum class
This adds AMREX_ENUM that can be used to define enum class with reflection. The new feature allows us to support enum class in ParmParse.
- Loading branch information
1 parent
b7083f0
commit 082ad2d
Showing
15 changed files
with
500 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#ifndef AMREX_ENUM_H_ | ||
#define AMREX_ENUM_H_ | ||
|
||
#include <AMReX_String.H> | ||
|
||
#include <algorithm> | ||
#include <array> | ||
#include <stdexcept> | ||
#include <string> | ||
#include <string_view> | ||
#include <type_traits> | ||
#include <utility> | ||
#include <vector> | ||
|
||
template <typename T> | ||
using amrex_enum_traits = decltype(amrex_get_enum_traits(std::declval<T>())); | ||
|
||
namespace amrex { | ||
template <typename T, typename ET = amrex_enum_traits<T>, | ||
std::enable_if_t<ET::value,int> = 0> | ||
T getEnum (std::string_view const& s) | ||
{ | ||
auto pos = ET::enum_names.find(s); | ||
if (pos == std::string_view::npos) { | ||
std::string error_msg("amrex::getEnum: Unknown enum: "); | ||
error_msg.append(s).append(" in AMREX_ENUM(").append(ET::class_name) | ||
.append(", ").append(ET::enum_names).append(")."); | ||
throw std::runtime_error(error_msg); | ||
} | ||
auto count = std::count(ET::enum_names.begin(), | ||
ET::enum_names.begin()+pos, ','); | ||
return static_cast<T>(count); | ||
} | ||
|
||
template <typename T, typename ET = amrex_enum_traits<T>, | ||
std::enable_if_t<ET::value,int> = 0> | ||
std::string getEnumNameString (T const& v) | ||
{ | ||
auto n = static_cast<int>(v); | ||
std::size_t pos = 0; | ||
for (int i = 0; i < n; ++i) { | ||
pos = ET::enum_names.find(',', pos); | ||
if (pos == std::string::npos) { | ||
std::string error_msg("amrex::getEnum: Unknown enum value: "); | ||
error_msg.append(std::to_string(n)).append(" in AMREX_ENUM(") | ||
.append(ET::class_name).append(", ").append(ET::enum_names) | ||
.append(")."); | ||
throw std::runtime_error(error_msg); | ||
} | ||
++pos; | ||
} | ||
auto pos2 = ET::enum_names.find(',', pos); | ||
return amrex::trim(std::string(ET::enum_names.substr(pos,pos2-pos))); | ||
} | ||
|
||
template <typename T, typename ET = amrex_enum_traits<T>, | ||
std::enable_if_t<ET::value,int> = 0> | ||
std::vector<std::string> getEnumNameStrings () | ||
{ | ||
return amrex::split(std::string(ET::enum_names), ", "); | ||
} | ||
|
||
template <typename T, typename ET = amrex_enum_traits<T>, | ||
std::enable_if_t<ET::value,int> = 0> | ||
std::string getEnumClassName () | ||
{ | ||
return std::string(ET::class_name); | ||
} | ||
} | ||
|
||
#define AMREX_ENUM(CLASS, ...) \ | ||
enum class CLASS : int { __VA_ARGS__ }; \ | ||
struct CLASS##_EnumTraits { \ | ||
using enum_class_t = CLASS; \ | ||
static constexpr bool value = true; \ | ||
static constexpr std::string_view class_name{#CLASS}; \ | ||
static constexpr std::string_view enum_names{#__VA_ARGS__}; \ | ||
}; \ | ||
CLASS##_EnumTraits amrex_get_enum_traits(CLASS) | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#ifndef AMREX_STRING_H_ | ||
#define AMREX_STRING_H_ | ||
#include <AMReX_Config.H> | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
namespace amrex { | ||
|
||
//! Converts all characters of the string into lower case based on std::locale | ||
std::string toLower (std::string s); | ||
|
||
//! Converts all characters of the string into uppercase based on std::locale | ||
std::string toUpper (std::string s); | ||
|
||
//! Trim leading and trailing characters in the optional `space` | ||
//! argument. | ||
std::string trim (std::string s, std::string const& space = " \t"); | ||
|
||
//! Returns rootNNNN where NNNN == num. | ||
std::string Concatenate (const std::string& root, | ||
int num, | ||
int mindigits = 5); | ||
|
||
//! Split a string using given tokens in `sep`. | ||
std::vector<std::string> split (std::string const& s, | ||
std::string const& sep = " \t"); | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#include <AMReX_String.H> | ||
#include <AMReX_BLassert.H> | ||
|
||
#include <algorithm> | ||
#include <cctype> | ||
#include <iomanip> | ||
#include <sstream> | ||
|
||
namespace amrex { | ||
|
||
std::string toLower (std::string s) | ||
{ | ||
std::transform(s.begin(), s.end(), s.begin(), | ||
[](unsigned char c) { return std::tolower(c); }); | ||
return s; | ||
} | ||
|
||
std::string toUpper (std::string s) | ||
{ | ||
std::transform(s.begin(), s.end(), s.begin(), | ||
[](unsigned char c) { return std::toupper(c); }); | ||
return s; | ||
} | ||
|
||
std::string trim(std::string s, std::string const& space) | ||
{ | ||
const auto sbegin = s.find_first_not_of(space); | ||
if (sbegin == std::string::npos) { return std::string{}; } | ||
const auto send = s.find_last_not_of(space); | ||
s = s.substr(sbegin, send-sbegin+1); | ||
return s; | ||
} | ||
|
||
std::string Concatenate (const std::string& root, int num, int mindigits) | ||
{ | ||
BL_ASSERT(mindigits >= 0); | ||
std::stringstream result; | ||
result << root << std::setfill('0') << std::setw(mindigits) << num; | ||
return result.str(); | ||
} | ||
|
||
std::vector<std::string> split (std::string const& s, std::string const& sep) | ||
{ | ||
std::vector<std::string> result; | ||
std::size_t pos_begin, pos_end = 0; | ||
while ((pos_begin = s.find_first_not_of(sep,pos_end)) != std::string::npos) { | ||
pos_end = s.find_first_of(sep,pos_begin); | ||
result.push_back(s.substr(pos_begin,pos_end-pos_begin)); | ||
if (pos_end == std::string::npos) { break; } | ||
} | ||
return result; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.