Skip to content

Commit e6b24ac

Browse files
committed
Refactor creation/cloning of output_t and derived classes
These were a bit of a mess. Now the implementations of the derived classes are more similar and a bit simpler. We'll use the "rule of five" in output_t and derived classes, so copy and move constructors and operators as well as (virtual) destructor are all spelled out. As far as possible constructors etc. are made private/protected. We only ever create those classes though the create_output() function or the clone() function. But we are using std::make_shared() internally so some constructors need to be public because, while conceptually the objects are created from inside the class itself, it is the make_shared function that actually does this and it can only use the public interface.
1 parent e9a6e7d commit e6b24ac

File tree

10 files changed

+123
-75
lines changed

10 files changed

+123
-75
lines changed

src/output-flex.cpp

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1980,45 +1980,50 @@ void output_flex_t::start()
19801980
}
19811981
}
19821982

1983+
output_flex_t::output_flex_t(output_flex_t const *other,
1984+
std::shared_ptr<middle_query_t> mid,
1985+
std::shared_ptr<db_copy_thread_t> copy_thread)
1986+
: output_t(other, std::move(mid)), m_tables(other->m_tables),
1987+
m_stage2_way_ids(other->m_stage2_way_ids),
1988+
m_copy_thread(std::move(copy_thread)), m_lua_state(other->m_lua_state),
1989+
m_expire(other->get_options()->expire_tiles_zoom,
1990+
other->get_options()->expire_tiles_max_bbox,
1991+
other->get_options()->projection),
1992+
m_process_node(other->m_process_node), m_process_way(other->m_process_way),
1993+
m_process_relation(other->m_process_relation),
1994+
m_select_relation_members(other->m_select_relation_members)
1995+
{
1996+
assert(m_table_connections.empty());
1997+
for (auto &table : *m_tables) {
1998+
m_table_connections.emplace_back(&table, m_copy_thread);
1999+
}
2000+
2001+
init_clone();
2002+
}
2003+
19832004
std::shared_ptr<output_t>
19842005
output_flex_t::clone(std::shared_ptr<middle_query_t> const &mid,
19852006
std::shared_ptr<db_copy_thread_t> const &copy_thread) const
19862007
{
1987-
return std::make_shared<output_flex_t>(
1988-
mid, m_thread_pool, *get_options(), copy_thread, true, m_lua_state,
1989-
m_process_node, m_process_way, m_process_relation,
1990-
m_select_relation_members, m_tables, m_stage2_way_ids);
2008+
return std::make_shared<output_flex_t>(this, mid, copy_thread);
19912009
}
19922010

