TypeList 之应用 GenScatterHierarchy

#include "stdafx.h"
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
//-----------------------------------------------------------------
class Widget
{
public:
 string m_name;
};
class ScrollBar:public Widget
{};
class Button:public Widget
{};
class GraphicButton:public Button
{};
//-----------------------------------------------------------------
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;
template<int n>
struct Int2Type
{
 enum{value = n};
};
//-----------------------------------------------------------------
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;
//-----------------------------------------------------------------
template<class TList,template<class>class Unit>class GenScatterHierarchy;
template<class T1,class T2,template<class>class Unit>
class GenScatterHierarchy<TypeList<T1,T2>,Unit>:
 public GenScatterHierarchy<T1,Unit>,
 public GenScatterHierarchy<T2,Unit>
{};
template<class AtomicType,template<class>class Unit>
class GenScatterHierarchy:public Unit<AtomicType>
{};
template<template<class>class Unit>
class GenScatterHierarchy<NullType,Unit>
{};
//---------------------------------------------------
//使用 TypeList 神不知鬼不觉的实现多继承
template<class T>
struct Holder
{
 T value;
};
//-----------------------------------------------------------------
//使用类型名来访问具体那一个父类的方法,比使用 static_cast 要方便很多
template<class T,class TList,template<class>class Unit>
Unit<T>& Field(GenScatterHierarchy<TList,Unit>& obj)
{//编译时,编译器就知道 Unit<T> 是 GenScatterHierarchy<TList,Unit>& 的父类
 //返回时,至返回与 Unit<T> 相关的东西,就避免使用了 static_cast
 return obj;
}
//-----------------------------------------------------------------
//通过索引来查找相应的父类的对象
//神一样的代码
template<class TList,template<class>class Unit>
Unit<typename TList::head>&FieldHelper(
 GenScatterHierarchy<TList,Unit>& obj,
 Int2Type<0>)
{
 GenScatterHierarchy<typename TList::head,Unit>&leftbase = obj;
 return leftbase;
}
template<int i,class TList,template<class>class Unit>
Unit<typename TypeAt<TList,i>::Result>&
 FieldHelper(GenScatterHierarchy<TList,Unit>&obj,Int2Type<i>)
{
 GenScatterHierarchy<typename TList::tail,Unit>&rightbase = obj;
 return FieldHelper(rightbase,Int2Type<i - 1>());
}
template<int i,class TList,template<class>class Unit>
Unit<typename TypeAt<TList,i>::Result>&
 Field(GenScatterHierarchy<TList,Unit>&obj)
{
 return FieldHelper(obj,Int2Type<i>());
}
//-----------------------------------------------------------------
int _tmain(int argc, char *argv[])
{
 cout<<"----------------------------------------------"<<endl;
 typedef GenScatterHierarchy<TYPELIST_3(int,string,Widget),Holder> WidgetInfo;
 WidgetInfo obj;
 (static_cast<Holder<string>&>(obj)).value = "hellow orld";
 cout<<"Holder<string>.value = "<<(static_cast<Holder<string>&>(obj)).value<<endl;
 (static_cast<Holder<int>&>(obj)).value = 1212;
 cout<<"Holder<int>.value = "<<(static_cast<Holder<int>&>(obj)).value<<endl;
 (static_cast<Holder<Widget>&>(obj)).value.m_name = "Don't surprise,I am a Widget!";
 cout<<"Holder<Widget>.value.m_name = "<<(static_cast<Holder<Widget>&>(obj)).value.m_name<<endl;
 cout<<"----------------------------------------------"<<endl;
 //看看下面是不是方便很多啦
 Holder<string> objStr = Field<string>(obj);
 cout<<"Holder<string> objStr.value = "<<objStr.value<<endl;
 Holder<int> objInt = Field<int>(obj);
 cout<<"Holder<int> objInt.value = "<<objInt.value<<endl;
 Holder<Widget> objWidget = Field<Widget>(obj);
 cout<<"Holder<Widget> objWidget.value.m_name = "<<objWidget.value.m_name<<endl;
 cout<<"----------------------------------------------"<<endl;
 //通过索引来查找相应的父类的对象
 //神一样的代码
 typedef GenScatterHierarchy<TYPELIST_4(int,int,string,Widget),Holder> WidgetInfo4;
 WidgetInfo4 obj4;
 Field<0>(obj4).value = 111;
 Field<1>(obj4).value = 222;
 Field<2>(obj4).value = "333";
 Field<3>(obj4).value.m_name = "Don't surprise,I am a Widget!";
 cout<<"Holder<int> obj4.value = "<<Field<0>(obj4).value<<endl;
 cout<<"Holder<int> obj4.value = "<<Field<1>(obj4).value<<endl;
 cout<<"Holder<string> obj4.value = "<<Field<2>(obj4).value<<endl;
 cout<<"Holder<Widget> obj4.value = "<<Field<3>(obj4).value.m_name<<endl;
 cout<<"----------------------------------------------"<<endl;
 system("PAUSE");
 return EXIT_SUCCESS;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值