1 #ifndef MSP_CORE_TYPELIST_H_
2 #define MSP_CORE_TYPELIST_H_
6 /** Sentinel type for type lists */
10 /** A "cons cell" for types. Composed of a head and a tail. To construct
11 longer lists, use another TypeCons as the tail. The tail of the last TypeCons
12 should by NullType. */
13 template<typename H, typename T>
21 /** Basic terminated type list of one element. */
25 typedef TypeCons<T1, NullType> Type;
28 /** Specialization to avoid creating a TypeCons with two NullTypes. */
30 struct TypeList1<NullType>
32 typedef NullType Type;
35 /** Specialization to avoid creating a TypeCons with another TypeCons as its
37 template<typename T1, typename T2>
38 struct TypeList1<TypeCons<T1, T2> >
40 typedef TypeCons<T1, T2> Type;
43 /** Basic terminated type list of two elements. */
44 template<typename T1, typename T2>
47 typedef TypeCons<T1, typename TypeList1<T2>::Type> Type;
50 /** Specialization to ignore an initial NullType. */
52 struct TypeList2<NullType, T1>
54 typedef typename TypeList1<T1>::Type Type;
57 /** Specialization to flatten an initial TypeCons. */
58 template<typename T1, typename T2, typename T3>
59 struct TypeList2<TypeCons<T1, T2>, T3>
61 typedef typename TypeList2<T1, typename TypeList2<T2, T3>::Type>::Type Type;
65 /** A terminated type list of five elements. Use the typedef Type inside to
66 obtain the actual list type. If any of the types is a type list itself, the
67 entire structure is flattened to a single list. */
68 template<typename T1, typename T2 = NullType, typename T3 = NullType, typename T4 = NullType, typename T5 = NullType>
71 typedef typename TypeList<T1, typename TypeList<T2, T3, T4, T5>::Type>::Type Type;
74 /** Specialization of TypeList for one element. */
76 struct TypeList<T1, NullType, NullType, NullType, NullType>
78 typedef typename TypeList1<T1>::Type Type;
81 /** Specialization of TypeList for two elements. */
82 template<typename T1, typename T2>
83 struct TypeList<T1, T2, NullType, NullType, NullType>
85 typedef typename TypeList2<T1, T2>::Type Type;
88 /** Specialization of TypeList for three elements. */
89 template<typename T1, typename T2, typename T3>
90 struct TypeList<T1, T2, T3, NullType, NullType>
92 typedef typename TypeList<T1, typename TypeList<T2, T3>::Type>::Type Type;
95 /** Specialization of TypeList for four elements. */
96 template<typename T1, typename T2, typename T3, typename T4>
97 struct TypeList<T1, T2, T3, T4, NullType>
99 typedef typename TypeList<T1, typename TypeList<T2, T3, T4>::Type>::Type Type;
103 /** A helper for selecting a type from a list based on a predicate. The
104 predicate should be a template taking a single type argument and defining a
105 compile-time constant with the name "value", which evaluates to true if the
106 type is considered a match. The result can be obtained from the member
107 typedef Type. If the list contains no matching type, an ugly and confusing
108 compiler error will result. */
109 template<typename L, template<typename> class P, bool f = P<typename L::Head>::value>
112 /** Specialization for a matching type. Picks the head of the list as the
114 template<typename L, template<typename> class P>
115 struct TypeChooser<L, P, true>
117 typedef typename L::Head Type;
120 /** Specialization for a non-matchin type. Recursively inspects the tail of
122 template<typename L, template<typename> class P>
123 struct TypeChooser<L, P, false>
125 typedef typename TypeChooser<typename L::Tail, P>::Type Type;