Skip to content

Commit f14e7ab

Browse files
authored
Merge pull request #1727 from joto/refactor-helper-check-lua-func-params
Refactoring: Add helper function to check Lua function parameters
2 parents 5245112 + 57d6e01 commit f14e7ab

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

src/output-flex.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -716,19 +716,32 @@ static void push_location(lua_State *lua_state,
716716
lua_pushnumber(lua_state, location.lat());
717717
}
718718

719-
int output_flex_t::app_get_bbox()
719+
/**
720+
* Helper function checking that Lua function "name" is called in the correct
721+
* context and without parameters.
722+
*/
723+
void output_flex_t::check_context_and_state(char const *name,
724+
char const *context, bool condition)
720725
{
721-
if (m_calling_context != calling_context::process_node &&
722-
m_calling_context != calling_context::process_way &&
723-
m_calling_context != calling_context::process_relation) {
726+
if (condition) {
724727
throw std::runtime_error{
725-
"The function get_bbox() can only be called from the "
726-
"process_node/way/relation() functions."};
728+
"The function {}() can only be called from the {}."_format(
729+
name, context)};
727730
}
728731

729732
if (lua_gettop(lua_state()) > 1) {
730-
throw std::runtime_error{"No parameter(s) needed for get_box()."};
733+
throw std::runtime_error{
734+
"No parameter(s) needed for {}()."_format(name)};
731735
}
736+
}
737+
738+
int output_flex_t::app_get_bbox()
739+
{
740+
check_context_and_state(
741+
"get_bbox", "process_node/way/relation() functions",
742+
m_calling_context != calling_context::process_node &&
743+
m_calling_context != calling_context::process_way &&
744+
m_calling_context != calling_context::process_relation);
732745

733746
if (m_calling_context == calling_context::process_node) {
734747
push_location(lua_state(), m_context_node->location());

src/output-flex.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ class output_flex_t : public output_t
187187

188188
flex_table_t const &get_table_from_param();
189189

190+
void check_context_and_state(char const *name, char const *context,
191+
bool condition);
192+
190193
void write_column(db_copy_mgr_t<db_deleter_by_type_and_id_t> *copy_mgr,
191194
flex_table_column_t const &column);
192195
void write_row(table_connection_t *table_connection,

0 commit comments

Comments
 (0)