Skip to content

Commit 2e897ba

Browse files
committed
Use the new luaX_for_each() function in more places
1 parent 6dc0405 commit 2e897ba

File tree

3 files changed

+14
-22
lines changed

3 files changed

+14
-22
lines changed

src/flex-write.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "flex-write.hpp"
1212
#include "geom-functions.hpp"
1313
#include "json-writer.hpp"
14+
#include "lua-utils.hpp"
1415
#include "wkb.hpp"
1516

1617
#include <algorithm>
@@ -188,17 +189,14 @@ static void write_json_table(json_writer_t *writer, lua_State *lua_state,
188189

189190
if (is_lua_array(lua_state)) {
190191
writer->start_array();
191-
lua_pushnil(lua_state);
192-
while (lua_next(lua_state, -2) != 0) {
192+
luaX_for_each(lua_state, [&]() {
193193
write_json(writer, lua_state, tables);
194194
writer->next();
195-
lua_pop(lua_state, 1);
196-
}
195+
});
197196
writer->end_array();
198197
} else {
199198
writer->start_object();
200-
lua_pushnil(lua_state);
201-
while (lua_next(lua_state, -2) != 0) {
199+
luaX_for_each(lua_state, [&]() {
202200
int const ltype_key = lua_type(lua_state, -2);
203201
if (ltype_key != LUA_TSTRING) {
204202
throw fmt_error("Incorrect data type '{}' as key.",
@@ -208,8 +206,7 @@ static void write_json_table(json_writer_t *writer, lua_State *lua_state,
208206
writer->key(key);
209207
write_json(writer, lua_state, tables);
210208
writer->next();
211-
lua_pop(lua_state, 1);
212-
}
209+
});
213210
writer->end_object();
214211
}
215212
}
@@ -403,8 +400,7 @@ void flex_write_column(lua_State *lua_state,
403400
if (ltype == LUA_TTABLE) {
404401
copy_mgr->new_hash();
405402

406-
lua_pushnil(lua_state);
407-
while (lua_next(lua_state, -2) != 0) {
403+
luaX_for_each(lua_state, [&]() {
408404
char const *const key = lua_tostring(lua_state, -2);
409405
char const *const val = lua_tostring(lua_state, -1);
410406
if (key == nullptr) {
@@ -422,8 +418,7 @@ void flex_write_column(lua_State *lua_state,
422418
lua_typename(lua_state, ltype_value), key);
423419
}
424420
copy_mgr->add_hash_elem(key, val);
425-
lua_pop(lua_state, 1);
426-
}
421+
});
427422

428423
copy_mgr->finish_hash();
429424
} else {

src/geom-transform.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "geom-functions.hpp"
1212
#include "geom-transform.hpp"
1313
#include "logging.hpp"
14+
#include "lua-utils.hpp"
1415

1516
#include <osmium/osm.hpp>
1617

@@ -153,8 +154,7 @@ void init_geom_transform(geom_transform_t *transform, lua_State *lua_state)
153154
assert(transform);
154155
assert(lua_state);
155156

156-
lua_pushnil(lua_state);
157-
while (lua_next(lua_state, -2) != 0) {
157+
luaX_for_each(lua_state, [&]() {
158158
char const *const field = lua_tostring(lua_state, -2);
159159
if (field == nullptr) {
160160
throw std::runtime_error{"All fields in geometry transformation "
@@ -169,9 +169,7 @@ void init_geom_transform(geom_transform_t *transform, lua_State *lua_state)
169169
show_warning = false;
170170
}
171171
}
172-
173-
lua_pop(lua_state, 1);
174-
}
172+
});
175173
}
176174

177175
std::unique_ptr<geom_transform_t>

src/tagtransform-lua.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ extern "C"
1414
}
1515

1616
#include "format.hpp"
17+
#include "lua-utils.hpp"
1718
#include "tagtransform-lua.hpp"
1819

1920
#include <stdexcept>
@@ -55,11 +56,10 @@ void lua_tagtransform_t::check_lua_function_exists(char const *func_name)
5556
*/
5657
static void get_out_tags(lua_State *lua_state, taglist_t *out_tags)
5758
{
58-
lua_pushnil(lua_state);
59-
while (lua_next(lua_state, -2) != 0) {
59+
luaX_for_each(lua_state, [&]() {
6060
auto const key_type = lua_type(lua_state, -2);
6161
// They key must be a string, otherwise the lua_tostring() function
62-
// below will change it to a string and the lua_next() iteration will
62+
// below will change it to a string and the Lua array iteration will
6363
// break.
6464
if (key_type != LUA_TSTRING) {
6565
throw fmt_error("Basic tag processing found incorrect data type"
@@ -79,8 +79,7 @@ static void get_out_tags(lua_State *lua_state, taglist_t *out_tags)
7979
char const *const key = lua_tostring(lua_state, -2);
8080
char const *const value = lua_tostring(lua_state, -1);
8181
out_tags->add_tag(key, value);
82-
lua_pop(lua_state, 1);
83-
}
82+
});
8483
lua_pop(lua_state, 1);
8584
}
8685

0 commit comments

Comments
 (0)