3 #include <Poco/Exception.h>
4 #include <Poco/Logger.h>
5 #include <Poco/Random.h>
15 template <
typename Raw>
25 typedef typename ValueMap::const_iterator
Value;
39 for (
Value it = valueMap.begin(); it != valueMap.end(); ++it) {
40 auto result = rawMap.insert(std::make_pair(it->first, it));
42 throw Poco::ExistsException(
43 "duplicate raw value " + std::to_string(it->first));
50 template <
typename Raw>
62 namesMap(initNamesMap(valueMap))
66 static NamesMap initNamesMap(
const ValueMap &valueMap)
70 for (Value it = valueMap.begin(); it != valueMap.end(); ++it) {
71 auto result = namesMap.insert(std::make_pair(it->second, it));
73 throw Poco::ExistsException(
74 "duplicate string value " + it->second);
80 const NamesMap namesMap;
104 template <
typename Base,
typename RawType =
typename Base::Raw,
114 Enum(
const Raw &raw):
115 Enum(fromRaw(raw).m_value)
120 Enum(
const Value &value):
123 if (value == Base::valueMap().end()) {
124 if (Base::valueMap().empty()) {
125 throw Poco::IllegalStateException(
126 "attempt to create enum that is empty");
130 throw Poco::IllegalStateException(
131 "invalid enum value determined");
145 Iterator &operator ++()
151 Iterator operator ++(
int)
153 return Iterator(m_current++);
156 bool operator ==(
const Iterator &it)
const
158 return m_current == it.m_current;
161 bool operator !=(
const Iterator &it)
const
163 return m_current != it.m_current;
182 Iterator begin()
const
184 return Iterator(rawMap().begin());
189 return Iterator(rawMap().end());
205 static NamesMapInitializer initializer(Base::valueMap());
206 return initializer.namesMap;
212 return m_value->first;
215 const std::string &toString()
const
217 return m_value->second;
220 static const Iterable &all()
222 static const Iterable it;
226 static ThisEnum parse(
const std::string &input)
228 auto it = namesMap().find(input);
229 if (it == namesMap().end()) {
230 throw Poco::InvalidArgumentException(
231 "failed to parse '" + input +
"'");
237 static ThisEnum fromRaw(
const Raw &raw)
239 auto it = rawMap().find(raw);
240 if (it == rawMap().end()) {
241 throw Poco::InvalidArgumentException(
242 "unrecognized raw value " + std::to_string(raw));
253 const std::size_t index = rnd.next(Base::valueMap().size());
254 Value it = Base::valueMap().begin();
256 for (std::size_t k = 0; it != Base::valueMap().end(); ++it, ++k) {
264 static ThisEnum fromRaw(
const unsigned int raw)
266 return fromRaw(Raw(raw));
274 bool operator ==(
const Enum &other)
const
276 return m_value == other.m_value;
279 bool operator ==(
const Enum::Raw &other)
const
281 return m_value->first == other;
284 bool operator !=(
const Enum &other)
const
286 return m_value != other.m_value;
289 bool operator !=(
const Enum::Raw &other)
const
291 return m_value->first != other;
294 bool operator <(
const Enum &other)
const
296 return m_value->first < other.m_value->first;
299 bool operator <(
const Enum::Raw &other)
const
301 return m_value->first < other;
304 bool operator >(
const Enum &other)
const
306 return m_value->first > other.m_value->first;
309 bool operator >(
const Enum::Raw &other)
const
311 return m_value->first > other;
314 bool operator <=(
const Enum &other)
const
316 return m_value->first <= other.m_value->first;
319 bool operator <=(
const Enum::Raw &other)
const
321 return m_value->first <= other;
324 bool operator >=(
const Enum &other)
const
326 return m_value->first >= other.m_value->first;
329 bool operator >=(
const Enum::Raw &other)
const
331 return m_value->first >= other;
338 template <
typename Base,
typename Raw =
typename Base::Raw,
342 return s + e.toString();
345 template <
typename Base,
typename Raw =
typename Base::Raw,
346 typename NamesMapInitializer = EnumNamesInitializer<Raw>>
347 inline std::string operator +(
const char *s,
const Enum<Base, Raw, NamesMapInitializer> &e)
349 return s + e.toString();
std::map< std::string, Value > NamesMap
Definition: Enum.h:33
std::map< Raw, Value > RawMap
Definition: Enum.h:29
ValueMap::const_iterator Value
Definition: Enum.h:25
std::map< Raw, std::string > ValueMap
Definition: Enum.h:20