Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
type_info.h
1 /*
2  * type_info class interface
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2008-13, 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_TYPE_INFO_H_
22 #define ELM_TYPE_INFO_H_
23 
24 #include <elm/arch.h>
25 #include <elm/string/String.h>
26 #include <elm/int.h>
27 #include <elm/meta.h>
28 
29 namespace elm {
30 
31 namespace intern {
32  template <class T> char test_class(int T::*);
33  template <class T> int test_class(...);
34 
35  template <class T>
36  struct is_scalar {
37  enum { _ = sizeof(test_class<T>(0)) != sizeof(char) };
38  };
39 }
40 
41 // type_info trait
42 typedef struct default_t {
43  enum { is_type = 0 };
44  enum { is_scalar = 0 };
45  enum { is_enum = 0, is_defined_enum = 0 };
46  enum { is_class = 0 };
47  enum { is_ptr = 0 };
48  enum { is_ref = 0 };
49  enum { is_deep = 0 };
50  enum { is_virtual = 0 };
51  enum { is_void = 0 };
52 } default_t;
53 
54 
55 // generic class
56 template <class T> class type_info: public default_t {
57 public:
58  enum { is_class = 1 };
59  enum { is_deep = 1 };
60  enum { is_virtual = 1 };
63  static cstring name(void) { return ""; }
64 
65  typedef T var_t;
66  typedef var_t embed_t;
67  static inline T& ref(T& v) { return v; }
68  static inline const T& get(const T& v) { return v; }
69  static inline void put(T& x, const T& v) { x = v; }
70 
71  typedef const T& in_t;
72  typedef T& out_t;
73  typedef const T& ret_t;
74  typedef T& mut_t;
75 };
76 
77 
78 // generic type
79 typedef struct type_t: public default_t {
80  enum { is_type = 1 };
81  enum { is_deep = 1 };
82 } type_t;
83 
84 // scalar type
85 template <class T>
86 struct scalar_t: public default_t {
87  enum { is_enum = 0 };
88  enum { is_scalar = 1 };
89  enum { is_deep = 0 };
90 
91  typedef T var_t;
92  typedef var_t embed_t;
93  static inline T& ref(T& v) { return v; }
94  static inline T get(const T& v) { return v; }
95  static inline void put(T& x, T v) { x = v; }
96 
97  typedef T in_t;
98  typedef T& out_t;
99  typedef T ret_t;
100  typedef T& mut_t;
101 };
102 
103 
104 // enum type
105 template <class T>
106 struct enum_t: public scalar_t<T> {
107  enum { is_enum = 1 };
108 };
109 
110 
111 // bool specialization
112 template <> struct type_info<bool>: public scalar_t<bool> {
113  static const bool min = false;
114  static const bool max = true;
115  static const bool null = false;
116  static inline CString name(void) { return "bool"; }
117 };
118 
119 
120 // void specialization
121 template <> struct type_info<void>: public default_t {
122  enum { is_void = 1 };
123 };
124 
125 
126 // integer specialization
127 template <class I>
128 struct signed_info: public scalar_t<I> {
129  static const int size = sizeof(I) * 8;
130  static const bool is_signed = true;
131  static const I min = I(-1) << (size - 1);
132  static const I max = ~min;
133  static const I null = 0;
134  static const int shift = 0;
135 };
136 template <class I> const I signed_info<I>::null;
137 template <class I> const I signed_info<I>::min;
138 template <class I> const I signed_info<I>::max;
139 
140 template <class I>
141 struct unsigned_info: public scalar_t<I> {
142  static const int size = sizeof(I) * 8;
143  static const bool is_signed = false;
144  static const I min = 0;
145  static const I max = I(-1);
146  static const I null = 0;
147  static const int shift = 0;
148 };
149 template <class I> const I unsigned_info<I>::null;
150 template <class I> const I unsigned_info<I>::min;
151 template <class I> const I unsigned_info<I>::max;
152 
153 template <> struct type_info<char>: public signed_info<char> { static cstring name(void); static const int shift = 0; };
154 template <> struct type_info<t::int8>: public signed_info<t::int8> { static cstring name(void); static const int shift = 0; };
155 template <> struct type_info<t::uint8>: public unsigned_info<t::uint8> { static cstring name(void); static const int shift = 0; };
156 template <> struct type_info<t::int16>: public signed_info<t::int16> { static cstring name(void); static const int shift = 1; };
157 template <> struct type_info<t::uint16>: public unsigned_info<t::uint16> { static cstring name(void); static const int shift = 1; };
158 template <> struct type_info<t::int32>: public signed_info<t::int32> { static cstring name(void); static const int shift = 2; };
159 template <> struct type_info<t::uint32>: public unsigned_info<t::uint32> { static cstring name(void); static const int shift = 2; };
160 template <> struct type_info<t::int64>: public signed_info<t::int64> { static cstring name(void); static const int shift = 3; };
161 template <> struct type_info<t::uint64>: public unsigned_info<t::uint64> { static cstring name(void); static const int shift = 3; };
162 
163 
164 // float specialization
165 template <> struct type_info<float>: public scalar_t<float> {
166  static const float min;
167  static const float max;
168  static const float null;
169  static cstring name(void);
170 };
171 template <> struct type_info<double>: public scalar_t<double> {
172  static const double min;
173  static const double max;
174  static const double null;
175  static cstring name(void);
176 };
177 template <> struct type_info<long double>: public scalar_t<long double> {
178  static const long double min;
179  static const long double max;
180  static const long double null;
181  static cstring name(void);
182 };
183 
184 
185 // String specialization
186 template <> struct type_info<cstring>: public default_t {
187  static const cstring null;
188  static cstring name(void);
189 
190  typedef cstring var_t;
191  typedef var_t embed_t;
192  static inline cstring& ref(cstring& v) { return v; }
193  static inline cstring get(const cstring& v) { return v; }
194  static inline void put(cstring& x, cstring v) { x = v; }
195 
196  typedef cstring in_t;
197  typedef cstring& out_t;
198  typedef cstring ret_t;
199  typedef cstring& mut_t;
200 };
201 
202 template <> struct type_info<string>: public default_t {
203  static const string null;
204  static cstring name(void);
205  enum { is_virtual = 1 };
206  enum { is_deep = 1 };
207 
208  typedef string var_t;
209  typedef var_t embed_t;
210  typedef const string& in_t;
211  typedef string& out_t;
212  typedef const string& ret_t;
213  typedef string& mut_t;
214 
215  static inline mut_t ref(var_t& v) { return v; }
216  static inline ret_t get(const var_t& v) { return v; }
217  static inline void put(var_t& x, in_t v) { x = v; }
218 };
219 
220 
221 // pointer specialization
222 template <class T> struct type_info<const T *>: public scalar_t<const T *> {
223  typedef T of;
224  enum { is_const = 1 };
225  enum { is_ptr = 1 };
226  static const T * const null;
227  static string name(void) { return "const " + type_info<T>::name() + " *"; }
228 };
229 template <class T> const T *const type_info<const T *>::null = nullptr;
230 
231 
232 template <class T> struct type_info<T *>: public scalar_t<T *> {
233  typedef T of;
234  enum { is_const = 0 };
235  enum { is_ptr = 1 };
236  static T * const null;
237  static string name(void) { return type_info<T>::name() + " *"; }
238 };
239 template <class T> T *const type_info<T *>::null = nullptr;
240 
241 
242 // reference specialization
243 template <class T>
244 class ref_t: public default_t {
245 public:
246  struct delegate {
247  public:
248  inline delegate(T *&ptr): p(ptr) { }
249  inline operator T&() const { return *p; }
250  inline delegate& operator=(T &r) { p = &r; return *this; }
251  private:
252  T *& p;
253  };
254 
255  typedef T *var_t;
256  typedef var_t embed_t;
257  static inline delegate ref(T *&v) { return delegate(v); }
258  static inline T& get(T *v) { return *v; }
259  static inline void put(T *& x, T& v) { x = &v; }
260 
261  typedef T& in_t;
262  typedef T& out_t;
263  typedef T& ret_t;
264  typedef delegate mut_t;
265 };
266 
267 template <class T> struct type_info<const T&>: public ref_t<const T> {
268  typedef T of;
269  enum { is_const = 1 };
270  static string name(void) { return "const " + type_info<T>::name() + "& "; }
271 };
272 
273 template <class T> struct type_info<T&>: public ref_t<T> {
274  typedef T of;
275  enum { is_const = 0 };
276  static string name(void) { return type_info<T>::name() + "& "; }
277 };
278 
279 template <class T> struct ti: type_info<T> { };
280 
281 namespace t {
282  template <class T> using var = typename type_info<T>::var_t;
283  template <class T> using in = typename type_info<T>::in_t;
284  template <class T> using out = typename type_info<T>::out_t;
285  template <class T> using ret = typename type_info<T>::ret_t;
286  template <class T> using mut = typename type_info<T>::mut_t;
287  template <class T> inline void put(var<T>& x, in<T> v) { type_info<T>::put(x, v); }
288  template <class T> inline ret<T> get(const var<T>& v) { return type_info<T>::get(v); }
289  template <class T> inline mut<T> ref(var<T>& x) { return type_info<T>::ref(x); }
290 } // t
291 
292 } // elm
293 
294 #endif /* ELM_TYPE_INFO_H_ */
elm::type_info::out_t
T & out_t
Definition: type_info.h:72
elm::unsigned_info::is_signed
static const bool is_signed
Definition: type_info.h:143
elm::type_info< T * >::null
static T *const null
Definition: type_info.h:236
elm::ref_t::get
static T & get(T *v)
Definition: type_info.h:258
elm::type_info< const T & >::of
T of
Definition: type_info.h:268
elm::t::out
typename type_info< T >::out_t out
Definition: type_info.h:284
elm::type_info::is_enum
@ is_enum
Definition: type_info.h:61
elm::signed_info
Definition: type_info.h:128
elm::type_t
elm::type_t type_t
elm::ref_t::ref
static delegate ref(T *&v)
Definition: type_info.h:257
elm::type_info< cstring >::ref
static cstring & ref(cstring &v)
Definition: type_info.h:192
elm::scalar_t::is_enum
@ is_enum
Definition: type_info.h:87
elm::type_info::mut_t
T & mut_t
Definition: type_info.h:74
elm::type_info< string >::put
static void put(var_t &x, in_t v)
Definition: type_info.h:217
elm::type_info::is_class
@ is_class
Definition: type_info.h:58
elm::type_info< float >::max
static const float max
Definition: type_info.h:167
elm::type_t
Definition: type_info.h:79
elm::type_info< long double >::min
static const long double min
Definition: type_info.h:178
elm::type_info< string >::out_t
string & out_t
Definition: type_info.h:211
elm::signed_info::max
static const I max
Definition: type_info.h:132
elm::t::int32
int int32
Definition: arch.h:30
elm::scalar_t::mut_t
T & mut_t
Definition: type_info.h:100
elm::intern::is_scalar
Definition: type_info.h:36
elm::t::ref
mut< T > ref(var< T > &x)
Definition: type_info.h:289
elm::ref_t::mut_t
delegate mut_t
Definition: type_info.h:264
elm::t::uint16
unsigned short uint16
Definition: arch.h:29
elm::default_t::is_type
@ is_type
Definition: type_info.h:43
elm::scalar_t::in_t
T in_t
Definition: type_info.h:97
elm::type_info< cstring >::ret_t
cstring ret_t
Definition: type_info.h:198
elm::scalar_t::out_t
T & out_t
Definition: type_info.h:98
elm::t::var
typename type_info< T >::var_t var
Definition: type_info.h:282
elm::default_t::is_class
@ is_class
Definition: type_info.h:46
elm::signed_info::min
static const I min
Definition: type_info.h:131
elm::type_info< const T * >::null
static const T *const null
Definition: type_info.h:226
elm::max
const T & max(const T &x, const T &y)
Definition: compare.h:108
elm::type_info< cstring >::get
static cstring get(const cstring &v)
Definition: type_info.h:193
elm::type_info< string >::in_t
const typedef string & in_t
Definition: type_info.h:210
elm::type_info< float >::min
static const float min
Definition: type_info.h:166
elm::ref_t::out_t
T & out_t
Definition: type_info.h:262
elm::type_info< double >::min
static const double min
Definition: type_info.h:172
elm::scalar_t::var_t
T var_t
Definition: type_info.h:91
elm::t::int64
long int64
Definition: arch.h:32
elm::type_info::in_t
const typedef T & in_t
Definition: type_info.h:71
elm::ref_t::delegate::operator=
delegate & operator=(T &r)
Definition: type_info.h:250
elm::ref_t::delegate::delegate
delegate(T *&ptr)
Definition: type_info.h:248
elm::intern::is_scalar::_
@ _
Definition: type_info.h:37
elm::default_t::is_deep
@ is_deep
Definition: type_info.h:49
elm::type_info< string >::embed_t
var_t embed_t
Definition: type_info.h:209
elm::type_info
Definition: type_info.h:56
elm::type_info::ret_t
const typedef T & ret_t
Definition: type_info.h:73
void
elm::ref_t::var_t
T * var_t
Definition: type_info.h:255
bool
elm::type_info< cstring >::put
static void put(cstring &x, cstring v)
Definition: type_info.h:194
elm::CString
Definition: CString.h:17
elm::scalar_t::ref
static T & ref(T &v)
Definition: type_info.h:93
elm::min
const T & min(const T &x, const T &y)
Definition: compare.h:104
elm::type_info< cstring >::in_t
cstring in_t
Definition: type_info.h:196
elm::type_t::is_type
@ is_type
Definition: type_info.h:80
elm::type_info< bool >::name
static CString name(void)
Definition: type_info.h:116
elm::default_t::is_defined_enum
@ is_defined_enum
Definition: type_info.h:45
elm::t::int16
short int16
Definition: arch.h:28
elm::ref_t::ret_t
T & ret_t
Definition: type_info.h:263
elm::t::in
typename type_info< T >::in_t in
Definition: type_info.h:283
elm::default_t::is_void
@ is_void
Definition: type_info.h:51
elm::ref_t::embed_t
var_t embed_t
Definition: type_info.h:256
elm::default_t::is_ptr
@ is_ptr
Definition: type_info.h:47
elm::scalar_t::get
static T get(const T &v)
Definition: type_info.h:94
elm::enum_t::is_enum
@ is_enum
Definition: type_info.h:107
elm::signed_info::size
static const int size
Definition: type_info.h:129
elm::scalar_t::is_deep
@ is_deep
Definition: type_info.h:89
elm::type_info::embed_t
var_t embed_t
Definition: type_info.h:66
elm::scalar_t::is_scalar
@ is_scalar
Definition: type_info.h:88
elm::type_info< cstring >::var_t
cstring var_t
Definition: type_info.h:190
elm
Definition: adapter.h:26
elm::t::uint64
unsigned long uint64
Definition: arch.h:33
elm::ref_t::in_t
T & in_t
Definition: type_info.h:261
elm::enum_t
Definition: type_info.h:106
elm::unsigned_info::min
static const I min
Definition: type_info.h:144
elm::unsigned_info::max
static const I max
Definition: type_info.h:145
elm::type_info::ref
static T & ref(T &v)
Definition: type_info.h:67
elm::signed_info::shift
static const int shift
Definition: type_info.h:134
elm::default_t::is_virtual
@ is_virtual
Definition: type_info.h:50
elm::type_info< cstring >::mut_t
cstring & mut_t
Definition: type_info.h:199
elm::default_t::is_enum
@ is_enum
Definition: type_info.h:45
elm::t::uint8
unsigned char uint8
Definition: arch.h:27
elm::type_info< string >::mut_t
string & mut_t
Definition: type_info.h:213
elm::t::put
void put(var< T > &x, in< T > v)
Definition: type_info.h:287
elm::ref_t::put
static void put(T *&x, T &v)
Definition: type_info.h:259
elm::type_info::is_deep
@ is_deep
Definition: type_info.h:59
elm::type_info< double >::max
static const double max
Definition: type_info.h:173
elm::type_info< T * >::name
static string name(void)
Definition: type_info.h:237
elm::type_info::name
static cstring name(void)
Definition: type_info.h:63
elm::ref_t
Definition: type_info.h:244
elm::scalar_t
Definition: type_info.h:86
elm::t::uint32
unsigned int uint32
Definition: arch.h:31
elm::t::get
ret< T > get(const var< T > &v)
Definition: type_info.h:288
elm::type_info< T & >::of
T of
Definition: type_info.h:274
elm::type_info< T & >::name
static string name(void)
Definition: type_info.h:276
elm::type_info::put
static void put(T &x, const T &v)
Definition: type_info.h:69
elm::String
Definition: String.h:30
elm::default_t
struct elm::default_t default_t
elm::type_info< T * >::of
T of
Definition: type_info.h:233
elm::type_info::var_t
T var_t
Definition: type_info.h:65
elm::signed_info::is_signed
static const bool is_signed
Definition: type_info.h:130
elm::intern::test_class
char test_class(int T::*)
elm::scalar_t::put
static void put(T &x, T v)
Definition: type_info.h:95
elm::ref_t::delegate
Definition: type_info.h:246
elm::t::ret
typename type_info< T >::ret_t ret
Definition: type_info.h:285
elm::type_info< long double >::max
static const long double max
Definition: type_info.h:179
elm::type_info< string >::get
static ret_t get(const var_t &v)
Definition: type_info.h:216
elm::type_info< string >::ref
static mut_t ref(var_t &v)
Definition: type_info.h:215
elm::default_t::is_ref
@ is_ref
Definition: type_info.h:48
elm::scalar_t::embed_t
var_t embed_t
Definition: type_info.h:92
elm::type_info< cstring >::out_t
cstring & out_t
Definition: type_info.h:197
elm::ti
Definition: type_info.h:279
elm::unsigned_info::size
static const int size
Definition: type_info.h:142
elm::type_t::is_deep
@ is_deep
Definition: type_info.h:81
elm::type_info< const T * >::of
T of
Definition: type_info.h:223
elm::type_info< const T & >::name
static string name(void)
Definition: type_info.h:270
elm::type_info::get
static const T & get(const T &v)
Definition: type_info.h:68
elm::scalar_t::ret_t
T ret_t
Definition: type_info.h:99
elm::unsigned_info::shift
static const int shift
Definition: type_info.h:147
elm::default_t
Definition: type_info.h:42
elm::type_info< string >::var_t
string var_t
Definition: type_info.h:208
elm::type_info::is_scalar
@ is_scalar
Definition: type_info.h:62
elm::t::mut
typename type_info< T >::mut_t mut
Definition: type_info.h:286
elm::type_info< string >::ret_t
const typedef string & ret_t
Definition: type_info.h:212
elm::unsigned_info
Definition: type_info.h:141
elm::t::int8
signed char int8
Definition: arch.h:26
elm::type_info::is_virtual
@ is_virtual
Definition: type_info.h:60
elm::type_info< const T * >::name
static string name(void)
Definition: type_info.h:227
elm::type_info< cstring >::embed_t
var_t embed_t
Definition: type_info.h:191
elm::default_t::is_scalar
@ is_scalar
Definition: type_info.h:44