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