c++ 泛型编程 之 自动生成代码

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhuyingqingfen/article/details/43938755

 http://download.csdn.net/detail/zhuyingqingfen/8457091

关于 C++ 泛型中的 TypeList ,参考  c++ 泛型编程 之 TypeLists


#ifndef GENSCATTERHIERARCHY_H_
#define GENSCATTERHIERARCHY_H_
#include "typelists.h"
#include "typetraits.h"


#if defined(_MSC_VER) && _MSC_VER >= 1300
// 'class1' : base-class 'class2' is already a base-class of 'class3'
#pragma warning( disable : 4584 )
#endif // _MSC_VER


//运用Typelists 自动产生Classes

//1. 产生“散乱的继承体系”(scattered hierarchies)
template<class TList,template<class>class Unit>class GenScatterHierarchy;

template<class T1,class T2,template<class>class Unit>
class GenScatterHierarchy<Typelist<T1,T2>,Unit>//如果第一个型别是TypeList,就递归产生GenScatterHierarchy<TList::Head,Unit>
	:public GenScatterHierarchy<T1,Unit>//和GenScatterHierarchy<TList::Tail,Unit>,并继承二者。
	,public GenScatterHierarchy<T2,Unit>
{
public:
	typedef Typelist<T1, T2> TList;
	typedef GenScatterHierarchy<T1, Unit> LeftBase;
	typedef GenScatterHierarchy<T2, Unit> RightBase;
};

template<class AtomicType,template<class>class Unit>//如果第一个型别是个单一型别(ActomicType,相对于Typelist
class GenScatterHierarchy : public Unit<AtomicType>// GenScatterHeierarchy便把该型别传递给Unit,然后继承Unit<T>
{
public:
	typedef Unit<AtomicType> LeftBase;
};
template<template<class> class Unit>
class GenScatterHierarchy<NullType,Unit>//空类
{

};

//2. 产生线性继承体系。
//1. 要求这个Unit有两个引数
//2. 要求Unit继承于Unit的第二个引数Base,这样GenLinearHierarchy才能生成串状继承结构
template<
	class TList,
	template<class AtomicType,class Base>class Unit,
	class Root = EmptyType
    >class GenLinearHierarchy;//声明

template<class T1,class T2,template<class,class>class Unit,class Root>
class GenLinearHierarchy<Typelist<T1,T2>,Unit,Root>
	: public Unit<T1,GenLinearHierarchy<T2,Unit,Root> >
{

};
template<class T,template<class,class>class Unit,class Root>
class GenLinearHierarchy<Typelist<T,NullType>,Unit,Root>
	:public Unit<T,Root>
{

};

//GenScatterHierarchy的访问辅助类
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>());
}

#endif


template<class T>
struct Holder
{
	T mValue;
}; 
void hierarchygenerators_Test()
{
	//GenScatterHierarchy
	typedef GenScatterHierarchy<TYPELIST_4(char,int,string,string),Holder> MyTestClass;
	MyTestClass obj;
	Field<0>(obj).mValue = 'a';
	int i = Field<1>(obj).mValue;
	typedef TYPELIST_2(int,int) f;
	//GenLinearHierarchy 
}


猜你喜欢

转载自blog.csdn.net/zhuyingqingfen/article/details/43938755