[ VIGRA Homepage | Function Index | Class Index | Namespaces | File List | Main Page ]

metaprogramming.hxx
1 /************************************************************************/
2 /* */
3 /* Copyright 1998-2002 by Ullrich Koethe */
4 /* */
5 /* This file is part of the VIGRA computer vision library. */
6 /* The VIGRA Website is */
7 /* http://hci.iwr.uni-heidelberg.de/vigra/ */
8 /* Please direct questions, bug reports, and contributions to */
9 /* ullrich.koethe@iwr.uni-heidelberg.de or */
10 /* vigra@informatik.uni-hamburg.de */
11 /* */
12 /* Permission is hereby granted, free of charge, to any person */
13 /* obtaining a copy of this software and associated documentation */
14 /* files (the "Software"), to deal in the Software without */
15 /* restriction, including without limitation the rights to use, */
16 /* copy, modify, merge, publish, distribute, sublicense, and/or */
17 /* sell copies of the Software, and to permit persons to whom the */
18 /* Software is furnished to do so, subject to the following */
19 /* conditions: */
20 /* */
21 /* The above copyright notice and this permission notice shall be */
22 /* included in all copies or substantial portions of the */
23 /* Software. */
24 /* */
25 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND */
26 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES */
27 /* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
28 /* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
29 /* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
30 /* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
31 /* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR */
32 /* OTHER DEALINGS IN THE SOFTWARE. */
33 /* */
34 /************************************************************************/
35 
36 #ifndef VIGRA_METAPROGRAMMING_HXX
37 #define VIGRA_METAPROGRAMMING_HXX
38 
39 #include "config.hxx"
40 #include <climits>
41 #include <limits>
42 
43 namespace vigra {
44 
45 template <int N>
46 class MetaInt
47 {
48  public:
49  static const int value = N;
50 };
51 
52 template <int N1, int N2>
53 class MetaMax
54 {
55  public:
56  static const int value = N1 < N2 ? N2 : N1;
57 };
58 
59 template <int N1, int N2>
60 class MetaMin
61 {
62  public:
63  static const int value = N1 < N2 ? N1 : N2;
64 };
65 
66 struct VigraTrueType
67 {
68  static const bool asBool = true, value = true;
69 };
70 
71 struct VigraFalseType
72 {
73  static const bool asBool = false, value = false;
74 };
75 
76 /** \addtogroup MultiArrayTags Multi-dimensional Array Tags
77  Meta-programming tags to mark array's as strided or unstrided.
78 */
79 
80 //@{
81 
82 /********************************************************/
83 /* */
84 /* StridedArrayTag */
85 /* */
86 /********************************************************/
87 
88 /** tag for marking a MultiArray strided.
89 
90 <b>\#include</b>
91 <<a href="multi__array_8hxx-source.html">vigra/multi_array.hxx</a>>
92 
93 Namespace: vigra
94 */
95 struct StridedArrayTag {};
96 
97 /********************************************************/
98 /* */
99 /* UnstridedArrayTag */
100 /* */
101 /********************************************************/
102 
103 /** tag for marking a MultiArray unstrided.
104 
105 <b>\#include</b>
106 <<a href="multi__array_8hxx-source.html">vigra/multi_array.hxx</a>>
107 
108 Namespace: vigra
109 */
111 
112 template<class T>
113 class TypeTraits
114 {
115  public:
116  typedef VigraFalseType isConst;
117  typedef VigraFalseType isPOD;
118  typedef VigraFalseType isBuiltinType;
119 };
120 
121 #ifndef NO_PARTIAL_TEMPLATE_SPECIALIZATION
122 
123 template<class T>
124 class TypeTraits<T const>
125 : public TypeTraits<T>
126 {
127  public:
128  typedef VigraTrueType isConst;
129 };
130 
131 template<class T>
132 class TypeTraits<T *>
133 {
134  public:
135  typedef VigraFalseType isConst;
136  typedef VigraTrueType isPOD;
137  typedef VigraTrueType isBuiltinType;
138 };
139 
140 template<class T>
141 class TypeTraits<T const *>
142 {
143  public:
144  typedef VigraFalseType isConst;
145  typedef VigraTrueType isPOD;
146  typedef VigraTrueType isBuiltinType;
147 };
148 
149 #endif // NO_PARTIAL_TEMPLATE_SPECIALIZATION
150 
151 namespace detail {
152 
153 template <int size>
154 struct SizeToType;
155 
156 } // namespace detail
157 
158 #define VIGRA_TYPE_TRAITS(type, size) \
159 template<> \
160 class TypeTraits<type> \
161 { \
162  public: \
163  typedef VigraFalseType isConst; \
164  typedef VigraTrueType isPOD; \
165  typedef VigraTrueType isBuiltinType; \
166  typedef char TypeToSize[size]; \
167 }; \
168  \
169 namespace detail { \
170  TypeTraits<type>::TypeToSize * typeToSize(type); \
171  \
172  template <> \
173  struct SizeToType<size> \
174  { \
175  typedef type result; \
176  }; \
177 }
178 
179 VIGRA_TYPE_TRAITS(char, 1)
180 VIGRA_TYPE_TRAITS(signed char, 2)
181 VIGRA_TYPE_TRAITS(unsigned char, 3)
182 VIGRA_TYPE_TRAITS(short, 4)
183 VIGRA_TYPE_TRAITS(unsigned short, 5)
184 VIGRA_TYPE_TRAITS(int, 6)
185 VIGRA_TYPE_TRAITS(unsigned int, 7)
186 VIGRA_TYPE_TRAITS(long, 8)
187 VIGRA_TYPE_TRAITS(unsigned long, 9)
188 VIGRA_TYPE_TRAITS(float, 10)
189 VIGRA_TYPE_TRAITS(double, 11)
190 VIGRA_TYPE_TRAITS(long double, 12)
191 #ifdef LLONG_MAX
192 VIGRA_TYPE_TRAITS(long long, 13)
193 VIGRA_TYPE_TRAITS(unsigned long long, 14)
194 #endif
195 
196 #undef VIGRA_TYPE_TRAITS
197 
198 //@}
199 
200 template <class A>
201 struct Not;
202 
203 template <>
204 struct Not<VigraTrueType>
205 {
206  typedef VigraFalseType result; // deprecated
207  static const bool boolResult = false; // deprecated
208  typedef VigraFalseType type;
209  static const bool value = false;
210 };
211 
212 template <>
213 struct Not<VigraFalseType>
214 {
215  typedef VigraTrueType result; // deprecated
216  static const bool boolResult = true; // deprecated
217  typedef VigraTrueType type;
218  static const bool value = true;
219 };
220 
221 template <class L, class R>
222 struct And;
223 
224 template <>
225 struct And<VigraFalseType, VigraFalseType>
226 {
227  typedef VigraFalseType result; // deprecated
228  static const bool boolResult = false; // deprecated
229  typedef VigraFalseType type;
230  static const bool value = false;
231 };
232 
233 template <>
234 struct And<VigraFalseType, VigraTrueType>
235 {
236  typedef VigraFalseType result; // deprecated
237  static const bool boolResult = false; // deprecated
238  typedef VigraFalseType type;
239  static const bool value = false;
240 };
241 
242 template <>
243 struct And<VigraTrueType, VigraFalseType>
244 {
245  typedef VigraFalseType result; // deprecated
246  static const bool boolResult = false; // deprecated
247  typedef VigraFalseType type;
248  static const bool value = false;
249 };
250 
251 template <>
252 struct And<VigraTrueType, VigraTrueType>
253 {
254  typedef VigraTrueType result; // deprecated
255  static const bool boolResult = true; // deprecated
256  typedef VigraTrueType type;
257  static const bool value = true;
258 };
259 
260 template <class L, class R>
261 struct Or;
262 
263 template <>
264 struct Or<VigraFalseType, VigraFalseType>
265 {
266  typedef VigraFalseType result; // deprecated
267  static const bool boolResult = false; // deprecated
268  typedef VigraFalseType type;
269  static const bool value = false;
270 };
271 
272 template <>
273 struct Or<VigraTrueType, VigraFalseType>
274 {
275  typedef VigraTrueType result; // deprecated
276  static const bool boolResult = true; // deprecated
277  typedef VigraTrueType type;
278  static const bool value = true;
279 };
280 
281 template <>
282 struct Or<VigraFalseType, VigraTrueType>
283 {
284  typedef VigraTrueType result; // deprecated
285  static const bool boolResult = true; // deprecated
286  typedef VigraTrueType type;
287  static const bool value = true;
288 };
289 
290 template <>
291 struct Or<VigraTrueType, VigraTrueType>
292 {
293  typedef VigraTrueType result; // deprecated
294  static const bool boolResult = true; // deprecated
295  typedef VigraTrueType type;
296  static const bool value = true;
297 };
298 
299 #ifndef NO_PARTIAL_TEMPLATE_SPECIALIZATION
300 
301 template <class PREDICATE, class TRUECASE, class FALSECASE>
302 struct If;
303 
304 template <class TRUECASE, class FALSECASE>
305 struct If<VigraTrueType, TRUECASE, FALSECASE>
306 {
307  typedef TRUECASE type;
308 };
309 
310 template <class TRUECASE, class FALSECASE>
311 struct If<VigraFalseType, TRUECASE, FALSECASE>
312 {
313  typedef FALSECASE type;
314 };
315 
316 template <bool PREDICATE, class TRUECASE, class FALSECASE>
317 struct IfBool;
318 
319 template <class TRUECASE, class FALSECASE>
320 struct IfBool<true, TRUECASE, FALSECASE>
321 {
322  typedef TRUECASE type;
323 };
324 
325 template <class TRUECASE, class FALSECASE>
326 struct IfBool<false, TRUECASE, FALSECASE>
327 {
328  typedef FALSECASE type;
329 };
330 
331 template <class L, class R>
332 struct IsSameType
333 {
334  typedef VigraFalseType result; // deprecated
335  static const bool boolResult = false; // deprecated
336  typedef VigraFalseType type;
337  static const bool value = false;
338 };
339 
340 template <class T>
341 struct IsSameType<T, T>
342 {
343  typedef VigraTrueType result; // deprecated
344  static const bool boolResult = true; // deprecated
345  typedef VigraTrueType type;
346  static const bool value = true;
347 };
348 
349 template <class L, class R>
350 struct IsDifferentType
351 {
352  typedef VigraTrueType type;
353  static const bool value = true;
354 };
355 
356 template <class T>
357 struct IsDifferentType<T, T>
358 {
359  typedef VigraFalseType type;
360  static const bool value = false;
361 };
362 
363 #endif // NO_PARTIAL_TEMPLATE_SPECIALIZATION
364 
365 template <class From, class To>
366 struct IsConvertibleTo
367 {
368  typedef char falseResult[1];
369  typedef char trueResult[2];
370 
371  static From const & check();
372 
373  static falseResult * testIsConvertible(...);
374  static trueResult * testIsConvertible(To const &);
375 
376  enum { resultSize = sizeof(*testIsConvertible(check())) };
377 
378  static const bool value = (resultSize == 2);
379  typedef typename
380  IfBool<value, VigraTrueType, VigraFalseType>::type
381  type;
382 };
383 
384 template <class DERIVED, class BASE>
385 struct IsDerivedFrom
386 {
387  typedef char falseResult[1];
388  typedef char trueResult[2];
389 
390  static falseResult * testIsDerivedFrom(...);
391  static trueResult * testIsDerivedFrom(BASE const *);
392 
393  enum { resultSize = sizeof(*testIsDerivedFrom((DERIVED const *)0)) };
394 
395  static const bool value = (resultSize == 2);
396  typedef typename
397  IfBool<value, VigraTrueType, VigraFalseType>::type
398  type;
399 
400  static const bool boolResult = value; // deprecated
401  typedef type result; // deprecated
402 };
403 
404 template <class T>
405 struct UnqualifiedType
406 {
407  typedef T type;
408 };
409 
410 template <class T>
411 struct UnqualifiedType<T const>
412 {
413  typedef T type;
414 };
415 
416 template <class T>
417 struct UnqualifiedType<T &>
418 : public UnqualifiedType<T>
419 {};
420 
421 template <class T>
422 struct UnqualifiedType<T const &>
423 : public UnqualifiedType<T>
424 {};
425 
426 template <class T>
427 struct UnqualifiedType<T *>
428 : public UnqualifiedType<T>
429 {};
430 
431 template <class T>
432 struct UnqualifiedType<T const *>
433 : public UnqualifiedType<T>
434 {};
435 
436 
437 template <class T>
438 struct has_result_type
439 {
440  typedef char falseResult[1];
441  typedef char trueResult[2];
442 
443  static falseResult * test(...);
444  template <class U>
445  static trueResult * test(U *, typename U::result_type * = 0);
446 
447  enum { resultSize = sizeof(*test((T*)0)) };
448 
449  static const bool value = (resultSize == 2);
450  typedef typename
451  IfBool<value, VigraTrueType, VigraFalseType>::type
452  type;
453 };
454 
455 template <class T>
456 struct has_value_type
457 {
458  typedef char falseResult[1];
459  typedef char trueResult[2];
460 
461  static falseResult * test(...);
462  template <class U>
463  static trueResult * test(U *, typename U::value_type * = 0);
464 
465  enum { resultSize = sizeof(*test((T*)0)) };
466 
467  static const bool value = (resultSize == 2);
468  typedef typename
469  IfBool<value, VigraTrueType, VigraFalseType>::type
470  type;
471 };
472 
473 template <class T>
474 struct IsIterator
475 {
476  typedef char falseResult[1];
477  typedef char trueResult[2];
478 
479  static falseResult * test(...);
480  template <class U>
481  static trueResult * test(U *, typename U::iterator_category * = 0);
482 
483  enum { resultSize = sizeof(*test((T*)0)) };
484 
485  static const bool value = (resultSize == 2);
486  typedef typename
487  IfBool<value, VigraTrueType, VigraFalseType>::type
488  type;
489 };
490 
491 template <class T>
492 struct IsIterator<T*>
493 {
494  static const bool value = true;
495  typedef VigraTrueType type;
496 };
497 
498 template <class T>
499 struct IsIterator<T const *>
500 {
501  static const bool value = true;
502  typedef VigraTrueType type;
503 };
504 
505 } // namespace vigra
506 
507 #endif /* VIGRA_METAPROGRAMMING_HXX */

© Ullrich Köthe (ullrich.koethe@iwr.uni-heidelberg.de)
Heidelberg Collaboratory for Image Processing, University of Heidelberg, Germany

html generated using doxygen and Python
vigra 1.7.1 (Thu Jun 14 2012)