#include "stdafx.h"
#include <iostream>
#include <string>
#include <typeinfo>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/push_back.hpp>
#include <boost/mpl/begin_end.hpp>
#include <boost/mpl/at.hpp>
#include <boost/mpl/erase.hpp>
#include <boost/mpl/pop_front.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/deref.hpp>
#include <boost/mpl/front.hpp>
#include <boost/mpl/back.hpp>
using std::type_info;
using std::string;
using std::cout;
using std::endl;
using boost::mpl::at ;
using boost::mpl::int_;
using boost::mpl::pop_front;
using boost::mpl::vector;
using boost::mpl::deref;
using boost::mpl::begin;
using boost::mpl::front;
using boost::mpl::back;
struct NullType{ };
template<int N>
struct Int2Type
{
enum{ vaule = N };
};
template <class T>
struct Holder{
typename T vaule;
};
//允许模板参数的最大值为10。 也可以自己嵌套写,用vector, 这样可以写更多的参数。
template <typename T1, typename T2 = NullType, typename T3 = NullType,
typename T4= NullType,typename T5 = NullType, typename T6 = NullType,
typename T7 = NullType,typename T8= NullType,typename T9 = NullType,
typename T10 = NullType>
struct my_vector{
typedef vector<T1,vector<T2,vector<T3,vector<T4,vector<T5,vector<T6,
vector<T7, vector<T8,vector<T9,vector<T10,NullType>>>>>>>>>> type;
};
template <typename T1>
struct my_vector<T1>{
typedef vector<T1,NullType> type;
};
template <typename T1, typename T2>
struct my_vector<T1,T2>{
typedef vector<T1,vector<T2,NullType>> type;
};
template <typename T1, typename T2, typename T3>
struct my_vector<T1,T2,T3>{
typedef vector<T1,vector<T2,vector<T3,NullType>>> type;
};
template <typename T1, typename T2, typename T3, typename T4>
struct my_vector<T1,T2,T3,T4>{
typedef vector<T1,vector<T2,vector<T3,vector<T4,NullType>>>> type;
};
template <typename T1, typename T2, typename T3, typename T4, typename T5>
struct my_vector<T1,T2,T3,T4,T5>{
typedef vector<T1,vector<T2,vector<T3,vector<T4,
vector<T5,NullType>>>>> type;
};
template <typename T1, typename T2, typename T3, typename T4,
typename T5,typename T6>
struct my_vector<T1,T2,T3,T4,T5,T6>{
typedef vector<T1,vector<T2,vector<T3,vector<T4,vector<T5,
vector<T6, NullType>>>>>> type;
};
template <typename T1, typename T2, typename T3, typename T4,
typename T5,typename T6,typename T7>
struct my_vector<T1,T2,T3,T4,T5,T6,T7>{
typedef vector<T1,vector<T2,vector<T3,vector<T4,vector<T5,vector<T6,
vector<T7, NullType>>>>>>> type;
};
template <typename T1, typename T2, typename T3, typename T4, typename T5,
typename T6,typename T7,typename T8>
struct my_vector<T1,T2,T3,T4,T5,T6,T7,T8>{
typedef vector<T1,vector<T2,vector<T3,vector<T4,vector<T5,vector<T6, vector<T7,
vector<T8,NullType>>>>>>>> type;
};
template <typename T1, typename T2, typename T3, typename T4,
typename T5,typename T6,typename T7,typename T8,typename T9>
struct my_vector<T1,T2,T3,T4,T5,T6,T7,T8,T9>{
typedef vector<T1,vector<T2,vector<T3,vector<T4,vector<T5,vector<T6,
vector<T7, vector<T8,vector<T9,NullType>>>>>>>>> type;
};
template<class TList, unsigned int index> struct TypeAt;
template <class Head, class Tail>
struct TypeAt<vector<Head, Tail>,0>
{
typedef Head Result;
};
template <class Head, class Tail, unsigned int i>
struct TypeAt<vector<Head, Tail>, i>
{
typedef typename TypeAt<Tail, i-1>::Result Result;
};
template <class TList, template<class> class Unit>
class GenScatterHierarchy;//主模板
template<class T1, class T2, template<class> class Unit>
class GenScatterHierarchy<vector<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>{
};
template<class TList, template<class> class Unit>
Unit<typename front<TList>::type >& FieldHelper(
GenScatterHierarchy<TList,Unit>& obj, Int2Type<0> )
{
GenScatterHierarchy<typename front<TList>::type, 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>)
{
typename GenScatterHierarchy<typename at<TList,int_<1>>::type, 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, _TCHAR* argv[])
{
typedef GenScatterHierarchy<typename my_vector<int,float,string>::type, Holder> Info;
Info obj;
float b = (static_cast<Holder<float>&>(obj)).vaule;//ok
//deref<begin<vector<int>::type>::type>::type a=9;
int a = Field<0>(obj).vaule;
float fl = Field<1>(obj).vaule;
string str = Field<2>(obj).vaule;
return 0;
}