The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
push_check.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2017 - 2025
3 Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY.
11
12 See the COPYING file for more details.
13*/
14
15#pragma once
16
19
20#include "lua/wrapper_lauxlib.h"
21#include "tstring.hpp"
22#include "map/location.hpp"
23#include "variable.hpp"
24
25#include <cassert>
26#include <limits>
27#include <string_view>
28#include <type_traits>
29
31 int index;
33 lua_index_raw(lua_State* L) : index(lua_gettop(L)) {}
34};
35
37{
38 template<typename T, typename T2 = void>
39 struct is_container : std::false_type {};
40
41 template<typename T>
42 struct is_container<T, std::void_t<
43 typename std::decay_t<T>::value_type,
44 typename std::decay_t<T>::iterator,
45 typename std::decay_t<T>::size_type,
46 typename std::decay_t<T>::reference>
47 > : std::true_type {};
48
49 template<class T, template<class> class U>
50 inline constexpr bool is_instance_of_v = std::false_type{};
51
52 template<template<class> class U, class V>
53 inline constexpr bool is_instance_of_v<U<V>,U> = std::true_type{};
54
55 template<typename T, typename T2 = void>
56 struct is_map : std::false_type {};
57
58 template<typename T>
59 struct is_map<T, std::void_t<
60 typename std::decay_t<T>::key_type,
61 typename std::decay_t<T>::mapped_type>
62 > : std::true_type {};
63
64 template<typename T, typename T2 = void>
65 struct is_pair : std::false_type {};
66
67 template<typename T>
68 struct is_pair<T, std::void_t<
69 typename std::decay_t<T>::first_type,
70 typename std::decay_t<T>::second_type>
71 > : std::true_type {};
72
73 template<typename T>
74 std::enable_if_t<std::is_same_v<T, lua_index_raw>, lua_index_raw>
75 lua_check(lua_State * L, int n)
76 {
77 return lua_index_raw{ lua_absindex(L, n) };
78 }
79 template<typename T>
80 std::enable_if_t<std::is_same_v<T, lua_index_raw>, lua_index_raw>
81 lua_to_or_default(lua_State * L, int n, const T& /*def*/)
82 {
83 return lua_index_raw{ lua_absindex(L, n) };
84 }
85 template<typename T>
86 std::enable_if_t<std::is_same_v<T, lua_index_raw>, void>
87 lua_push(lua_State * L, lua_index_raw n)
88 {
89 lua_pushvalue(L, n.index);
90 }
91
92 //std::string
93 template<typename T>
94 std::enable_if_t<std::is_same_v<T, std::string>, std::string>
95 lua_check(lua_State *L, int n)
96 {
97 return std::string(luaW_tostring(L, n));
98 }
99 template<typename T>
100 std::enable_if_t<std::is_same_v<T, std::string>, std::string>
101 lua_to_or_default(lua_State *L, int n, const T& def)
102 {
103 return std::string(luaW_tostring_or_default(L, n, def));
104 }
105 template<typename T>
106 std::enable_if_t<std::is_same_v<T, std::string>, void>
107 lua_push(lua_State *L, const T& val)
108 {
109 lua_pushlstring(L, val.c_str(), val.size());
110 }
111
112 //std::string_view
113 template<typename T>
114 std::enable_if_t<std::is_same_v<T, std::string_view>, std::string_view>
115 lua_check(lua_State *L, int n)
116 {
117 return luaW_tostring(L, n);
118 }
119 template<typename T>
120 std::enable_if_t<std::is_same_v<T, std::string_view>, std::string_view>
121 lua_to_or_default(lua_State *L, int n, const T& def)
122 {
123 return luaW_tostring_or_default(L, n, def);
124 }
125 template<typename T>
126 std::enable_if_t<std::is_same_v<T, std::string_view>, void>
127 lua_push(lua_State *L, const T& val)
128 {
129 lua_pushlstring(L, val.data(), val.size());
130 }
131
132 //config
133 template<typename T>
134 std::enable_if_t<std::is_same_v<T, config>, config>
135 lua_check(lua_State *L, int n)
136 {
137 return luaW_checkconfig(L, n);
138 }
139 template<typename T>
140 std::enable_if_t<std::is_same_v<T, config>, config>
141 lua_to_or_default(lua_State *L, int n, const T& def)
142 {
143 config cfg;
144 return luaW_toconfig(L, n, cfg) ? cfg : def;
145 }
146 template<typename T>
147 std::enable_if_t<std::is_same_v<T, config>, void>
148 lua_push(lua_State *L, const config& val)
149 {
150 luaW_pushconfig(L, val);
151 }
152
153 //vconfig
154 template<typename T>
155 std::enable_if_t<std::is_same_v<T, vconfig>, vconfig>
156 lua_check(lua_State *L, int n)
157 {
158 return luaW_checkvconfig(L, n);
159 }
160 template<typename T>
161 std::enable_if_t<std::is_same_v<T, vconfig>, vconfig>
162 lua_to_or_default(lua_State *L, int n, const T& def)
163 {
165 return luaW_tovconfig(L, n, cfg) ? cfg : def;
166 }
167 template<typename T>
168 std::enable_if_t<std::is_same_v<T, vconfig>, void>
169 lua_push(lua_State *L, const vconfig& val)
170 {
171 luaW_pushvconfig(L, val);
172 }
173
174 //location
175 template<typename T>
176 std::enable_if_t<std::is_same_v<T, map_location>, map_location>
177 lua_check(lua_State *L, int n)
178 {
179 return luaW_checklocation(L, n);
180 }
181 template<typename T>
182 std::enable_if_t<std::is_same_v<T, map_location>, map_location>
183 lua_to_or_default(lua_State *L, int n, const T& def)
184 {
185 map_location res;
186 if (!luaW_tolocation(L, n, res)) {
187 return def;
188 }
189 return res;
190 }
191 template<typename T>
192 std::enable_if_t<std::is_same_v<T, map_location>, void>
193 lua_push(lua_State *L, const map_location& val)
194 {
195 luaW_pushlocation(L, val);
196 }
197
198 //t_string
199 template<typename T>
200 std::enable_if_t<std::is_same_v<T, t_string>, t_string>
201 lua_check(lua_State *L, int n)
202 {
203 return luaW_checktstring(L, n);
204 }
205 template<typename T>
206 std::enable_if_t<std::is_same_v<T, t_string>, void>
207 lua_push(lua_State *L, const t_string& val)
208 {
209 luaW_pushtstring(L, val);
210 }
211
212 //widget
213 //lua_check for widget is not supported because lua_check returns by value
214 template<typename T>
215 std::enable_if_t<std::is_same_v<T, gui2::widget>, void>
216 lua_push(lua_State *L, gui2::widget& val)
217 {
218 luaW_pushwidget(L, val);
219 }
220
221 //bool
222 template<typename T>
223 std::enable_if_t<std::is_same_v<T, bool>, bool>
224 lua_check(lua_State *L, int n)
225 {
226 return luaW_toboolean(L, n);
227 }
228 template<typename T>
229 std::enable_if_t<std::is_same_v<T, bool>, bool>
230 lua_to_or_default(lua_State *L, int n, const T& /*def*/)
231 {
232 return luaW_toboolean(L, n);
233 }
234 template<typename T>
235 std::enable_if_t<std::is_same_v<T, bool>, void>
236 lua_push(lua_State *L, bool val)
237 {
238 lua_pushboolean(L, val);
239 }
240
241 //double, float
242 template<typename T>
243 std::enable_if_t<std::is_floating_point_v<T>, T>
244 lua_check(lua_State *L, int n)
245 {
246 return luaL_checknumber(L, n);
247 }
248 template<typename T>
249 std::enable_if_t<std::is_floating_point_v<T>, T>
250 lua_to_or_default(lua_State *L, int n, const T& def)
251 {
252 int isnum;
253 lua_Number d = lua_tonumberx(L, n, &isnum);
254 if (!isnum) {
255 return def;
256 }
257 return d;
258 }
259 template<typename T>
260 std::enable_if_t<std::is_floating_point_v<T>, void>
261 lua_push(lua_State *L, T val)
262 {
263 lua_pushnumber(L, val);
264 }
265
266 //integer types
267 template<typename T>
268 std::enable_if_t<std::is_integral_v<T> && !std::is_same_v<T, bool>, T>
269 lua_check(lua_State *L, int n)
270 {
271 lua_Integer res = luaL_checkinteger(L, n);
272 // lua_Integer is 64-bit; narrowing it to a smaller (or differently-signed) T
273 // should saturate rather than silently wrap on out-of-range script input.
274 if constexpr(std::is_signed_v<T>) {
275 if(res > static_cast<lua_Integer>(std::numeric_limits<T>::max())) {
276 return std::numeric_limits<T>::max();
277 }
278 if(res < static_cast<lua_Integer>(std::numeric_limits<T>::min())) {
279 return std::numeric_limits<T>::min();
280 }
281 } else {
282 if(res < 0) {
283 return 0;
284 }
285 if(static_cast<unsigned long long>(res) > static_cast<unsigned long long>(std::numeric_limits<T>::max())) {
286 return std::numeric_limits<T>::max();
287 }
288 }
289 return static_cast<T>(res);
290 }
291 template<typename T>
292 std::enable_if_t<std::is_integral_v<T> && !std::is_same_v<T, bool>, T>
293 lua_to_or_default(lua_State *L, int n, const T& def)
294 {
295 int isnum;
296 lua_Integer res = lua_tointegerx(L, n, &isnum);
297 if (!isnum) {
298 return def;
299 }
300 return res;
301 }
302
303 template<typename T>
304 std::enable_if_t<std::is_integral_v<T> && !std::is_same_v<T, bool>, void>
305 lua_push(lua_State *L, T val)
306 {
307 lua_pushinteger(L, val);
308 }
309
310 //std::pair
311 //Not sure if the not_<is_const> is required; only (maybe) if std::map matches is_container
312 template<typename T>
313 std::enable_if_t<is_pair<T>::value && !std::is_const_v<typename T::first_type>, T>
314 lua_check(lua_State *L, int n)
315 {
316 T result;
317 if (lua_istable(L, n)) {
318 lua_rawgeti(L, n, 1);
319 result.first = lua_check<const typename T::first_type&>(L, -1);
320 lua_rawgeti(L, n, 2);
321 result.second = lua_check<const typename T::second_type&>(L, -1);
322 lua_pop(L, 2);
323 }
324 return result;
325 }
326 template<typename T>
327 std::enable_if_t<is_pair<T>::value && !std::is_const_v<typename T::first_type>, void>
328 lua_push(lua_State *L, const T& val)
329 {
330 lua_newtable(L);
331 lua_push<const typename T::first_type&>(L, val.first);
332 lua_rawseti(L, -2, 1);
333 lua_push<const typename T::second_type&>(L, val.second);
334 lua_rawseti(L, -2, 2);
335 }
336
337 //std::vector and similar but not std::string
338 template<typename T>
339 std::enable_if_t<is_container<T>::value && !std::is_same_v<T, std::string> && !std::is_same_v<T, std::string_view>, T>
340 lua_check(lua_State * L, int n)
341 {
342 if (lua_istable(L, n))
343 {
344 T res;
345 for (int i = 1, i_end = lua_rawlen(L, n); i <= i_end; ++i)
346 {
347 lua_rawgeti(L, n, i);
348 // By using insert instead of push_back, it magically "just works" for sets too.
349 res.insert(res.end(), lua_check_impl::lua_check<std::decay_t<typename T::reference>>(L, -1));
350 lua_pop(L, 1);
351 }
352 return res;
353 }
354 else
355 {
356 luaL_argerror(L, n, "Table expected");
357 throw "luaL_argerror returned"; //shouldn't happen, luaL_argerror always throws.
358 }
359 }
360
361 //also accepts things like std::vector<int>() | std::adaptors::transformed(..)
362 template<typename T>
363 std::enable_if_t<
364 is_container<T>::value && !std::is_same_v<T, std::string> && !std::is_same_v<T, std::string_view> && !is_map<T>::value
365 , void
366 >
367 lua_push(lua_State * L, const T& list )
368 {
369 // NOTE: T might be some boost::iterator_range type where size might be < 0. (unfortunately in this case size() does not return T::size_type)
370 assert(list.size() >= 0);
371 lua_createtable(L, list.size(), 0);
372 int i = 1;
373 for(typename T::const_iterator iter = list.begin(); iter != list.end(); ++iter) {
374 lua_check_impl::lua_push<std::decay_t<typename T::reference>>(L, *iter);
375 lua_rawseti(L, -2, i++);
376 }
377 }
378
379 //accepts std::map TODO: add a check function for that
380 template<typename T>
381 std::enable_if_t<is_map<T>::value, void>
382 lua_push(lua_State * L, const T& map )
383 {
384 lua_newtable(L);
385 for(const typename T::value_type& pair : map)
386 {
387 lua_check_impl::lua_push<std::decay_t<typename T::key_type>>(L, pair.first);
388 lua_check_impl::lua_push<std::decay_t<typename T::mapped_type>>(L, pair.second);
389 lua_settable(L, -3);
390 }
391 }
392
393 // enum_base
394 template<typename T>
395 typename T::type
396 lua_check(lua_State *L, int n)
397 {
398 std::string str = lua_check_impl::lua_check<std::string>(L, n);
399 utils::optional<typename T::type> val = T::get_enum(str);
400 if(!val) {
401 luaL_argerror(L, n, ("cannot convert " + str + " to enum.").c_str());
402 }
403 return *val;
404 }
405
406 //optional
407 template<typename T>
408 std::enable_if_t<is_instance_of_v<T, utils::optional>, T>
409 lua_check(lua_State *L, int n)
410 {
411 if(lua_isnoneornil(L, n)) {
412 return T();
413 }
414 return lua_check_impl::lua_check<typename T::value_type>(L, n);
415 }
416
417 template<typename T>
418 std::enable_if_t<is_instance_of_v<T, utils::optional>, void>
419 lua_push(lua_State *L, const T& opt)
420 {
421 if(opt) {
422 lua_check_impl::lua_push<typename T::value_type>(L, *opt);
423 } else {
424 lua_pushnil(L);
425 }
426 }
427}
428
429template<typename T>
430std::decay_t<T> lua_check(lua_State *L, int n)
431{
432 //remove possible const& to make life easier for the impl namespace.
433 return lua_check_impl::lua_check<std::decay_t<T>>(L, n);
434}
435
436template<typename T>
437std::decay_t<T> lua_to_or_default(lua_State *L, int n, const T& def)
438{
439 //remove possible const& to make life easier for the impl namespace.
440 return lua_check_impl::lua_to_or_default<std::decay_t<T>>(L, n, def);
441}
442
443template<typename T>
444void lua_push(lua_State *L, const T& val)
445{
446 return lua_check_impl::lua_push<std::decay_t<T>>(L, val);
447}
448
449/**
450 * returns t[k] where k is the table at index @a index and k is @a k or @a def if it is not convertible to the correct type.
451 *
452 */
453template<typename T>
454std::decay_t<T> luaW_table_get_def(lua_State *L, int index, std::string_view k, const T& def)
455{
456 if(!lua_istable(L, index)) {
457 luaL_argerror(L, index, "table expected");
458 }
459 if(index < 0) {
460 //with the next lua_pushstring negative indicies will no longer be correct otherwise.
461 --index;
462 }
463 lua_pushlstring(L, k.data(), k.size());
464 lua_gettable(L, index);
465 if(lua_isnoneornil(L, -1)) {
466 lua_pop(L, 1);
467 return def;
468 }
469 T res = lua_check_impl::lua_to_or_default<std::decay_t<T>>(L, -1, def);
470 lua_pop(L, 1);
471 return res;
472}
473
474
475template<typename T>
476void luaW_table_set(lua_State *L, int index, std::string_view k, const T& value)
477{
478 if(!lua_istable(L, index)) {
479 luaL_argerror(L, index, "table expected");
480 }
481
482 index = lua_absindex(L, index);
483 lua_pushlstring(L, k.data(), k.size());
484 lua_push(L, value);
485 lua_settable(L, index);
486}
A config object defines a single node in a WML file, with access to child nodes.
Definition config.hpp:157
Base class for all widgets.
Definition widget.hpp:55
A variable-expanding proxy for the config class.
Definition variable.hpp:45
static vconfig unconstructed_vconfig()
This is just a wrapper for the default constructor; it exists for historical reasons and to make it c...
Definition variable.cpp:153
const config * cfg
void luaW_pushconfig(lua_State *L, const config &cfg)
Converts a config object to a Lua table pushed at the top of the stack.
config luaW_checkconfig(lua_State *L, int index)
Converts an optional table or vconfig to a config object.
void luaW_pushlocation(lua_State *L, const map_location &ml)
Converts a map location object to a Lua table pushed at the top of the stack.
void luaW_pushtstring(lua_State *L, const t_string &v)
Pushes a t_string on the top of the stack.
bool luaW_tovconfig(lua_State *L, int index, vconfig &vcfg)
Gets an optional vconfig from either a table or a userdata.
void luaW_pushvconfig(lua_State *L, const vconfig &cfg)
Pushes a vconfig on the top of the stack.
bool luaW_toboolean(lua_State *L, int n)
std::string_view luaW_tostring(lua_State *L, int index)
std::string_view luaW_tostring_or_default(lua_State *L, int index, std::string_view def)
bool luaW_toconfig(lua_State *L, int index, config &cfg)
Converts an optional table or vconfig to a config object.
vconfig luaW_checkvconfig(lua_State *L, int index, bool allow_missing)
Gets an optional vconfig from either a table or a userdata.
bool luaW_tolocation(lua_State *L, int index, map_location &loc)
Converts an optional table or pair of integers to a map location object.
map_location luaW_checklocation(lua_State *L, int index)
Converts an optional table or pair of integers to a map location object.
t_string luaW_checktstring(lua_State *L, int index)
Converts a scalar to a translatable string.
void luaW_pushwidget(lua_State *L, gui2::widget &w)
std::enable_if_t< std::is_same_v< T, lua_index_raw >, lua_index_raw > lua_check(lua_State *L, int n)
constexpr bool is_instance_of_v
std::enable_if_t< std::is_same_v< T, lua_index_raw >, lua_index_raw > lua_to_or_default(lua_State *L, int n, const T &)
std::enable_if_t< std::is_same_v< T, lua_index_raw >, void > lua_push(lua_State *L, lua_index_raw n)
STL namespace.
std::decay_t< T > lua_to_or_default(lua_State *L, int n, const T &def)
std::decay_t< T > lua_check(lua_State *L, int n)
std::decay_t< T > luaW_table_get_def(lua_State *L, int index, std::string_view k, const T &def)
returns t[k] where k is the table at index index and k is k or def if it is not convertible to the co...
void lua_push(lua_State *L, const T &val)
void luaW_table_set(lua_State *L, int index, std::string_view k, const T &value)
std::unique_ptr< MIX_Audio, decltype(&MIX_DestroyAudio)> value
Definition sound.cpp:139
lua_index_raw(int i)
lua_index_raw(lua_State *L)
Encapsulates the map of the game.
Definition location.hpp:46
static map_location::direction n
#define i
#define d