Libosmium  2.17.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
6This file is part of Osmium (https://osmcode.org/libosmium).
7
8Copyright 2013-2021 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/handler.hpp>
37
38#include <memory>
39#include <utility>
40
41namespace 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() = 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_) \
93template <typename THandler> \
94auto _name_##_dispatch(THandler& handler, const osmium::_type_& object, int) -> decltype(handler._name_(object), void()) { \
95 handler._name_(object); \
96} \
97template <typename THandler> \
98auto _name_##_dispatch(THandler& handler, const osmium::_type_& object, long) -> decltype(handler(object), void()) { \
99 handler(object); \
100}
101
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 void node(const osmium::Node& node) final {
130 node_dispatch(m_handler, node, 0);
131 }
132
133 void way(const osmium::Way& way) final {
134 way_dispatch(m_handler, way, 0);
135 }
136
137 void relation(const osmium::Relation& relation) final {
138 relation_dispatch(m_handler, relation, 0);
139 }
140
141 void area(const osmium::Area& area) final {
142 area_dispatch(m_handler, area, 0);
143 }
144
145 void changeset(const osmium::Changeset& changeset) final {
146 changeset_dispatch(m_handler, changeset, 0);
147 }
148
149 void flush() final {
150 flush_dispatch(m_handler, 0);
151 }
152
153 }; // class HandlerWrapper
154
155 } // namespace detail
156
158
159 using impl_ptr = std::unique_ptr<osmium::handler::detail::HandlerWrapperBase>;
161
162 public:
163
165 m_impl(new osmium::handler::detail::HandlerWrapperBase) {
166 }
167
168 template <typename THandler, typename... TArgs>
169 void set(TArgs&&... args) {
170 m_impl.reset(new osmium::handler::detail::HandlerWrapper<THandler>{std::forward<TArgs>(args)...});
171 }
172
173 void node(const osmium::Node& node) {
174 m_impl->node(node);
175 }
176
177 void way(const osmium::Way& way) {
178 m_impl->way(way);
179 }
180
182 m_impl->relation(relation);
183 }
184
185 void area(const osmium::Area& area) {
186 m_impl->area(area);
187 }
188
190 m_impl->changeset(changeset);
191 }
192
193 void flush() {
194 m_impl->flush();
195 }
196
197 }; // class DynamicHandler
198
199 } // namespace handler
200
201} // namespace osmium
202
203#endif // OSMIUM_DYNAMIC_HANDLER_HPP
Definition: area.hpp:126
An OSM Changeset, a group of changes made by a single user over a short period of time.
Definition: changeset.hpp:148
Definition: node.hpp:48
Definition: relation.hpp:168
Definition: way.hpp:72
Definition: dynamic_handler.hpp:157
impl_ptr m_impl
Definition: dynamic_handler.hpp:160
void node(const osmium::Node &node)
Definition: dynamic_handler.hpp:173
void changeset(const osmium::Changeset &changeset)
Definition: dynamic_handler.hpp:189
void set(TArgs &&... args)
Definition: dynamic_handler.hpp:169
void way(const osmium::Way &way)
Definition: dynamic_handler.hpp:177
void flush()
Definition: dynamic_handler.hpp:193
void area(const osmium::Area &area)
Definition: dynamic_handler.hpp:185
std::unique_ptr< osmium::handler::detail::HandlerWrapperBase > impl_ptr
Definition: dynamic_handler.hpp:159
DynamicHandler()
Definition: dynamic_handler.hpp:164
void relation(const osmium::Relation &relation)
Definition: dynamic_handler.hpp:181
Definition: handler.hpp:71
#define OSMIUM_DYNAMIC_HANDLER_DISPATCH(_name_, _type_)
Definition: dynamic_handler.hpp:92
Definition: attr.hpp:342
@ forward
Linestring has same direction as way.
@ area
Definition: entity_bits.hpp:72
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
Definition: location.hpp:551