1 #ifndef OSM2PGSQL_TESTS_COMMON_BUFFER_HPP
2 #define OSM2PGSQL_TESTS_COMMON_BUFFER_HPP
3 
4 /**
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  *
7  * This file is part of osm2pgsql (https://osm2pgsql.org/).
8  *
9  * Copyright (C) 2006-2021 by the osm2pgsql developer community.
10  * For a full list of authors see the git log.
11  */
12 
13 #include "format.hpp"
14 #include "osmtypes.hpp"
15 
16 #include <osmium/memory/buffer.hpp>
17 #include <osmium/opl.hpp>
18 #include <osmium/osm.hpp>
19 
20 /**
21  * Wrapper around an Osmium buffer to create test objects in with some
22  * convenience.
23  */
24 class test_buffer_t
25 {
26 public:
buffer() const27     osmium::memory::Buffer const &buffer() const noexcept { return m_buffer; }
28 
add_node(std::string const & data)29     osmium::Node const &add_node(std::string const &data)
30     {
31         return m_buffer.get<osmium::Node>(add_opl(data));
32     }
33 
add_way(std::string const & data)34     osmium::Way &add_way(std::string const &data)
35     {
36         return m_buffer.get<osmium::Way>(add_opl(data));
37     }
38 
add_way(osmid_t wid,idlist_t const & ids)39     osmium::Way &add_way(osmid_t wid, idlist_t const &ids)
40     {
41         assert(!ids.empty());
42         std::string nodes;
43 
44         for (auto const id : ids) {
45             nodes += "n{},"_format(id);
46         }
47 
48         nodes.resize(nodes.size() - 1);
49 
50         return add_way("w{} N{}"_format(wid, nodes));
51     }
52 
add_relation(std::string const & data)53     osmium::Relation const &add_relation(std::string const &data)
54     {
55         return m_buffer.get<osmium::Relation>(add_opl(data));
56     }
57 
58 private:
add_opl(std::string const & data)59     std::size_t add_opl(std::string const &data)
60     {
61         auto const offset = m_buffer.committed();
62         osmium::opl_parse(data.c_str(), m_buffer);
63         return offset;
64     }
65 
66     osmium::memory::Buffer m_buffer{4096,
67                                     osmium::memory::Buffer::auto_grow::yes};
68 };
69 
70 #endif // OSM2PGSQL_TESTS_COMMON_BUFFER_HPP
71