Elm  1.0
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
enum_info.h
1 /*
2  * enum_info class interface
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2013, IRIT UPS.
6  *
7  * OTAWA is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * OTAWA is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with OTAWA; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #ifndef ELM_ENUM_INFO_H_
22 #define ELM_ENUM_INFO_H_
23 
24 #include <elm/io/IOException.h>
25 #include <elm/type_info.h>
26 
27 namespace elm {
28 
29 template <class T>
30 struct enum_info: public enum_t {
31  typedef struct value_t {
32  inline value_t(cstring n, const T& v) { name = n; value = v; }
34  T value;
35  } value_t;
36  enum { is_defined_enum = 1 };
37  static value_t values[];
38  static cstring name(void);
39 
40  // value building
41  static inline value_t value(const char *n, T v) { return value_t(n, v); }
42  static inline value_t last(void) { return value_t("", T(0)); }
43 
44  // usage
45  static cstring toString(T v)
46  { for(int i = 0; values[i].name; i++) if(v == values[i].value) return values[i].name; return "???"; }
47  static T fromString(const string& name)
48  { for(int i = 0; values[i].name; i++) if(name == values[i].name) return values[i].value; throw io::IOException("bad enum value"); }
49 
50  // iterator on values
51  class iterator {
52  friend struct enum_info;
53  public:
54  inline iterator(const iterator& it): i(it.i), vs(it.vs) { }
55  iterator& operator++(void) { i++; return *this; }
56  iterator& operator++(int) { i++; return *this; }
57  inline bool operator==(const iterator& it) const
58  { return vs == it.vs && (i == it.i || (it.i < 0 && !vs[i].name) || (i < 0 && !vs[it.i].name)); }
59  inline bool operator!=(const iterator& it) const { return !operator==(it); }
60  inline cstring name(void) const { return values[i].name; }
61  inline T value(void) const { return values[i].value; }
62  private:
63  inline iterator(value_t *_vs, int _i): i(_i), vs(_vs) { }
64  int i;
65  value_t *vs;
66  };
67  inline static iterator begin(void) { return iterator(values, 0); }
68  inline static iterator end(void) { return iterator(values, -1); }
69 };
70 
71 } // elm
72 
73 #endif /* ELM_ENUM_INFO_H_ */
Definition: enum_info.h:51
iterator & operator++(void)
Definition: enum_info.h:55
Definition: CString.h:17
struct elm::enum_info::value_t value_t
Definition: enum_info.h:30
static cstring toString(T v)
Definition: enum_info.h:45
static value_t values[]
Definition: enum_info.h:37
bool operator==(const iterator &it) const
Definition: enum_info.h:57
Definition: enum_info.h:31
static value_t value(const char *n, T v)
Definition: enum_info.h:41
T value
Definition: enum_info.h:34
value_t(cstring n, const T &v)
Definition: enum_info.h:32
static iterator begin(void)
Definition: enum_info.h:67
Definition: type_info.h:100
static T fromString(const string &name)
Definition: enum_info.h:47
iterator(const iterator &it)
Definition: enum_info.h:54
Definition: IOException.h:29
iterator & operator++(int)
Definition: enum_info.h:56
cstring name(void) const
Definition: enum_info.h:60
static value_t last(void)
Definition: enum_info.h:42
static cstring name(void)
Definition: enum_info.h:36
cstring name
Definition: enum_info.h:33
Definition: rtti.h:31
static iterator end(void)
Definition: enum_info.h:68
T value(void) const
Definition: enum_info.h:61
bool operator!=(const iterator &it) const
Definition: enum_info.h:59