Libosmium 2.22.0 Fast and flexible C++ library for working with OpenStreetMap data

Loading...
Searching...
No Matches
dynamic_handler.hpp
Go to the documentation of this file.
1#ifndef OSMIUM_DYNAMIC_HANDLER_HPP
2#define OSMIUM_DYNAMIC_HANDLER_HPP
3
4/*
5
6 This file is part of Osmium (https://osmcode.org/libosmium).
7
8 Copyright 2013-2025 Jochen Topf <jochen@topf.org> and others (see README).
9
10 Boost Software License - Version 1.0 - August 17th, 2003
11
12 Permission is hereby granted, free of charge, to any person or organization
13 obtaining a copy of the software and accompanying documentation covered by
14 this license (the "Software") to use, reproduce, display, distribute,
15 execute, and transmit the Software, and to prepare derivative works of the
16 Software, and to permit third-parties to whom the Software is furnished to
17 do so, all subject to the following:
18
19 The copyright notices in the Software and this entire statement, including
20 the above license grant, this restriction and the following disclaimer,
21 must be included in all copies of the Software, in whole or in part, and
22 all derivative works of the Software, unless such copies or derivative
23 works are solely in the form of machine-executable object code generated by
24 a source language processor.
25
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 DEALINGS IN THE SOFTWARE.
33
34*/
35
36#include <osmium/handler.hpp>
37
38#include <memory>
39#include <utility>
40
41 namespace osmium {
42
43 class Node;
44 class Way;
45 class Relation;
46 class Area;
47 class Changeset;
48
49 namespace handler {
50
51 namespace detail {
52
53 class HandlerWrapperBase {
54
55 public:
56
57 HandlerWrapperBase() = default;
58
59 HandlerWrapperBase(const HandlerWrapperBase&) = default;
60 HandlerWrapperBase& operator=(const HandlerWrapperBase&) = default;
61
62 HandlerWrapperBase(HandlerWrapperBase&&) noexcept = default;
63 HandlerWrapperBase& operator=(HandlerWrapperBase&&) noexcept = default;
64
65 virtual ~HandlerWrapperBase() noexcept = default;
66
67 virtual void node(const osmium::Node& /*node*/) {
68 }
69
70 virtual void way(const osmium::Way& /*way*/) {
71 }
72
73 virtual void relation(const osmium::Relation& /*relation*/) {
74 }
75
76 virtual void area(const osmium::Area& /*area*/) {
77 }
78
79 virtual void changeset(const osmium::Changeset& /*changeset*/) {
80 }
81
82 virtual void flush() {
83 }
84
85 }; // class HandlerWrapperBase
86
87
88 // The following uses trick from
89 // https://stackoverflow.com/questions/257288/is-it-possible-to-write-a-c-template-to-check-for-a-functions-existence
90 // to either call handler style functions or visitor style operator().
91
92#define OSMIUM_DYNAMIC_HANDLER_DISPATCH(_name_, _type_) \
93 template <typename THandler> \
94 auto _name_##_dispatch(THandler& handler, const osmium::_type_& object, int) -> decltype(handler._name_(object), void()) { \
95 handler._name_(object); \
96} \
97 template <typename THandler> \
98 auto _name_##_dispatch(THandler& handler, const osmium::_type_& object, long) -> decltype(handler(object), void()) { \
99 handler(object); \
100}
101
102 OSMIUM_DYNAMIC_HANDLER_DISPATCH(node, Node)
103 OSMIUM_DYNAMIC_HANDLER_DISPATCH(way, Way)
104 OSMIUM_DYNAMIC_HANDLER_DISPATCH(relation, Relation)
105 OSMIUM_DYNAMIC_HANDLER_DISPATCH(changeset, Changeset)
106 OSMIUM_DYNAMIC_HANDLER_DISPATCH(area, Area)
107
108 template <typename THandler>
109 auto flush_dispatch(THandler& handler, int /*dispatch*/) -> decltype(handler.flush(), void()) {
110 handler.flush();
111 }
112
113 template <typename THandler>
114 void flush_dispatch(THandler& /*handler*/, long /*dispatch*/) { // NOLINT(google-runtime-int)
115 }
116
117 template <typename THandler>
118 class HandlerWrapper : public HandlerWrapperBase {
119
120 THandler m_handler;
121
122 public:
123
124 template <typename... TArgs>
125 explicit HandlerWrapper(TArgs&&... args) :
126 m_handler(std::forward<TArgs>(args)...) {
127 }
128
129 HandlerWrapper(const HandlerWrapper&) = default;
130 HandlerWrapper& operator=(const HandlerWrapper&) = default;
131
132 HandlerWrapper(HandlerWrapper&&) noexcept = default;
133 HandlerWrapper& operator=(HandlerWrapper&&) noexcept = default;
134
135 ~HandlerWrapper() noexcept override = default;
136
137 void node(const osmium::Node& node) final {
138 node_dispatch(m_handler, node, 0);
139 }
140
141 void way(const osmium::Way& way) final {
142 way_dispatch(m_handler, way, 0);
143 }
144
145 void relation(const osmium::Relation& relation) final {
146 relation_dispatch(m_handler, relation, 0);
147 }
148
149 void area(const osmium::Area& area) final {
150 area_dispatch(m_handler, area, 0);
151 }
152
153 void changeset(const osmium::Changeset& changeset) final {
154 changeset_dispatch(m_handler, changeset, 0);
155 }
156
157 void flush() final {
158 flush_dispatch(m_handler, 0);
159 }
160
161 }; // class HandlerWrapper
162
163 } // namespace detail
164
165 class DynamicHandler : public osmium::handler::Handler {
166
167 using impl_ptr = std::unique_ptr<osmium::handler::detail::HandlerWrapperBase>;
168 impl_ptr m_impl;
169
170 public:
171
172 DynamicHandler() :
173 m_impl(new osmium::handler::detail::HandlerWrapperBase) {
174 }
175
176 template <typename THandler, typename... TArgs>
177 void set(TArgs&&... args) {
178 m_impl.reset(new osmium::handler::detail::HandlerWrapper<THandler>{std::forward<TArgs>(args)...});
179 }
180
181 void node(const osmium::Node& node) {
182 m_impl->node(node);
183 }
184
185 void way(const osmium::Way& way) {
186 m_impl->way(way);
187 }
188
189 void relation(const osmium::Relation& relation) {
190 m_impl->relation(relation);
191 }
192
193 void area(const osmium::Area& area) {
194 m_impl->area(area);
195 }
196
197 void changeset(const osmium::Changeset& changeset) {
198 m_impl->changeset(changeset);
199 }
200
201 void flush() {
202 m_impl->flush();
203 }
204
205 }; // class DynamicHandler
206
207 } // namespace handler
208
209} // namespace osmium
210
211#endif // OSMIUM_DYNAMIC_HANDLER_HPP
osmium::Area
Definition area.hpp:125
osmium::Changeset
An OSM Changeset, a group of changes made by a single user over a short period of time.
Definition changeset.hpp:146
osmium::Node
Definition node.hpp:48
osmium::Relation
Definition relation.hpp:161
osmium::Way
Definition way.hpp:72
osmium::handler::DynamicHandler
Definition dynamic_handler.hpp:165
osmium::handler::DynamicHandler::m_impl
impl_ptr m_impl
Definition dynamic_handler.hpp:168
osmium::handler::DynamicHandler::node
void node(const osmium::Node &node)
Definition dynamic_handler.hpp:181
osmium::handler::DynamicHandler::changeset
void changeset(const osmium::Changeset &changeset)
Definition dynamic_handler.hpp:197
osmium::handler::DynamicHandler::set
void set(TArgs &&... args)
Definition dynamic_handler.hpp:177
osmium::handler::DynamicHandler::way
void way(const osmium::Way &way)
Definition dynamic_handler.hpp:185
osmium::handler::DynamicHandler::flush
void flush()
Definition dynamic_handler.hpp:201
osmium::handler::DynamicHandler::area
void area(const osmium::Area &area)
Definition dynamic_handler.hpp:193
osmium::handler::DynamicHandler::impl_ptr
std::unique_ptr< osmium::handler::detail::HandlerWrapperBase > impl_ptr
Definition dynamic_handler.hpp:167
osmium::handler::DynamicHandler::DynamicHandler
DynamicHandler()
Definition dynamic_handler.hpp:172
osmium::handler::DynamicHandler::relation
void relation(const osmium::Relation &relation)
Definition dynamic_handler.hpp:189
osmium::handler::Handler
Definition handler.hpp:71
OSMIUM_DYNAMIC_HANDLER_DISPATCH
#define OSMIUM_DYNAMIC_HANDLER_DISPATCH(_name_, _type_)
Definition dynamic_handler.hpp:92
handler.hpp
detail
Definition attr.hpp:342
osmium::geom::direction::forward
@ forward
Linestring has same direction as way.
osmium::osm_entity_bits::area
@ area
Definition entity_bits.hpp:75
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
osmium::item_type::relation
@ relation
osmium::item_type::node
@ node
osmium::item_type::changeset
@ changeset
osmium::item_type::way
@ way
std
Definition location.hpp:555