Mantid
Loading...
Searching...
No Matches
EnumeratedString.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2023 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7#pragma once
8#include <boost/algorithm/string.hpp>
9#include <sstream>
10#include <string>
11#include <type_traits>
12#include <vector>
13
14namespace Mantid {
15namespace Kernel {
24namespace {
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); };
29} // namespace
30
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>); // cause a compiler error if E is not an enum
44
45public:
47
48 EnumeratedString(const E e) {
50 this->operator=(e);
51 }
52
53 EnumeratedString(const std::string &s) {
55 this->operator=(s);
56 }
57
59
60 // treat the object as either the enum, or a string
61 operator E() const { return value; }
62 operator std::string() const { return name; }
63 // explicitly define copy assignment operator to avoid deprecation warnings
65 // assign the object either by the enum, or by string
67 if (int(e) >= 0 && size_t(e) < names->size()) {
68 value = e;
69 name = names->at(size_t(e));
70 } else {
71 std::stringstream msg;
72 msg << "Invalid enumerator " << int(e) << " for enumerated string " << typeid(E).name();
73 throw std::runtime_error(msg.str());
74 }
75 return *this;
76 }
77 EnumeratedString &operator=(const std::string &s) {
78 E e = findEFromString(s);
79 if (e != E::enum_count) {
80 value = e;
81 name = s;
82 } else {
83 std::stringstream msg;
84 msg << "Invalid string \"" << s << "\" for EnumeratedString";
85#ifdef _DEBUG
86 msg << typeid(E).name();
87#endif
88 throw std::runtime_error(msg.str());
89 }
90 return *this;
91 }
92
93 // for comparison of the object to either enums or strings
94 bool operator==(const E e) const { return value == e; }
95 bool operator!=(const E e) const { return value != e; }
96
97 bool operator==(const std::string &s) const { return (*stringComparator)(name, s); }
98 bool operator!=(const std::string &s) const { return !(*stringComparator)(name, s); }
99
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)); }
102
103 bool operator==(const EnumeratedString &es) const { return value == es.value; }
104 bool operator!=(const EnumeratedString &es) const { return value != es.value; }
105
106 template <typename OtherEnumType, const std::vector<std::string> *OtherEnumStrings>
108 return false; // Different enum types are always different
109 }
110
111 template <typename OtherEnumType, const std::vector<std::string> *OtherEnumStrings>
113 return !(*this == other);
114 }
115
116 const char *c_str() const { return name.c_str(); }
117 static size_t size() { return names->size(); }
118
119private:
121 std::string name;
122
123 // given a string, find the corresponding enum value
124 E findEFromString(const std::string &s) {
125 E e = E(0);
126 for (; size_t(e) < names->size(); e = E(size_t(e) + 1))
127 if ((*stringComparator)(s, names->at(size_t(e))))
128 break;
129 return e;
130 }
131
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());
138 }
139 }
140};
141} // namespace Kernel
142} // namespace Mantid
bool operator==(const char *s) 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
bool operator==(const EnumeratedString &es) const
bool operator!=(const EnumeratedString &es) const
EnumeratedString(const std::string &s)
bool operator!=(const std::string &s) const
E findEFromString(const std::string &s)
EnumeratedString & operator=(E e)
bool operator!=(const char *s) const
EnumeratedString & operator=(const EnumeratedString &other)=default
bool operator==(const std::string &s) const
Helper class which provides the Collimation Length for SANS instruments.