Libosmium  2.15.4
Fast and flexible C++ library for working with OpenStreetMap data
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-2019 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() = 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 
104  OSMIUM_DYNAMIC_HANDLER_DISPATCH(relation, Relation)
105  OSMIUM_DYNAMIC_HANDLER_DISPATCH(changeset, Changeset)
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
osmium::geom::direction::forward
@ forward
Linestring has same direction as way.
osmium::handler::DynamicHandler::flush
void flush()
Definition: dynamic_handler.hpp:193
osmium::osm_entity_bits::node
@ node
Definition: entity_bits.hpp:68
osmium::handler::DynamicHandler::way
void way(const osmium::Way &way)
Definition: dynamic_handler.hpp:177
osmium::handler::DynamicHandler::area
void area(const osmium::Area &area)
Definition: dynamic_handler.hpp:185
osmium::handler::DynamicHandler::node
void node(const osmium::Node &node)
Definition: dynamic_handler.hpp:173
osmium::handler::Handler
Definition: handler.hpp:71
detail
Definition: attr.hpp:333
osmium::Node
Definition: node.hpp:48
osmium::handler::DynamicHandler::relation
void relation(const osmium::Relation &relation)
Definition: dynamic_handler.hpp:181
osmium::handler::DynamicHandler::m_impl
impl_ptr m_impl
Definition: dynamic_handler.hpp:160
osmium::handler::DynamicHandler::impl_ptr
std::unique_ptr< osmium::handler::detail::HandlerWrapperBase > impl_ptr
Definition: dynamic_handler.hpp:159
osmium
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
osmium::osm_entity_bits::area
@ area
Definition: entity_bits.hpp:72
osmium::handler::DynamicHandler::DynamicHandler
DynamicHandler()
Definition: dynamic_handler.hpp:164
osmium::osm_entity_bits::relation
@ relation
Definition: entity_bits.hpp:70
osmium::Way
Definition: way.hpp:72
osmium::Relation
Definition: relation.hpp:168
handler.hpp
std
Definition: location.hpp:550
osmium::Area
Definition: area.hpp:126
osmium::osm_entity_bits::way
@ way
Definition: entity_bits.hpp:69
OSMIUM_DYNAMIC_HANDLER_DISPATCH
#define OSMIUM_DYNAMIC_HANDLER_DISPATCH(_name_, _type_)
Definition: dynamic_handler.hpp:92
osmium::handler::DynamicHandler::changeset
void changeset(const osmium::Changeset &changeset)
Definition: dynamic_handler.hpp:189
osmium::osm_entity_bits::changeset
@ changeset
Definition: entity_bits.hpp:75
osmium::handler::DynamicHandler::set
void set(TArgs &&... args)
Definition: dynamic_handler.hpp:169
osmium::Changeset
An OSM Changeset, a group of changes made by a single user over a short period of time.
Definition: changeset.hpp:148
osmium::handler::DynamicHandler
Definition: dynamic_handler.hpp:157