Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
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<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_ */
elm::enum_info::iterator::operator==
bool operator==(const iterator &it) const
Definition: enum_info.h:57
elm::enum_info::value_t
struct elm::enum_info::value_t value_t
elm::enum_info
Definition: enum_info.h:30
elm::enum_info::value_t::name
cstring name
Definition: enum_info.h:33
elm::enum_info::begin
static iterator begin(void)
Definition: enum_info.h:67
elm::enum_info::value_t::value
T value
Definition: enum_info.h:34
elm::enum_info::last
static value_t last(void)
Definition: enum_info.h:42
value
elm::enum_info::is_defined_enum
@ is_defined_enum
Definition: enum_info.h:36
elm::CString
Definition: CString.h:17
elm::enum_info::name
static cstring name(void)
elm::enum_info::value_t
Definition: enum_info.h:31
elm::enum_info::fromString
static T fromString(const string &name)
Definition: enum_info.h:47
elm::enum_info::iterator::value
T value(void) const
Definition: enum_info.h:61
elm::enum_info::end
static iterator end(void)
Definition: enum_info.h:68
elm::enum_info::iterator::operator++
iterator & operator++(int)
Definition: enum_info.h:56
elm::enum_info::iterator::name
cstring name(void) const
Definition: enum_info.h:60
elm
Definition: adapter.h:26
elm::enum_t
Definition: type_info.h:106
elm::enum_info::iterator
Definition: enum_info.h:51
elm::enum_info::iterator::operator!=
bool operator!=(const iterator &it) const
Definition: enum_info.h:59
elm::enum_info::value_t::value_t
value_t(cstring n, const T &v)
Definition: enum_info.h:32
elm::io::IOException
Definition: IOException.h:29
elm::enum_info::values
static value_t values[]
Definition: enum_info.h:37
elm::enum_info::toString
static cstring toString(T v)
Definition: enum_info.h:45
elm::enum_info::iterator::operator++
iterator & operator++(void)
Definition: enum_info.h:55
elm::enum_info::iterator::iterator
iterator(const iterator &it)
Definition: enum_info.h:54
elm::enum_info::value
static value_t value(const char *n, T v)
Definition: enum_info.h:41