Skip to content

Refactor: Split out some code from output_flex_t class #2324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 81 additions & 2 deletions src/flex-lua-expire-output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
#include "expire-output.hpp"
#include "format.hpp"
#include "lua-utils.hpp"
#include "pgsql.hpp"
#include "util.hpp"

#include <lua.hpp>

Expand Down Expand Up @@ -67,6 +65,13 @@ create_expire_output(lua_State *lua_state, std::string const &default_schema,
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,
Expand All @@ -88,3 +93,77 @@ int setup_flex_expire_output(lua_State *lua_state,

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_name) != 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;
}
24 changes: 23 additions & 1 deletion src/flex-lua-expire-output.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
* For a full list of authors see the git log.
*/

#include "expire-output.hpp"
#include "flex-lua-wrapper.hpp"

#include <string>
#include <vector>

class expire_output_t;
struct lua_State;

static char const *const osm2pgsql_expire_output_name =
Expand All @@ -23,4 +25,24 @@ int setup_flex_expire_output(lua_State *lua_state,
std::string const &default_schema,
std::vector<expire_output_t> *expire_outputs);

class lua_wrapper_expire_output : public lua_wrapper_base<expire_output_t>
{
public:
static void init(lua_State *lua_state);

lua_wrapper_expire_output(lua_State *lua_state,
expire_output_t *expire_output)
: lua_wrapper_base(lua_state, expire_output)
{
}

int __tostring() const;
int filename() const;
int maxzoom() const;
int minzoom() const;
int schema() const;
int table() const;

}; // class lua_wrapper_expire_output

#endif // OSM2PGSQL_FLEX_LUA_EXPIRE_OUTPUT_HPP
86 changes: 86 additions & 0 deletions src/flex-lua-table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
#include "flex-lua-index.hpp"
#include "flex-table.hpp"
#include "lua-utils.hpp"
#include "output-flex.hpp"
#include "pgsql-capabilities.hpp"
#include "util.hpp"

#include <lua.hpp>

Expand Down Expand Up @@ -416,6 +418,12 @@ void setup_flex_table_indexes(lua_State *lua_state, flex_table_t *table,
lua_pop(lua_state, 1); // "indexes"
}

TRAMPOLINE_WRAPPED_OBJECT(table, __tostring)
TRAMPOLINE_WRAPPED_OBJECT(table, cluster)
TRAMPOLINE_WRAPPED_OBJECT(table, columns)
TRAMPOLINE_WRAPPED_OBJECT(table, name)
TRAMPOLINE_WRAPPED_OBJECT(table, schema)

} // anonymous namespace