19932011
output_flex_t::output_flex_t(
19942012
std::shared_ptr<middle_query_t> const &mid,
19952013
std::shared_ptr<thread_pool_t> thread_pool, options_t const &o,
1996-
std::shared_ptr<db_copy_thread_t> const &copy_thread, bool is_clone,
1997-
std::shared_ptr<lua_State> lua_state, prepared_lua_function_t process_node,
1998-
prepared_lua_function_t process_way,
1999-
prepared_lua_function_t process_relation,
2000-
prepared_lua_function_t select_relation_members,
2001-
std::shared_ptr<std::vector<flex_table_t>> tables,
2002-
std::shared_ptr<idset_t> stage2_way_ids)
2003-
: output_t(mid, std::move(thread_pool), o), m_tables(std::move(tables)),
2004-
m_stage2_way_ids(std::move(stage2_way_ids)), m_copy_thread(copy_thread),
2005-
m_lua_state(std::move(lua_state)),
2006-
m_expire(o.expire_tiles_zoom, o.expire_tiles_max_bbox, o.projection),
2007-
m_process_node(process_node), m_process_way(process_way),
2008-
m_process_relation(process_relation),
2009-
m_select_relation_members(select_relation_members)
2014+
std::shared_ptr<db_copy_thread_t> const &copy_thread)
2015+
: output_t(mid, std::move(thread_pool), o), m_copy_thread(copy_thread),
2016+
m_expire(o.expire_tiles_zoom, o.expire_tiles_max_bbox, o.projection)
20102017
{
20112018
assert(copy_thread);
20122019

2013-
if (!is_clone) {
2014-
init_lua(get_options()->style);
2020+
init_lua(get_options()->style);
20152021

2016-
// If the osm2pgsql.select_relation_members() Lua function is defined
2017-
// it means we need two-stage processing which in turn means we need
2018-
// the full ways stored in the middle.
2019-
if (m_select_relation_members) {
2020-
m_output_requirements.full_ways = true;
2021-
}
2022+
// If the osm2pgsql.select_relation_members() Lua function is defined
2023+
// it means we need two-stage processing which in turn means we need
2024+
// the full ways stored in the middle.
2025+
if (m_select_relation_members) {
2026+
m_output_requirements.full_ways = true;
20222027
}
20232028

20242029
if (m_tables->empty()) {
@@ -2030,10 +2035,6 @@ output_flex_t::output_flex_t(
20302035
for (auto &table : *m_tables) {
20312036
m_table_connections.emplace_back(&table, m_copy_thread);
20322037
}
2033-
2034-
if (is_clone) {
2035-
init_clone();
2036-
}
20372038
}
20382039

20392040
/**

src/output-flex.hpp

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -97,20 +97,17 @@ class prepared_lua_function_t
9797

9898
class output_flex_t : public output_t
9999
{
100-
101100
public:
101+
/// Constructor for new objects
102102
output_flex_t(
103103
std::shared_ptr<middle_query_t> const &mid,
104104
std::shared_ptr<thread_pool_t> thread_pool, options_t const &options,
105-
std::shared_ptr<db_copy_thread_t> const &copy_thread,
106-
bool is_clone = false, std::shared_ptr<lua_State> lua_state = nullptr,
107-
prepared_lua_function_t process_node = {},
108-
prepared_lua_function_t process_way = {},
109-
prepared_lua_function_t process_relation = {},
110-
prepared_lua_function_t select_relation_members = {},
111-
std::shared_ptr<std::vector<flex_table_t>> tables =
112-
std::make_shared<std::vector<flex_table_t>>(),
113-
std::shared_ptr<idset_t> stage2_way_ids = std::make_shared<idset_t>());
105+
std::shared_ptr<db_copy_thread_t> const &copy_thread);
106+
107+
/// Constructor for cloned objects
108+
output_flex_t(output_flex_t const *other,
109+
std::shared_ptr<middle_query_t> mid,
110+
std::shared_ptr<db_copy_thread_t> copy_thread);
114111

115112
output_flex_t(output_flex_t const &) = delete;
116113
output_flex_t &operator=(output_flex_t const &) = delete;
@@ -281,12 +278,14 @@ class output_flex_t : public output_t
281278

282279
}; // relation_cache_t
283280

284-
std::shared_ptr<std::vector<flex_table_t>> m_tables;
281+
std::shared_ptr<std::vector<flex_table_t>> m_tables =
282+
std::make_shared<std::vector<flex_table_t>>();
283+
285284
std::vector<table_connection_t> m_table_connections;
286285

287286
// This is shared between all clones of the output and must only be
288287
// accessed while protected using the lua_mutex.
289-
std::shared_ptr<idset_t> m_stage2_way_ids;
288+
std::shared_ptr<idset_t> m_stage2_way_ids = std::make_shared<idset_t>();
290289

291290
std::shared_ptr<db_copy_thread_t> m_copy_thread;
292291

@@ -300,10 +299,10 @@ class output_flex_t : public output_t
300299
relation_cache_t m_relation_cache;
301300
osmium::Node const *m_context_node = nullptr;
302301

303-
prepared_lua_function_t m_process_node;
304-
prepared_lua_function_t m_process_way;
305-
prepared_lua_function_t m_process_relation;
306-
prepared_lua_function_t m_select_relation_members;
302+
prepared_lua_function_t m_process_node{};
303+
prepared_lua_function_t m_process_way{};
304+
prepared_lua_function_t m_process_relation{};
305+
prepared_lua_function_t m_select_relation_members{};
307306

308307
calling_context m_calling_context = calling_context::main;
309308

src/output-gazetteer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include <memory>
2222
#include <string>
2323

24+
output_gazetteer_t::~output_gazetteer_t() = default;
25+
2426
void output_gazetteer_t::delete_unused_classes(char osm_type, osmid_t osm_id)
2527
{
2628
if (get_options()->append) {

src/output-gazetteer.hpp

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,32 +28,39 @@ struct middle_query_t;
2828

2929
class output_gazetteer_t : public output_t
3030
{
31-
output_gazetteer_t(output_gazetteer_t const *other,
32-
std::shared_ptr<middle_query_t> const &cloned_mid,
33-
std::shared_ptr<db_copy_thread_t> const &copy_thread)
34-
: output_t(cloned_mid, other->m_thread_pool, *other->get_options()),
35-
m_copy(copy_thread), m_proj(other->get_options()->projection),
36-
m_osmium_buffer(PLACE_BUFFER_SIZE, osmium::memory::Buffer::auto_grow::yes)
37-
{}
38-
3931
public:
32+
/// Constructor for new objects
4033
output_gazetteer_t(std::shared_ptr<middle_query_t> const &mid,
4134
std::shared_ptr<thread_pool_t> thread_pool,
4235
options_t const &options,
4336
std::shared_ptr<db_copy_thread_t> const &copy_thread)
4437
: output_t(mid, std::move(thread_pool), options), m_copy(copy_thread),
45-
m_proj(options.projection),
46-
m_osmium_buffer(PLACE_BUFFER_SIZE, osmium::memory::Buffer::auto_grow::yes)
38+
m_proj(options.projection)
4739
{
4840
m_style.load_style(options.style);
4941
}
5042

43+
/// Constructor for cloned objects
44+
output_gazetteer_t(output_gazetteer_t const *other,
45+
std::shared_ptr<middle_query_t> const &mid,
46+
std::shared_ptr<db_copy_thread_t> const &copy_thread)
47+
: output_t(other, mid), m_copy(copy_thread),
48+
m_proj(other->get_options()->projection)
49+
{}
50+
51+
output_gazetteer_t(output_gazetteer_t const &) = delete;
52+
output_gazetteer_t &operator=(output_gazetteer_t const &) = delete;
53+
54+
output_gazetteer_t(output_gazetteer_t &&) = delete;
55+
output_gazetteer_t &operator=(output_gazetteer_t &&) = delete;
56+
57+
~output_gazetteer_t() override;
58+
5159
std::shared_ptr<output_t>
5260
clone(std::shared_ptr<middle_query_t> const &mid,
5361
std::shared_ptr<db_copy_thread_t> const &copy_thread) const override
5462
{
55-
return std::shared_ptr<output_t>(
56-
new output_gazetteer_t{this, mid, copy_thread});
63+
return std::make_shared<output_gazetteer_t>(this, mid, copy_thread);
5764
}
5865

5966
void start() override;
@@ -93,7 +100,8 @@ class output_gazetteer_t : public output_t
93100
gazetteer_style_t m_style;
94101

95102
std::shared_ptr<reprojection> m_proj;
96-
osmium::memory::Buffer m_osmium_buffer;
103+
osmium::memory::Buffer m_osmium_buffer{
104+
PLACE_BUFFER_SIZE, osmium::memory::Buffer::auto_grow::yes};
97105
};
98106

99107
#endif // OSM2PGSQL_OUTPUT_GAZETTEER_HPP

src/output-null.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99

1010
#include "output-null.hpp"
1111

12-
std::shared_ptr<output_t>
13-
output_null_t::clone(std::shared_ptr<middle_query_t> const &mid,
14-
std::shared_ptr<db_copy_thread_t> const &) const
12+
std::shared_ptr<output_t> output_null_t::clone(
13+
std::shared_ptr<middle_query_t> const & /*mid*/,
14+
std::shared_ptr<db_copy_thread_t> const & /*copy_thread*/) const
1515
{
16-
return std::make_shared<output_null_t>(mid, m_thread_pool, *get_options());
16+
return std::make_shared<output_null_t>(*this);
1717
}
1818

1919
output_null_t::output_null_t(std::shared_ptr<middle_query_t> const &mid,

src/output-null.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ class output_null_t : public output_t
2222
std::shared_ptr<thread_pool_t> thread_pool,
2323
options_t const &options);
2424

25+
output_null_t(output_null_t const &) = default;
26+
output_null_t &operator=(output_null_t const &) = default;
27+
28+
output_null_t(output_null_t &&) = default;
29+
output_null_t &operator=(output_null_t &&) = default;
30+
2531
~output_null_t() override;
2632

2733
std::shared_ptr<output_t>

src/output-pgsql.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,7 @@ std::shared_ptr<output_t> output_pgsql_t::clone(
416416
std::shared_ptr<middle_query_t> const &mid,
417417
std::shared_ptr<db_copy_thread_t> const &copy_thread) const
418418
{
419-
return std::shared_ptr<output_t>(
420-
new output_pgsql_t{this, mid, copy_thread});
419+
return std::make_shared<output_pgsql_t>(this, mid, copy_thread);
421420
}
422421

423422
output_pgsql_t::output_pgsql_t(
@@ -481,8 +480,7 @@ output_pgsql_t::output_pgsql_t(
481480
output_pgsql_t::output_pgsql_t(
482481
output_pgsql_t const *other, std::shared_ptr<middle_query_t> const &mid,
483482
std::shared_ptr<db_copy_thread_t> const &copy_thread)
484-
: output_t(mid, other->m_thread_pool, *other->get_options()),
485-
m_tagtransform(other->m_tagtransform->clone()),
483+
: output_t(other, mid), m_tagtransform(other->m_tagtransform->clone()),
486484
m_enable_way_area(other->m_enable_way_area),
487485
m_proj(get_options()->projection),
488486
m_expire(get_options()->expire_tiles_zoom,

src/output-pgsql.hpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@
2727

2828
class output_pgsql_t : public output_t
2929
{
30-
output_pgsql_t(output_pgsql_t const *other,
31-
std::shared_ptr<middle_query_t> const &mid,
32-
std::shared_ptr<db_copy_thread_t> const &copy_thread);
33-
3430
public:
3531
enum table_id
3632
{
@@ -41,11 +37,23 @@ class output_pgsql_t : public output_t
4137
t_MAX
4238
};
4339

40+
/// Constructor for new objects
4441
output_pgsql_t(std::shared_ptr<middle_query_t> const &mid,
4542
std::shared_ptr<thread_pool_t> thread_pool,
4643
options_t const &options,
4744
std::shared_ptr<db_copy_thread_t> const &copy_thread);
4845

46+
/// Constructor for cloned objects
47+
output_pgsql_t(output_pgsql_t const *other,
48+
std::shared_ptr<middle_query_t> const &mid,
49+
std::shared_ptr<db_copy_thread_t> const &copy_thread);
50+
51+
output_pgsql_t(output_pgsql_t const &) = delete;
52+
output_pgsql_t &operator=(output_pgsql_t const &) = delete;
53+
54+
output_pgsql_t(output_pgsql_t &&) = delete;
55+
output_pgsql_t &operator=(output_pgsql_t &&) = delete;
56+
4957
~output_pgsql_t() override;
5058

5159
std::shared_ptr<output_t>

src/output.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,12 @@ output_t::output_t(std::shared_ptr<middle_query_t> mid,
6666
options_t const &options)
6767
: m_mid(std::move(mid)), m_options(&options),
6868
m_thread_pool(std::move(thread_pool))
69+
{}
6970

71+
output_t::output_t(output_t const *other, std::shared_ptr<middle_query_t> mid)
72+
: m_mid(std::move(mid)), m_options(other->m_options),
73+
m_thread_pool(other->m_thread_pool),
74+
m_output_requirements(other->m_output_requirements)
7075
{}
7176

7277
output_t::~output_t() = default;

src/output.hpp

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,24 @@ struct middle_query_t;
3232
class output_t
3333
{
3434
public:
35+
/// Factory method for creating instances of classes derived from output_t.
3536
static std::shared_ptr<output_t>
3637
create_output(std::shared_ptr<middle_query_t> const &mid,
3738
std::shared_ptr<thread_pool_t> thread_pool,
3839
options_t const &options);
3940

40-
output_t(std::shared_ptr<middle_query_t> mid,
41-
std::shared_ptr<thread_pool_t> thread_pool,
42-
options_t const &options);
41+
output_t(output_t const &) = default;
42+
output_t &operator=(output_t const &) = default;
43+
44+
output_t(output_t &&) = default;
45+
output_t &operator=(output_t &&) = default;
4346

4447
virtual ~output_t();
4548

49+
/**
50+
* This function clones instances of derived classes of output_t, it must
51+
* be implemented in derived classes.
52+
*/
4653
virtual std::shared_ptr<output_t>
4754
clone(std::shared_ptr<middle_query_t> const &mid,
4855
std::shared_ptr<db_copy_thread_t> const &copy_thread) const = 0;
@@ -96,8 +103,23 @@ class output_t
96103
private:
97104
std::shared_ptr<middle_query_t> m_mid;
98105
options_t const *m_options;
106+
std::shared_ptr<thread_pool_t> m_thread_pool;
99107

100108
protected:
109+
/**
110+
* Constructor used for creating a new object using the create_output()
111+
* function.
112+
*/
113+
output_t(std::shared_ptr<middle_query_t> mid,
114+
std::shared_ptr<thread_pool_t> thread_pool,
115+
options_t const &options);
116+
117+
/**
118+
* Constructor used for cloning an existing output using clone(). It gets
119+
* a new middle query pointer, everything else is copied over.
120+
*/
121+
output_t(output_t const *other, std::shared_ptr<middle_query_t> mid);
122+
101123
thread_pool_t &thread_pool() const noexcept
102124
{
103125
assert(m_thread_pool);
@@ -112,7 +134,6 @@ class output_t
112134

113135
const options_t *get_options() const noexcept { return m_options; };
114136

115-
std::shared_ptr<thread_pool_t> m_thread_pool;
116137
output_requirements m_output_requirements{};
117138
};
118139

0 commit comments

Comments
 (0)