Libosmium  2.10.3
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-2016 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  template <typename THandler>
110  class HandlerWrapper : public HandlerWrapperBase {
111 
112  THandler m_handler;
113 
114  public:
115 
116  template <typename... TArgs>
117  HandlerWrapper(TArgs&&... args) :
118  m_handler(std::forward<TArgs>(args)...) {
119  }
120 
121  void node(const osmium::Node& node) final {
122  node_dispatch(m_handler, node, 0);
123  }
124 
125  void way(const osmium::Way& way) final {
126  way_dispatch(m_handler, way, 0);
127  }
128 
129  void relation(const osmium::Relation& relation) final {
130  relation_dispatch(m_handler, relation, 0);
131  }
132 
133  void area(const osmium::Area& area) final {
134  area_dispatch(m_handler, area, 0);
135  }
136 
137  void changeset(const osmium::Changeset& changeset) final {
138  changeset_dispatch(m_handler, changeset, 0);
139  }
140 
141  void flush() final {
142  flush_dispatch(m_handler, 0);
143  }
144 
145  }; // class HandlerWrapper
146 
147  } // namespace detail
148 
150 
151  using impl_ptr = std::unique_ptr<osmium::handler::detail::HandlerWrapperBase>;
153 
154  public:
155 
157  m_impl(impl_ptr(new osmium::handler::detail::HandlerWrapperBase)) {
158  }
159 
160  template <typename THandler, typename... TArgs>
161  void set(TArgs&&... args) {
162  m_impl = impl_ptr(new osmium::handler::detail::HandlerWrapper<THandler>(std::forward<TArgs>(args)...));
163  }
164 
165  void node(const osmium::Node& node) {
166  m_impl->node(node);
167  }
168 
169  void way(const osmium::Way& way) {
170  m_impl->way(way);
171  }
172 
173  void relation(const osmium::Relation& relation) {
174  m_impl->relation(relation);
175  }
176 
177  void area(const osmium::Area& area) {
178  m_impl->area(area);
179  }
180 
181  void changeset(const osmium::Changeset& changeset) {
182  m_impl->changeset(changeset);
183  }
184 
185  void flush() {
186  m_impl->flush();
187  }
188 
189  }; // class DynamicHandler
190 
191  } // namespace handler
192 
193 } // namespace osmium
194 
195 #endif // OSMIUM_DYNAMIC_HANDLER_HPP
std::unique_ptr< osmium::handler::detail::HandlerWrapperBase > impl_ptr
Definition: dynamic_handler.hpp:151
void relation(const osmium::Relation &relation)
Definition: dynamic_handler.hpp:173
Definition: relation.hpp:163
Definition: area.hpp:117
Definition: handler.hpp:71
Definition: dynamic_handler.hpp:149
void changeset(const osmium::Changeset &changeset)
Definition: dynamic_handler.hpp:181
Definition: way.hpp:67
#define OSMIUM_DYNAMIC_HANDLER_DISPATCH(_name_, _type_)
Definition: dynamic_handler.hpp:85
void node(const osmium::Node &node)
Definition: dynamic_handler.hpp:165
Namespace for everything in the Osmium library.
Definition: assembler.hpp:73
Definition: attr.hpp:333
void area(const osmium::Area &area)
Definition: dynamic_handler.hpp:177
impl_ptr m_impl
Definition: dynamic_handler.hpp:152
void flush()
Definition: dynamic_handler.hpp:185
Definition: node.hpp:48
DynamicHandler()
Definition: dynamic_handler.hpp:156
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:169