int setup_flex_table(lua_State *lua_state, std::vector<flex_table_t> *tables,
Expand All @@ -442,3 +450,81 @@ int setup_flex_table(lua_State *lua_state, std::vector<flex_table_t> *tables,

return 1;
}

/**
* Define the osm2pgsql.Table class/metatable.
*/
void lua_wrapper_table::init(lua_State *lua_state)
{
lua_getglobal(lua_state, "osm2pgsql");
if (luaL_newmetatable(lua_state, osm2pgsql_table_name) != 1) {
throw std::runtime_error{"Internal error: Lua newmetatable failed."};
}
lua_pushvalue(lua_state, -1); // Copy of new metatable

// Add metatable as osm2pgsql.Table so we can access it from Lua
lua_setfield(lua_state, -3, "Table");

// 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_table___tostring);
luaX_add_table_func(lua_state, "insert", lua_trampoline_table_insert);
luaX_add_table_func(lua_state, "name", lua_trampoline_table_name);
luaX_add_table_func(lua_state, "schema", lua_trampoline_table_schema);
luaX_add_table_func(lua_state, "cluster", lua_trampoline_table_cluster);
luaX_add_table_func(lua_state, "columns", lua_trampoline_table_columns);

lua_pop(lua_state, 2);
}

int lua_wrapper_table::__tostring() const
{
std::string const str{fmt::format("osm2pgsql.Table[{}]", self().name())};
luaX_pushstring(lua_state(), str);

return 1;
}

int lua_wrapper_table::cluster() const
{
lua_pushboolean(lua_state(), self().cluster_by_geom());
return 1;
}

int lua_wrapper_table::columns() const
{
lua_createtable(lua_state(), (int)self().num_columns(), 0);

int n = 0;
for (auto const &column : self().columns()) {
lua_pushinteger(lua_state(), ++n);
lua_newtable(lua_state());

luaX_add_table_str(lua_state(), "name", column.name().c_str());
luaX_add_table_str(lua_state(), "type", column.type_name().c_str());
luaX_add_table_str(lua_state(), "sql_type",
column.sql_type_name().c_str());
luaX_add_table_str(lua_state(), "sql_modifiers",
column.sql_modifiers().c_str());
luaX_add_table_bool(lua_state(), "not_null", column.not_null());
luaX_add_table_bool(lua_state(), "create_only", column.create_only());

lua_rawset(lua_state(), -3);
}

return 1;
}

int lua_wrapper_table::name() const
{
luaX_pushstring(lua_state(), self().name());
return 1;
}

int lua_wrapper_table::schema() const
{
luaX_pushstring(lua_state(), self().schema());
return 1;
}
20 changes: 20 additions & 0 deletions src/flex-lua-table.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* For a full list of authors see the git log.
*/

#include "flex-lua-wrapper.hpp"

#include <string>
#include <vector>

Expand All @@ -24,4 +26,22 @@ int setup_flex_table(lua_State *lua_state, std::vector<flex_table_t> *tables,
std::string const &default_schema, bool updatable,
bool append_mode);

class lua_wrapper_table : public lua_wrapper_base<flex_table_t>
{
public:
static void init(lua_State *lua_state);

lua_wrapper_table(lua_State *lua_state, flex_table_t *table)
: lua_wrapper_base(lua_state, table)
{
}

int __tostring() const;
int cluster() const;
int columns() const;
int name() const;
int schema() const;

}; // class lua_wrapper_table

#endif // OSM2PGSQL_FLEX_LUA_TABLE_HPP
60 changes: 60 additions & 0 deletions src/flex-lua-wrapper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#ifndef OSM2PGSQL_FLEX_LUA_WRAPPER_HPP
#define OSM2PGSQL_FLEX_LUA_WRAPPER_HPP

/**
* 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 "output-flex.hpp"

#include <exception>

#define TRAMPOLINE_WRAPPED_OBJECT(obj_name, func_name) \
int lua_trampoline_##obj_name##_##func_name(lua_State *lua_state) \
{ \
try { \
auto *flex = \
static_cast<output_flex_t *>(luaX_get_context(lua_state)); \
auto &obj = flex->get_##obj_name##_from_param(); \
return lua_wrapper_##obj_name{lua_state, &obj}.func_name(); \
} catch (std::exception const &e) { \
return luaL_error(lua_state, "Error in '" #func_name "': %s\n", \
e.what()); \
} catch (...) { \
return luaL_error(lua_state, \
"Unknown error in '" #func_name "'.\n"); \
} \
}

struct lua_State;

/**
* Helper class for wrapping C++ classes in Lua "classes".
*/
template <typename WRAPPED>
class lua_wrapper_base
{
public:
lua_wrapper_base(lua_State *lua_state, WRAPPED *wrapped)
: m_lua_state(lua_state), m_self(wrapped)
{
}

protected:
lua_State *lua_state() const noexcept { return m_lua_state; }

WRAPPED const &self() const noexcept { return *m_self; }
WRAPPED &self() noexcept { return *m_self; }

private:
lua_State *m_lua_state;
WRAPPED *m_self;

}; // class lua_wrapper_base;

#endif // OSM2PGSQL_FLEX_LUA_WRAPPER_HPP
Loading