forked from osm2pgsql-dev/osm2pgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflex-lua-expire-output.cpp
More file actions
169 lines (141 loc) · 5.39 KB
/
flex-lua-expire-output.cpp
File metadata and controls
169 lines (141 loc) · 5.39 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This file is part of osm2pgsql (https://osm2pgsql.org/).
*
* Copyright (C) 2006-2025 by the osm2pgsql developer community.
* For a full list of authors see the git log.
*/
#include "flex-lua-expire-output.hpp"
#include "expire-output.hpp"
#include "format.hpp"
#include "lua-utils.hpp"
#include <lua.hpp>
namespace {
expire_output_t &
create_expire_output(lua_State *lua_state, std::string const &default_schema,
std::vector<expire_output_t> *expire_outputs)
{
auto &new_expire_output = expire_outputs->emplace_back();
// optional "filename" field
auto const *filename = luaX_get_table_string(lua_state, "filename", -1,
"The expire output", "");
new_expire_output.set_filename(filename);
lua_pop(lua_state, 1); // "filename"
// optional "schema" and "table" fields
auto const *schema = luaX_get_table_string(
lua_state, "schema", -1, "The expire output", default_schema.c_str());
check_identifier(schema, "schema field");
auto const *table =
luaX_get_table_string(lua_state, "table", -2, "The expire output", "");
check_identifier(table, "table field");
new_expire_output.set_schema_and_table(schema, table);
lua_pop(lua_state, 2); // "schema" and "table"
if (new_expire_output.filename().empty() &&
new_expire_output.table().empty()) {
throw std::runtime_error{
"Must set 'filename' and/or 'table' on expire output."};
}
// required "maxzoom" field
auto const maxzoom = luaX_get_table_optional_uint32(
lua_state, "maxzoom", -1, "The 'maxzoom' field in a expire output", 1,
20, "1 and 20");
new_expire_output.set_minzoom(maxzoom);
new_expire_output.set_maxzoom(maxzoom);
lua_pop(lua_state, 1); // "maxzoom"
// optional "minzoom" field
auto const minzoom = luaX_get_table_optional_uint32(
lua_state, "minzoom", -1, "The 'minzoom' field in a expire output", 1,
maxzoom, "1 and 'maxzoom'");
if (minzoom > 0) {
new_expire_output.set_minzoom(minzoom);
}
lua_pop(lua_state, 1); // "minzoom"
return new_expire_output;
}
TRAMPOLINE_WRAPPED_OBJECT(expire_output, tostring)
TRAMPOLINE_WRAPPED_OBJECT(expire_output, filename)
TRAMPOLINE_WRAPPED_OBJECT(expire_output, maxzoom)
TRAMPOLINE_WRAPPED_OBJECT(expire_output, minzoom)
TRAMPOLINE_WRAPPED_OBJECT(expire_output, schema)
TRAMPOLINE_WRAPPED_OBJECT(expire_output, table)
} // anonymous namespace
int setup_flex_expire_output(lua_State *lua_state,
std::string const &default_schema,
std::vector<expire_output_t> *expire_outputs)
{
if (lua_type(lua_state, 1) != LUA_TTABLE) {
throw std::runtime_error{
"Argument #1 to 'define_expire_output' must be a Lua table."};
}
create_expire_output(lua_state, default_schema, expire_outputs);
void *ptr = lua_newuserdata(lua_state, sizeof(std::size_t));
auto *num = new (ptr) std::size_t{};
*num = expire_outputs->size() - 1;
luaL_getmetatable(lua_state, OSM2PGSQL_EXPIRE_OUTPUT_CLASS);
lua_setmetatable(lua_state, -2);
return 1;
}
/**
* Define the osm2pgsql.ExpireOutput class/metatable.
*/
void lua_wrapper_expire_output::init(lua_State *lua_state)
{
lua_getglobal(lua_state, "osm2pgsql");
if (luaL_newmetatable(lua_state, OSM2PGSQL_EXPIRE_OUTPUT_CLASS) != 1) {
throw std::runtime_error{"Internal error: Lua newmetatable failed."};
}
lua_pushvalue(lua_state, -1); // Copy of new metatable
// Add metatable as osm2pgsql.ExpireOutput so we can access it from Lua
lua_setfield(lua_state, -3, "ExpireOutput");
// Now add functions to metatable
lua_pushvalue(lua_state, -1);
lua_setfield(lua_state, -2, "__index");
luaX_add_table_func(lua_state, "__tostring",
lua_trampoline_expire_output_tostring);
luaX_add_table_func(lua_state, "filename",
lua_trampoline_expire_output_filename);
luaX_add_table_func(lua_state, "maxzoom",
lua_trampoline_expire_output_maxzoom);
luaX_add_table_func(lua_state, "minzoom",
lua_trampoline_expire_output_minzoom);
luaX_add_table_func(lua_state, "schema",
lua_trampoline_expire_output_schema);
luaX_add_table_func(lua_state, "table", lua_trampoline_expire_output_table);
lua_pop(lua_state, 2);
}
int lua_wrapper_expire_output::tostring() const
{
std::string const str =
fmt::format("osm2pgsql.ExpireOutput[minzoom={},maxzoom={},filename={},"
"schema={},table={}]",
self().minzoom(), self().maxzoom(), self().filename(),
self().schema(), self().table());
luaX_pushstring(lua_state(), str);
return 1;
}
int lua_wrapper_expire_output::filename() const
{
luaX_pushstring(lua_state(), self().filename());
return 1;
}
int lua_wrapper_expire_output::maxzoom() const
{
lua_pushinteger(lua_state(), self().maxzoom());
return 1;
}
int lua_wrapper_expire_output::minzoom() const
{
lua_pushinteger(lua_state(), self().minzoom());
return 1;
}
int lua_wrapper_expire_output::schema() const
{
luaX_pushstring(lua_state(), self().schema());
return 1;
}
int lua_wrapper_expire_output::table() const
{
luaX_pushstring(lua_state(), self().table());
return 1;
}