]> git.tdb.fi Git - libs/core.git/blobdiff - source/core/typelist.h
Exactly-sized integer types, the C++ way
[libs/core.git] / source / core / typelist.h
diff --git a/source/core/typelist.h b/source/core/typelist.h
new file mode 100644 (file)
index 0000000..bd8bf44
--- /dev/null
@@ -0,0 +1,130 @@
+#ifndef CORE_TYPELIST_H_
+#define CORE_TYPELIST_H_
+
+namespace Msp {
+
+/** Sentinel type for type lists */
+struct NullType { };
+
+
+/** A "cons cell" for types.  Composed of a head and a tail.  To construct
+longer lists, use another TypeCons as the tail.  The tail of the last TypeCons
+should by NullType. */
+template<typename H, typename T>
+struct TypeCons
+{
+       typedef H Head;
+       typedef T Tail;
+};
+
+
+/** Basic terminated type list of one element. */
+template<typename T1>
+struct TypeList1
+{
+       typedef TypeCons<T1, NullType> Type;
+};
+
+/** Specialization to avoid creating a TypeCons with two NullTypes. */
+template<>
+struct TypeList1<NullType>
+{
+       typedef NullType Type;
+};
+
+/** Specialization to avoid creating a TypeCons with another TypeCons as its
+head. */
+template<typename T1, typename T2>
+struct TypeList1<TypeCons<T1, T2> >
+{
+       typedef TypeCons<T1, T2> Type;
+};
+
+/** Basic terminated type list of two elements. */
+template<typename T1, typename T2>
+struct TypeList2
+{
+       typedef TypeCons<T1, typename TypeList1<T2>::Type> Type;
+};
+
+/** Specialization to ignore an initial NullType. */
+template<typename T1>
+struct TypeList2<NullType, T1>
+{
+       typedef typename TypeList1<T1>::Type Type;
+};
+
+/** Specialization to flatten an initial TypeCons. */
+template<typename T1, typename T2, typename T3>
+struct TypeList2<TypeCons<T1, T2>, T3>
+{
+       typedef typename TypeList2<T1, typename TypeList2<T2, T3>::Type>::Type Type;
+};
+
+
+/** A terminated type list of five elements.  Use the typedef Type inside to
+obtain the actual list type.  If any of the types is a type list itself, the
+entire structure is flattened to a single list. */
+template<typename T1, typename T2 = NullType, typename T3 = NullType, typename T4 = NullType, typename T5 = NullType>
+struct TypeList
+{
+       typedef typename TypeList<T1, typename TypeList<T2, T3, T4, T5>::Type>::Type Type;
+};
+
+/** Specialization of TypeList for one element. */
+template<typename T1>
+struct TypeList<T1, NullType, NullType, NullType, NullType>
+{
+       typedef typename TypeList1<T1>::Type Type;
+};
+
+/** Specialization of TypeList for two elements. */
+template<typename T1, typename T2>
+struct TypeList<T1, T2, NullType, NullType, NullType>
+{
+       typedef typename TypeList2<T1, T2>::Type Type;
+};
+
+/** Specialization of TypeList for three elements. */
+template<typename T1, typename T2, typename T3>
+struct TypeList<T1, T2, T3, NullType, NullType>
+{
+       typedef typename TypeList<T1, typename TypeList<T2, T3>::Type>::Type Type;
+};
+
+/** Specialization of TypeList for four elements. */
+template<typename T1, typename T2, typename T3, typename T4>
+struct TypeList<T1, T2, T3, T4, NullType>
+{
+       typedef typename TypeList<T1, typename TypeList<T2, T3, T4>::Type>::Type Type;
+};
+
+
+/** A helper for selecting a type from a list based on a predicate.  The
+predicate should be a template taking a single type argument and defining a
+compile-time constant with the name "value", which evaluates to true if the
+type is considered a match.  The result can be obtained from the member
+typedef Type.  If the list contains no matching type, an ugly and confusing
+compiler error will result. */
+template<typename L, template<typename> class P, bool f = P<typename L::Head>::value>
+struct TypeChooser;
+
+/** Specialization for a matching type.  Picks the head of the list as the
+result. */
+template<typename L, template<typename> class P>
+struct TypeChooser<L, P, true>
+{
+       typedef typename L::Head Type;
+};
+
+/** Specialization for a non-matchin type.  Recursively inspects the tail of
+the list. */
+template<typename L, template<typename> class P>
+struct TypeChooser<L, P, false>
+{
+       typedef typename TypeChooser<typename L::Tail, P>::Type Type;
+};
+
+} // namespace Msp
+
+#endif