相对于TypeList源码,下面代码还是很简单的,就不多说废话了。
#ifndef TYPELIST_ADD_H
#define TYPELIST_ADD_H
//-------------------------------------------------------------------------------
//Loki库自己的SUPERSUBCLASS_STRICT在DevCpp下编译不过,lokiVC6port提供的版本同下。
//-------------------------------------------------------------------------------
#define SUPERSUBCLASS_STRICT_FIX(T, U) (SUPERSUBCLASS(T, U) && !::Loki::Conversion<const T *, const U *>::sameType)
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
template <class TList, class T> struct MostBased;
template <class T>
struct MostBased<NullType, T>
{
typedef T Result;
};
template <class Head, class Tail, class T>
struct MostBased<Typelist<Head, Tail>, T>
{
private:
typedef typename MostBased<Tail, T>::Result Candidate;
public:
typedef typename Select<SUPERSUBCLASS(Head, Candidate),
Head, Candidate>::Result Result;
};
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
template <class Tlist, class T> struct NearestBased;
template <class Tlist, class T, class CurNearBase> struct NearestBasedHelper;
template <class Head, class Tail, class T, class CurNearBase>
struct NearestBasedHelper<Typelist<Head, Tail>, T, CurNearBase>
{
private:
typedef typename Select< SUPERSUBCLASS_STRICT_FIX(Head, T) &&
SUPERSUBCLASS_STRICT_FIX(CurNearBase, Head),
Head, CurNearBase>::Result curnearbase; //搜索处于两个类之间的类
public:
typedef typename NearestBasedHelper<Tail, T, curnearbase>::Result Result;
};
template <class T, class CurNearBase>
struct NearestBasedHelper<NullType, T, CurNearBase>
{
typedef CurNearBase Result;
};
template <class T> //保护对空TypeList发生操作。
struct NearestBased<NullType, T>
{
// typedef T Result;
};
template <class Head, class Tail, class T>
struct NearestBased<Typelist<Head, Tail>, T>
{
private:
typedef MostBased<Typelist<Head, Tail>, T>::Result mostbased; //找到最基类,然后夹杀法。
public:
typedef typename NearestBasedHelper<Typelist<Head, Tail>, T, mostbased>::Result Result;
};
//-------------------------------------------------------------------------------
//排序完成后,派生类一定在它的基类前面。
//-------------------------------------------------------------------------------
template <class TList> struct Sort;
template <>
struct Sort<NullType>
{
typedef NullType Result;
};
template <class Head, class Tail>
struct Sort<Typelist<Head, Tail> >
{
private:
typedef typename MostDerived<Tail, Head>::Result TheMostDerived;
typedef typename Replace<Tail, TheMostDerived, Head>::Result L;
public:
typedef Typelist<TheMostDerived, typename Sort<L>::Result> Result;
};
#endif