Libosmium: include/osmium/visitor.hpp Source File

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

Loading...
Searching...
No Matches
visitor.hpp
Go to the documentation of this file.
1#ifndef OSMIUM_VISITOR_HPP
2#define OSMIUM_VISITOR_HPP
3
4/*
5
6This file is part of Osmium (https://osmcode.org/libosmium).
7
8Copyright 2013-2026 Jochen Topf <jochen@topf.org> and others (see README).
9
10Boost Software License - Version 1.0 - August 17th, 2003
11
12Permission is hereby granted, free of charge, to any person or organization
13obtaining a copy of the software and accompanying documentation covered by
14this license (the "Software") to use, reproduce, display, distribute,
15execute, and transmit the Software, and to prepare derivative works of the
16Software, and to permit third-parties to whom the Software is furnished to
17do so, all subject to the following:
18
19The copyright notices in the Software and this entire statement, including
20the above license grant, this restriction and the following disclaimer,
21must be included in all copies of the Software, in whole or in part, and
22all derivative works of the Software, unless such copies or derivative
23works are solely in the form of machine-executable object code generated by
24a source language processor.
25
26THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32DEALINGS IN THE SOFTWARE.
33
34*/
35
36#include <osmium/fwd.hpp>
37#include <osmium/handler.hpp>
38#include <osmium/io/reader_iterator.hpp> // IWYU pragma: keep
39#include <osmium/memory/buffer.hpp>
40#include <osmium/osm.hpp>
41#include <osmium/osm/entity.hpp>
42#include <osmium/osm/item_type.hpp>
43
44#include <type_traits>
45#include <utility>
46
47namespace osmium {
48
49 namespace detail {
50
51 template <typename T, typename U>
52 using ConstIfConst = std::conditional_t<std::is_const<T>::value, std::add_const_t<U>, U>;
53
54 template <typename THandler, typename TItem>
55 inline void apply_item_impl(TItem& item, THandler&& handler) {
56 switch (item.type()) {
57 case osmium::item_type::undefined:
58 break;
59 case osmium::item_type::node:
60 std::forward<THandler>(handler).osm_object(static_cast<ConstIfConst<TItem, osmium::OSMObject>&>(item));
61 std::forward<THandler>(handler).node(static_cast<ConstIfConst<TItem, osmium::Node>&>(item));
62 break;
63 case osmium::item_type::way:
64 std::forward<THandler>(handler).osm_object(static_cast<ConstIfConst<TItem, osmium::OSMObject>&>(item));
65 std::forward<THandler>(handler).way(static_cast<ConstIfConst<TItem, osmium::Way>&>(item));
66 break;
67 case osmium::item_type::relation:
68 std::forward<THandler>(handler).osm_object(static_cast<ConstIfConst<TItem, osmium::OSMObject>&>(item));
69 std::forward<THandler>(handler).relation(static_cast<ConstIfConst<TItem, osmium::Relation>&>(item));
70 break;
71 case osmium::item_type::area:
72 std::forward<THandler>(handler).osm_object(static_cast<ConstIfConst<TItem, osmium::OSMObject>&>(item));
73 std::forward<THandler>(handler).area(static_cast<ConstIfConst<TItem, osmium::Area>&>(item));
74 break;
75 case osmium::item_type::changeset:
76 std::forward<THandler>(handler).changeset(static_cast<ConstIfConst<TItem, osmium::Changeset>&>(item));
77 break;
78 case osmium::item_type::tag_list:
79 std::forward<THandler>(handler).tag_list(static_cast<ConstIfConst<TItem, osmium::TagList>&>(item));
80 break;
81 case osmium::item_type::way_node_list:
82 std::forward<THandler>(handler).way_node_list(static_cast<ConstIfConst<TItem, osmium::WayNodeList>&>(item));
83 break;
84 case osmium::item_type::relation_member_list:
85 case osmium::item_type::relation_member_list_with_full_members:
86 std::forward<THandler>(handler).relation_member_list(static_cast<ConstIfConst<TItem, osmium::RelationMemberList>&>(item));
87 break;
88 case osmium::item_type::outer_ring:
89 std::forward<THandler>(handler).outer_ring(static_cast<ConstIfConst<TItem, osmium::OuterRing>&>(item));
90 break;
91 case osmium::item_type::inner_ring:
92 std::forward<THandler>(handler).inner_ring(static_cast<ConstIfConst<TItem, osmium::InnerRing>&>(item));
93 break;
94 case osmium::item_type::changeset_discussion:
95 std::forward<THandler>(handler).changeset_discussion(static_cast<ConstIfConst<TItem, osmium::ChangesetDiscussion>&>(item));
96 break;
97 }
98 }
99
100 template <typename THandler>
101 inline void apply_item_impl(const osmium::OSMEntity& item, THandler&& handler) {
102 switch (item.type()) {
103 case osmium::item_type::node:
104 std::forward<THandler>(handler).osm_object(static_cast<const osmium::OSMObject&>(item));
105 std::forward<THandler>(handler).node(static_cast<const osmium::Node&>(item));
106 break;
107 case osmium::item_type::way:
108 std::forward<THandler>(handler).osm_object(static_cast<const osmium::OSMObject&>(item));
109 std::forward<THandler>(handler).way(static_cast<const osmium::Way&>(item));
110 break;
111 case osmium::item_type::relation:
112 std::forward<THandler>(handler).osm_object(static_cast<const osmium::OSMObject&>(item));
113 std::forward<THandler>(handler).relation(static_cast<const osmium::Relation&>(item));
114 break;
115 case osmium::item_type::area:
116 std::forward<THandler>(handler).osm_object(static_cast<const osmium::OSMObject&>(item));
117 std::forward<THandler>(handler).area(static_cast<const osmium::Area&>(item));
118 break;
119 case osmium::item_type::changeset:
120 std::forward<THandler>(handler).changeset(static_cast<const osmium::Changeset&>(item));
121 break;
122 default:
123 throw osmium::unknown_type{};
124 }
125 }
126
127 template <typename THandler>
128 inline void apply_item_impl(osmium::OSMEntity& item, THandler&& handler) {
129 switch (item.type()) {
130 case osmium::item_type::node:
131 std::forward<THandler>(handler).osm_object(static_cast<osmium::OSMObject&>(item));
132 std::forward<THandler>(handler).node(static_cast<osmium::Node&>(item));
133 break;
134 case osmium::item_type::way:
135 std::forward<THandler>(handler).osm_object(static_cast<osmium::OSMObject&>(item));
136 std::forward<THandler>(handler).way(static_cast<osmium::Way&>(item));
137 break;
138 case osmium::item_type::relation:
139 std::forward<THandler>(handler).osm_object(static_cast<osmium::OSMObject&>(item));
140 std::forward<THandler>(handler).relation(static_cast<osmium::Relation&>(item));
141 break;
142 case osmium::item_type::area:
143 std::forward<THandler>(handler).osm_object(static_cast<osmium::OSMObject&>(item));
144 std::forward<THandler>(handler).area(static_cast<osmium::Area&>(item));
145 break;
146 case osmium::item_type::changeset:
147 std::forward<THandler>(handler).changeset(static_cast<osmium::Changeset&>(item));
148 break;
149 default:
150 throw osmium::unknown_type{};
151 }
152 }
153
154 template <typename THandler>
155 inline void apply_item_impl(const osmium::OSMObject& item, THandler&& handler) {
156 switch (item.type()) {
157 case osmium::item_type::node:
158 std::forward<THandler>(handler).osm_object(item);
159 std::forward<THandler>(handler).node(static_cast<const osmium::Node&>(item));
160 break;
161 case osmium::item_type::way:
162 std::forward<THandler>(handler).osm_object(item);
163 std::forward<THandler>(handler).way(static_cast<const osmium::Way&>(item));
164 break;
165 case osmium::item_type::relation:
166 std::forward<THandler>(handler).osm_object(item);
167 std::forward<THandler>(handler).relation(static_cast<const osmium::Relation&>(item));
168 break;
169 case osmium::item_type::area:
170 std::forward<THandler>(handler).osm_object(item);
171 std::forward<THandler>(handler).area(static_cast<const osmium::Area&>(item));
172 break;
173 default:
174 throw osmium::unknown_type{};
175 }
176 }
177
178 template <typename THandler>
179 inline void apply_item_impl(osmium::OSMObject& item, THandler&& handler) {
180 switch (item.type()) {
181 case osmium::item_type::node:
182 std::forward<THandler>(handler).osm_object(item);
183 std::forward<THandler>(handler).node(static_cast<osmium::Node&>(item));
184 break;
185 case osmium::item_type::way:
186 std::forward<THandler>(handler).osm_object(item);
187 std::forward<THandler>(handler).way(static_cast<osmium::Way&>(item));
188 break;
189 case osmium::item_type::relation:
190 std::forward<THandler>(handler).osm_object(item);
191 std::forward<THandler>(handler).relation(static_cast<osmium::Relation&>(item));
192 break;
193 case osmium::item_type::area:
194 std::forward<THandler>(handler).osm_object(item);
195 std::forward<THandler>(handler).area(static_cast<osmium::Area&>(item));
196 break;
197 default:
198 throw osmium::unknown_type{};
199 }
200 }
201
202 template <typename TFunc>
203 struct wrapper_handler : TFunc {
204
205 template <typename T>
206 explicit wrapper_handler(T&& func) : TFunc(std::forward<T>(func)) { // NOLINT(bugprone-forwarding-reference-overload)
207 }
208
209 // Fallback that always matches.
210 void operator()(const osmium::memory::Item& /*item*/) const noexcept {
211 }
212
213 // The function we are wrapping.
214 using TFunc::operator();
215
216 void osm_object(const osmium::OSMObject& /*osm_object*/) const noexcept {
217 }
218
219 void node(const osmium::Node& node) const {
220 operator()(node);
221 }
222
223 // cppcheck-suppress constParameter
224 void node(osmium::Node& node) const {
225 operator()(node);
226 }
227
228 void way(const osmium::Way& way) const {
229 operator()(way);
230 }
231
232 // cppcheck-suppress constParameter
233 void way(osmium::Way& way) const {
234 operator()(way);
235 }
236
237 void relation(const osmium::Relation& relation) const {
238 operator()(relation);
239 }
240
241 // cppcheck-suppress constParameter
242 void relation(osmium::Relation& relation) const {
243 operator()(relation);
244 }
245
246 void area(const osmium::Area& area) const {
247 operator()(area);
248 }
249
250 // cppcheck-suppress constParameter
251 void area(osmium::Area& area) const {
252 operator()(area);
253 }
254
255 void changeset(const osmium::Changeset& changeset) const {
256 operator()(changeset);
257 }
258
259 // cppcheck-suppress constParameter
260 void changeset(osmium::Changeset& changeset) const {
261 operator()(changeset);
262 }
263
264 void tag_list(const osmium::TagList& /*tag_list*/) const noexcept {
265 }
266
267 void way_node_list(const osmium::WayNodeList& /*way_node_list*/) const noexcept {
268 }
269
270 void relation_member_list(const osmium::RelationMemberList& /*relation_member_list*/) const noexcept {
271 }
272
273 void outer_ring(const osmium::OuterRing& /*outer_ring*/) const noexcept {
274 }
275
276 void inner_ring(const osmium::InnerRing& /*inner_ring*/) const noexcept {
277 }
278
279 void changeset_discussion(const osmium::ChangesetDiscussion& /*changeset_discussion*/) const noexcept {
280 }
281
282 void flush() const noexcept {
283 }
284
285 }; // struct wrapper_handler
286
287 // Is the class T derived from osmium::handler::Handler?
288 template <typename T>
289 using is_handler = std::is_base_of<osmium::handler::Handler, std::remove_reference_t<T>>;
290
291 // This is already a handler, use it as it is.
292 template <typename T, typename = std::enable_if_t<is_handler<T>::value>>
293 T make_handler(T&& func) {
294 return std::forward<T>(func);
295 }
296
297 // This is not a handler, but a functor. Wrap a handler around it.
298 template <typename T, typename = std::enable_if_t<!is_handler<T>::value>>
299 wrapper_handler<std::decay_t<T>> make_handler(T&& func) {
300 return wrapper_handler<std::decay_t<T>>(std::forward<T>(func));
301 }
302
303 } // namespace detail
304
305 template <typename TItem, typename... THandlers>
306 inline void apply_item(TItem& item, THandlers&&... handlers) {
307 (void)std::initializer_list<int>{
308 (detail::apply_item_impl(item, std::forward<THandlers>(handlers)), 0)...};
309 }
310
311 template <typename... THandlers>
312 inline void apply_flush(THandlers&&... handlers) {
313 (void)std::initializer_list<int>{
314 (std::forward<THandlers>(handlers).flush(), 0)...};
315 }
316
317 template <typename TIterator, typename... THandlers>
318 inline void apply_impl(TIterator it, TIterator end, THandlers&&... handlers) {
319 for (; it != end; ++it) {
320 apply_item(*it, handlers...);
321 }
322 apply_flush(std::forward<THandlers>(handlers)...);
323 }
324
325 template <typename TIterator, typename... THandlers>
326 inline void apply(TIterator it, TIterator end, THandlers&&... handlers) {
327 apply_impl(it, end, detail::make_handler<THandlers>(std::forward<THandlers>(handlers))...);
328 }
329
330 template <typename TContainer, typename... THandlers>
331 inline void apply(TContainer& c, THandlers&&... handlers) {
332 using std::begin;
333 using std::end;
334 apply(begin(c), end(c), std::forward<THandlers>(handlers)...);
335 }
336
337 template <typename... THandlers>
338 inline void apply(const osmium::memory::Buffer& buffer, THandlers&&... handlers) {
339 apply(buffer.cbegin(), buffer.cend(), std::forward<THandlers>(handlers)...);
340 }
341
342} // namespace osmium
343
344#endif // OSMIUM_VISITOR_HPP
buffer.hpp
osmium::memory::Item::type
item_type type() const noexcept
Definition item.hpp:171
entity.hpp
fwd.hpp
handler.hpp
item_type.hpp
osmium::geom::direction::forward
@ forward
Linestring has same direction as way.
Definition factory.hpp:120
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
osmium::apply_item
void apply_item(TItem &item, THandlers &&... handlers)
Definition visitor.hpp:306
osmium::apply
void apply(TIterator it, TIterator end, THandlers &&... handlers)
Definition visitor.hpp:326
osmium::apply_flush
void apply_flush(THandlers &&... handlers)
Definition visitor.hpp:312
osmium::apply_impl
void apply_impl(TIterator it, TIterator end, THandlers &&... handlers)
Definition visitor.hpp:318
osmium::item_type::tag_list
@ tag_list
Definition item_type.hpp:53
osmium::item_type::relation_member_list
@ relation_member_list
Definition item_type.hpp:55
osmium::item_type::way_node_list
@ way_node_list
Definition item_type.hpp:54
osmium::item_type::changeset_discussion
@ changeset_discussion
Definition item_type.hpp:59
osmium::item_type::relation_member_list_with_full_members
@ relation_member_list_with_full_members
Definition item_type.hpp:56
osmium::item_type::relation
@ relation
Definition item_type.hpp:50
osmium::item_type::node
@ node
Definition item_type.hpp:48
osmium::item_type::changeset
@ changeset
Definition item_type.hpp:52
osmium::item_type::area
@ area
Definition item_type.hpp:51
osmium::item_type::undefined
@ undefined
Definition item_type.hpp:47
osmium::item_type::way
@ way
Definition item_type.hpp:49
osmium::item_type::outer_ring
@ outer_ring
Definition item_type.hpp:57
osmium::item_type::inner_ring
@ inner_ring
Definition item_type.hpp:58
osm.hpp
reader_iterator.hpp