26 #include <string_view>
27 #include <type_traits>
37 template<
typename T,
typename T2 =
void>
42 typename std::decay_t<T>::value_type,
43 typename std::decay_t<T>::iterator,
44 typename std::decay_t<T>::size_type,
45 typename std::decay_t<T>::reference>
46 > : std::true_type {};
48 template<
class T,
template<
class>
class U>
51 template<
template<
class>
class U,
class V>
52 inline constexpr
bool is_instance_of_v<U<V>,U> = std::true_type{};
54 template<
typename T,
typename T2 =
void>
59 typename std::decay_t<T>::key_type,
60 typename std::decay_t<T>::mapped_type>
61 > : std::true_type {};
63 template<
typename T,
typename T2 =
void>
68 typename std::decay_t<T>::first_type,
69 typename std::decay_t<T>::second_type>
70 > : std::true_type {};
73 std::enable_if_t<std::is_same_v<T, lua_index_raw>,
lua_index_raw>
79 std::enable_if_t<std::is_same_v<T, lua_index_raw>,
lua_index_raw>
85 std::enable_if_t<std::is_same_v<T, lua_index_raw>,
void>
88 lua_pushvalue(L,
n.index);
93 std::enable_if_t<std::is_same_v<T, std::string>, std::string>
96 return luaL_checkstring(L,
n);
99 std::enable_if_t<std::is_same_v<T, std::string>, std::string>
102 return luaL_optstring(L,
n, def.c_str());
105 std::enable_if_t<std::is_same_v<T, std::string>,
void>
108 lua_pushlstring(L, val.c_str(), val.size());
113 std::enable_if_t<std::is_same_v<T, std::string_view>, std::string_view>
119 std::enable_if_t<std::is_same_v<T, std::string_view>, std::string_view>
125 std::enable_if_t<std::is_same_v<T, std::string_view>,
void>
128 lua_pushlstring(L, val.data(), val.size());
133 std::enable_if_t<std::is_same_v<T, config>,
config>
139 std::enable_if_t<std::is_same_v<T, config>,
config>
146 std::enable_if_t<std::is_same_v<T, config>,
void>
154 std::enable_if_t<std::is_same_v<T, vconfig>,
vconfig>
160 std::enable_if_t<std::is_same_v<T, vconfig>,
vconfig>
167 std::enable_if_t<std::is_same_v<T, vconfig>,
void>
175 std::enable_if_t<std::is_same_v<T, map_location>,
map_location>
181 std::enable_if_t<std::is_same_v<T, map_location>,
map_location>
191 std::enable_if_t<std::is_same_v<T, map_location>,
void>
199 std::enable_if_t<std::is_same_v<T, t_string>,
t_string>
205 std::enable_if_t<std::is_same_v<T, t_string>,
void>
214 std::enable_if_t<std::is_same_v<T, gui2::widget>,
void>
222 std::enable_if_t<std::is_same_v<T, bool>,
bool>
228 std::enable_if_t<std::is_same_v<T, bool>,
bool>
234 std::enable_if_t<std::is_same_v<T, bool>,
void>
237 lua_pushboolean(L, val);
242 std::enable_if_t<std::is_floating_point_v<T>, T>
245 return luaL_checknumber(L,
n);
248 std::enable_if_t<std::is_floating_point_v<T>, T>
252 lua_Number
d = lua_tonumberx(L,
n, &isnum);
259 std::enable_if_t<std::is_floating_point_v<T>,
void>
262 lua_pushnumber(L, val);
267 std::enable_if_t<std::is_integral_v<T> && !std::is_same_v<T, bool>, T>
270 return luaL_checkinteger(L,
n);
273 std::enable_if_t<std::is_integral_v<T> && !std::is_same_v<T, bool>, T>
277 lua_Integer res = lua_tointegerx(L,
n, &isnum);
285 std::enable_if_t<std::is_integral_v<T> && !std::is_same_v<T, bool>,
void>
288 lua_pushinteger(L, val);
294 std::enable_if_t<is_pair<T>::value && !std::is_const_v<typename T::first_type>, T>
298 if (lua_istable(L,
n)) {
299 lua_rawgeti(L,
n, 1);
300 result.first = lua_check<const typename T::first_type&>(L, -1);
301 lua_rawgeti(L,
n, 2);
302 result.second = lua_check<const typename T::second_type&>(L, -1);
308 std::enable_if_t<is_pair<T>::value && !std::is_const_v<typename T::first_type>,
void>
312 lua_push<const typename T::first_type&>(L, val.first);
313 lua_rawseti(L, -2, 1);
314 lua_push<const typename T::second_type&>(L, val.second);
315 lua_rawseti(L, -2, 2);
320 std::enable_if_t<is_container<T>::value && !std::is_same_v<T, std::string> && !std::is_same_v<T, std::string_view>, T>
323 if (lua_istable(L,
n))
326 for (
int i = 1, i_end = lua_rawlen(L,
n);
i <= i_end; ++
i)
328 lua_rawgeti(L,
n,
i);
337 luaL_argerror(L,
n,
"Table expected");
338 throw "luaL_argerror returned";
345 is_container<T>::value && !std::is_same_v<T, std::string> && !std::is_same_v<T, std::string_view> && !is_map<T>::value
351 assert(list.size() >= 0);
352 lua_createtable(L, list.size(), 0);
354 for(
typename T::const_iterator iter = list.begin(); iter != list.end(); ++iter) {
355 lua_check_impl::lua_push<std::decay_t<typename T::reference>>(L, *iter);
356 lua_rawseti(L, -2,
i++);
362 std::enable_if_t<is_map<T>::value,
void>
366 for(
const typename T::value_type& pair : map)
368 lua_check_impl::lua_push<std::decay_t<typename T::key_type>>(L, pair.first);
369 lua_check_impl::lua_push<std::decay_t<typename T::mapped_type>>(L, pair.second);
379 std::string str = lua_check_impl::lua_check<std::string>(L,
n);
380 utils::optional<typename T::type> val = T::get_enum(str);
382 luaL_argerror(L,
n, (
"cannot convert " + str +
" to enum.").c_str());
389 std::enable_if_t<is_instance_of_v<T, utils::optional>, T>
392 if(lua_isnoneornil(L,
n)) {
395 return lua_check_impl::lua_check<typename T::value_type>(L,
n);
399 std::enable_if_t<is_instance_of_v<T, utils::optional>,
void>
403 lua_check_impl::lua_push<typename T::value_type>(L, *opt);
414 return lua_check_impl::lua_check<std::decay_t<T>>(L,
n);
421 return lua_check_impl::lua_to_or_default<std::decay_t<T>>(L,
n, def);
427 return lua_check_impl::lua_push<std::decay_t<T>>(L, val);
437 if(!lua_istable(L,
index)) {
438 luaL_argerror(L,
index,
"table expected");
444 lua_pushlstring(L, k.data(), k.size());
445 lua_gettable(L,
index);
446 if(lua_isnoneornil(L, -1)) {
450 T res = lua_check_impl::lua_to_or_default<std::decay_t<T>>(L, -1, def);
459 if(!lua_istable(L,
index)) {
460 luaL_argerror(L,
index,
"table expected");
464 lua_pushlstring(L, k.data(), k.size());
466 lua_settable(L,
index);
A config object defines a single node in a WML file, with access to child nodes.
A variable-expanding proxy for the config class.
static vconfig unconstructed_vconfig()
This is just a wrapper for the default constructor; it exists for historical reasons and to make it c...
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.
std::enable_if_t< std::is_same_v< T, lua_index_raw >, lua_index_raw > lua_check(lua_State *L, int n)
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 &)
constexpr bool is_instance_of_v
std::enable_if_t< std::is_same_v< T, lua_index_raw >, void > lua_push(lua_State *L, lua_index_raw n)
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
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)
void lua_push(lua_State *L, const T &val)
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 luaW_table_set(lua_State *L, int index, std::string_view k, const T &value)
lua_index_raw(lua_State *L)
Encapsulates the map of the game.
static map_location::direction n