Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So I stole some of Luc Danton's sequence advice to sFuller and Pubby (about sequences), and I generated this "stop messing around with <code>operator,</code>" version:</p> <pre><code>#include &lt;iostream&gt; struct lua_State {}; template&lt;typename T&gt; T CheckLuaValue( int n, lua_State* l) { std::cout &lt;&lt; "arg[" &lt;&lt; n &lt;&lt; "] gotten\n"; return T(n); } template&lt;int ...&gt; struct seq { }; // generates a seq&lt; First, ..., Last-1 &gt; as "type" template&lt;int First, int Last&gt; struct gen_seq { template&lt;int N, int... S&gt; struct helper : helper&lt;N-1, N-1, S...&gt; {}; template&lt;int... S&gt; struct helper&lt;First, S...&gt; { typedef seq&lt;S...&gt; type; }; typedef typename helper&lt;Last&gt;::type type; }; template&lt; typename X &gt; struct MemberFunctionWrapper; template&lt; typename F &gt; struct MemberFunctionHelper { typedef F MethodPtr; }; template&lt;class InstanceType, typename ReturnType, typename... Params&gt; struct MemberFunctionWrapper&lt; ReturnType(InstanceType::*)(Params...) &gt; { typedef MemberFunctionHelper&lt;ReturnType(InstanceType::*)(Params...)&gt; Helper; typedef typename Helper::MethodPtr MethodPtr; static MethodPtr&amp; GetFunctionPointer() {static MethodPtr pFunc; return pFunc;} static InstanceType*&amp; GetMemberPointer() {static InstanceType* pThis;return pThis;} template&lt;int n, typename Param&gt; static auto GetLuaValue( lua_State* luaState )-&gt;decltype(CheckLuaValue&lt;Param&gt;(n,luaState)) { return CheckLuaValue&lt;Param&gt;(n,luaState); } template&lt; typename sequence &gt; struct call; template&lt; int... I &gt; struct call&lt;seq&lt;I...&gt;&gt; { ReturnType operator()( lua_State* luaState, InstanceType* instance, MethodPtr method ) const { return (instance-&gt;*method)( GetLuaValue&lt;I,Params&gt;( luaState )... ); } }; static int CFunctionWrapper( lua_State* luaState) { MethodPtr func = GetFunctionPointer(); InstanceType* instance = GetMemberPointer(); ReturnType retval = call&lt; typename gen_seq&lt; 1, sizeof...(Params)+1 &gt;::type &gt;()( luaState, instance, func ); return 0; } }; struct test{ int foo(int x, double d){std::cout &lt;&lt; "x:" &lt;&lt; x &lt;&lt; " d:" &lt;&lt; d &lt;&lt; "\n";}}; int main(){ typedef MemberFunctionWrapper&lt; int(test::*)(int, double) &gt; wrapper; test bar; wrapper::GetFunctionPointer() = &amp;test::foo; wrapper::GetMemberPointer() = &amp;bar; wrapper::CFunctionWrapper(0); } </code></pre> <p>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.</p> <p>Here is a test run: <a href="http://ideone.com/XVmQQ6" rel="nofollow">http://ideone.com/XVmQQ6</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload