Skip to content

Commit 20f061d

Browse files
committed
New luaX_set_up_metatable() helper function
Reduces boilerplate code for initialization of Lua classes.
1 parent 48a6bd4 commit 20f061d

File tree

7 files changed

+85
-130
lines changed

7 files changed

+85
-130
lines changed

src/flex-lua-expire-output.cpp

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -99,31 +99,14 @@ int setup_flex_expire_output(lua_State *lua_state,
9999
*/
100100
void lua_wrapper_expire_output::init(lua_State *lua_state)
101101
{
102-
lua_getglobal(lua_state, "osm2pgsql");
103-
if (luaL_newmetatable(lua_state, OSM2PGSQL_EXPIRE_OUTPUT_CLASS) != 1) {
104-
throw std::runtime_error{"Internal error: Lua newmetatable failed."};
105-
}
106-
lua_pushvalue(lua_state, -1); // Copy of new metatable
107-
108-
// Add metatable as osm2pgsql.ExpireOutput so we can access it from Lua
109-
lua_setfield(lua_state, -3, "ExpireOutput");
110-
111-
// Now add functions to metatable
112-
lua_pushvalue(lua_state, -1);
113-
lua_setfield(lua_state, -2, "__index");
114-
luaX_add_table_func(lua_state, "__tostring",
115-
lua_trampoline_expire_output_tostring);
116-
luaX_add_table_func(lua_state, "filename",
117-
lua_trampoline_expire_output_filename);
118-
luaX_add_table_func(lua_state, "maxzoom",
119-
lua_trampoline_expire_output_maxzoom);
120-
luaX_add_table_func(lua_state, "minzoom",
121-
lua_trampoline_expire_output_minzoom);
122-
luaX_add_table_func(lua_state, "schema",
123-
lua_trampoline_expire_output_schema);
124-
luaX_add_table_func(lua_state, "table", lua_trampoline_expire_output_table);
125-
126-
lua_pop(lua_state, 2);
102+
luaX_set_up_metatable(
103+
lua_state, "ExpireOutput", OSM2PGSQL_EXPIRE_OUTPUT_CLASS,
104+
{{"__tostring", lua_trampoline_expire_output_tostring},
105+
{"filename", lua_trampoline_expire_output_filename},
106+
{"maxzoom", lua_trampoline_expire_output_maxzoom},
107+
{"minzoom", lua_trampoline_expire_output_minzoom},
108+
{"schema", lua_trampoline_expire_output_schema},
109+
{"table", lua_trampoline_expire_output_table}});
127110
}
128111

129112
int lua_wrapper_expire_output::tostring() const

src/flex-lua-geom.cpp

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -300,37 +300,25 @@ int geom_transform(lua_State *lua_state)
300300

301301
void init_geometry_class(lua_State *lua_state)
302302
{
303-
lua_getglobal(lua_state, "osm2pgsql");
304-
if (luaL_newmetatable(lua_state, OSM2PGSQL_GEOMETRY_CLASS) != 1) {
305-
throw std::runtime_error{"Internal error: Lua newmetatable failed."};
306-
}
307-
lua_pushvalue(lua_state, -1); // Copy of new metatable
308-
309-
// Add metatable as osm2pgsql.Geometry so we can access it from Lua
310-
lua_setfield(lua_state, -3, "Geometry");
311-
312-
lua_pushvalue(lua_state, -1);
313-
lua_setfield(lua_state, -2, "__index");
314-
luaX_add_table_func(lua_state, "__gc", geom_gc);
315-
luaX_add_table_func(lua_state, "__len", geom_num_geometries);
316-
luaX_add_table_func(lua_state, "__tostring", geom_tostring);
317-
luaX_add_table_func(lua_state, "area", geom_area);
318-
luaX_add_table_func(lua_state, "length", geom_length);
319-
luaX_add_table_func(lua_state, "centroid", geom_centroid);
320-
luaX_add_table_func(lua_state, "get_bbox", geom_get_bbox);
321-
luaX_add_table_func(lua_state, "geometry_n", geom_geometry_n);
322-
luaX_add_table_func(lua_state, "geometry_type", geom_geometry_type);
323-
luaX_add_table_func(lua_state, "is_null", geom_is_null);
324-
luaX_add_table_func(lua_state, "line_merge", geom_line_merge);
325-
luaX_add_table_func(lua_state, "reverse", geom_reverse);
326-
luaX_add_table_func(lua_state, "num_geometries", geom_num_geometries);
327-
luaX_add_table_func(lua_state, "pole_of_inaccessibility",
328-
geom_pole_of_inaccessibility);
329-
luaX_add_table_func(lua_state, "segmentize", geom_segmentize);
330-
luaX_add_table_func(lua_state, "simplify", geom_simplify);
331-
luaX_add_table_func(lua_state, "spherical_area", geom_spherical_area);
332-
luaX_add_table_func(lua_state, "srid", geom_srid);
333-
luaX_add_table_func(lua_state, "transform", geom_transform);
334-
335-
lua_pop(lua_state, 2); // __index, global osmp2gsql
303+
luaX_set_up_metatable(
304+
lua_state, "Geometry", OSM2PGSQL_GEOMETRY_CLASS,
305+
{{"__gc", geom_gc},
306+
{"__len", geom_num_geometries},
307+
{"__tostring", geom_tostring},
308+
{"area", geom_area},
309+
{"length", geom_length},
310+
{"centroid", geom_centroid},
311+
{"get_bbox", geom_get_bbox},
312+
{"geometry_n", geom_geometry_n},
313+
{"geometry_type", geom_geometry_type},
314+
{"is_null", geom_is_null},
315+
{"line_merge", geom_line_merge},
316+
{"reverse", geom_reverse},
317+
{"num_geometries", geom_num_geometries},
318+
{"pole_of_inaccessibility", geom_pole_of_inaccessibility},
319+
{"segmentize", geom_segmentize},
320+
{"simplify", geom_simplify},
321+
{"spherical_area", geom_spherical_area},
322+
{"srid", geom_srid},
323+
{"transform", geom_transform}});
336324
}

src/flex-lua-locator.cpp

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -70,30 +70,14 @@ void lua_wrapper_locator::init(lua_State *lua_state,
7070
{
7171
s_connection_params = connection_params;
7272

73-
lua_getglobal(lua_state, "osm2pgsql");
74-
if (luaL_newmetatable(lua_state, OSM2PGSQL_LOCATOR_CLASS) != 1) {
75-
throw std::runtime_error{"Internal error: Lua newmetatable failed."};
76-
}
77-
lua_pushvalue(lua_state, -1); // Copy of new metatable
78-
79-
// Add metatable as osm2pgsql.Locator so we can access it from Lua
80-
lua_setfield(lua_state, -3, "Locator");
81-
82-
// Now add functions to metatable
83-
lua_pushvalue(lua_state, -1);
84-
lua_setfield(lua_state, -2, "__index");
85-
luaX_add_table_func(lua_state, "__tostring",
86-
lua_trampoline_locator_tostring);
87-
luaX_add_table_func(lua_state, "name", lua_trampoline_locator_name);
88-
luaX_add_table_func(lua_state, "add_bbox", lua_trampoline_locator_add_bbox);
89-
luaX_add_table_func(lua_state, "add_from_db",
90-
lua_trampoline_locator_add_from_db);
91-
luaX_add_table_func(lua_state, "all_intersecting",
92-
lua_trampoline_locator_all_intersecting);
93-
luaX_add_table_func(lua_state, "first_intersecting",
94-
lua_trampoline_locator_first_intersecting);
95-
96-
lua_pop(lua_state, 2);
73+
luaX_set_up_metatable(
74+
lua_state, "Locator", OSM2PGSQL_LOCATOR_CLASS,
75+
{{"__tostring", lua_trampoline_locator_tostring},
76+
{"name", lua_trampoline_locator_name},
77+
{"add_bbox", lua_trampoline_locator_add_bbox},
78+
{"add_from_db", lua_trampoline_locator_add_from_db},
79+
{"all_intersecting", lua_trampoline_locator_all_intersecting},
80+
{"first_intersecting", lua_trampoline_locator_first_intersecting}});
9781
}
9882

9983
int lua_wrapper_locator::tostring() const

src/flex-lua-table.cpp

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -457,27 +457,13 @@ int setup_flex_table(lua_State *lua_state, std::vector<flex_table_t> *tables,
457457
*/
458458
void lua_wrapper_table::init(lua_State *lua_state)
459459
{
460-
lua_getglobal(lua_state, "osm2pgsql");
461-
if (luaL_newmetatable(lua_state, OSM2PGSQL_TABLE_CLASS) != 1) {
462-
throw std::runtime_error{"Internal error: Lua newmetatable failed."};
463-
}
464-
lua_pushvalue(lua_state, -1); // Copy of new metatable
465-
466-
// Add metatable as osm2pgsql.Table so we can access it from Lua
467-
lua_setfield(lua_state, -3, "Table");
468-
469-
// Now add functions to metatable
470-
lua_pushvalue(lua_state, -1);
471-
lua_setfield(lua_state, -2, "__index");
472-
luaX_add_table_func(lua_state, "__tostring",
473-
lua_trampoline_table_tostring);
474-
luaX_add_table_func(lua_state, "insert", lua_trampoline_table_insert);
475-
luaX_add_table_func(lua_state, "name", lua_trampoline_table_name);
476-
luaX_add_table_func(lua_state, "schema", lua_trampoline_table_schema);
477-
luaX_add_table_func(lua_state, "cluster", lua_trampoline_table_cluster);
478-
luaX_add_table_func(lua_state, "columns", lua_trampoline_table_columns);
479-
480-
lua_pop(lua_state, 2);
460+
luaX_set_up_metatable(lua_state, "Table", OSM2PGSQL_TABLE_CLASS,
461+
{{"__tostring", lua_trampoline_table_tostring},
462+
{"insert", lua_trampoline_table_insert},
463+
{"name", lua_trampoline_table_name},
464+
{"schema", lua_trampoline_table_schema},
465+
{"cluster", lua_trampoline_table_cluster},
466+
{"columns", lua_trampoline_table_columns}});
481467
}
482468

483469
int lua_wrapper_table::tostring() const

src/lua-utils.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,32 @@ void luaX_add_table_func(lua_State *lua_state, char const *key,
115115
lua_rawset(lua_state, -3);
116116
}
117117

118+
void luaX_set_up_metatable(
119+
lua_State *lua_state, char const *name, char const *luaclass,
120+
std::initializer_list<std::pair<char const *, lua_CFunction>> map)
121+
122+
{
123+
lua_getglobal(lua_state, "osm2pgsql");
124+
if (luaL_newmetatable(lua_state, luaclass) != 1) {
125+
throw std::runtime_error{"Internal error: Lua newmetatable failed."};
126+
}
127+
lua_pushvalue(lua_state, -1); // Copy of new metatable
128+
129+
// Add metatable under the specified name so we can access it from Lua
130+
lua_setfield(lua_state, -3, name);
131+
132+
// Now add functions to metatable
133+
lua_pushvalue(lua_state, -1);
134+
lua_setfield(lua_state, -2, "__index");
135+
for (auto const &[key, func] : map) {
136+
lua_pushstring(lua_state, key);
137+
lua_pushcfunction(lua_state, func);
138+
lua_rawset(lua_state, -3);
139+
}
140+
141+
lua_settop(lua_state, 0);
142+
}
143+
118144
char const *luaX_get_table_string(lua_State *lua_state, char const *key,
119145
int table_index, char const *error_msg)
120146
{

src/lua-utils.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include <cassert>
1919
#include <cstdint>
20+
#include <initializer_list>
2021
#include <string_view>
2122
#include <utility>
2223

@@ -38,6 +39,10 @@ void luaX_add_table_bool(lua_State *lua_state, char const *key,
3839
void luaX_add_table_func(lua_State *lua_state, char const *key,
3940
lua_CFunction func) noexcept;
4041

42+
void luaX_set_up_metatable(
43+
lua_State *lua_state, char const *name, char const *luaclass,
44+
std::initializer_list<std::pair<char const *, lua_CFunction>> map);
45+
4146
template <typename COLLECTION, typename FUNC>
4247
void luaX_add_table_array(lua_State *lua_state, char const *key,
4348
COLLECTION const &collection, FUNC const &func)

src/output-flex.cpp

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,33 +1305,16 @@ void output_flex_t::init_lua(std::string const &filename,
13051305

13061306
assert(lua_gettop(lua_state()) == 0);
13071307

1308-
lua_getglobal(lua_state(), "osm2pgsql");
1309-
if (luaL_newmetatable(lua_state(), OSM2PGSQL_OSMOBJECT_CLASS) != 1) {
1310-
throw std::runtime_error{"Internal error: Lua newmetatable failed."};
1311-
}
1312-
lua_pushvalue(lua_state(), -1); // Copy of new metatable
1313-
1314-
// Add metatable as osm2pgsql.OSMObject so we can access it from Lua
1315-
lua_setfield(lua_state(), -3, "OSMObject");
1316-
1317-
lua_pushvalue(lua_state(), -1);
1318-
lua_setfield(lua_state(), -2, "__index");
1319-
luaX_add_table_func(lua_state(), "get_bbox", lua_trampoline_app_get_bbox);
1320-
luaX_add_table_func(lua_state(), "as_linestring",
1321-
lua_trampoline_app_as_linestring);
1322-
luaX_add_table_func(lua_state(), "as_point",
1323-
lua_trampoline_app_as_point);
1324-
luaX_add_table_func(lua_state(), "as_polygon",
1325-
lua_trampoline_app_as_polygon);
1326-
luaX_add_table_func(lua_state(), "as_multipoint",
1327-
lua_trampoline_app_as_multipoint);
1328-
luaX_add_table_func(lua_state(), "as_multilinestring",
1329-
lua_trampoline_app_as_multilinestring);
1330-
luaX_add_table_func(lua_state(), "as_multipolygon",
1331-
lua_trampoline_app_as_multipolygon);
1332-
luaX_add_table_func(lua_state(), "as_geometrycollection",
1333-
lua_trampoline_app_as_geometrycollection);
1334-
lua_settop(lua_state(), 0);
1308+
luaX_set_up_metatable(
1309+
lua_state(), "OSMObject", OSM2PGSQL_OSMOBJECT_CLASS,
1310+
{{"get_bbox", lua_trampoline_app_get_bbox},
1311+
{"as_linestring", lua_trampoline_app_as_linestring},
1312+
{"as_point", lua_trampoline_app_as_point},
1313+
{"as_polygon", lua_trampoline_app_as_polygon},
1314+
{"as_multipoint", lua_trampoline_app_as_multipoint},
1315+
{"as_multilinestring", lua_trampoline_app_as_multilinestring},
1316+
{"as_multipolygon", lua_trampoline_app_as_multipolygon},
1317+
{"as_geometrycollection", lua_trampoline_app_as_geometrycollection}});
13351318

13361319
// Load compiled in init.lua
13371320
if (luaL_dostring(lua_state(), lua_init())) {

0 commit comments

Comments
 (0)