http://download.csdn.net/detail/zhuyingqingfen/8457091
For typelist in C + + generics, refer to the typelists of C + + generic programming
#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 ' #prag Ma Warning (disable:4584) #endif//_msc_ver//uses typelists to automatically generate CLASSES//1. Produce "Scattered inheritance system" (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>//If the first type is typelist, a recursive genscatterhierarchy<tlist::head,unit> is generated:p ublic genscatterhierarchy<t1,unit>//and Genscatterhierarchy<tlist::tail,unit>, and inherit both. , 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>//If the first type is a single type (Actomictype,The type is passed to the unit relative to the Typelistclass genscatterhierarchy:public unit<atomictype>//genscatterheierarchy, and then the unit is inherited <t>{public:typedef unit<atomictype> leftbase;}; Template<template<class> class Unit>class genscatterhierarchy<nulltype,unit>//empty class {};//2. Generates a linear inheritance system. 1. This unit is required to have two arguments//2. The unit is required to inherit from the second argument base of the unit so that the Genlinearhierarchy can generate a string-like inheritance structure Template<class tlist,template<class Atomictype,class Base>class unit,class Root = emptytype >class genlinearhierarchy;//declaration template<class T1,class T2,template<c Lass,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>:p ublic Unit<T,Root>{};// Genscatterhierarchy Access helper class 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 () {//genscatterhierarchytypedef 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}
Automatic generation of code for C + + generic programming