comparison tango/tango/core/Traits.d @ 132:1700239cab2e trunk

[svn r136] MAJOR UNSTABLE UPDATE!!! Initial commit after moving to Tango instead of Phobos. Lots of bugfixes... This build is not suitable for most things.
author lindquist
date Fri, 11 Jan 2008 17:57:40 +0100
parents
children
comparison
equal deleted inserted replaced
131:5825d48b27d1 132:1700239cab2e
1 /**
2 * The traits module defines tools useful for obtaining detailed type
3 * information at compile-time.
4 *
5 * Copyright: Copyright (C) 2005-2006 Sean Kelly. All rights reserved.
6 * License: BSD style: $(LICENSE)
7 * Authors: Sean Kelly
8 */
9 module tango.core.Traits;
10
11
12 /**
13 *
14 */
15 template isCharType( T )
16 {
17 const bool isCharType = is( T == char ) ||
18 is( T == wchar ) ||
19 is( T == dchar );
20 }
21
22
23 /**
24 *
25 */
26 template isSignedIntegerType( T )
27 {
28 const bool isSignedIntegerType = is( T == byte ) ||
29 is( T == short ) ||
30 is( T == int ) ||
31 is( T == long )/+||
32 is( T == cent )+/;
33 }
34
35
36 /**
37 *
38 */
39 template isUnsignedIntegerType( T )
40 {
41 const bool isUnsignedIntegerType = is( T == ubyte ) ||
42 is( T == ushort ) ||
43 is( T == uint ) ||
44 is( T == ulong )/+||
45 is( T == ucent )+/;
46 }
47
48
49 /**
50 *
51 */
52 template isIntegerType( T )
53 {
54 const bool isIntegerType = isSignedIntegerType!(T) ||
55 isUnsignedIntegerType!(T);
56 }
57
58
59 /**
60 *
61 */
62 template isRealType( T )
63 {
64 const bool isRealType = is( T == float ) ||
65 is( T == double ) ||
66 is( T == real );
67 }
68
69
70 /**
71 *
72 */
73 template isComplexType( T )
74 {
75 const bool isComplexType = is( T == cfloat ) ||
76 is( T == cdouble ) ||
77 is( T == creal );
78 }
79
80
81 /**
82 *
83 */
84 template isImaginaryType( T )
85 {
86 const bool isImaginaryType = is( T == ifloat ) ||
87 is( T == idouble ) ||
88 is( T == ireal );
89 }
90
91
92 /**
93 *
94 */
95 template isFloatingPointType( T )
96 {
97 const bool isFloatingPointType = isRealType!(T) ||
98 isComplexType!(T) ||
99 isImaginaryType!(T);
100 }
101
102
103 /**
104 *
105 */
106 template isPointerType( T )
107 {
108 const bool isPointerType = is( typeof(*T) );
109 }
110
111
112 /**
113 *
114 */
115 template isReferenceType( T )
116 {
117
118 const bool isReferenceType = isPointerType!(T) ||
119 is( T == class ) ||
120 is( T == interface ) ||
121 is( T == delegate );
122 }
123
124
125 /**
126 *
127 */
128 template isDynamicArrayType( T )
129 {
130 const bool isDynamicArrayType = is( typeof(T.init[0])[] == T );
131 }
132
133
134 /**
135 *
136 */
137 template isStaticArrayType( T )
138 {
139 const bool isStaticArrayType = is( typeof(T.init)[(T).sizeof / typeof(T.init).sizeof] == T );
140 }
141
142
143 /**
144 *
145 */
146 private template isAssocArrayType( T )
147 {
148 const bool isAssocArrayType = is( typeof(T.init.values[0])[typeof(T.init.keys[0])] == T );
149 }
150
151
152 /**
153 *
154 */
155 template isCallableType( T )
156 {
157 const bool isCallableType = is( T == function ) ||
158 is( typeof(*T) == function ) ||
159 is( T == delegate ) ||
160 is( typeof(T.opCall) == function );
161 }
162
163
164 /**
165 *
166 */
167 template ReturnTypeOf( Fn )
168 {
169 static if( is( Fn Ret == return ) )
170 alias Ret ReturnTypeOf;
171 else
172 static assert( false, "Argument has no return type." );
173 }
174
175
176 /**
177 *
178 */
179 template ReturnTypeOf( alias fn )
180 {
181 alias ReturnTypeOf!(typeof(fn)) ReturnTypeOf;
182 }
183
184
185 /**
186 *
187 */
188 template ParameterTupleOf( Fn )
189 {
190 static if( is( Fn Params == function ) )
191 alias Params ParameterTupleOf;
192 else static if( is( Fn Params == delegate ) )
193 alias ParameterTupleOf!(Params) ParameterTupleOf;
194 else static if( is( Fn Params == Params* ) )
195 alias ParameterTupleOf!(Params) ParameterTupleOf;
196 else
197 static assert( false, "Argument has no parameters." );
198 }
199
200
201 /**
202 *
203 */
204 template ParameterTupleOf( alias fn )
205 {
206 alias ParameterTupleOf!(typeof(fn)) ParameterTupleOf;
207 }
208
209
210 /**
211 *
212 */
213 template BaseTypeTupleOf( T )
214 {
215 static if( is( T Base == super ) )
216 alias Base BaseTypeTupleOf;
217 else
218 static assert( false, "Argument is not a class or interface." );
219 }