Evo C++ Library v0.5.1
enum.h
Go to the documentation of this file.
1 // Evo C++ Library
2 /* Copyright 2019 Justin Crowell
3 Distributed under the BSD 2-Clause License -- see included file LICENSE.txt for details.
4 */
6 
7 #pragma once
8 #ifndef INCL_evo_enum_h
9 #define INCL_evo_enum_h
10 
11 #include "substring.h"
12 
13 namespace evo {
16 
18 
63 template<class T>
64 struct EnumMapIterator : EnumIterator<typename T::Type, (int)T::FIRST, (int)T::LAST> {
67 #if defined(EVO_OLDCC2)
68  typedef typename BaseType::EnumType EnumType;
69 #else
70  using typename BaseType::EnumType;
71 #endif
72 
73  // Documented by parents
74 
76  }
77 
78  EnumMapIterator(const This& src) : EnumIterator<typename T::Type, (int)T::FIRST, (int)T::LAST>(src) {
79  }
80 
81  EnumMapIterator(EnumType value) : EnumIterator<typename T::Type, (int)T::FIRST, (int)T::LAST>(value) {
82  }
83 
84  EnumMapIterator(IteratorPos pos) : EnumIterator<typename T::Type, (int)T::FIRST, (int)T::LAST>(pos) {
85  }
86 
87  This& operator=(const This& src) {
89  return *this;
90  }
91 
92  This& operator=(EnumType value) {
93  BaseType::operator=(value);
94  return *this;
95  }
96 
97  This& operator=(IteratorPos pos) {
99  return *this;
100  }
101 
106  SubString result;
107  if (!BaseType::end_)
108  result = T::get_string(BaseType::value_);
109  return result;
110  }
111 };
112 
114 
134 #define EVO_ENUM_MAP(ENUM, FIRST_VAL, LAST_VAL, UNKNOWN_VAL, ...) \
135  struct ENUM ## Enum { \
136  typedef ENUM Type; \
137  typedef EnumMapIterator< ENUM ## Enum > Iter; \
138  static const ENUM FIRST = FIRST_VAL; \
139  static const ENUM LAST = LAST_VAL; \
140  static const ENUM UNKNOWN = UNKNOWN_VAL; \
141  static const evo::SubStringMapList& map() { \
142  static const evo::SubString LIST[] = { __VA_ARGS__ }; \
143  static const evo::SubStringMapList MAP(LIST, evo::fixed_array_size(LIST)); \
144  return MAP; \
145  } \
146  static ENUM get_enum(const evo::SubString& key) \
147  { return map().find_enum<ENUM>(key, FIRST, LAST, UNKNOWN); } \
148  static ENUM get_enum(int val) \
149  { return (val < (int)FIRST || val > (int)LAST ? UNKNOWN : (ENUM)val); } \
150  static int get_int(ENUM val) \
151  { return (int)val; } \
152  static SubString get_string(ENUM val) \
153  { return map().get_enum_string(val, FIRST, LAST); } \
154  }
155 
168 #define EVO_ENUM_REMAP(ENUM, FIRST_VAL, LAST_VAL, UNKNOWN_VAL, REMAP_ARRAY, ...) \
169  struct ENUM ## Enum { \
170  typedef ENUM Type; \
171  typedef EnumMapIterator< ENUM ## Enum > Iter; \
172  static const ENUM FIRST = FIRST_VAL; \
173  static const ENUM LAST = LAST_VAL; \
174  static const ENUM UNKNOWN = UNKNOWN_VAL; \
175  static const ENUM* get_remap_array() { \
176  return REMAP_ARRAY; \
177  } \
178  static const SizeT* get_reverse_remap_array() { \
179  static const SubStringMapList::ReverseRemap< ENUM ## Enum > REVERSE(get_remap_array()); \
180  return REVERSE.array; \
181  } \
182  static const evo::SubStringMapList& map() { \
183  static const evo::SubString LIST[] = { __VA_ARGS__ }; \
184  static const evo::SubStringMapList MAP(LIST, evo::fixed_array_size(LIST)); \
185  return MAP; \
186  } \
187  static ENUM get_enum(const evo::SubString& key) \
188  { return map().find_enum_remap<Type>(get_remap_array(), key, FIRST, LAST, UNKNOWN); } \
189  static ENUM get_enum(int val) \
190  { return (val < (int)FIRST || val > (int)LAST ? UNKNOWN : (ENUM)val); } \
191  static int get_int(ENUM val) \
192  { return (int)val; } \
193  static SubString get_string(ENUM val) \
194  { return map().get_enum_string_remap(get_reverse_remap_array(), val, FIRST, LAST); } \
195  }
196 
219 #define EVO_ENUM_MAP_PREFIXED(ENUM, PREFIX, ...) \
220  EVO_ENUM_MAP(ENUM, (ENUM)((int)(PREFIX ## UNKNOWN) + 1), (ENUM)((int)(PREFIX ## ENUM_END) - 1), PREFIX ## UNKNOWN, __VA_ARGS__)
221 
232 #define EVO_ENUM_REMAP_PREFIXED(ENUM, PREFIX, REMAP_ARRAY, ...) \
233  EVO_ENUM_REMAP(ENUM, (ENUM)((int)(PREFIX ## UNKNOWN) + 1), (ENUM)((int)(PREFIX ## ENUM_END) - 1), PREFIX ## UNKNOWN, REMAP_ARRAY, __VA_ARGS__)
234 
235 #if defined(EVO_CPP11)
236 
251  #define EVO_ENUM_CLASS_MAP(ENUM, ...) \
252  struct ENUM ## Enum { \
253  typedef ENUM Type; \
254  typedef EnumMapIterator< ENUM ## Enum > Iter; \
255  static const ENUM FIRST = (ENUM)((int)(ENUM::UNKNOWN) + 1); \
256  static const ENUM LAST = (ENUM)((int)(ENUM::ENUM_END) - 1); \
257  static const evo::SubStringMapList& map() { \
258  static const evo::SubString LIST[] = { __VA_ARGS__ }; \
259  static const evo::SubStringMapList MAP(LIST, evo::fixed_array_size(LIST)); \
260  return MAP; \
261  } \
262  static ENUM get_enum(const evo::SubString& key) \
263  { return map().find_enum_class<ENUM>(key); } \
264  static ENUM get_enum(int val) \
265  { return (val <= (int)ENUM::UNKNOWN || val >= (int)ENUM::ENUM_END ? ENUM::UNKNOWN : (ENUM)val); } \
266  static int get_int(ENUM val) \
267  { return (int)val; } \
268  static SubString get_string(ENUM val) \
269  { return map().get_enum_class_string(val); } \
270  }
271 
281  #define EVO_ENUM_CLASS_REMAP(ENUM, REMAP_ARRAY, ...) \
282  struct ENUM ## Enum { \
283  typedef ENUM Type; \
284  typedef EnumMapIterator< ENUM ## Enum > Iter; \
285  static const ENUM FIRST = (ENUM)((int)(ENUM::UNKNOWN) + 1); \
286  static const ENUM LAST = (ENUM)((int)(ENUM::ENUM_END) - 1); \
287  static const ENUM* get_remap_array() { \
288  return REMAP_ARRAY; \
289  } \
290  static const SizeT* get_reverse_remap_array() { \
291  static const SubStringMapList::ReverseRemap< ENUM ## Enum > REVERSE(get_remap_array()); \
292  return REVERSE.array; \
293  } \
294  static const evo::SubStringMapList& map() { \
295  static const evo::SubString LIST[] = { __VA_ARGS__ }; \
296  static const evo::SubStringMapList MAP(LIST, evo::fixed_array_size(LIST)); \
297  return MAP; \
298  } \
299  static ENUM get_enum(const evo::SubString& key) \
300  { return map().find_enum_remap<Type>(get_remap_array(), key, FIRST, LAST, Type::UNKNOWN); } \
301  static ENUM get_enum(int val) \
302  { return (val <= (int)ENUM::UNKNOWN || val >= (int)ENUM::ENUM_END ? ENUM::UNKNOWN : (ENUM)val); } \
303  static int get_int(ENUM val) \
304  { return (int)val; } \
305  static SubString get_string(ENUM val) \
306  { return map().get_enum_string_remap(get_reverse_remap_array(), val, FIRST, LAST); } \
307  }
308 #endif
309 
311 
326 #define EVO_ENUM_TRAITS(ENUM, TRAITS, START_VAL, ...) \
327  struct ENUM ## EnumTraits { \
328  typedef ENUM Type; \
329  static const ENUM START = START_VAL; \
330  static const TRAITS& get(ENUM value) { \
331  return data()[(int)value - (int)START]; \
332  } \
333  private: \
334  static const TRAITS* data() { \
335  static const TRAITS DATA[] = { __VA_ARGS__ }; \
336  return DATA; \
337  } \
338  };
339 
340 #if defined(EVO_CPP11)
341 
355  #define EVO_ENUM_CLASS_TRAITS(ENUM, TRAITS, ...) \
356  struct ENUM ## EnumTraits { \
357  typedef ENUM Type; \
358  static const ENUM START = ENUM::UNKNOWN; \
359  static const TRAITS& get(ENUM value) { \
360  return data()[(int)value - (int)START]; \
361  } \
362  private: \
363  static const TRAITS* data() { \
364  static const TRAITS DATA[] = { __VA_ARGS__ }; \
365  return DATA; \
366  } \
367  };
368 #endif
369 
371 
372 }
373 #endif
SubString value_str() const
Get current enum string value.
Definition: enum.h:105
Evo SubString container.
This & operator=(EnumType value)
Definition: enum.h:92
This & operator=(const This &src)
Assignment operator.
Definition: iter.h:1261
Expanded EnumIterator used with enum traits.
Definition: enum.h:64
IteratorPos
Iterator position value.
Definition: iter.h:20
EnumMapIterator< T > This
Definition: enum.h:65
EnumType value_
Definition: iter.h:1362
static const T FIRST
Definition: iter.h:1231
EnumMapIterator(IteratorPos pos)
Definition: enum.h:84
Iterator template for sequential enum values.
Definition: iter.h:1227
static const T LAST
Definition: iter.h:1232
T EnumType
Definition: iter.h:1230
EnumMapIterator(EnumType value)
Definition: enum.h:81
EnumMapIterator()
Definition: enum.h:75
Evo C++ Library namespace.
Definition: alg.h:11
This & operator=(IteratorPos pos)
Definition: enum.h:97
EnumIterator< typename T::Type,(int) T::FIRST,(int) T::LAST > BaseType
Definition: enum.h:66
This & operator=(const This &src)
Definition: enum.h:87
Reference and access existing string data.
Definition: substring.h:229
bool end_
Definition: iter.h:1363
EnumMapIterator(const This &src)
Definition: enum.h:78
EnumType value() const
Get current enum value.
Definition: iter.h:1349