Skip to content

Commit 91ff1e6

Browse files
committed
Rename string_id_list to string_id_list_t and put into utils.hpp
1 parent 989f490 commit 91ff1e6

File tree

4 files changed

+56
-28
lines changed

4 files changed

+56
-28
lines changed

src/middle-pgsql.cpp

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -229,37 +229,11 @@ void middle_pgsql_t::buffer_store_tags(osmium::OSMObject const &obj, bool attrs)
229229
}
230230
}
231231

232-
/**
233-
* Class for building a stringified list of ids in the format "{id1,id2,id3}"
234-
* for use in PostgreSQL queries.
235-
*/
236-
class string_id_list
237-
{
238-
public:
239-
void add(osmid_t id)
240-
{
241-
fmt::format_to(std::back_inserter(m_list), "{},", id);
242-
}
243-
244-
bool empty() const noexcept { return m_list.size() == 1; }
245-
246-
std::string const &get()
247-
{
248-
assert(!empty());
249-
m_list.back() = '}';
250-
return m_list;
251-
}
252-
253-
private:
254-
std::string m_list{"{"};
255-
256-
}; // class string_id_list
257-
258232
std::size_t middle_query_pgsql_t::get_way_node_locations_db(
259233
osmium::WayNodeList *nodes) const
260234
{
261235
size_t count = 0;
262-
string_id_list id_list;
236+
util::string_id_list_t id_list;
263237

264238
// get nodes where possible from cache,
265239
// at the same time build a list for querying missing nodes from DB
@@ -446,7 +420,7 @@ middle_query_pgsql_t::rel_way_members_get(osmium::Relation const &rel,
446420
rolelist_t *roles,
447421
osmium::memory::Buffer &buffer) const
448422
{
449-
string_id_list id_list;
423+
util::string_id_list_t id_list;
450424

451425
for (auto const &m : rel.members()) {
452426
if (m.type() == osmium::item_type::way) {

src/util.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@
33

44
namespace util {
55

6+
void string_id_list_t::add(osmid_t id)
7+
{
8+
fmt::format_to(std::back_inserter(m_list), "{},", id);
9+
}
10+
11+
std::string const &string_id_list_t::get()
12+
{
13+
assert(!empty());
14+
m_list.back() = '}';
15+
return m_list;
16+
}
17+
618
std::string human_readable_duration(uint64_t seconds)
719
{
820
if (seconds < 60) {

src/util.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,24 @@ class double_to_buffer
5252
std::array<char, buffer_size> m_buffer;
5353
};
5454

55+
/**
56+
* Class for building a stringified list of ids in the format "{id1,id2,id3}"
57+
* for use in PostgreSQL queries.
58+
*/
59+
class string_id_list_t
60+
{
61+
public:
62+
void add(osmid_t id);
63+
64+
bool empty() const noexcept { return m_list.size() == 1; }
65+
66+
std::string const &get();
67+
68+
private:
69+
std::string m_list{"{"};
70+
71+
}; // class string_id_list_t
72+
5573
/**
5674
* Helper class for timing with a granularity of seconds. The timer will
5775
* start on construction and is stopped by calling stop().

tests/test-util.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,30 @@ TEST_CASE("double_to_buffer 3.141", "[NoDB]")
3535
REQUIRE(std::strcmp(buffer.c_str(), "3.141") == 0);
3636
}
3737

38+
TEST_CASE("string_id_list_t with one element", "[NoDB]")
39+
{
40+
util::string_id_list_t list;
41+
REQUIRE(list.empty());
42+
43+
list.add(17);
44+
45+
REQUIRE_FALSE(list.empty());
46+
REQUIRE(list.get() == "{17}");
47+
}
48+
49+
TEST_CASE("string_id_list_t with several elements", "[NoDB]")
50+
{
51+
util::string_id_list_t list;
52+
REQUIRE(list.empty());
53+
54+
list.add(17);
55+
list.add(3);
56+
list.add(99);
57+
58+
REQUIRE_FALSE(list.empty());
59+
REQUIRE(list.get() == "{17,3,99}");
60+
}
61+
3862
TEST_CASE("human readable time durations", "[NoDB]")
3963
{
4064
REQUIRE(util::human_readable_duration(0) == "0s");

0 commit comments

Comments
 (0)