36 #ifndef VIGRA_METAPROGRAMMING_HXX
37 #define VIGRA_METAPROGRAMMING_HXX
48 #pragma warning( push )
49 #pragma warning( disable : 4503 )
56 static const int value = N;
59 template <
int N1,
int N2>
63 static const int value = N1 < N2 ? N2 : N1;
66 template <
int N1,
int N2>
70 static const int value = N1 < N2 ? N1 : N2;
75 static const bool asBool =
true, value =
true;
80 static const bool asBool =
false, value =
false;
119 typedef VigraFalseType isConst;
120 typedef VigraFalseType isPOD;
121 typedef VigraFalseType isBuiltinType;
124 #ifndef NO_PARTIAL_TEMPLATE_SPECIALIZATION
127 class TypeTraits<T const>
128 :
public TypeTraits<T>
131 typedef VigraTrueType isConst;
135 class TypeTraits<T *>
138 typedef VigraFalseType isConst;
139 typedef VigraTrueType isPOD;
140 typedef VigraTrueType isBuiltinType;
144 class TypeTraits<T const *>
147 typedef VigraFalseType isConst;
148 typedef VigraTrueType isPOD;
149 typedef VigraTrueType isBuiltinType;
152 #endif // NO_PARTIAL_TEMPLATE_SPECIALIZATION
161 #define VIGRA_TYPE_TRAITS(type, size) \
163 class TypeTraits<type> \
166 typedef VigraFalseType isConst; \
167 typedef VigraTrueType isPOD; \
168 typedef VigraTrueType isBuiltinType; \
169 typedef char TypeToSize[size]; \
173 TypeTraits<type>::TypeToSize * typeToSize(type); \
176 struct SizeToType<size> \
178 typedef type result; \
182 VIGRA_TYPE_TRAITS(
char, 1)
183 VIGRA_TYPE_TRAITS(
signed char, 2)
184 VIGRA_TYPE_TRAITS(
unsigned char, 3)
185 VIGRA_TYPE_TRAITS(
short, 4)
186 VIGRA_TYPE_TRAITS(
unsigned short, 5)
187 VIGRA_TYPE_TRAITS(
int, 6)
188 VIGRA_TYPE_TRAITS(
unsigned int, 7)
189 VIGRA_TYPE_TRAITS(
long, 8)
190 VIGRA_TYPE_TRAITS(
unsigned long, 9)
191 VIGRA_TYPE_TRAITS(
float, 10)
192 VIGRA_TYPE_TRAITS(
double, 11)
193 VIGRA_TYPE_TRAITS(
long double, 12)
195 VIGRA_TYPE_TRAITS(
long long, 13)
196 VIGRA_TYPE_TRAITS(
unsigned long long, 14)
199 #undef VIGRA_TYPE_TRAITS
207 struct Not<VigraTrueType>
209 typedef VigraFalseType result;
210 static const bool boolResult =
false;
211 typedef VigraFalseType type;
212 static const bool value =
false;
216 struct Not<VigraFalseType>
218 typedef VigraTrueType result;
219 static const bool boolResult =
true;
220 typedef VigraTrueType type;
221 static const bool value =
true;
224 template <
class L,
class R>
228 struct And<VigraFalseType, VigraFalseType>
230 typedef VigraFalseType result;
231 static const bool boolResult =
false;
232 typedef VigraFalseType type;
233 static const bool value =
false;
237 struct And<VigraFalseType, VigraTrueType>
239 typedef VigraFalseType result;
240 static const bool boolResult =
false;
241 typedef VigraFalseType type;
242 static const bool value =
false;
246 struct And<VigraTrueType, VigraFalseType>
248 typedef VigraFalseType result;
249 static const bool boolResult =
false;
250 typedef VigraFalseType type;
251 static const bool value =
false;
255 struct And<VigraTrueType, VigraTrueType>
257 typedef VigraTrueType result;
258 static const bool boolResult =
true;
259 typedef VigraTrueType type;
260 static const bool value =
true;
263 template <
class L,
class R>
267 struct Or<VigraFalseType, VigraFalseType>
269 typedef VigraFalseType result;
270 static const bool boolResult =
false;
271 typedef VigraFalseType type;
272 static const bool value =
false;
276 struct Or<VigraTrueType, VigraFalseType>
278 typedef VigraTrueType result;
279 static const bool boolResult =
true;
280 typedef VigraTrueType type;
281 static const bool value =
true;
285 struct Or<VigraFalseType, VigraTrueType>
287 typedef VigraTrueType result;
288 static const bool boolResult =
true;
289 typedef VigraTrueType type;
290 static const bool value =
true;
294 struct Or<VigraTrueType, VigraTrueType>
296 typedef VigraTrueType result;
297 static const bool boolResult =
true;
298 typedef VigraTrueType type;
299 static const bool value =
true;
302 #ifndef NO_PARTIAL_TEMPLATE_SPECIALIZATION
304 template <
class PREDICATE,
class TRUECASE,
class FALSECASE>
307 template <
class TRUECASE,
class FALSECASE>
308 struct If<VigraTrueType, TRUECASE, FALSECASE>
310 typedef TRUECASE type;
313 template <
class TRUECASE,
class FALSECASE>
314 struct If<VigraFalseType, TRUECASE, FALSECASE>
316 typedef FALSECASE type;
319 template <
bool PREDICATE,
class TRUECASE,
class FALSECASE>
322 template <
class TRUECASE,
class FALSECASE>
323 struct IfBool<true, TRUECASE, FALSECASE>
325 typedef TRUECASE type;
328 template <
class TRUECASE,
class FALSECASE>
329 struct IfBool<false, TRUECASE, FALSECASE>
331 typedef FALSECASE type;
334 template <
class L,
class R>
337 typedef VigraFalseType result;
338 static const bool boolResult =
false;
339 typedef VigraFalseType type;
340 static const bool value =
false;
344 struct IsSameType<T, T>
346 typedef VigraTrueType result;
347 static const bool boolResult =
true;
348 typedef VigraTrueType type;
349 static const bool value =
true;
352 template <
class L,
class R>
353 struct IsDifferentType
355 typedef VigraTrueType type;
356 static const bool value =
true;
360 struct IsDifferentType<T, T>
362 typedef VigraFalseType type;
363 static const bool value =
false;
366 #endif // NO_PARTIAL_TEMPLATE_SPECIALIZATION
368 template <
class From,
class To>
369 struct IsConvertibleTo
371 typedef char falseResult[1];
372 typedef char trueResult[2];
374 static From
const & check();
376 static falseResult * testIsConvertible(...);
377 static trueResult * testIsConvertible(To
const &);
379 enum { resultSize =
sizeof(*testIsConvertible(check())) };
381 static const bool value = (resultSize == 2);
383 IfBool<value, VigraTrueType, VigraFalseType>::type
387 template <
class DERIVED,
class BASE>
390 typedef char falseResult[1];
391 typedef char trueResult[2];
393 static falseResult * testIsDerivedFrom(...);
394 static trueResult * testIsDerivedFrom(BASE
const *);
396 enum { resultSize =
sizeof(*testIsDerivedFrom((DERIVED
const *)0)) };
398 static const bool value = (resultSize == 2);
400 IfBool<value, VigraTrueType, VigraFalseType>::type
403 static const bool boolResult = value;
408 struct UnqualifiedType
414 struct UnqualifiedType<T const>
420 struct UnqualifiedType<T &>
421 :
public UnqualifiedType<T>
425 struct UnqualifiedType<T const &>
426 :
public UnqualifiedType<T>
430 struct UnqualifiedType<T *>
431 :
public UnqualifiedType<T>
435 struct UnqualifiedType<T const *>
436 :
public UnqualifiedType<T>
439 template <
bool,
class T =
void>
442 struct enable_if<true, T> {
typedef T type; };
446 template <
class T,
template<
class>
class USER>
449 typedef char falseResult[1];
450 typedef char trueResult[2];
452 static falseResult * test(...);
453 static trueResult * test(USER<sfinae_void>);
455 enum { resultSize =
sizeof(*test((T*)0)) };
457 static const bool value = (resultSize == 2);
459 IfBool<value, VigraTrueType, VigraFalseType>::type
464 struct has_argument_type :
public sfinae_test<T, has_argument_type>
466 template <
class U> has_argument_type(U*,
typename U::argument_type* = 0);
470 struct has_result_type :
public sfinae_test<T, has_result_type>
472 template <
class U> has_result_type(U*,
typename U::result_type* = 0);
476 struct has_value_type :
public sfinae_test<T, has_value_type>
478 template <
class U> has_value_type(U*,
typename U::value_type* = 0);
482 struct IsIterator :
public sfinae_test<T, IsIterator>
484 template <
class U> IsIterator(U*,
typename U::iterator_category* = 0);
488 struct IsIterator<T*>
490 static const bool value =
true;
491 typedef VigraTrueType type;
495 struct IsIterator<T const *>
497 static const bool value =
true;
498 typedef VigraTrueType type;
502 struct has_real_promote_type :
public sfinae_test<T, has_real_promote_type>
505 has_real_promote_type(U*,
typename U::real_promote_type* = 0);
508 template <class T, bool P = has_real_promote_type<T>::value>
509 struct get_optional_real_promote
514 struct get_optional_real_promote<T, true>
516 typedef typename T::real_promote_type type;
522 typedef char falseResult[1];
523 typedef char trueResult[2];
525 static falseResult * test(...);
526 template <
class U,
unsigned n>
527 static trueResult * test(U (*)[n]);
529 enum { resultSize =
sizeof(*test((T*)0)) };
531 static const bool value = (resultSize == 2);
533 IfBool<value, VigraTrueType, VigraFalseType>::type
538 template <
class D,
class B,
class Z>
inline
539 D & static_cast_2(Z & z)
541 return static_cast<D &
>(
static_cast<B &
>(z));
545 class copy_if_same_as
549 copy_if_same_as(
const copy_if_same_as &);
550 void operator=(
const copy_if_same_as &);
552 copy_if_same_as(
const A & x,
const A & y)
553 : copied(&x == &y), data(copied ? new A(y) : &x) {}
559 const A & operator()()
const {
return *data; }
564 struct true_test :
public VigraTrueType {};
567 struct false_test : VigraFalseType {};
569 template <
class PC,
class T,
class F>
572 static const bool value = IfBool<PC::value, T, F>::type::value;
578 template <
class A,
class B>
579 static const A & at(
const A & a,
const B &) {
return a; }
580 template <
class A,
class B>
581 static A & at( A & a, B &) {
return a; }
584 struct choose_type<false>
586 template <
class A,
class B>
587 static const B & at(
const A &,
const B & b) {
return b; }
588 template <
class A,
class B>
589 static B & at( A &, B & b) {
return b; }
595 static const bool value = !std::numeric_limits<X>::is_signed
596 && std::numeric_limits<X>::is_integer;
599 struct EnableMetaLog2
600 :
public enable_if<HasMetaLog2<X>::value> {};
602 class vigra_error_MetaLog2_accepts_only_unsigned_types_and_no_;
605 template <
class X =
unsigned long,
606 X n = ~(X(0)),
unsigned s = 1,
unsigned t = 0,
bool q = 1,
607 X m = 0, X z = 0, X u = 1,
class =
void>
609 :
public vigra_error_MetaLog2_accepts_only_unsigned_types_and_no_<X>
611 template <
class X, X n,
unsigned s,
unsigned t,
bool q, X m, X z, X u>
612 struct MetaLog2 <X, n, s, t, q, m, z, u, typename EnableMetaLog2<X>::type>
614 static const unsigned value
615 = t + MetaLog2<X, (n >> s), s * (1 + q), s, !q, n / 2, z, u>::value;
617 template <
class X,
unsigned s,
unsigned t,
bool q, X m, X z, X u>
618 struct MetaLog2<X, z, s, t, q, m, z, u, typename EnableMetaLog2<X>::type>
620 static const unsigned value
621 = 1 + MetaLog2<X, m / 2, 2, 1, 1, 0, z, u>::value;
623 template <
class X,
unsigned s,
unsigned t,
bool q, X z, X u>
624 struct MetaLog2<X, z, s, t, q, u, z, u, typename EnableMetaLog2<X>::type>
626 static const unsigned value = 2;
628 template <
class X,
unsigned s,
unsigned t,
bool q, X z, X u>
629 struct MetaLog2<X, z, s, t, q, z, z, u, typename EnableMetaLog2<X>::type>
631 static const unsigned value = 1;
633 template <
class X, X z, X u>
634 struct MetaLog2<X, z, 1, 0, 1, z, z, u, typename EnableMetaLog2<X>::type>
638 static const unsigned value = 0;
641 template <
int X,
unsigned int N>
644 static const long long value = MetaPow<X, N-1>::value * X;
650 static const long long value = 1;
659 template<
class HEAD,
class TAIL=
void>
662 typedef TypeList<HEAD, TAIL> type;
667 template <
class List,
class T>
670 template <
class Head,
class Tail,
class T>
671 struct Contains<TypeList<Head, Tail>, T>
673 typedef typename Contains<Tail, T>::type type;
676 template <
class Head,
class Tail>
677 struct Contains<TypeList<Head, Tail>, Head>
679 typedef VigraTrueType type;
683 struct Contains<void, T>
685 typedef VigraFalseType type;
688 template <
class List,
class T>
691 template <
class Head,
class Tail,
class T>
692 struct Remove<TypeList<Head, Tail>, T>
694 typedef TypeList<Head, typename Remove<Tail, T>::type> type;
697 template <
class Head,
class Tail>
698 struct Remove<TypeList<Head, Tail>, Head>
704 struct Remove<void, T>
709 template <
class A,
class Tail=
void>
712 typedef TypeList<A, typename Tail::type> type;
715 template <
class Head,
class Tail,
class List>
716 struct Push<TypeList<Head, Tail>, List>
718 typedef typename Push<Tail, List>::type Rest;
719 typedef TypeList<Head, Rest> type;
722 template <
class Head,
class Tail>
723 struct Push<TypeList<Head, Tail>, void>
725 typedef TypeList<Head, Tail> type;
731 typedef TypeList<A> type;
741 struct Push<void, void>
746 template <
class A,
class Tail=
void>
749 typedef typename Contains<Tail, A>::type AlreadyInList;
750 typedef typename If<AlreadyInList, typename Tail::type, TypeList<A, typename Tail::type> >::type type;
753 template <
class Head,
class Tail,
class List>
754 struct PushUnique<TypeList<Head, Tail>, List>
756 typedef typename PushUnique<Tail, List>::type Rest;
757 typedef typename Contains<Rest, Head>::type HeadAlreadyInList;
758 typedef typename If<HeadAlreadyInList, Rest, TypeList<Head, Rest> >::type type;
761 template <
class Head,
class Tail>
762 struct PushUnique<TypeList<Head, Tail>, void>
764 typedef TypeList<Head, Tail> type;
768 struct PushUnique<A, void>
770 typedef TypeList<A> type;
774 struct PushUnique<void, A>
780 struct PushUnique<void, void>
785 template <
class T01=void,
class T02=void,
class T03=void,
class T04=void,
class T05=void,
786 class T06=void,
class T07=void,
class T08=void,
class T09=void,
class T10=void,
787 class T11=void,
class T12=void,
class T13=void,
class T14=void,
class T15=void,
788 class T16=void,
class T17=void,
class T18=void,
class T19=void,
class T20=
void>
791 typedef typename Push<T19, T20>::type L19;
792 typedef typename Push<T18, L19>::type L18;
793 typedef typename Push<T17, L18>::type L17;
794 typedef typename Push<T16, L17>::type L16;
795 typedef typename Push<T15, L16>::type L15;
796 typedef typename Push<T14, L15>::type L14;
797 typedef typename Push<T13, L14>::type L13;
798 typedef typename Push<T12, L13>::type L12;
799 typedef typename Push<T11, L12>::type L11;
800 typedef typename Push<T10, L11>::type L10;
801 typedef typename Push<T09, L10>::type L09;
802 typedef typename Push<T08, L09>::type L08;
803 typedef typename Push<T07, L08>::type L07;
804 typedef typename Push<T06, L07>::type L06;
805 typedef typename Push<T05, L06>::type L05;
806 typedef typename Push<T04, L05>::type L04;
807 typedef typename Push<T03, L04>::type L03;
808 typedef typename Push<T02, L03>::type L02;
809 typedef typename Push<T01, L02>::type L01;
813 template <
class T01=void,
class T02=void,
class T03=void,
class T04=void,
class T05=void,
814 class T06=void,
class T07=void,
class T08=void,
class T09=void,
class T10=void,
815 class T11=void,
class T12=void,
class T13=void,
class T14=void,
class T15=void,
816 class T16=void,
class T17=void,
class T18=void,
class T19=void,
class T20=
void>
817 struct MakeTypeListUnique
819 typedef typename PushUnique<T19, T20>::type L19;
820 typedef typename PushUnique<T18, L19>::type L18;
821 typedef typename PushUnique<T17, L18>::type L17;
822 typedef typename PushUnique<T16, L17>::type L16;
823 typedef typename PushUnique<T15, L16>::type L15;
824 typedef typename PushUnique<T14, L15>::type L14;
825 typedef typename PushUnique<T13, L14>::type L13;
826 typedef typename PushUnique<T12, L13>::type L12;
827 typedef typename PushUnique<T11, L12>::type L11;
828 typedef typename PushUnique<T10, L11>::type L10;
829 typedef typename PushUnique<T09, L10>::type L09;
830 typedef typename PushUnique<T08, L09>::type L08;
831 typedef typename PushUnique<T07, L08>::type L07;
832 typedef typename PushUnique<T06, L07>::type L06;
833 typedef typename PushUnique<T05, L06>::type L05;
834 typedef typename PushUnique<T04, L05>::type L04;
835 typedef typename PushUnique<T03, L04>::type L03;
836 typedef typename PushUnique<T02, L03>::type L02;
837 typedef typename PushUnique<T01, L02>::type L01;
842 #if defined(_MSC_VER)
843 #pragma warning( pop )
Definition: metaprogramming.hxx:100
Definition: metaprogramming.hxx:113