我将如何生成可变参数? [英] How would I 'generate variadic parameters'?

查看:98
本文介绍了我将如何生成可变参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种方法在这种情况下将一个可变量的参数传递给一个函数:

  template< typename .. 。 
struct午餐
{
午餐(T ...){}
};

template< typename T>
T CheckLuaValue(lua_State * luaState,int index)
{
// Do Stuff
返回值;
}

template< class MemberType,typename ReturnType,typename ... Params>
struct MemberFunctionWrapper< ReturnType(MemberType :: *)(Params ...)>
{
static int CFunctionWrapper(lua_State * luaState)
{
ReturnType(MemberType :: *)(Params ...)functionPointer = GetFunctionPointer
MemberType * member = GetMemberPointer();

int index = 1;

//为Params中的每个类型获取一个值
Lunch< Params ...>
{
(CheckLuaValue< Params>(luaState,index),index ++,void(),0)...
}

CheckLuaValue在Params中返回每个类型的值。我的问题是,我现在需要一个方法来调用我的函数与所有这些值

  member-> * functionPointer(returnedLuaValues ); 

}
};

我该如何做?

运算符,版本:

  #include< iostream> 
struct lua_State {};
template< typename T>
T CheckLuaValue(int n,lua_State * l)
{
std :: cout< arg [<< n<< ] gotten\\\
;
return T(n);
}

template< int ...>
struct seq {};

//产生seq<首先,...,Last-1> astype
template< int First,int Last>
struct gen_seq
{
template< int N,int ... S>
struct helper:helper< N-1,N-1,S ...> {};
template< int ... S>
struct helper< First,S ...> {
typedef seq< S ...>类型;
};
typedef typename helper< Last> :: type type;
};

template<类型名X>
struct MemberFunctionWrapper;

template<类型名F>
struct MemberFunctionHelper
{
typedef F MethodPtr;
};
template< class InstanceType,typename ReturnType,typename ... Params>
structure MemberFunctionWrapper< ReturnType(InstanceType :: *)(Params ...)>
{
typedef MemberFunctionHelper< ReturnType(InstanceType :: *)(Params ...)>帮手;
typedef typename Helper :: MethodPtr MethodPtr;
static MethodPtr& GetFunctionPointer(){static MethodPtr pFunc; return pFunc;}
static InstanceType *& GetMemberPointer(){static InstanceType * pThis; return pThis;}
template< int n,typename Param>
static auto GetLuaValue(lua_State * luaState) - > decltype(CheckLuaValue< Param>(n,luaState))
{
return CheckLuaValue< Param&
}

template<类型名序列>
struct call;

template< int>
struct call< seq< I ...>>
{
ReturnType operator()(lua_State * luaState,InstanceType * instance,MethodPtr method)const
{
return(instance-> * method)(GetLuaValue< I,Params> ;(luaState)...);
}
};
static int CFunctionWrapper(lua_State * luaState)
{
MethodPtr func = GetFunctionPointer();
InstanceType * instance = GetMemberPointer();
ReturnType retval = call< typename gen_seq& 1,sizeof ...(Params)+1> :: type>()(luaState,instance,func);
return 0;
}
};

struct test {int foo(int x,double d){std :: cout< x:<< x < d:<< d<< \\\
;}};
int main(){
typedef MemberFunctionWrapper< int(test :: *)(int,double)>包装
test bar;
wrapper :: GetFunctionPointer()=& test :: foo;
wrapper :: GetMemberPointer()=& bar;
wrapper :: CFunctionWrapper(0);
}

现在,注意CheckLuaValue的调用可能是无序的它可以在arg 1之前从Lua请求arg 2),但正确的一个将被传递到正确的参数。



这里是一个测试运行: http://ideone.com/XVmQQ6


I need a way to pass a variable amount of parameters to a function in this circumstance:

template<typename ...T>
struct Lunch
{
    Lunch(T...){}
};

template<typename T>
T CheckLuaValue(lua_State* luaState,int index)
{
    //Do Stuff
    return value;
}

template <class MemberType, typename ReturnType, typename... Params>
struct MemberFunctionWrapper <ReturnType (MemberType::*) (Params...)>
{
    static int CFunctionWrapper (lua_State* luaState)
    {
        ReturnType (MemberType::*)(Params...) functionPointer = GetFunctionPointer();
        MemberType* member = GetMemberPointer();

        int index = 1;

        //Get a value for each type in Params
        Lunch<Params...>
        {
           (CheckLuaValue<Params>(luaState,index), index++, void(), 0)...
        };

CheckLuaValue returns a value for each type in Params. My problem is that I now need a way to call my function with all of those values

        member->*functionPointer(returnedLuaValues); 

    }
};

How would I go about doing this?

解决方案

So I stole some of Luc Danton's sequence advice to sFuller and Pubby (about sequences), and I generated this "stop messing around with operator," version:

#include <iostream>
struct lua_State {};
template<typename T>
T CheckLuaValue( int n, lua_State* l)
{
    std::cout << "arg[" << n << "] gotten\n";
    return T(n);
}

template<int ...>
struct seq { };

// generates a seq< First, ..., Last-1 > as "type"
template<int First, int Last>
struct gen_seq
{
  template<int N, int... S>
  struct helper : helper<N-1, N-1, S...> {};
  template<int... S>
  struct helper<First, S...> {
    typedef seq<S...> type;
  };
  typedef typename helper<Last>::type type;
};

template< typename X >
struct MemberFunctionWrapper;

template< typename F >
struct MemberFunctionHelper
{
    typedef F MethodPtr;
};
template<class InstanceType, typename ReturnType, typename... Params>
struct MemberFunctionWrapper< ReturnType(InstanceType::*)(Params...) >
{
  typedef MemberFunctionHelper<ReturnType(InstanceType::*)(Params...)> Helper;
  typedef typename Helper::MethodPtr MethodPtr;
  static MethodPtr& GetFunctionPointer() {static MethodPtr pFunc; return pFunc;}
  static InstanceType*& GetMemberPointer() {static InstanceType* pThis;return pThis;}
  template<int n, typename Param>
  static auto GetLuaValue( lua_State* luaState )->decltype(CheckLuaValue<Param>(n,luaState))
  {
    return CheckLuaValue<Param>(n,luaState);
  }

  template< typename sequence >
  struct call;

  template< int... I >
  struct call<seq<I...>>
  {
    ReturnType operator()( lua_State* luaState, InstanceType* instance, MethodPtr method ) const
    {
      return (instance->*method)( GetLuaValue<I,Params>( luaState )... );
    }
  };
  static int CFunctionWrapper( lua_State* luaState)
  {
    MethodPtr func = GetFunctionPointer();
    InstanceType* instance = GetMemberPointer();
    ReturnType retval = call< typename gen_seq< 1, sizeof...(Params)+1 >::type >()( luaState, instance, func );
    return 0;
  }
};

struct test{ int foo(int x, double d){std::cout << "x:" << x << " d:" << d << "\n";}};
int main(){
    typedef MemberFunctionWrapper< int(test::*)(int, double) > wrapper;
    test bar;
    wrapper::GetFunctionPointer() = &test::foo;
    wrapper::GetMemberPointer() = &bar;
    wrapper::CFunctionWrapper(0);
}

now, note that the calls to CheckLuaValue can be out of order (ie, it can ask for arg 2 from Lua before arg 1), but the right one will be passed to the right argument.

Here is a test run: http://ideone.com/XVmQQ6

这篇关于我将如何生成可变参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