码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-02-25 18:39:48      阅读:286      评论:0      收藏:0      [点我收藏+]

标签:c++   泛型   结构   模板   

 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 
}


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

标签:c++   泛型   结构   模板   

原文地址:http://blog.csdn.net/zhuyingqingfen/article/details/43938755

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!