📄 typelist.h
字号:
//##############################################################################
//The dnc Library
//Copyright (c) 2005 Dreamsoft 赵纯华
//提供Loki程序库的TypeList功能
//
// TypeList是整个dnc程序库的基础,为dnc库的实现提供最为基础的设施.这里的代码来自
//Loki程序库,约有改动.
//##############################################################################
//
//Last update: 2005-11-9
#ifndef __DNC_TYPELIST_H__
#define __DNC_TYPELIST_H__
#ifndef __DNC_NULLTYPE_H__
#include "nulltype.h"
#endif
namespace dnc{
//------------------------------------------------------------------------------
// class template TypeList
// The building block of typelists of any length
// Use it through the dnc_typelist_NN macros
// Defines nested types:
// Head (first element, a non-typelist type by convention)
// Tail (second element, can be another typelist)
//------------------------------------------------------------------------------
template <class T, class U>
struct TypeList
{
typedef T Head;
typedef U Tail;
};
namespace TL
{
//------------------------------------------------------------------------------
// class template Length
// Computes the length of a typelist
// Invocation (TList is a typelist):
// Length<TList>::value
// returns a compile-time constant containing the length of TList, not counting
// the end terminator (which by convention is NullType)
//------------------------------------------------------------------------------
template <class TList> struct Length;
template <> struct Length<NullType>
{
enum { value = 0 };
};
template <class T, class U>
struct Length< TypeList<T, U> >
{
enum { value = 1 + Length<U>::value };
};
//------------------------------------------------------------------------------
// class template TypeAt
// Finds the type at a given index in a typelist
// Invocation (TList is a typelist and index is a compile-time integral
// constant):
// TypeAt<TList, index>::Result
// returns the type in position 'index' in TList
// If you pass an out-of-bounds index, the result is a compile-time error
//------------------------------------------------------------------------------
template <class TList, unsigned int index> struct TypeAt;
template <class Head, class Tail>
struct TypeAt<TypeList<Head, Tail>, 0>
{
typedef Head Result;
};
template <class Head, class Tail, unsigned int i>
struct TypeAt<TypeList<Head, Tail>, i>
{
typedef typename TypeAt<Tail, i - 1>::Result Result;
};
//------------------------------------------------------------------------------
// class template TypeAtNonStrict
// Finds the type at a given index in a typelist
// Invocations (TList is a typelist and index is a compile-time integral
// constant):
// a) TypeAt<TList, index>::Result
// returns the type in position 'index' in TList, or NullType if index is
// out-of-bounds
// b) TypeAt<TList, index, D>::Result
// returns the type in position 'index' in TList, or D if index is out-of-bounds
//------------------------------------------------------------------------------
template <class TList, unsigned int index,
typename DefaultType = NullType>
struct TypeAtNonStrict
{
typedef DefaultType Result;
};
template <class Head, class Tail, typename DefaultType>
struct TypeAtNonStrict<TypeList<Head, Tail>, 0, DefaultType>
{
typedef Head Result;
};
template <class Head, class Tail, unsigned int i, typename DefaultType>
struct TypeAtNonStrict<TypeList<Head, Tail>, i, DefaultType>
{
typedef typename
TypeAtNonStrict<Tail, i - 1, DefaultType>::Result Result;
};
//------------------------------------------------------------------------------
// class template IndexOf
// Finds the index of a type in a typelist
// Invocation (TList is a typelist and T is a type):
// IndexOf<TList, T>::value
// returns the position of T in TList, or NullType if T is not found in TList
//------------------------------------------------------------------------------
template <class TList, class T> struct IndexOf;
template <class T>
struct IndexOf<NullType, T>
{
enum { value = -1 };
};
template <class T, class Tail>
struct IndexOf<TypeList<T, Tail>, T>
{
enum { value = 0 };
};
template <class Head, class Tail, class T>
struct IndexOf<TypeList<Head, Tail>, T>
{
private:
enum { temp = IndexOf<Tail, T>::value };
public:
enum { value = (temp == -1 ? -1 : 1 + temp) };
};
//------------------------------------------------------------------------------
// class template Append
// Appends a type or a typelist to another
// Invocation (TList is a typelist and T is either a type or a typelist):
// Append<TList, T>::Result
// returns a typelist that is TList followed by T and NullType-terminated
//------------------------------------------------------------------------------
template <class TList, class T> struct Append;
template <> struct Append<NullType, NullType>
{
typedef NullType Result;
};
template <class T> struct Append<NullType, T>
{
typedef TypeList<T,NullType> Result;
};
template <class Head, class Tail>
struct Append<NullType, TypeList<Head, Tail> >
{
typedef TypeList<Head, Tail> Result;
};
template <class Head, class Tail, class T>
struct Append<TypeList<Head, Tail>, T>
{
typedef TypeList<Head,
typename Append<Tail, T>::Result>
Result;
};
//------------------------------------------------------------------------------
// class template Erase
// Erases the first occurence, if any, of a type in a typelist
// Invocation (TList is a typelist and T is a type):
// Erase<TList, T>::Result
// returns a typelist that is TList without the first occurence of T
//------------------------------------------------------------------------------
template <class TList, class T> struct Erase;
template <class T> // Specialization 1
struct Erase<NullType, T>
{
typedef NullType Result;
};
template <class T, class Tail> // Specialization 2
struct Erase<TypeList<T, Tail>, T>
{
typedef Tail Result;
};
template <class Head, class Tail, class T> // Specialization 3
struct Erase<TypeList<Head, Tail>, T>
{
typedef TypeList<Head,
typename Erase<Tail, T>::Result>
Result;
};
//------------------------------------------------------------------------------
// class template EraseAll
// Erases all first occurences, if any, of a type in a typelist
// Invocation (TList is a typelist and T is a type):
// EraseAll<TList, T>::Result
// returns a typelist that is TList without any occurence of T
//------------------------------------------------------------------------------
template <class TList, class T> struct EraseAll;
template <class T>
struct EraseAll<NullType, T>
{
typedef NullType Result;
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -