1. <small id='Kkfns'></small><noframes id='Kkfns'>

      2. <legend id='Kkfns'><style id='Kkfns'><dir id='Kkfns'><q id='Kkfns'></q></dir></style></legend>

        <tfoot id='Kkfns'></tfoot>
          <bdo id='Kkfns'></bdo><ul id='Kkfns'></ul>

      3. <i id='Kkfns'><tr id='Kkfns'><dt id='Kkfns'><q id='Kkfns'><span id='Kkfns'><b id='Kkfns'><form id='Kkfns'><ins id='Kkfns'></ins><ul id='Kkfns'></ul><sub id='Kkfns'></sub></form><legend id='Kkfns'></legend><bdo id='Kkfns'><pre id='Kkfns'><center id='Kkfns'></center></pre></bdo></b><th id='Kkfns'></th></span></q></dt></tr></i><div id='Kkfns'><tfoot id='Kkfns'></tfoot><dl id='Kkfns'><fieldset id='Kkfns'></fieldset></dl></div>

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

        How to use variadic templates to make a generic Lua function wrapper?(如何使用可变参数模板制作通用的 Lua 函数包装器?)
          • <tfoot id='Y7028'></tfoot>

            <legend id='Y7028'><style id='Y7028'><dir id='Y7028'><q id='Y7028'></q></dir></style></legend>

              <bdo id='Y7028'></bdo><ul id='Y7028'></ul>
            • <small id='Y7028'></small><noframes id='Y7028'>

              <i id='Y7028'><tr id='Y7028'><dt id='Y7028'><q id='Y7028'><span id='Y7028'><b id='Y7028'><form id='Y7028'><ins id='Y7028'></ins><ul id='Y7028'></ul><sub id='Y7028'></sub></form><legend id='Y7028'></legend><bdo id='Y7028'><pre id='Y7028'><center id='Y7028'></center></pre></bdo></b><th id='Y7028'></th></span></q></dt></tr></i><div id='Y7028'><tfoot id='Y7028'></tfoot><dl id='Y7028'><fieldset id='Y7028'></fieldset></dl></div>

                <tbody id='Y7028'></tbody>

                  本文介绍了如何使用可变参数模板制作通用的 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 ...

                  这篇关于如何使用可变参数模板制作通用的 Lua 函数包装器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Constructor initialization Vs assignment(构造函数初始化 Vs 赋值)
                  Is a `=default` move constructor equivalent to a member-wise move constructor?(`=default` 移动构造函数是否等同于成员移动构造函数?)
                  Has the new C++11 member initialization feature at declaration made initialization lists obsolete?(声明时新的 C++11 成员初始化功能是否使初始化列表过时了?)
                  Order of constructor call in virtual inheritance(虚继承中构造函数调用的顺序)
                  How to use sfinae for selecting constructors?(如何使用 sfinae 选择构造函数?)
                  Initializing a union with a non-trivial constructor(使用非平凡的构造函数初始化联合)

                    <small id='kjPmj'></small><noframes id='kjPmj'>

                    1. <legend id='kjPmj'><style id='kjPmj'><dir id='kjPmj'><q id='kjPmj'></q></dir></style></legend>
                        <bdo id='kjPmj'></bdo><ul id='kjPmj'></ul>

                        <i id='kjPmj'><tr id='kjPmj'><dt id='kjPmj'><q id='kjPmj'><span id='kjPmj'><b id='kjPmj'><form id='kjPmj'><ins id='kjPmj'></ins><ul id='kjPmj'></ul><sub id='kjPmj'></sub></form><legend id='kjPmj'></legend><bdo id='kjPmj'><pre id='kjPmj'><center id='kjPmj'></center></pre></bdo></b><th id='kjPmj'></th></span></q></dt></tr></i><div id='kjPmj'><tfoot id='kjPmj'></tfoot><dl id='kjPmj'><fieldset id='kjPmj'></fieldset></dl></div>

                        <tfoot id='kjPmj'></tfoot>

                            <tbody id='kjPmj'></tbody>