Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
Enum.h
1 /*
2  * Enum class interface
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2007, 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_RTTI_ENUM_H_
22 #define ELM_RTTI_ENUM_H_
23 
24 #include <elm/data/Vector.h>
25 #include "Type.h"
26 
27 namespace elm { namespace rtti {
28 
29 // value_t structure
30 
31 class Enum: public Type, public Enumerable {
32 public:
33 
34  class Value {
35  public:
36  static inline Value end(void) { return Value("", 0); }
37  inline Value(void): _value(0) { }
38  inline Value(cstring name, int value): _name(name), _value(value) { }
39  inline cstring name(void) const { return _name; }
40  inline int value(void) const { return _value; }
41  private:
42  cstring _name;
43  int _value;
44  };
45 
46  class make {
47  friend class Enum;
48  public:
49  inline make(cstring name): _name(name) { }
50  inline make& value(cstring name, int value) { _values.add(Value(name, value)); return *this; }
51  inline make& alias(cstring name, int value) { _aliases.add(Value(name, value)); return *this; }
52  private:
53  cstring _name;
54  Vector<Value> _values;
55  Vector<Value> _aliases;
56  };
57 
58  Enum(const make& make);
59  Enum(cstring name, const Value values[]);
60 
62  inline Iter values(void) const { return Iter(_values); }
63 
64  // Enumerable interface
65  virtual const Type& type(void) const;
66  virtual int valueFor(string text) const;
67  virtual cstring nameFor(int value) const;
68 
69  // Type interface
70  virtual bool canCast(const Type *t) const;
71  virtual bool isEnum(void) const;
72  virtual const Enumerable& asEnum(void) const;
73 
74 private:
75  Vector<Value> _values;
76  Vector<Value> _map;
77 };
78 
80  { return rtti::Enum::Value(name, value); }
81 
82 // New support for enumeration
83 #define ELM_DECLARE_ENUM(name) \
84  namespace elm { namespace rtti { template <> const elm::rtti::Type& _type<name>::_(void); } } \
85  inline elm::io::Output& operator<<(elm::io::Output& out, name value) { out << elm::type_of<name>().asEnum().nameFor(value); return out; } \
86  inline elm::io::Input& operator>>(elm::io::Input& in, name& value) \
87  { value = static_cast<name>(elm::type_of<name>().asEnum().valueFor(in.scanWord())); return in; }
88 
89 #define ELM_DEFINE_ENUM(type, desc) \
90  namespace elm { namespace rtti { template <> const elm::rtti::Type& _type<type>::_(void) { return desc; } } }
91 
92 #define ELM_BEGIN_ENUM(type) \
93  namespace elm { namespace rtti { template <> const elm::rtti::Type& _type<type>::_(void) { \
94  static elm::rtti::Enum _(Enum::make(#type)
95 
96 #define ELM_END_ENUM \
97  ); \
98  return _; \
99  } } }
100 
101 
102 #ifndef ELM_NO_SHORTCUT
103 # define DECLARE_ENUM(name) ELM_DECLARE_ENUM(name)
104 # define DEFINE_ENUM(type, desc) ELM_DEFINE_ENUM (type, desc)
105 # define BEGIN_ENUM(type) ELM_BEGIN_ENUM(type)
106 # define END_ENUM ELM_END_ENUM
107 #endif
108 
109 } } // elm::rtti
110 
111 #endif /* INCLUDE_ELM_RTTI_ENUM_H_ */
elm::rtti::Enum::make::alias
make & alias(cstring name, int value)
Definition: Enum.h:51
elm::rtti::Enum::Value::Value
Value(void)
Definition: Enum.h:37
elm::rtti::Enum::valueFor
virtual int valueFor(string text) const
Definition: rtti.cpp:1218
elm::rtti::Type::name
string name(void) const
Definition: Type.h:91
elm::rtti::Enum::asEnum
virtual const Enumerable & asEnum(void) const
Definition: rtti.cpp:1255
elm::rtti::Enum::Iter
Vector< Value >::Iter Iter
Definition: Enum.h:61
elm::rtti::Enum::values
Iter values(void) const
Definition: Enum.h:62
elm::rtti::Enumerable
Definition: Type.h:42
elm::rtti::Enum
Definition: Enum.h:31
elm::rtti::Enum::isEnum
virtual bool isEnum(void) const
Definition: rtti.cpp:1249
elm::rtti::Enum::Value
Definition: Enum.h:34
value
elm::CString
Definition: CString.h:17
elm::rtti::Enum::Enum
Enum(const make &make)
Definition: rtti.cpp:1191
elm::rtti::value
rtti::Enum::Value value(cstring name, int value)
Definition: Enum.h:79
elm::rtti::Enum::make::make
make(cstring name)
Definition: Enum.h:49
elm
Definition: adapter.h:26
elm::Vector
Definition: Vector.h:34
elm::rtti::Enum::canCast
virtual bool canCast(const Type *t) const
Definition: rtti.cpp:1243
elm::rtti::Enum::make::value
make & value(cstring name, int value)
Definition: Enum.h:50
elm::rtti::Enum::type
virtual const Type & type(void) const
Definition: rtti.cpp:1210
elm::rtti::Enum::Value::end
static Value end(void)
Definition: Enum.h:36
elm::rtti::Enum::Value::Value
Value(cstring name, int value)
Definition: Enum.h:38
elm::rtti::Enum::Value::name
cstring name(void) const
Definition: Enum.h:39
elm::rtti::make
Definition: Class.h:251
elm::rtti::Type
Definition: Type.h:83
elm::rtti::Enum::Value::value
int value(void) const
Definition: Enum.h:40
elm::Vector::Iter
Definition: Vector.h:67
elm::rtti::Enum::nameFor
virtual cstring nameFor(int value) const
Definition: rtti.cpp:1227
elm::rtti::Enum::make
Definition: Enum.h:46