Pioneer
LuaCall.h
Go to the documentation of this file.
1 // Copyright © 2008-2021 Pioneer Developers. See AUTHORS.txt for details
2 // Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3 
4 #pragma once
5 
6 /*
7  This file implements a generic method to call a C++ member function with
8  arguments passed in from Lua. It's intended to back a generic method of
9  binding code to Lua with minimal boilerplate needed.
10 */
11 
12 #include "Lua.h"
13 #include "LuaPushPull.h"
14 
15 // Backend to bind call a C++ member function with arguments from Lua.
16 // Parameter `index` points one-behind the first argument, where the object
17 // pointer should traditionally be
18 
19 template<class T, typename Ret>
20 Ret pi_lua_multiple_call(lua_State *l, int index, T *ptr, Ret (T::*fn)())
21 {
22  return (ptr->*fn)();
23 }
24 
25 template<class T, typename Ret, typename Arg1>
26 Ret pi_lua_multiple_call(lua_State *l, int index, T *ptr, Ret (T::*fn)(Arg1))
27 {
28  auto arg1 = LuaPull<Arg1>(l, index+1);
29  return (ptr->*fn)(arg1);
30 }
31 
32 template<class T, typename Ret, typename Arg1, typename Arg2>
33 Ret pi_lua_multiple_call(lua_State *l, int index, T *ptr, Ret (T::*fn)(Arg1, Arg2))
34 {
35  auto arg1 = LuaPull<Arg1>(l, index+1);
36  auto arg2 = LuaPull<Arg2>(l, index+2);
37  return (ptr->*fn)(arg1, arg2);
38 }
39 
40 template<class T, typename Ret, typename Arg1, typename Arg2, typename Arg3>
41 Ret pi_lua_multiple_call(lua_State *l, int index, T *ptr, Ret (T::*fn)(Arg1, Arg2, Arg3))
42 {
43  auto arg1 = LuaPull<Arg1>(l, index+1);
44  auto arg2 = LuaPull<Arg2>(l, index+2);
45  auto arg3 = LuaPull<Arg3>(l, index+3);
46  return (ptr->*fn)(arg1, arg2, arg3);
47 }
48 
49 template<class T, typename Ret, typename Arg1, typename Arg2, typename Arg3, typename Arg4>
50 Ret pi_lua_multiple_call(lua_State *l, int index, T *ptr, Ret (T::*fn)(Arg1, Arg2, Arg3, Arg4))
51 {
52  auto arg1 = LuaPull<Arg1>(l, index+1);
53  auto arg2 = LuaPull<Arg2>(l, index+2);
54  auto arg3 = LuaPull<Arg3>(l, index+3);
55  auto arg4 = LuaPull<Arg4>(l, index+4);
56  return (ptr->*fn)(arg1, arg2, arg3, arg4);
57 }
58 
59 template<class T, typename Ret, typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5>
60 Ret pi_lua_multiple_call(lua_State *l, int index, T *ptr, Ret (T::*fn)(Arg1, Arg2, Arg3, Arg4, Arg5))
61 {
62  auto arg1 = LuaPull<Arg1>(l, index+1);
63  auto arg2 = LuaPull<Arg2>(l, index+2);
64  auto arg3 = LuaPull<Arg3>(l, index+3);
65  auto arg4 = LuaPull<Arg4>(l, index+4);
66  auto arg5 = LuaPull<Arg5>(l, index+5);
67  return (ptr->*fn)(arg1, arg2, arg3, arg4, arg5);
68 }
69 
70 template<class T, typename Ret, typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5, typename Arg6>
71 Ret pi_lua_multiple_call(lua_State *l, int index, T *ptr, Ret (T::*fn)(Arg1, Arg2, Arg3, Arg4, Arg5, Arg6))
72 {
73  auto arg1 = LuaPull<Arg1>(l, index+1);
74  auto arg2 = LuaPull<Arg2>(l, index+2);
75  auto arg3 = LuaPull<Arg3>(l, index+3);
76  auto arg4 = LuaPull<Arg4>(l, index+4);
77  auto arg5 = LuaPull<Arg5>(l, index+5);
78  auto arg6 = LuaPull<Arg6>(l, index+6);
79  return (ptr->*fn)(arg1, arg2, arg3, arg4, arg5, arg6);
80 }
81 
82 template<class T, typename Ret, typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5, typename Arg6, typename Arg7>
83 Ret pi_lua_multiple_call(lua_State *l, int index, T *ptr, Ret (T::*fn)(Arg1, Arg2, Arg3, Arg4, Arg5, Arg6, Arg7))
84 {
85  auto arg1 = LuaPull<Arg1>(l, index+1);
86  auto arg2 = LuaPull<Arg2>(l, index+2);
87  auto arg3 = LuaPull<Arg3>(l, index+3);
88  auto arg4 = LuaPull<Arg4>(l, index+4);
89  auto arg5 = LuaPull<Arg5>(l, index+5);
90  auto arg6 = LuaPull<Arg6>(l, index+6);
91  auto arg7 = LuaPull<Arg7>(l, index+7);
92  return (ptr->*fn)(arg1, arg2, arg3, arg4, arg5, arg6, arg7);
93 }
94 
95 // Pull an object and arguments for the passed function from Lua.
96 // Intended to ease the amount of boilerplate code needed to bind a method to Lua.
97 
98 template<class T, typename Ret, typename ...Args>
99 Ret pi_lua_generic_call(lua_State *l, int index, Ret (T::*fn)(Args...))
100 {
101  T *ptr = LuaPull<T>(l, index);
102  return pi_lua_multiple_call<T, Ret, Args...>(l, index, ptr, fn);
103 }
Ret pi_lua_generic_call(lua_State *l, int index, Ret(T::*fn)(Args...))
Definition: LuaCall.h:99
Ret pi_lua_multiple_call(lua_State *l, int index, T *ptr, Ret(T::*fn)())
Definition: LuaCall.h:20