TypeList 之 DerivedToFront 把类型链表中类型按继承层次排序(子类 -> 基类)

本文介绍了一种使用C++模板元编程实现类型列表的操作方法,包括获取长度、查找索引、追加删除类型等实用功能,并展示了如何对类型进行排序,特别针对继承关系的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <cstdlib>
#include <iostream>

using namespace std;

template<bool flag,typename T,typename U>
struct Select
{
  typedef T Result;
};
template<typename T,typename U>
struct Select<false,T,U>
{
  typedef U Result;
};
template<class T,class U>
class Conversion
{
  typedef char small;
  class Big{char dummy[2];};
  static small Test(T);
  static Big Test(...);
  static T MakeT();
  static U MakeU();
public:
  enum{exists = sizeof(Test(MakeU())) == sizeof(small)};
  enum{exist2ways = exists && Conversion<U,T>::exists};
  enum{sametype = false};
};
template<class T>
class Conversion<T,T>
{public:enum{exists = 1,exist2ways = 1,sametype = 1};};
#define SUPPERSUBCLASS(T,U)/
        (Conversion<const T*,const U*>::exists)&&/
        !(Conversion<const T*,const void*>::sametype)//防止 T* 是void*类型
#define SUPPERSUBCLASS_STRICT(T,U)/
        (Conversion<const T,const U>::exists)&&/
        !(Conversion<const T,const void>::sametype)//防止 T* 是void*类型
class NullType;
namespace TL
{
  template<class T,class U>
  struct TypeList
  {
    typedef T head;
    typedef U tail;
  };
  #define TYPELIST_1(T1) TypeList<T1,NullType>
  #define TYPELIST_2(T1,T2) TypeList<T1,TYPELIST_1(T2)>
  #define TYPELIST_3(T1,T2,T3) TypeList<T1,TYPELIST_2(T2,T3)>
  #define TYPELIST_4(T1,T2,T3,T4) TypeList<T1,TYPELIST_3(T2,T3,T4)>
  //-----------------------------------------------------------------
  //Length
  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};
  };
  //---------------------------------------------------------------
  //利用索引查找对象
  template<class T,int U>struct TypeAt;
  template<class head,class tail>
  struct TypeAt<TypeList<head,tail>,0>
  {
    typedef head Result;
  };
  template<class head,class tail,int i>
  struct TypeAt<TypeList<head,tail>,i>
  {
    typedef typename TypeAt<tail,i - 1>::Result Result;
  };
  //---------------------------------------------------------------
  //indexof
  template<class TList,class T>struct IndexOf;
  template<class T>
  struct IndexOf<NullType,T>
  {
    enum{value = -1};
  };
  template<class Tail,class T>
  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};
  };
  //---------------------------------------------------------------
  //Append
  template<class TList,class T>struct Append;
  template<>
  struct Append<NullType,NullType>
  {
    typedef NullType Result;
  };
  template<class T>
  struct Append<NullType,T>
  {
    typedef TYPELIST_1(T) 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;
  };
  //---------------------------------------------------------------
  //Erase
  template<class TList,class T>struct Erase;
  template<class T>
  struct Erase<NullType,T>
  {
    typedef NullType Result;
  };
  template<class T,class tail>
  struct Erase<TypeList<T,tail>,T>
  {
    typedef tail Result;
  };
  template<class head,class tail,class T>
  struct Erase<TypeList<head,tail>,T>
  {
    typedef TypeList<head,typename Erase<tail,T>::Result> Result;
  };
  //---------------------------------------------------------------
  //NoDuplicate
  template<class TList>struct NoDuplicate;
  template<>struct NoDuplicate<NullType>
  {
    typedef NullType Result;
  };
  template<class head,class tail>
  struct NoDuplicate<TypeList<head,tail> >
  {
  private:
    typedef typename NoDuplicate<tail>::Result L1;
    typedef typename Erase<L1,head>::Result L2;
  public:
    typedef TypeList<head,L2> Result;
  };
  //---------------------------------------------------------------
  //TypeList 之 Replace,把类型列表中的 T 类型换成 U 类型
  template<class TList,class T,class U>struct Replace;
  template<class T,class U>
  struct Replace<NullType,T,U>
  {
    typedef NullType Result;
  };
  template<class T,class U,class tail>
  struct Replace<TypeList<T,tail>,T,U>
  {
    typedef TypeList<U,tail> Result;
  };
  template<class head,class tail,class T,class U>
  struct Replace<TypeList<head,tail>,T,U>
  {
    typedef TypeList<head,typename Replace<tail,T,U>::Result> Result;
  };
  //---------------------------------------------------------------
  //TypeList 之 MostDerived
  template<class TList,class T>struct MostDerived;
  template<class T>
  struct MostDerived<NullType,T>
  {
    typedef T Result;
  };
  template<class head,class tail,class T>
  struct MostDerived<TypeList<head,tail>,T>
  {
  private:
    typedef typename MostDerived<tail,T>::Result Candidate;
  public:
    typedef typename Select<SUPPERSUBCLASS(Candidate,head),head,Candidate>::Result Result;
  };
  //---------------------------------------------------------------
  //TypeList 之 DerivedToFront 把类型链表中类型按继承层次排序(子类 -> 基类)
  template<class TList>struct DerivedToFront;
  template<>
  struct DerivedToFront<NullType>
  {
    typedef NullType Result;
  };
  template<class head,class tail>
  struct DerivedToFront<TypeList<head,tail> >
  {
  private:
    typedef typename MostDerived<TypeList<head,tail>,head>::Result TheMostDerivedType;
    typedef typename Replace<tail,TheMostDerivedType,head>::Result L;
  public:
    typedef TypeList<TheMostDerivedType,L> Result;
  };
}
using namespace TL;
class Widget
{};
class ScrollBar:public Widget
{};
class Button:public Widget
{};
class GraphicButton:public Button
{};
int main(int argc, char *argv[])
{
    typedef TYPELIST_4(Button,ScrollBar,GraphicButton,Widget) MyList;
    cout<<"排序前----------------------------------------"<<endl;
    cout<<"GraphicButton的索引:/t"<<IndexOf<MyList,GraphicButton>::value<<endl;
    cout<<"Button的索引:/t/t"<<IndexOf<MyList,Button>::value<<endl;
    cout<<"ScrollBar的索引:/t"<<IndexOf<MyList,ScrollBar>::value<<endl;
    cout<<"Widget的索引:/t/t"<<IndexOf<MyList,Widget>::value<<endl;
    typedef DerivedToFront<MyList>::Result MyList2;
    cout<<"排序后----------------------------------------"<<endl;
    cout<<"GraphicButton的索引:/t"<<IndexOf<MyList2,GraphicButton>::value<<endl;
    cout<<"Button的索引:/t/t"<<IndexOf<MyList2,Button>::value<<endl;
    cout<<"ScrollBar的索引:/t"<<IndexOf<MyList2,ScrollBar>::value<<endl;
    cout<<"Widget的索引:/t/t"<<IndexOf<MyList2,Widget>::value<<endl;
    cout<<"----------------------------------------------"<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值