Libosmium  2.12.2
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 (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2017 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 <memory>
37 #include <utility>
38 
39 #include <osmium/handler.hpp>
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  virtual ~HandlerWrapperBase() {
58  }
59 
60  virtual void node(const osmium::Node&) {
61  }
62 
63  virtual void way(const osmium::Way&) {
64  }
65 
66  virtual void relation(const osmium::Relation&) {
67  }
68 
69  virtual void area(const osmium::Area&) {
70  }
71 
72  virtual void changeset(const osmium::Changeset&) {
73  }
74 
75  virtual void flush() {
76  }
77 
78  }; // class HandlerWrapperBase
79 
80 
81  // The following uses trick from
82  // http://stackoverflow.com/questions/257288/is-it-possible-to-write-a-c-template-to-check-for-a-functions-existence
83  // to either call handler style functions or visitor style operator().
84 
85 #define OSMIUM_DYNAMIC_HANDLER_DISPATCH(_name_, _type_) \
86 template <typename THandler> \
87 auto _name_##_dispatch(THandler& handler, const osmium::_type_& object, int) -> decltype(handler._name_(object), void()) { \
88  handler._name_(object); \
89 } \
90 template <typename THandler> \
91 auto _name_##_dispatch(THandler& handler, const osmium::_type_& object, long) -> decltype(handler(object), void()) { \
92  handler(object); \
93 }
94 
97  OSMIUM_DYNAMIC_HANDLER_DISPATCH(relation, Relation)
98  OSMIUM_DYNAMIC_HANDLER_DISPATCH(changeset, Changeset)
100 
101  template <typename THandler>
102  auto flush_dispatch(THandler& handler, int) -> decltype(handler.flush(), void()) {
103  handler.flush();
104  }
105 
106  template <typename THandler>
107  void flush_dispatch(THandler&, long) {
108  }
109 
110  template <typename THandler>
111  class HandlerWrapper : public HandlerWrapperBase {
112 
113  THandler m_handler;
114 
115  public:
116 
117  template <typename... TArgs>
118  HandlerWrapper(TArgs&&... args) :
119  m_handler(std::forward<TArgs>(args)...) {
120  }
121 
122  void node(const osmium::Node& node) final {
123  node_dispatch(m_handler, node, 0);
124  }
125 
126  void way(const osmium::Way& way) final {
127  way_dispatch(m_handler, way, 0);
128  }
129 
130  void relation(const osmium::Relation& relation) final {
131  relation_dispatch(m_handler, relation, 0);
132  }
133 
134  void area(const osmium::Area& area) final {
135  area_dispatch(m_handler, area, 0);
136  }
137 
138  void changeset(const osmium::Changeset& changeset) final {
139  changeset_dispatch(m_handler, changeset, 0);
140  }
141 
142  void flush() final {
143  flush_dispatch(m_handler, 0);
144  }
145 
146  }; // class HandlerWrapper
147 
148  } // namespace detail
149 
151 
152  using impl_ptr = std::unique_ptr<osmium::handler::detail::HandlerWrapperBase>;
154 
155  public:
156 
158  m_impl(impl_ptr(new osmium::handler::detail::HandlerWrapperBase)) {
159  }
160 
161  template <typename THandler, typename... TArgs>
162  void set(TArgs&&... args) {
163  m_impl = impl_ptr(new osmium::handler::detail::HandlerWrapper<THandler>(std::forward<TArgs>(args)...));
164  }
165 
166  void node(const osmium::Node& node) {
167  m_impl->node(node);
168  }
169 
170  void way(const osmium::Way& way) {
171  m_impl->way(way);
172  }
173 
174  void relation(const osmium::Relation& relation) {
175  m_impl->relation(relation);
176  }
177 
178  void area(const osmium::Area& area) {
179  m_impl->area(area);
180  }
181 
182  void changeset(const osmium::Changeset& changeset) {
183  m_impl->changeset(changeset);
184  }
185 
186  void flush() {
187  m_impl->flush();
188  }
189 
190  }; // class DynamicHandler
191 
192  } // namespace handler
193 
194 } // namespace osmium
195 
196 #endif // OSMIUM_DYNAMIC_HANDLER_HPP
std::unique_ptr< osmium::handler::detail::HandlerWrapperBase > impl_ptr
Definition: dynamic_handler.hpp:152
void relation(const osmium::Relation &relation)
Definition: dynamic_handler.hpp:174
Definition: relation.hpp:168
Definition: area.hpp:126
Definition: handler.hpp:71
Definition: dynamic_handler.hpp:150
void changeset(const osmium::Changeset &changeset)
Definition: dynamic_handler.hpp:182
Definition: way.hpp:72
#define OSMIUM_DYNAMIC_HANDLER_DISPATCH(_name_, _type_)
Definition: dynamic_handler.hpp:85
void node(const osmium::Node &node)
Definition: dynamic_handler.hpp:166
Namespace for everything in the Osmium library.
Definition: assembler.hpp:63
Definition: attr.hpp:333
void area(const osmium::Area &area)
Definition: dynamic_handler.hpp:178
impl_ptr m_impl
Definition: dynamic_handler.hpp:153
void flush()
Definition: dynamic_handler.hpp:186
Definition: node.hpp:48
DynamicHandler()
Definition: dynamic_handler.hpp:157
An OSM Changeset, a group of changes made by a single user over a short period of time...
Definition: changeset.hpp:148
void way(const osmium::Way &way)
Definition: dynamic_handler.hpp:170