Luanti 5.17.0-dev
Loading...
Searching...
No Matches
helper.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2018 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
4
5#pragma once
6
7#include <string_view>
8#include <type_traits>
9
10extern "C" {
11#include <lua.h>
12}
13
15{
16public:
17
29 static bool geti(lua_State *L, int table, int i)
30 {
31 lua_rawgeti(L, table, i + 1);
32 if (lua_isnil(L, -1)) {
33 lua_pop(L, 1);
34 return false;
35 }
36 return true;
37 }
38
47 template <typename F>
48 static void for_ipairs(lua_State *L, int table, const F &f)
49 {
50 if (table < 0)
51 table = lua_gettop(L) + table + 1;
52 static_assert(std::is_same_v<std::invoke_result_t<F>, void>);
53 for (int i = 0; geti(L, table, i); ++i, lua_pop(L, 1))
54 f();
55 }
56
57
58protected:
67 template <typename T>
68 static T readParam(lua_State *L, int index);
69
79 template <typename T>
80 static inline T readParam(lua_State *L, int index, const T &default_value)
81 {
82 return lua_isnoneornil(L, index) ? default_value : readParam<T>(L, index);
83 }
84};
85
86// (only declared for documentation purposes:)
87
98template <>
99std::string_view LuaHelper::readParam(lua_State *L, int index);
Definition helper.h:15
static bool geti(lua_State *L, int table, int i)
Utility for list iteration.
Definition helper.h:29
static T readParam(lua_State *L, int index)
Read a value using a template type T from Lua state L at index.
static void for_ipairs(lua_State *L, int table, const F &f)
Iterate values t[1], t[2], ... of the given table in order.
Definition helper.h:48
static T readParam(lua_State *L, int index, const T &default_value)
Read a value using a template type T from Lua state L at index.
Definition helper.h:80
static std::vector< table_key > table
Definition keycode.cpp:33