Celeritas  0.5.0-86+4a8eea4
Public Member Functions | Static Public Member Functions | List of all members
celeritas::StringEnumMapper< T > Class Template Reference

Map strings to enums for user input. More...

#include <StringEnumMapper.hh>

Public Types

Type aliases
using EnumCStringFuncPtr = char const *(T)
 

Public Member Functions

template<class U >
 StringEnumMapper (U &&enum_to_string, char const *desc=nullptr)
 Construct with a "stringify" function. More...
 
operator() (std::string_view s) const
 Convert a string_view to the corresponding enum.
 

Static Public Member Functions

static StringEnumMapper< T > from_cstring_func (EnumCStringFuncPtr, char const *desc=nullptr)
 Construct using a to_cstring function.
 

Detailed Description

template<class T>
class celeritas::StringEnumMapper< T >

Map strings to enums for user input.

Note that since a map is built at construction time, instances of this class should be static to amortize the cost. The strings being converted must exceed the lifetime of this class. (Usually it references const char*.)

Todo:
If the size of the strings is short and there aren't a lot of them, it will be faster to use a fixed-size array and search over them.

Example:

void from_json(const nlohmann::json& j, GeantSetupOptions& opts)
{
static auto gspl_from_string
to_cstring, "physics list");
opts.physics = gspl_from_string(j.at("physics").get<std::string>());
}
static StringEnumMapper< T > from_cstring_func(EnumCStringFuncPtr, char const *desc=nullptr)
Construct using a to_cstring function.
Definition: StringEnumMapper.hh:80

Constructor & Destructor Documentation

◆ StringEnumMapper()

template<class T >
template<class U >
celeritas::StringEnumMapper< T >::StringEnumMapper ( U &&  enum_to_string,
char const *  desc = nullptr 
)
inlineexplicit

Construct with a "stringify" function.

The result just has to be implicitly convertible to a std::string_view .


The documentation for this class was generated from the following file: