8#include <boost/algorithm/string.hpp>
25std::function<bool(
const std::string &,
const std::string &)> compareStrings =
26 [](
const std::string &
x,
const std::string &
y) {
return x ==
y; };
27std::function<bool(
const std::string &,
const std::string &)> compareStringsCaseInsensitive =
28 [](
const std::string &
x,
const std::string &
y) {
return boost::iequals(
x,
y); };
31template <
class E, const std::vector<std::
string> *names,
32 std::function<
bool(const std::
string &, const std::
string &)> *
stringComparator = &compareStrings>
43 static_assert(std::is_enum_v<E>);
61 operator E()
const {
return value; }
62 operator std::string()
const {
return name; }
67 if (
int(e) >= 0 &&
size_t(e) < names->size()) {
69 name = names->at(
size_t(e));
71 std::stringstream msg;
72 msg <<
"Invalid enumerator " << int(e) <<
" for enumerated string " <<
typeid(E).
name();
73 throw std::runtime_error(msg.str());
79 if (e != E::enum_count) {
83 std::stringstream msg;
84 msg <<
"Invalid string \"" << s <<
"\" for EnumeratedString";
86 msg <<
typeid(E).
name();
88 throw std::runtime_error(msg.str());
97 bool operator==(
const std::string &s)
const {
return (*stringComparator)(
name, s); }
98 bool operator!=(
const std::string &s)
const {
return !(*stringComparator)(
name, s); }
100 bool operator==(
const char *s)
const {
return (*stringComparator)(
name, std::string(s)); }
101 bool operator!=(
const char *s)
const {
return !(*stringComparator)(
name, std::string(s)); }
106 template <
typename OtherEnumType, const std::vector<std::
string> *OtherEnumStrings>
111 template <
typename OtherEnumType, const std::vector<std::
string> *OtherEnumStrings>
113 return !(*
this == other);
117 static size_t size() {
return names->size(); }
126 for (; size_t(e) < names->size(); e = E(
size_t(e) + 1))
127 if ((*stringComparator)(s, names->at(
size_t(e))))
133 if (
size_t(E::enum_count) != names->size()) {
134 std::stringstream msg;
135 msg <<
"Size of " <<
typeid(E).
name() <<
" incompatible with vector of names: ";
136 msg << size_t(E::enum_count) <<
" vs. " << names->size() << std::endl;
137 throw std::runtime_error(msg.str());
bool operator==(const char *s) const
const char * c_str() const
EnumeratedString & operator=(const std::string &s)
EnumeratedString(const EnumeratedString &es)
bool operator==(const EnumeratedString< OtherEnumType, OtherEnumStrings > &) const
bool operator!=(const EnumeratedString< OtherEnumType, OtherEnumStrings > &other) const
void ensureCompatibleSize()
EnumeratedString(const E e)
bool operator==(const EnumeratedString &es) const
bool operator!=(const EnumeratedString &es) const
EnumeratedString(const std::string &s)
bool operator!=(const std::string &s) const
bool operator!=(const E e) const
E findEFromString(const std::string &s)
EnumeratedString & operator=(E e)
bool operator!=(const char *s) const
bool operator==(const E e) const
EnumeratedString & operator=(const EnumeratedString &other)=default
bool operator==(const std::string &s) const
Helper class which provides the Collimation Length for SANS instruments.