2009-07-16 43 views
1

我試圖初始化一個參數列表,用於fusion :: invoke。 的ARG遊戲的所有形式:如何迭代boost :: fusion序列?

template <typename Type> 
struct ArgWrapper 
{ 
    inline ArgWrapper(){} 
    inline void Setup(lua_State*L,int idx) 
    { 
     //setup this value from the lua state... 
     //in reality this class is specialised for different lua types 
    } 
    operator Type(){return value;} 
    Type value; 
}; 

所以我可以做,例如

int add(int a,int b){return a+b;} 
fusion::vector<ArgsWrapper<int>,ArgsWrapper<int> > v; 
fusion::at_c<0>(v).value=1; 
fusion::at_c<1>(v).value=2; 
std::cout<<fusion::invoke(add,v)//prints 3 

但是,如果我有型FusionListType的融合序列,在那裏我知道序列中的每個類型一個類型的ArgWrapper,我如何遍歷該列表並在每個元素上調用Setup函數(我只有一個lua_State指針,並希望將它用作安裝程序的第一個參數,並且我想使用序列中的位置作爲第二個參數)。

因此,對於大小爲3的向量我想要得到的邏輯是:

lua_State*L; 
fusion::at_c<0>.Setup(L,1); 
fusion::at_c<1>.Setup(L,2); 
fusion::at_c<2>.Setup(L,3); 

我曾嘗試:

template<typename ArgWrapperType,int N> 
void FillArgWrapper(ArgWrapperType arg,lua_State*L) 
{ 
    fusion::at_c<N>(arg).Setup(L,N+1); 
} 
template<typename ArgWrapperType> 
void FillArgWrapper<ArgWrapperType,0>(ArgWrapperType arg,lua_State*L) 
{ 
    fusion::at_c<0>(arg).Setup(L,1); 
} 

但這無法編譯,說function template partial specialisation ‘FillArgWrapper<ArgWrapperType, 0>’ is not allowed

在此先感謝。

回答

4

好的,我想通了。我需要使用一個結構:

template <typename ArgWrapperList,u32 N=mpl::size<ArgWrapperList>::value-1> 
struct ArgWrapperListFiller 
{ 
    static inline void Setup(ArgWrapperList &args,lua_State*L) 
    { 
     fusion::at_c<N>(args).Setup(L,N+1); 
     ArgWrapperListFiller<ArgWrapperList,N-1>::Setup(args,L); 
    } 
}; 
template <typename ArgWrapperList> //base case, do not recurse 
struct ArgWrapperListFiller<ArgWrapperList,0> 
{ 
    static inline void Fill(ArgWrapperList &args,lua_State*L) 
    { 
     fusion::at_c<0>(args).Setup(L,1); 
    }; 
};