Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use variadic templates to make a generic Lua function wrapper?

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?

(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)

like image 823
Alex Avatar asked Jul 21 '12 07:07

Alex


1 Answers

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);
}

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 ...

like image 187
sellibitze Avatar answered Sep 30 '22 16:09

sellibitze