Loki中的Tuple主要是基于GenScatterHierarchy和TypeList,继承TypeList中所有类
但是对于Tuple本身的操作完全没有定义~使用起来还是不够方便的~我在原始基础上构造了我自己的EasyTuple,可以更好的支持域操作。而且实现了一个可对Tuple所有域进行某一操作的TupleOperator模板(比如可用一个输出模板实例化TupleOperator函数输出整个Tuple,一个用一个AddOne模板实例化TupleOperator将所有域加1)
下面是EasyTuple的定义与简单应用
template<typename TypeList>
class EasyTuple : public Loki::GenScatterHierarchy<TypeList,Loki::TupleUnit>
{
public:
template<int FieldId>
inline void SetField(const typename Loki::TL::TypeAt<TypeList,FieldId>::Result & v)
{
Loki::Field<FieldId>(*this) = v;
}
template<int FieldId>
inline const typename Loki::FieldHelper<EasyTuple,FieldId>::ResultType GetField()
{
return Loki::Field<FieldId>(*this);
}
template<int FieldId>
typename Loki::FieldHelper<EasyTuple,FieldId>::ResultType& operator[](Loki::Int2Type<FieldId>)
{
return Loki::Field<FieldId>(*this);
}
};
template <class TupleUnitType>
struct PrintTupleUnit
{
void operator()(TupleUnitType& obj)
{ std::cout << '[' << obj << ']';}
};
template <class TupleUnitType>
struct AddTupleUnit
{
void operator()(TupleUnitType& obj)
{ obj = obj+1; }
};
#define EASYTUPLE_3(T1,T2,T3) EasyTuple<LOKI_TYPELIST_3(T1,T2,T3)>
#define EASYTUPLE_3_INDEX(I1,I2,I3) Loki::Int2Type<0> I1;Loki::Int2Type<1> I2;Loki::Int2Type<2> I3;
//用宏定义3个用于索引的项,这样可以用自定义的索引访问域成员
int main(int argc, char* argv[])
{
EASYTUPLE_3_INDEX(index0,index1,index2)
typedef EASYTUPLE_3(bool,int,float) Tuple;
Tuple tuple;
tuple[index0] = true;
tuple[index1] = 1;
tuple[index2] = 1.3f;
TupleOperator<3,Tuple,PrintTupleUnit> operator_output;
operator_output(tuple);
TupleOperator<3,Tuple,AddTupleUnit> operator_add;
operator_add(tuple);
operator_output(tuple);
return 0;
}
输出为[1,1,1.3][1,2,2.3]
但是对于Tuple本身的操作完全没有定义~使用起来还是不够方便的~我在原始基础上构造了我自己的EasyTuple,可以更好的支持域操作。而且实现了一个可对Tuple所有域进行某一操作的TupleOperator模板(比如可用一个输出模板实例化TupleOperator函数输出整个Tuple,一个用一个AddOne模板实例化TupleOperator将所有域加1)
下面是EasyTuple的定义与简单应用
template<typename TypeList>
class EasyTuple : public Loki::GenScatterHierarchy<TypeList,Loki::TupleUnit>
{
public:
template<int FieldId>
inline void SetField(const typename Loki::TL::TypeAt<TypeList,FieldId>::Result & v)
{
Loki::Field<FieldId>(*this) = v;
}
template<int FieldId>
inline const typename Loki::FieldHelper<EasyTuple,FieldId>::ResultType GetField()
{
return Loki::Field<FieldId>(*this);
}
template<int FieldId>
typename Loki::FieldHelper<EasyTuple,FieldId>::ResultType& operator[](Loki::Int2Type<FieldId>)
{
return Loki::Field<FieldId>(*this);
}
};
template <class TupleUnitType>
struct PrintTupleUnit
{
void operator()(TupleUnitType& obj)
{ std::cout << '[' << obj << ']';}
};
template <class TupleUnitType>
struct AddTupleUnit
{
void operator()(TupleUnitType& obj)
{ obj = obj+1; }
};
#define EASYTUPLE_3(T1,T2,T3) EasyTuple<LOKI_TYPELIST_3(T1,T2,T3)>
#define EASYTUPLE_3_INDEX(I1,I2,I3) Loki::Int2Type<0> I1;Loki::Int2Type<1> I2;Loki::Int2Type<2> I3;
//用宏定义3个用于索引的项,这样可以用自定义的索引访问域成员
int main(int argc, char* argv[])
{
EASYTUPLE_3_INDEX(index0,index1,index2)
typedef EASYTUPLE_3(bool,int,float) Tuple;
Tuple tuple;
tuple[index0] = true;
tuple[index1] = 1;
tuple[index2] = 1.3f;
TupleOperator<3,Tuple,PrintTupleUnit> operator_output;
operator_output(tuple);
TupleOperator<3,Tuple,AddTupleUnit> operator_add;
operator_add(tuple);
operator_output(tuple);
return 0;
}
输出为[1,1,1.3][1,2,2.3]