Pioneer
LuaPushPull.h
Go to the documentation of this file.
1 // Copyright © 2008-2022 Pioneer Developers. See AUTHORS.txt for details
2 // Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3 
4 #ifndef _LUAPUSHPULL_H
5 #define _LUAPUSHPULL_H
6 
7 #include <lua.hpp>
8 #include "Lua.h"
9 
10 #include <cstddef>
11 #include <string>
12 #include <tuple>
13 
14 inline void pi_lua_generic_push(lua_State *l, bool value) { lua_pushboolean(l, value); }
15 inline void pi_lua_generic_push(lua_State *l, int value) { lua_pushinteger(l, value); }
16 inline void pi_lua_generic_push(lua_State *l, unsigned int value) { lua_pushinteger(l, value); }
17 inline void pi_lua_generic_push(lua_State *l, double value) { lua_pushnumber(l, value); }
18 inline void pi_lua_generic_push(lua_State *l, const char *value) { lua_pushstring(l, value); }
19 inline void pi_lua_generic_push(lua_State *l, const std::string &value)
20 {
21  lua_pushlstring(l, value.c_str(), value.size());
22 }
23 inline void pi_lua_generic_push(lua_State *l, const std::nullptr_t &value) { lua_pushnil(l); }
24 
25 inline void pi_lua_generic_pull(lua_State *l, int index, bool &out) { out = lua_toboolean(l, index); }
26 inline void pi_lua_generic_pull(lua_State *l, int index, int &out) { out = luaL_checkinteger(l, index); }
27 inline void pi_lua_generic_pull(lua_State *l, int index, unsigned int &out) { out = luaL_checkunsigned(l, index); }
28 inline void pi_lua_generic_pull(lua_State *l, int index, float &out) { out = luaL_checknumber(l, index); }
29 inline void pi_lua_generic_pull(lua_State *l, int index, double &out) { out = luaL_checknumber(l, index); }
30 inline void pi_lua_generic_pull(lua_State *l, int index, const char *&out) { out = luaL_checkstring(l, index); }
31 inline void pi_lua_generic_pull(lua_State *l, int index, std::string &out)
32 {
33  size_t len;
34  const char *buf = luaL_checklstring(l, index, &len);
35  std::string(buf, len).swap(out);
36 }
37 
38 template <typename Type>
39 inline void LuaPush(lua_State *l, Type value)
40 {
41  pi_lua_generic_push(l, value);
42 }
43 
44 template <typename Type>
45 inline typename std::remove_reference<Type>::type LuaPull(lua_State *l, int index)
46 {
47  typename std::decay<Type>::type value;
48  pi_lua_generic_pull(l, index, value);
49  return value;
50 }
51 
52 // Pull a value with an optional default.
53 template <typename Type>
54 inline Type LuaPull(lua_State *l, int index, Type defaultVal)
55 {
56  Type value = defaultVal;
57  if (lua_gettop(l) >= index && !lua_isnil(l, index))
58  pi_lua_generic_pull(l, index, value);
59  return value;
60 }
61 
62 inline bool pi_lua_strict_pull(lua_State *l, int index, bool &out)
63 {
64  if (lua_type(l, index) == LUA_TBOOLEAN) {
65  out = lua_toboolean(l, index);
66  return true;
67  }
68  return false;
69 }
70 inline bool pi_lua_strict_pull(lua_State *l, int index, int &out)
71 {
72  if (lua_type(l, index) == LUA_TNUMBER) {
73  out = lua_tointeger(l, index);
74  return true;
75  }
76  return false;
77 }
78 inline bool pi_lua_strict_pull(lua_State *l, int index, float &out)
79 {
80  if (lua_type(l, index) == LUA_TNUMBER) {
81  out = lua_tonumber(l, index);
82  return true;
83  }
84  return false;
85 }
86 inline bool pi_lua_strict_pull(lua_State *l, int index, double &out)
87 {
88  if (lua_type(l, index) == LUA_TNUMBER) {
89  out = lua_tonumber(l, index);
90  return true;
91  }
92  return false;
93 }
94 inline bool pi_lua_strict_pull(lua_State *l, int index, const char *&out)
95 {
96  if (lua_type(l, index) == LUA_TSTRING) {
97  out = lua_tostring(l, index);
98  return true;
99  }
100  return false;
101 }
102 inline bool pi_lua_strict_pull(lua_State *l, int index, std::string &out)
103 {
104  if (lua_type(l, index) == LUA_TSTRING) {
105  size_t len;
106  const char *buf = lua_tolstring(l, index, &len);
107  std::string(buf, len).swap(out);
108  return true;
109  }
110  return false;
111 }
112 template <typename... Types>
113 inline void pi_lua_multiple_push(lua_State *l, Types... args);
114 
115 #if defined(_MSC_VER) // Non-variadic version for MSVC
116 template <typename Arg1>
117 inline void pi_lua_multiple_push(lua_State *l, Arg1 arg1)
118 {
119  pi_lua_generic_push(l, arg1);
120 }
121 
122 template <typename Arg1, typename Arg2>
123 inline void pi_lua_multiple_push(lua_State *l, Arg1 arg1, Arg2 arg2)
124 {
125  pi_lua_generic_push(l, arg1);
126  pi_lua_generic_push(l, arg2);
127 }
128 
129 template <typename Arg1, typename Arg2, typename Arg3>
130 inline void pi_lua_multiple_push(lua_State *l, Arg1 arg1, Arg2 arg2, Arg3 arg3)
131 {
132  pi_lua_generic_push(l, arg1);
133  pi_lua_generic_push(l, arg2);
134  pi_lua_generic_push(l, arg3);
135 }
136 
137 template <typename Arg1, typename Arg2, typename Arg3, typename Arg4>
138 inline void pi_lua_multiple_push(lua_State *l, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4)
139 {
140  pi_lua_generic_push(l, arg1);
141  pi_lua_generic_push(l, arg2);
142  pi_lua_generic_push(l, arg3);
143  pi_lua_generic_push(l, arg4);
144 }
145 #else // Just use the normal variadic version
146 template <typename Head, typename... Tail>
147 inline void pi_lua_multiple_push(lua_State *l, Head arg1, Tail... rest)
148 {
149  pi_lua_generic_push(l, arg1);
150  pi_lua_multiple_push(l, rest...);
151 }
152 #endif
153 
154 inline void pi_lua_multiple_push(lua_State *l)
155 {
156  return;
157 }
158 
159 #if defined(_MSC_VER)
160 template <typename Arg1, typename Arg2>
161 inline std::tuple<Arg1, Arg2> pi_lua_multiple_pull(lua_State *l, int beg)
162 {
163  beg = lua_absindex(l, beg);
164  Arg1 arg1;
165  Arg2 arg2;
166  pi_lua_generic_pull(l, beg, arg1);
167  pi_lua_generic_pull(l, beg + 1, arg2);
168  return std::make_tuple(arg1, arg2);
169 }
170 template <typename Arg1, typename Arg2, typename Arg3>
171 inline std::tuple<Arg1, Arg2, Arg3> pi_lua_multiple_pull(lua_State *l, int beg)
172 {
173  beg = lua_absindex(l, beg);
174  Arg1 arg1;
175  Arg2 arg2;
176  Arg3 arg3;
177  pi_lua_generic_pull(l, beg, arg1);
178  pi_lua_generic_pull(l, beg + 1, arg2);
179  pi_lua_generic_pull(l, beg + 2, arg3);
180  return std::make_tuple(arg1, arg2, arg3);
181 }
182 template <typename Arg1, typename Arg2, typename Arg3, typename Arg4>
183 inline std::tuple<Arg1, Arg2, Arg3, Arg4> pi_lua_multiple_pull(lua_State *l, int beg)
184 {
185  beg = lua_absindex(l, beg);
186  Arg1 arg1;
187  Arg2 arg2;
188  Arg3 arg3;
189  Arg4 arg4;
190  pi_lua_generic_pull(l, beg, arg1);
191  pi_lua_generic_pull(l, beg + 1, arg2);
192  pi_lua_generic_pull(l, beg + 2, arg3);
193  pi_lua_generic_pull(l, beg + 3, arg4);
194  return std::make_tuple(arg1, arg2, arg3, arg4);
195 }
196 #else
197 // The _bogus parameter is used to bring the empty type list case into the template world
198 // to solve name resolution problems.
199 template <int _bogus, typename Head, typename... Tail>
200 inline std::tuple<Head, Tail...> __helper_pi_lua_multiple_pull(lua_State *l, int beg)
201 {
202  beg = lua_absindex(l, beg);
203  std::tuple<Tail...> rest = __helper_pi_lua_multiple_pull<_bogus, Tail...>(l, beg + 1);
204  Head hd;
205  pi_lua_generic_pull(l, beg, hd);
206  std::tuple<Head> first(hd);
207  return std::tuple_cat(first, rest);
208 }
209 
210 template <int _bogus>
211 inline std::tuple<> __helper_pi_lua_multiple_pull(lua_State *l, int beg)
212 {
213  return std::tuple<>();
214 }
215 
216 template <typename... Types>
217 inline std::tuple<Types...> pi_lua_multiple_pull(lua_State *l, int beg)
218 {
219  return __helper_pi_lua_multiple_pull<0, Types...>(l, beg);
220 }
221 #endif
222 
223 #endif
void LuaPush(lua_State *l, Type value)
Definition: LuaPushPull.h:39
std::tuple< Head, Tail... > __helper_pi_lua_multiple_pull(lua_State *l, int beg)
Definition: LuaPushPull.h:200
bool pi_lua_strict_pull(lua_State *l, int index, bool &out)
Definition: LuaPushPull.h:62
std::tuple< Types... > pi_lua_multiple_pull(lua_State *l, int beg)
Definition: LuaPushPull.h:217
std::remove_reference< Type >::type LuaPull(lua_State *l, int index)
Definition: LuaPushPull.h:45
void pi_lua_multiple_push(lua_State *l, Types... args)
void pi_lua_generic_push(lua_State *l, bool value)
Definition: LuaPushPull.h:14
void pi_lua_generic_pull(lua_State *l, int index, bool &out)
Definition: LuaPushPull.h:25