如何使用可变参数模板制作通用的 Lua 函数包装器?

对于我当前的项目,我一直在为 Lua 包装器编写大量 C/C++.其中很多是简单的 setter 和 getter,所以我设法编写了一些模板来轻松生成这些,如下所示:

For my current project, I've been writing a lot of C/C++ to Lua wrappers. A large number of these are simple setters and getters, so I managed to write some templates that make it easy to generate these, like so:

//        Class       Return      Field
template <typename T, typename U, U T::*Member>
int luaU_get(lua_State* L)
{
    T* obj = luaW_check<T>(L, 1);
    luaU_push<U>(L, obj->*Member);
    return 1;
}

static luaL_reg Foo_Table[] =
{
    ...
    // Now I can just use this generic template to avoid
    // writing simple getter functions
    { "getbar", luaU_get<Foo, Bar, &Foo::bar> }, 
    ...
};

我也想为任意函数的简单函数包装器做类似的事情.例如,能够做到这一点就好了:

I would like to do something similar for simple function wrappers for arbitrary functions too. For example, it would be nice to be able to do this:

template <typename T, typename U, U (T::*Func)(), typename... Args>
int luaU_func(lua_State* L)
{
     // ...?
}

static luaL_reg Foo_Table[] =
{
    ...
    { "baz", luaU_func<Foo, int, &Foo::baz, int, float> }, 
    ...
};

这个想法是模板在编译时实际上是这样的:

The idea is that the template effectively turn out to be this when compiled:

int luaU_func(lua_State* L)
{
     luaU_push<int>(L, luaW_check<Foo>(L, 1)->baz(luaU_check<int>(L, 2), luaU_check<float>(L, 3)));
     return 1;
}

我试过只使用 ... 扩展器,对我来说问题是整数索引值映射到正确的参数.我想不出让它们正常工作的方法.这种事情有可能吗?

I've tried just using the ... expander, the problem for me is the integer index values to map to the proper arguments. I can't think of a way to get them working right. Is such a thing even possible?

(这里已经有了一些魔法;我为 lua_push 和 lua_check 之类的东西写了一些模板化的包装器.所有这些现有的包装器都可以找到 这里)

(there is a little bit of magic going on here already; I wrote some templated wrappers for things like lua_push and lua_check. All of those existing wrappers can be found here)

推荐答案

诀窍是通过部分特化包含包装函数的类模板来利用模板参数推导:

The trick is to exploit template argument deduction by partially specializing a class template that contains the wrapper function:

// Lua API dummies ...

struct lua_State {};

template<class T> void luaU_push(lua_State*,T);
template<class T> T* luaW_check(lua_State*,int);
template<class T> T luaU_check(lua_State*,int);


// metaprogramming for creating indices ...

template<int...Ints>
struct int_pack {};

template<int Begin, int Count, int...Tail>
struct make_int_range_type {
    typedef typename make_int_range_type<Begin,Count-1,Begin+Count-1,Tail...>::type type;
};

template<int Begin, int...Tail>
struct make_int_range_type<Begin,0,Tail...> {
    typedef int_pack<Tail...> type;
};

template<int Begin, int Count>
inline typename make_int_range_type<Begin,Count>::type
make_int_range()
{ return typename make_int_range_type<Begin,Count>::type(); }


// the actual wrapper ...

template<class MemFunPtrType, MemFunPtrType PMF>
struct lua_mem_func_wrapper;

template<class Clazz, class ReturnType, class...Args, ReturnType(Clazz::*PMF)(Args...)>
struct lua_mem_func_wrapper<ReturnType(Clazz::*)(Args...),PMF> {
    static int doit(lua_State* L) {
        return doit_impl(L,make_int_range<2,sizeof...(Args)>());
    }
private:
    template<int...Indices>
    static int doit_impl(lua_State* L, int_pack<Indices...>) {
        luaU_push<ReturnType>(L,
            (luaW_check<Clazz>(L, 1)->*PMF)(
                luaU_check<Args>(L, Indices)...
            )
        );
        return 1;
    }
};

#define GET_MEM_FUN_WRAPPER(...) &lua_mem_func_wrapper<decltype(__VA_ARGS__),__VA_ARGS__>::doit


// testing ...

struct foo {
    int baz(int, float);
};

void test() {
    auto* ptr = GET_MEM_FUN_WRAPPER(&foo::baz);
}

此代码使用选项 -c --std=c++0x 在 G++ 4.6.1 下编译.要查看它是否真的符合您的要求,请对其进行测试...

This code compiles under G++ 4.6.1 using the options -c --std=c++0x. To see whether it really does what you want, please test it ...

相关文章