1 | //////////////////////////////////////////////////////////////////////////////// |
---|
2 | // The Loki Library |
---|
3 | // Copyright (c) 2001 by Andrei Alexandrescu |
---|
4 | // This code accompanies the book: |
---|
5 | // Alexandrescu, Andrei. "Modern C++ Design: Generic Programming and Design |
---|
6 | // Patterns Applied". Copyright (c) 2001. Addison-Wesley. |
---|
7 | // Permission to use, copy, modify, distribute and sell this software for any |
---|
8 | // purpose is hereby granted without fee, provided that the above copyright |
---|
9 | // notice appear in all copies and that both that copyright notice and this |
---|
10 | // permission notice appear in supporting documentation. |
---|
11 | // The author or Addison-Wesley Longman make no representations about the |
---|
12 | // suitability of this software for any purpose. It is provided "as is" |
---|
13 | // without express or implied warranty. |
---|
14 | // |
---|
15 | // Changes by Orxonox (Reto Grieder) |
---|
16 | // Removed all stdInt, etc. type traits and function pointer traits |
---|
17 | // and added UnqualifiedReferredType. |
---|
18 | //////////////////////////////////////////////////////////////////////////////// |
---|
19 | #ifndef LOKI_TYPETRAITS_INC_ |
---|
20 | #define LOKI_TYPETRAITS_INC_ |
---|
21 | |
---|
22 | // $Id: TypeTraits.h 1069 2010-04-19 03:09:59Z rich_sposato $ |
---|
23 | |
---|
24 | |
---|
25 | #include <loki/NullType.h> |
---|
26 | |
---|
27 | #if (defined _MSC_VER) && (_MSC_VER < 1400) |
---|
28 | #include <string> |
---|
29 | #endif |
---|
30 | |
---|
31 | |
---|
32 | #ifdef _MSC_VER |
---|
33 | #pragma warning( push ) |
---|
34 | #pragma warning( disable : 4180 ) //qualifier applied to function type has no meaning; ignored |
---|
35 | #endif |
---|
36 | |
---|
37 | namespace Loki |
---|
38 | { |
---|
39 | //////////////////////////////////////////////////////////////////////////////// |
---|
40 | // class template IsCustomUnsignedInt |
---|
41 | // Offers a means to integrate nonstandard built-in unsigned integral types |
---|
42 | // (such as unsigned __int64 or unsigned long long int) with the TypeTraits |
---|
43 | // class template defined below. |
---|
44 | // Invocation: IsCustomUnsignedInt<T> where T is any type |
---|
45 | // Defines 'value', an enum that is 1 iff T is a custom built-in unsigned |
---|
46 | // integral type |
---|
47 | // Specialize this class template for nonstandard unsigned integral types |
---|
48 | // and define value = 1 in those specializations |
---|
49 | //////////////////////////////////////////////////////////////////////////////// |
---|
50 | |
---|
51 | template <typename T> |
---|
52 | struct IsCustomUnsignedInt |
---|
53 | { |
---|
54 | enum { value = 0 }; |
---|
55 | }; |
---|
56 | |
---|
57 | //////////////////////////////////////////////////////////////////////////////// |
---|
58 | // class template IsCustomSignedInt |
---|
59 | // Offers a means to integrate nonstandard built-in unsigned integral types |
---|
60 | // (such as unsigned __int64 or unsigned long long int) with the TypeTraits |
---|
61 | // class template defined below. |
---|
62 | // Invocation: IsCustomSignedInt<T> where T is any type |
---|
63 | // Defines 'value', an enum that is 1 iff T is a custom built-in signed |
---|
64 | // integral type |
---|
65 | // Specialize this class template for nonstandard unsigned integral types |
---|
66 | // and define value = 1 in those specializations |
---|
67 | //////////////////////////////////////////////////////////////////////////////// |
---|
68 | |
---|
69 | template <typename T> |
---|
70 | struct IsCustomSignedInt |
---|
71 | { |
---|
72 | enum { value = 0 }; |
---|
73 | }; |
---|
74 | |
---|
75 | //////////////////////////////////////////////////////////////////////////////// |
---|
76 | // class template IsCustomFloat |
---|
77 | // Offers a means to integrate nonstandard floating point types with the |
---|
78 | // TypeTraits class template defined below. |
---|
79 | // Invocation: IsCustomFloat<T> where T is any type |
---|
80 | // Defines 'value', an enum that is 1 iff T is a custom built-in |
---|
81 | // floating point type |
---|
82 | // Specialize this class template for nonstandard unsigned integral types |
---|
83 | // and define value = 1 in those specializations |
---|
84 | //////////////////////////////////////////////////////////////////////////////// |
---|
85 | |
---|
86 | template <typename T> |
---|
87 | struct IsCustomFloat |
---|
88 | { |
---|
89 | enum { value = 0 }; |
---|
90 | }; |
---|
91 | |
---|
92 | //////////////////////////////////////////////////////////////////////////////// |
---|
93 | // Helper types for class template TypeTraits defined below |
---|
94 | //////////////////////////////////////////////////////////////////////////////// |
---|
95 | |
---|
96 | namespace Private |
---|
97 | { |
---|
98 | template <typename U> struct AddPointer |
---|
99 | { |
---|
100 | typedef U* Result; |
---|
101 | }; |
---|
102 | |
---|
103 | template <typename U> struct AddPointer<U&> |
---|
104 | { |
---|
105 | typedef U* Result; |
---|
106 | }; |
---|
107 | |
---|
108 | template <class U> struct AddReference |
---|
109 | { |
---|
110 | typedef U & Result; |
---|
111 | }; |
---|
112 | |
---|
113 | template <class U> struct AddReference<U &> |
---|
114 | { |
---|
115 | typedef U & Result; |
---|
116 | }; |
---|
117 | |
---|
118 | template <> struct AddReference<void> |
---|
119 | { |
---|
120 | typedef NullType Result; |
---|
121 | }; |
---|
122 | |
---|
123 | template <class U> struct AddParameterType |
---|
124 | { |
---|
125 | typedef const U & Result; |
---|
126 | }; |
---|
127 | |
---|
128 | template <class U> struct AddParameterType<U &> |
---|
129 | { |
---|
130 | typedef U & Result; |
---|
131 | }; |
---|
132 | |
---|
133 | template <> struct AddParameterType<void> |
---|
134 | { |
---|
135 | typedef NullType Result; |
---|
136 | }; |
---|
137 | }// namespace Private |
---|
138 | |
---|
139 | //////////////////////////////////////////////////////////////////////////////// |
---|
140 | // class template TypeTraits |
---|
141 | // |
---|
142 | // Figures out at compile time various properties of any given type |
---|
143 | // Invocations (T is a type, TypeTraits<T>::Property): |
---|
144 | // |
---|
145 | // - isPointer : returns true if T is a pointer type |
---|
146 | // - PointeeType : returns the type to which T points if T is a pointer |
---|
147 | // type, NullType otherwise |
---|
148 | // - isReference : returns true if T is a reference type |
---|
149 | // - ReferredType : returns the type to which T refers if T is a reference |
---|
150 | // type, NullType otherwise |
---|
151 | // - ParameterType : returns the optimal type to be used as a parameter for |
---|
152 | // functions that take Ts |
---|
153 | // - isConst : returns true if T is a const-qualified type |
---|
154 | // - NonConstType : Type with removed 'const' qualifier from T, if any |
---|
155 | // - isVolatile : returns true if T is a volatile-qualified type |
---|
156 | // - NonVolatileType : Type with removed 'volatile' qualifier from T, if any |
---|
157 | // - UnqualifiedType : Type with removed 'const' and 'volatile' qualifiers from |
---|
158 | // T, if any |
---|
159 | // - ParameterType : returns the optimal type to be used as a parameter |
---|
160 | // for functions that take 'const T's |
---|
161 | // |
---|
162 | //////////////////////////////////////////////////////////////////////////////// |
---|
163 | |
---|
164 | template <typename T> |
---|
165 | class TypeTraits |
---|
166 | { |
---|
167 | private: |
---|
168 | |
---|
169 | template <class U> struct ReferenceTraits |
---|
170 | { |
---|
171 | enum { result = false }; |
---|
172 | typedef U ReferredType; |
---|
173 | }; |
---|
174 | |
---|
175 | template <class U> struct ReferenceTraits<U&> |
---|
176 | { |
---|
177 | enum { result = true }; |
---|
178 | typedef U ReferredType; |
---|
179 | }; |
---|
180 | |
---|
181 | template <class U> struct PointerTraits |
---|
182 | { |
---|
183 | enum { result = false }; |
---|
184 | typedef NullType PointeeType; |
---|
185 | }; |
---|
186 | |
---|
187 | template <class U> struct PointerTraits<U*> |
---|
188 | { |
---|
189 | enum { result = true }; |
---|
190 | typedef U PointeeType; |
---|
191 | }; |
---|
192 | |
---|
193 | template <class U> struct PointerTraits<U*&> |
---|
194 | { |
---|
195 | enum { result = true }; |
---|
196 | typedef U PointeeType; |
---|
197 | }; |
---|
198 | |
---|
199 | template <class U> struct PToMTraits |
---|
200 | { |
---|
201 | enum { result = false }; |
---|
202 | }; |
---|
203 | |
---|
204 | template <class U, class V> struct PToMTraits<U V::*> |
---|
205 | { |
---|
206 | enum { result = true }; |
---|
207 | }; |
---|
208 | |
---|
209 | template <class U, class V> struct PToMTraits<U V::*&> |
---|
210 | { |
---|
211 | enum { result = true }; |
---|
212 | }; |
---|
213 | |
---|
214 | template <class U> struct UnConst |
---|
215 | { |
---|
216 | typedef U Result; |
---|
217 | enum { isConst = 0 }; |
---|
218 | }; |
---|
219 | |
---|
220 | template <class U> struct UnConst<const U> |
---|
221 | { |
---|
222 | typedef U Result; |
---|
223 | enum { isConst = 1 }; |
---|
224 | }; |
---|
225 | |
---|
226 | template <class U> struct UnConst<const U&> |
---|
227 | { |
---|
228 | typedef U& Result; |
---|
229 | enum { isConst = 1 }; |
---|
230 | }; |
---|
231 | |
---|
232 | template <class U> struct UnVolatile |
---|
233 | { |
---|
234 | typedef U Result; |
---|
235 | enum { isVolatile = 0 }; |
---|
236 | }; |
---|
237 | |
---|
238 | template <class U> struct UnVolatile<volatile U> |
---|
239 | { |
---|
240 | typedef U Result; |
---|
241 | enum { isVolatile = 1 }; |
---|
242 | }; |
---|
243 | |
---|
244 | template <class U> struct UnVolatile<volatile U&> |
---|
245 | { |
---|
246 | typedef U& Result; |
---|
247 | enum { isVolatile = 1 }; |
---|
248 | }; |
---|
249 | |
---|
250 | public: |
---|
251 | typedef typename UnConst<T>::Result |
---|
252 | NonConstType; |
---|
253 | typedef typename UnVolatile<T>::Result |
---|
254 | NonVolatileType; |
---|
255 | typedef typename UnVolatile<typename UnConst<T>::Result>::Result |
---|
256 | UnqualifiedType; |
---|
257 | typedef typename PointerTraits<UnqualifiedType>::PointeeType |
---|
258 | PointeeType; |
---|
259 | typedef typename ReferenceTraits<T>::ReferredType |
---|
260 | ReferredType; |
---|
261 | typedef typename ReferenceTraits<typename UnVolatile<typename UnConst<T>::Result>::Result>::ReferredType |
---|
262 | UnqualifiedReferredType; |
---|
263 | |
---|
264 | enum { isConst = UnConst<T>::isConst }; |
---|
265 | enum { isVolatile = UnVolatile<T>::isVolatile }; |
---|
266 | enum { isReference = ReferenceTraits<UnqualifiedType>::result }; |
---|
267 | enum { isPointer = PointerTraits< |
---|
268 | typename ReferenceTraits<UnqualifiedType>::ReferredType >::result}; |
---|
269 | |
---|
270 | }; |
---|
271 | } |
---|
272 | |
---|
273 | #ifdef _MSC_VER |
---|
274 | #pragma warning( pop ) |
---|
275 | #endif // _MSC_VER |
---|
276 | |
---|
277 | |
---|
278 | #endif // end file guardian |
---|
279 | |
---|