-
-
Notifications
You must be signed in to change notification settings - Fork 477
Expand file tree
/
Copy pathlua-utils.hpp
More file actions
126 lines (107 loc) · 4.45 KB
/
lua-utils.hpp
File metadata and controls
126 lines (107 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#ifndef OSM2PGSQL_LUA_UTILS_HPP
#define OSM2PGSQL_LUA_UTILS_HPP
/**
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This file is part of osm2pgsql (https://osm2pgsql.org/).
*
* Copyright (C) 2006-2026 by the osm2pgsql developer community.
* For a full list of authors see the git log.
*/
// This file contains helper functions for talking to Lua. It is used from
// the flex output backend. All functions start with "luaX_".
#include <lua.hpp>
#include <cassert>
#include <cstdint>
#include <initializer_list>
#include <string_view>
#include <utility>
void luaX_set_context(lua_State *lua_state, void *ptr) noexcept;
void *luaX_get_context(lua_State *lua_state) noexcept;
void luaX_pushstring(lua_State *lua_state, std::string_view str) noexcept;
void luaX_add_table_str(lua_State *lua_state, char const *key,
char const *value) noexcept;
void luaX_add_table_str(lua_State *lua_state, char const *key,
std::string_view value) noexcept;
void luaX_add_table_int(lua_State *lua_state, char const *key,
int64_t value) noexcept;
void luaX_add_table_num(lua_State *lua_state, char const *key,
double value) noexcept;
void luaX_add_table_bool(lua_State *lua_state, char const *key,
bool value) noexcept;
void luaX_add_table_func(lua_State *lua_state, char const *key,
lua_CFunction func) noexcept;
void luaX_set_up_metatable(
lua_State *lua_state, char const *name, char const *luaclass,
std::initializer_list<std::pair<char const *, lua_CFunction>> map);
template <typename COLLECTION, typename FUNC>
void luaX_add_table_array(lua_State *lua_state, char const *key,
COLLECTION const &collection, FUNC const &func)
{
lua_pushstring(lua_state, key);
lua_createtable(lua_state, (int)collection.size(), 0);
int n = 0;
for (auto const &member : collection) {
lua_pushinteger(lua_state, ++n);
func(member);
lua_rawset(lua_state, -3);
}
lua_rawset(lua_state, -3);
}
char const *luaX_get_table_string(lua_State *lua_state, char const *key,
int table_index, char const *error_msg);
char const *luaX_get_table_string(lua_State *lua_state, char const *key,
int table_index, char const *error_msg,
char const *default_value);
uint32_t luaX_get_table_optional_uint32(lua_State *lua_state, char const *key,
int table_index, char const *error_msg,
uint32_t min, uint32_t max,
char const *range);
uint64_t luaX_get_table_optional_uint64(lua_State *lua_state, char const *key,
int table_index, char const *error_msg,
uint64_t min, uint64_t max,
char const *range);
bool luaX_get_table_bool(lua_State *lua_state, char const *key, int table_index,
char const *error_msg, bool default_value);
int luaX_pcall(lua_State *lua_state, int narg, int nres);
/**
* Returns true if the value on top of the stack is an empty Lua table.
*
* \pre Value on top of the Lua stack must be a Lua table.
* \post Stack is unchanged.
*/
bool luaX_is_empty_table(lua_State *lua_state);
/**
* Check that the value on the top of the Lua stack is a simple array.
* This means that all keys must be consecutive integers starting from 1.
*
* \returns True if this is an array (also for Lua tables without any items)
*
* \pre Value on top of the Lua stack must be a Lua table.
* \post Stack is unchanged.
*/
bool luaX_is_array(lua_State *lua_state);
/**
* Call a function for each item in a Lua array table. The item value will
* be on the top of the stack inside that function.
*
* \pre Value on top of the Lua stack must be a Lua array table.
* \pre The function must leave the Lua stack in the same condition it found
* it in.
* \post Stack is unchanged.
*/
template <typename FUNC>
void luaX_for_each(lua_State *lua_state, FUNC const &func)
{
assert(lua_istable(lua_state, -1));
lua_pushnil(lua_state);
while (lua_next(lua_state, -2) != 0) {
#ifndef NDEBUG
int const top = lua_gettop(lua_state);
#endif
func();
assert(top == lua_gettop(lua_state));
lua_pop(lua_state, 1);
}
}
#endif // OSM2PGSQL_LUA_UTILS_HPP