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;
123 typedef VigraFalseType isConst;
124 typedef VigraFalseType isPOD;
125 typedef VigraFalseType isBuiltinType;
128 #ifndef NO_PARTIAL_TEMPLATE_SPECIALIZATION
131 class TypeTraits<T const>
132 :
public TypeTraits<T>
135 typedef VigraTrueType isConst;
139 class TypeTraits<T *>
142 typedef VigraFalseType isConst;
143 typedef VigraTrueType isPOD;
144 typedef VigraTrueType isBuiltinType;
148 class TypeTraits<T const *>
151 typedef VigraFalseType isConst;
152 typedef VigraTrueType isPOD;
153 typedef VigraTrueType isBuiltinType;
156 #endif // NO_PARTIAL_TEMPLATE_SPECIALIZATION
165 #define VIGRA_TYPE_TRAITS(type, size) \
167 class TypeTraits<type> \
170 typedef VigraFalseType isConst; \
171 typedef VigraTrueType isPOD; \
172 typedef VigraTrueType isBuiltinType; \
173 typedef char TypeToSize[size]; \
177 TypeTraits<type>::TypeToSize * typeToSize(type); \
180 struct SizeToType<size> \
182 typedef type result; \
186 VIGRA_TYPE_TRAITS(
char, 1)
187 VIGRA_TYPE_TRAITS(
signed char, 2)
188 VIGRA_TYPE_TRAITS(
unsigned char, 3)
189 VIGRA_TYPE_TRAITS(
short, 4)
190 VIGRA_TYPE_TRAITS(
unsigned short, 5)
191 VIGRA_TYPE_TRAITS(
int, 6)
192 VIGRA_TYPE_TRAITS(
unsigned int, 7)
193 VIGRA_TYPE_TRAITS(
long, 8)
194 VIGRA_TYPE_TRAITS(
unsigned long, 9)
195 VIGRA_TYPE_TRAITS(
float, 10)
196 VIGRA_TYPE_TRAITS(
double, 11)
197 VIGRA_TYPE_TRAITS(
long double, 12)
199 VIGRA_TYPE_TRAITS(
long long, 13)
200 VIGRA_TYPE_TRAITS(
unsigned long long, 14)
203 #undef VIGRA_TYPE_TRAITS
211 struct Not<VigraTrueType>
213 typedef VigraFalseType result;
214 static const bool boolResult =
false;
215 typedef VigraFalseType type;
216 static const bool value =
false;
220 struct Not<VigraFalseType>
222 typedef VigraTrueType result;
223 static const bool boolResult =
true;
224 typedef VigraTrueType type;
225 static const bool value =
true;
228 template <
class L,
class R>
232 struct And<VigraFalseType, VigraFalseType>
234 typedef VigraFalseType result;
235 static const bool boolResult =
false;
236 typedef VigraFalseType type;
237 static const bool value =
false;
241 struct And<VigraFalseType, VigraTrueType>
243 typedef VigraFalseType result;
244 static const bool boolResult =
false;
245 typedef VigraFalseType type;
246 static const bool value =
false;
250 struct And<VigraTrueType, VigraFalseType>
252 typedef VigraFalseType result;
253 static const bool boolResult =
false;
254 typedef VigraFalseType type;
255 static const bool value =
false;
259 struct And<VigraTrueType, VigraTrueType>
261 typedef VigraTrueType result;
262 static const bool boolResult =
true;
263 typedef VigraTrueType type;
264 static const bool value =
true;
267 template <
class L,
class R>
271 struct Or<VigraFalseType, VigraFalseType>
273 typedef VigraFalseType result;
274 static const bool boolResult =
false;
275 typedef VigraFalseType type;
276 static const bool value =
false;
280 struct Or<VigraTrueType, VigraFalseType>
282 typedef VigraTrueType result;
283 static const bool boolResult =
true;
284 typedef VigraTrueType type;
285 static const bool value =
true;
289 struct Or<VigraFalseType, VigraTrueType>
291 typedef VigraTrueType result;
292 static const bool boolResult =
true;
293 typedef VigraTrueType type;
294 static const bool value =
true;
298 struct Or<VigraTrueType, VigraTrueType>
300 typedef VigraTrueType result;
301 static const bool boolResult =
true;
302 typedef VigraTrueType type;
303 static const bool value =
true;
306 #ifndef NO_PARTIAL_TEMPLATE_SPECIALIZATION
308 template <
class PREDICATE,
class TRUECASE,
class FALSECASE>
311 template <
class TRUECASE,
class FALSECASE>
312 struct If<VigraTrueType, TRUECASE, FALSECASE>
314 typedef TRUECASE type;
317 template <
class TRUECASE,
class FALSECASE>
318 struct If<VigraFalseType, TRUECASE, FALSECASE>
320 typedef FALSECASE type;
323 template <
bool PREDICATE,
class TRUECASE,
class FALSECASE>
326 template <
class TRUECASE,
class FALSECASE>
327 struct IfBool<true, TRUECASE, FALSECASE>
329 typedef TRUECASE type;
332 template <
class TRUECASE,
class FALSECASE>
333 struct IfBool<false, TRUECASE, FALSECASE>
335 typedef FALSECASE type;
338 template <
class L,
class R>
341 typedef VigraFalseType result;
342 static const bool boolResult =
false;
343 typedef VigraFalseType type;
344 static const bool value =
false;
348 struct IsSameType<T, T>
350 typedef VigraTrueType result;
351 static const bool boolResult =
true;
352 typedef VigraTrueType type;
353 static const bool value =
true;
356 template <
class L,
class R>
357 struct IsDifferentType
359 typedef VigraTrueType type;
360 static const bool value =
true;
364 struct IsDifferentType<T, T>
366 typedef VigraFalseType type;
367 static const bool value =
false;
370 #endif // NO_PARTIAL_TEMPLATE_SPECIALIZATION
372 template <
class From,
class To>
373 struct IsConvertibleTo
375 typedef char falseResult[1];
376 typedef char trueResult[2];
378 static From
const & check();
380 static falseResult * testIsConvertible(...);
381 static trueResult * testIsConvertible(To
const &);
383 enum { resultSize =
sizeof(*testIsConvertible(check())) };
385 static const bool value = (resultSize == 2);
387 IfBool<value, VigraTrueType, VigraFalseType>::type
391 template <
class DERIVED,
class BASE>
394 typedef char falseResult[1];
395 typedef char trueResult[2];
397 static falseResult * testIsDerivedFrom(...);
398 static trueResult * testIsDerivedFrom(BASE
const *);
400 enum { resultSize =
sizeof(*testIsDerivedFrom((DERIVED
const *)0)) };
402 static const bool value = (resultSize == 2);
404 IfBool<value, VigraTrueType, VigraFalseType>::type
407 static const bool boolResult = value;
412 struct UnqualifiedType
418 struct UnqualifiedType<T const>
424 struct UnqualifiedType<T &>
425 :
public UnqualifiedType<T>
429 struct UnqualifiedType<T const &>
430 :
public UnqualifiedType<T>
434 struct UnqualifiedType<T *>
435 :
public UnqualifiedType<T>
439 struct UnqualifiedType<T const *>
440 :
public UnqualifiedType<T>
443 template <
bool,
class T =
void>
446 struct enable_if<true, T> {
typedef T type; };
450 template <
class T,
template<
class>
class USER>
453 typedef char falseResult[1];
454 typedef char trueResult[2];
456 static falseResult * test(...);
457 static trueResult * test(USER<sfinae_void>);
459 enum { resultSize =
sizeof(*test((T*)0)) };
461 static const bool value = (resultSize == 2);
463 IfBool<value, VigraTrueType, VigraFalseType>::type
468 struct has_argument_type :
public sfinae_test<T, has_argument_type>
470 template <
class U> has_argument_type(U*,
typename U::argument_type* = 0);
474 struct has_result_type :
public sfinae_test<T, has_result_type>
476 template <
class U> has_result_type(U*,
typename U::result_type* = 0);
480 struct has_value_type :
public sfinae_test<T, has_value_type>
482 template <
class U> has_value_type(U*,
typename U::value_type* = 0);
486 struct IsIterator :
public sfinae_test<T, IsIterator>
488 template <
class U> IsIterator(U*,
typename U::iterator_category* = 0);
492 struct IsIterator<T*>
494 static const bool value =
true;
495 typedef VigraTrueType type;
499 struct IsIterator<T const *>
501 static const bool value =
true;
502 typedef VigraTrueType type;
506 struct has_real_promote_type :
public sfinae_test<T, has_real_promote_type>
509 has_real_promote_type(U*,
typename U::real_promote_type* = 0);
512 template <class T, bool P = has_real_promote_type<T>::value>
513 struct get_optional_real_promote
518 struct get_optional_real_promote<T, true>
520 typedef typename T::real_promote_type type;
526 typedef char falseResult[1];
527 typedef char trueResult[2];
529 static falseResult * test(...);
530 template <
class U,
unsigned n>
531 static trueResult * test(U (*)[n]);
533 enum { resultSize =
sizeof(*test((T*)0)) };
535 static const bool value = (resultSize == 2);
537 IfBool<value, VigraTrueType, VigraFalseType>::type
542 template <
class D,
class B,
class Z>
inline
543 D & static_cast_2(Z & z)
545 return static_cast<D &
>(
static_cast<B &
>(z));
549 class copy_if_same_as
553 copy_if_same_as(
const copy_if_same_as &);
554 void operator=(
const copy_if_same_as &);
556 copy_if_same_as(
const A & x,
const A & y)
557 : copied(&x == &y), data(copied ? new A(y) : &x) {}
563 const A & operator()()
const {
return *data; }
568 struct true_test :
public VigraTrueType {};
571 struct false_test : VigraFalseType {};
573 template <
class PC,
class T,
class F>
576 static const bool value = IfBool<PC::value, T, F>::type::value;
582 template <
class A,
class B>
583 static const A & at(
const A & a,
const B &) {
return a; }
584 template <
class A,
class B>
585 static A & at( A & a, B &) {
return a; }
588 struct choose_type<false>
590 template <
class A,
class B>
591 static const B & at(
const A &,
const B & b) {
return b; }
592 template <
class A,
class B>
593 static B & at( A &, B & b) {
return b; }
599 static const bool value = !std::numeric_limits<X>::is_signed
600 && std::numeric_limits<X>::is_integer;
603 struct EnableMetaLog2
604 :
public enable_if<HasMetaLog2<X>::value> {};
606 class vigra_error_MetaLog2_accepts_only_unsigned_types_and_no_;
609 template <
class X =
unsigned long,
610 X n = ~(X(0)),
unsigned s = 1,
unsigned t = 0,
bool q = 1,
611 X m = 0, X z = 0, X u = 1,
class =
void>
613 :
public vigra_error_MetaLog2_accepts_only_unsigned_types_and_no_<X>
615 template <
class X, X n,
unsigned s,
unsigned t,
bool q, X m, X z, X u>
616 struct MetaLog2 <X, n, s, t, q, m, z, u, typename EnableMetaLog2<X>::type>
618 static const unsigned value
619 = t + MetaLog2<X, (n >> s), s * (1 + q), s, !q, n / 2, z, u>::value;
621 template <
class X,
unsigned s,
unsigned t,
bool q, X m, X z, X u>
622 struct MetaLog2<X, z, s, t, q, m, z, u, typename EnableMetaLog2<X>::type>
624 static const unsigned value
625 = 1 + MetaLog2<X, m / 2, 2, 1, 1, 0, z, u>::value;
627 template <
class X,
unsigned s,
unsigned t,
bool q, X z, X u>
628 struct MetaLog2<X, z, s, t, q, u, z, u, typename EnableMetaLog2<X>::type>
630 static const unsigned value = 2;
632 template <
class X,
unsigned s,
unsigned t,
bool q, X z, X u>
633 struct MetaLog2<X, z, s, t, q, z, z, u, typename EnableMetaLog2<X>::type>
635 static const unsigned value = 1;
637 template <
class X, X z, X u>
638 struct MetaLog2<X, z, 1, 0, 1, z, z, u, typename EnableMetaLog2<X>::type>
642 static const unsigned value = 0;
651 template<
class HEAD,
class TAIL=
void>
654 typedef TypeList<HEAD, TAIL> type;
659 template <
class List,
class T>
662 template <
class Head,
class Tail,
class T>
663 struct Contains<TypeList<Head, Tail>, T>
665 typedef typename Contains<Tail, T>::type type;
668 template <
class Head,
class Tail>
669 struct Contains<TypeList<Head, Tail>, Head>
671 typedef VigraTrueType type;
675 struct Contains<void, T>
677 typedef VigraFalseType type;
680 template <
class List,
class T>
683 template <
class Head,
class Tail,
class T>
684 struct Remove<TypeList<Head, Tail>, T>
686 typedef TypeList<Head, typename Remove<Tail, T>::type> type;
689 template <
class Head,
class Tail>
690 struct Remove<TypeList<Head, Tail>, Head>
696 struct Remove<void, T>
701 template <
class A,
class Tail=
void>
704 typedef TypeList<A, typename Tail::type> type;
707 template <
class Head,
class Tail,
class List>
708 struct Push<TypeList<Head, Tail>, List>
710 typedef typename Push<Tail, List>::type Rest;
711 typedef TypeList<Head, Rest> type;
714 template <
class Head,
class Tail>
715 struct Push<TypeList<Head, Tail>, void>
717 typedef TypeList<Head, Tail> type;
723 typedef TypeList<A> type;
733 struct Push<void, void>
738 template <
class A,
class Tail=
void>
741 typedef typename Contains<Tail, A>::type AlreadyInList;
742 typedef typename If<AlreadyInList, typename Tail::type, TypeList<A, typename Tail::type> >::type type;
745 template <
class Head,
class Tail,
class List>
746 struct PushUnique<TypeList<Head, Tail>, List>
748 typedef typename PushUnique<Tail, List>::type Rest;
749 typedef typename Contains<Rest, Head>::type HeadAlreadyInList;
750 typedef typename If<HeadAlreadyInList, Rest, TypeList<Head, Rest> >::type type;
753 template <
class Head,
class Tail>
754 struct PushUnique<TypeList<Head, Tail>, void>
756 typedef TypeList<Head, Tail> type;
760 struct PushUnique<A, void>
762 typedef TypeList<A> type;
766 struct PushUnique<void, A>
772 struct PushUnique<void, void>
777 template <
class T01=void,
class T02=void,
class T03=void,
class T04=void,
class T05=void,
778 class T06=void,
class T07=void,
class T08=void,
class T09=void,
class T10=void,
779 class T11=void,
class T12=void,
class T13=void,
class T14=void,
class T15=void,
780 class T16=void,
class T17=void,
class T18=void,
class T19=void,
class T20=
void>
783 typedef typename Push<T19, T20>::type L19;
784 typedef typename Push<T18, L19>::type L18;
785 typedef typename Push<T17, L18>::type L17;
786 typedef typename Push<T16, L17>::type L16;
787 typedef typename Push<T15, L16>::type L15;
788 typedef typename Push<T14, L15>::type L14;
789 typedef typename Push<T13, L14>::type L13;
790 typedef typename Push<T12, L13>::type L12;
791 typedef typename Push<T11, L12>::type L11;
792 typedef typename Push<T10, L11>::type L10;
793 typedef typename Push<T09, L10>::type L09;
794 typedef typename Push<T08, L09>::type L08;
795 typedef typename Push<T07, L08>::type L07;
796 typedef typename Push<T06, L07>::type L06;
797 typedef typename Push<T05, L06>::type L05;
798 typedef typename Push<T04, L05>::type L04;
799 typedef typename Push<T03, L04>::type L03;
800 typedef typename Push<T02, L03>::type L02;
801 typedef typename Push<T01, L02>::type L01;
805 template <
class T01=void,
class T02=void,
class T03=void,
class T04=void,
class T05=void,
806 class T06=void,
class T07=void,
class T08=void,
class T09=void,
class T10=void,
807 class T11=void,
class T12=void,
class T13=void,
class T14=void,
class T15=void,
808 class T16=void,
class T17=void,
class T18=void,
class T19=void,
class T20=
void>
809 struct MakeTypeListUnique
811 typedef typename PushUnique<T19, T20>::type L19;
812 typedef typename PushUnique<T18, L19>::type L18;
813 typedef typename PushUnique<T17, L18>::type L17;
814 typedef typename PushUnique<T16, L17>::type L16;
815 typedef typename PushUnique<T15, L16>::type L15;
816 typedef typename PushUnique<T14, L15>::type L14;
817 typedef typename PushUnique<T13, L14>::type L13;
818 typedef typename PushUnique<T12, L13>::type L12;
819 typedef typename PushUnique<T11, L12>::type L11;
820 typedef typename PushUnique<T10, L11>::type L10;
821 typedef typename PushUnique<T09, L10>::type L09;
822 typedef typename PushUnique<T08, L09>::type L08;
823 typedef typename PushUnique<T07, L08>::type L07;
824 typedef typename PushUnique<T06, L07>::type L06;
825 typedef typename PushUnique<T05, L06>::type L05;
826 typedef typename PushUnique<T04, L05>::type L04;
827 typedef typename PushUnique<T03, L04>::type L03;
828 typedef typename PushUnique<T02, L03>::type L02;
829 typedef typename PushUnique<T01, L02>::type L01;
834 #if defined(_MSC_VER)
835 #pragma warning( pop )
Definition: metaprogramming.hxx:102
Definition: metaprogramming.hxx:117