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
rtti.h
1 /*
2  * $Id$
3  * RTTI module
4  *
5  * This file is part of OTAWA
6  * Copyright (c) 2007, IRIT UPS.
7  *
8  * OTAWA is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * OTAWA is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with OTAWA; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 #ifndef ELM_RTTI_H
23 #define ELM_RTTI_H
24 
25 #include <elm/string.h>
26 #include <elm/types.h>
27 
28 namespace elm {
29 
30 // value_t structure
31 class value_t {
32  CString _name;
33  int _value;
34 public:
35  static inline value_t end(void) { return value_t("", 0); }
36  inline value_t(CString name, int value): _name(name), _value(value) { }
37  inline CString name(void) const { return _name; }
38  inline int value(void) const { return _value; }
39 };
40 inline value_t value(CString name, int value) {
41  return value_t(name, value);
42 }
43 
44 
45 // Field class
46 template <class V>
47 class Field {
48  CString _name;
49  V& _value;
50 public:
51  inline Field(CString name, V& value): _name(name), _value(value) { }
52  inline CString name(void) const { return _name; };
53  inline V& value(void) const { return _value; };
54 };
55 template <class T>
56 inline Field<T> field(CString name, T& value) {
57  return Field<T>(name, value);
58 }
59 
60 
61 // DefaultField class
62 template <class T>
63 class DefaultField: public Field<T> {
64  const T& def;
65 public:
66  inline DefaultField(CString name, T& value, const T& _default)
67  : Field<T>(name, value), def(_default) { }
68  inline const T& defaultValue(void) const { return def; }
69 };
70 template <class T>
71 inline DefaultField<T> field(CString name, T& value, const T& def) {
72  return DefaultField<T>(name, value, def);
73 }
74 
75 
76 // Enumerations
77 #define ELM_ENUM(type) \
78  namespace elm { \
79  template <> struct type_info<type>: public enum_t { \
80  static value_t __vals[]; \
81  static inline CString name(void) { return "<enum " #type ">"; } \
82  static inline value_t *values(void) { return __vals; } \
83  }; \
84  }
85 #define ELM_ENUM_BEGIN(type) \
86  namespace elm { \
87  struct value_t type_info<type>::__vals[] = {
88 #define ELM_ENUM_END \
89  , value("", 0) \
90  }; \
91  }
92 #define ELM_VALUE(name) elm::value(elm::_unqualify(#name), name)
93 
94 #ifndef ELM_NO_SHORTCUT
95 # define ENUM(type) ELM_ENUM(type)
96 # define VALUE(name) ELM_VALUE(name)
97 # define ENUM_BEGIN(type) ELM_ENUM_BEGIN(type)
98 # define ENUM_END ELM_ENUM_END
99 #endif
100 
101 
102 // AbstractClass class
104  CString _name;
105  AbstractClass *_base;
106 public:
108  : _name(name), _base(base) { };
109  virtual ~AbstractClass(void) { }
110  inline CString name(void) const { return _name; };
111  inline AbstractClass *base(void) const { return _base; };
112  virtual void *instantiate(void) = 0;
113  bool baseOf(AbstractClass *clazz);
114 };
115 
116 
117 // Class class
118 template <class T>
119 class Class: public AbstractClass {
120 public:
122  : AbstractClass(name, base) { };
123  virtual void *instantiate(void) { return new T; };
124 };
125 
126 
127 // _unqualify function
128 inline CString _unqualify(CString name) {
129  int pos = name.lastIndexOf(':');
130  if(pos < 0)
131  return name;
132  else
133  return name.substring(pos + 1);
134 }
135 
136 } // elm
137 
138 #endif // ELM_RTTI_H
CString substring(int pos) const
Definition: string.h:19
Definition: CString.h:17
const T & defaultValue(void) const
Definition: rtti.h:68
int value(void) const
Definition: rtti.h:38
static value_t end(void)
Definition: rtti.h:35
AbstractClass * base(void) const
Definition: rtti.h:111
DefaultField(CString name, T &value, const T &_default)
Definition: rtti.h:66
value_t(CString name, int value)
Definition: rtti.h:36
Field(CString name, V &value)
Definition: rtti.h:51
value_t value(CString name, int value)
Definition: rtti.h:40
int lastIndexOf(char chr) const
Definition: CString.h:45
Class(CString name, AbstractClass *base=0)
Definition: rtti.h:121
CString name(void) const
Definition: rtti.h:110
bool baseOf(AbstractClass *clazz)
Definition: type_info.cpp:241
Definition: rtti.h:47
Field< T > field(CString name, T &value)
Definition: rtti.h:56
CString name(void) const
Definition: rtti.h:37
virtual void * instantiate(void)
Definition: rtti.h:123
CString name(void) const
Definition: rtti.h:52
Definition: rtti.h:119
V & value(void) const
Definition: rtti.h:53
Definition: rtti.h:103
Definition: rtti.h:63
CString _unqualify(CString name)
Definition: rtti.h:128
AbstractClass(CString name, AbstractClass *base=0)
Definition: rtti.h:107
virtual ~AbstractClass(void)
Definition: rtti.h:109
Definition: rtti.h:31
virtual void * instantiate(void)=0