1 |
/** |
2 |
* type_traits.h: Some templates that provide similar functionality as tr1::type_traits, but specialised for Ermyth. |
3 |
* |
4 |
* Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team |
5 |
* Rights to this code are as documented in doc/pod/gplicense.pod. |
6 |
* |
7 |
* $Id: type_traits.h,v 1.4 2007-09-05 11:23:13 pippijn Exp $ |
8 |
*/ |
9 |
|
10 |
#ifndef ERMYTH_TYPE_TRAITS_H |
11 |
#define ERMYTH_TYPE_TRAITS_H |
12 |
|
13 |
namespace types |
14 |
{ |
15 |
template<typename T, T v> |
16 |
struct integral_constant |
17 |
{ |
18 |
typedef T value_type; |
19 |
typedef integral_constant<T, v> type; |
20 |
static T const value = v; |
21 |
}; |
22 |
|
23 |
typedef integral_constant<bool, true> true_type; |
24 |
typedef integral_constant<bool, false> false_type; |
25 |
|
26 |
template<typename> |
27 |
struct is_integral |
28 |
: false_type |
29 |
{ |
30 |
}; |
31 |
|
32 |
#define ADD_INTEGRAL(type) \ |
33 |
template<> \ |
34 |
struct is_integral<type> \ |
35 |
: true_type \ |
36 |
{ \ |
37 |
} |
38 |
|
39 |
ADD_INTEGRAL ( bool ); |
40 |
ADD_INTEGRAL ( signed char ); |
41 |
ADD_INTEGRAL (unsigned char ); |
42 |
ADD_INTEGRAL ( signed short); |
43 |
ADD_INTEGRAL (unsigned short); |
44 |
ADD_INTEGRAL ( signed int ); |
45 |
ADD_INTEGRAL (unsigned int ); |
46 |
ADD_INTEGRAL ( signed long ); |
47 |
ADD_INTEGRAL (unsigned long ); |
48 |
|
49 |
#undef ADD_INTEGRAL |
50 |
|
51 |
enum name |
52 |
{ |
53 |
type_nothing, |
54 |
type_bool, |
55 |
type_char, |
56 |
type_int, |
57 |
type_charptr |
58 |
}; |
59 |
|
60 |
typedef integral_constant<name, type_nothing> nothing_type; |
61 |
template<typename> |
62 |
struct get |
63 |
: public nothing_type |
64 |
{ |
65 |
}; |
66 |
|
67 |
#define ADD_TYPE(real_type, type_name) \ |
68 |
typedef integral_constant<name, type_ ## type_name> type_name ## _type; \ |
69 |
template<> \ |
70 |
struct get<real_type> \ |
71 |
: public type_name ## _type \ |
72 |
{ \ |
73 |
} |
74 |
|
75 |
ADD_TYPE (bool, bool); |
76 |
ADD_TYPE (char, char); |
77 |
ADD_TYPE (int, int); |
78 |
ADD_TYPE (char *, charptr); |
79 |
|
80 |
#undef ADD_TYPE |
81 |
} |
82 |
|
83 |
#endif |