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 };
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 };
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_ */
Definition: CString.h:17
Definition: String.h:30
Definition: type_info.h:244
static T & get(T *v)
Definition: type_info.h:258
T & ret_t
Definition: type_info.h:263
T * var_t
Definition: type_info.h:255
T & out_t
Definition: type_info.h:262
static void put(T *&x, T &v)
Definition: type_info.h:259
var_t embed_t
Definition: type_info.h:256
T & in_t
Definition: type_info.h:261
static delegate ref(T *&v)
Definition: type_info.h:257
delegate mut_t
Definition: type_info.h:264
Definition: type_info.h:56
const T & ret_t
Definition: type_info.h:73
T & out_t
Definition: type_info.h:72
@ is_scalar
Definition: type_info.h:62
static cstring name(void)
Definition: type_info.h:63
var_t embed_t
Definition: type_info.h:66
static T & ref(T &v)
Definition: type_info.h:67
T var_t
Definition: type_info.h:65
@ is_virtual
Definition: type_info.h:60
static void put(T &x, const T &v)
Definition: type_info.h:69
@ is_class
Definition: type_info.h:58
const T & in_t
Definition: type_info.h:71
@ is_enum
Definition: type_info.h:61
static const T & get(const T &v)
Definition: type_info.h:68
@ is_deep
Definition: type_info.h:59
T & mut_t
Definition: type_info.h:74
typename type_info< T >::var_t var
Definition: type_info.h:282
unsigned long uint64
Definition: arch.h:33
typename type_info< T >::mut_t mut
Definition: type_info.h:286
short int16
Definition: arch.h:28
ret< T > get(const var< T > &v)
Definition: type_info.h:288
unsigned char uint8
Definition: arch.h:27
typename type_info< T >::ret_t ret
Definition: type_info.h:285
long int64
Definition: arch.h:32
typename type_info< T >::out_t out
Definition: type_info.h:284
mut< T > ref(var< T > &x)
Definition: type_info.h:289
signed char int8
Definition: arch.h:26
unsigned int uint32
Definition: arch.h:31
unsigned short uint16
Definition: arch.h:29
typename type_info< T >::in_t in
Definition: type_info.h:283
int int32
Definition: arch.h:30
void put(var< T > &x, in< T > v)
Definition: type_info.h:287
const T & max(const T &x, const T &y)
Definition: compare.h:108
const T & min(const T &x, const T &y)
Definition: compare.h:104
char test_class(int T::*)
Definition: adapter.h:26
struct elm::default_t default_t
elm::type_t type_t
Definition: type_info.h:42
@ is_virtual
Definition: type_info.h:50
@ is_class
Definition: type_info.h:46
@ is_defined_enum
Definition: type_info.h:45
@ is_enum
Definition: type_info.h:45
@ is_deep
Definition: type_info.h:49
@ is_scalar
Definition: type_info.h:44
@ is_void
Definition: type_info.h:51
@ is_type
Definition: type_info.h:43
@ is_ref
Definition: type_info.h:48
@ is_ptr
Definition: type_info.h:47
Definition: type_info.h:106
@ is_enum
Definition: type_info.h:107
Definition: type_info.h:36
@ _
Definition: type_info.h:37
Definition: type_info.h:246
delegate(T *&ptr)
Definition: type_info.h:248
delegate & operator=(T &r)
Definition: type_info.h:250
Definition: type_info.h:86
T var_t
Definition: type_info.h:91
@ is_enum
Definition: type_info.h:87
static void put(T &x, T v)
Definition: type_info.h:95
T & out_t
Definition: type_info.h:98
@ is_scalar
Definition: type_info.h:88
@ is_deep
Definition: type_info.h:89
static T & ref(T &v)
Definition: type_info.h:93
T & mut_t
Definition: type_info.h:100
T in_t
Definition: type_info.h:97
var_t embed_t
Definition: type_info.h:92
T ret_t
Definition: type_info.h:99
static T get(const T &v)
Definition: type_info.h:94
Definition: type_info.h:128
static const bool is_signed
Definition: type_info.h:130
static const int shift
Definition: type_info.h:134
static const int size
Definition: type_info.h:129
static const I min
Definition: type_info.h:131
static const I max
Definition: type_info.h:132
Definition: type_info.h:279
static string name(void)
Definition: type_info.h:237
T of
Definition: type_info.h:233
static T *const null
Definition: type_info.h:236
static string name(void)
Definition: type_info.h:276
T of
Definition: type_info.h:274
static CString name(void)
Definition: type_info.h:116
static cstring name(void)
static string name(void)
Definition: type_info.h:227
static const T *const null
Definition: type_info.h:226
T of
Definition: type_info.h:223
static string name(void)
Definition: type_info.h:270
T of
Definition: type_info.h:268
cstring var_t
Definition: type_info.h:190
cstring ret_t
Definition: type_info.h:198
static void put(cstring &x, cstring v)
Definition: type_info.h:194
cstring & out_t
Definition: type_info.h:197
var_t embed_t
Definition: type_info.h:191
static cstring & ref(cstring &v)
Definition: type_info.h:192
cstring & mut_t
Definition: type_info.h:199
cstring in_t
Definition: type_info.h:196
static cstring get(const cstring &v)
Definition: type_info.h:193
static const double min
Definition: type_info.h:172
static const double max
Definition: type_info.h:173
static const float max
Definition: type_info.h:167
static const float min
Definition: type_info.h:166
static const long double min
Definition: type_info.h:178
static const long double max
Definition: type_info.h:179
static ret_t get(const var_t &v)
Definition: type_info.h:216
var_t embed_t
Definition: type_info.h:209
string & mut_t
Definition: type_info.h:213
static void put(var_t &x, in_t v)
Definition: type_info.h:217
static mut_t ref(var_t &v)
Definition: type_info.h:215
string & out_t
Definition: type_info.h:211
string var_t
Definition: type_info.h:208
const string & in_t
Definition: type_info.h:210
const string & ret_t
Definition: type_info.h:212
Definition: type_info.h:79
@ is_deep
Definition: type_info.h:81
@ is_type
Definition: type_info.h:80
Definition: type_info.h:141
static const bool is_signed
Definition: type_info.h:143
static const int shift
Definition: type_info.h:147
static const int size
Definition: type_info.h:142
static const I min
Definition: type_info.h:144
static const I max
Definition: type_info.h:145