Libosmium  2.6.1
Fast and flexible C++ library for working with OpenStreetMap data
visitor.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_VISITOR_HPP
2 #define OSMIUM_VISITOR_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 <type_traits>
37 
38 #include <osmium/fwd.hpp>
39 #include <osmium/io/reader_iterator.hpp> // IWYU pragma: keep
40 #include <osmium/memory/buffer.hpp>
41 #include <osmium/osm.hpp>
42 #include <osmium/osm/entity.hpp>
43 #include <osmium/osm/item_type.hpp>
44 
45 namespace osmium {
46 
47  namespace memory {
48  class Item;
49  } // namespace memory
50 
51  namespace detail {
52 
53  template <typename T, typename U>
54  using ConstIfConst = typename std::conditional<std::is_const<T>::value, typename std::add_const<U>::type, U>::type;
55 
56  template <typename THandler, typename TItem>
57  inline void apply_item_recurse(TItem& item, THandler& handler) {
58  switch (item.type()) {
60  break;
62  handler.osm_object(static_cast<ConstIfConst<TItem, osmium::OSMObject>&>(item));
63  handler.node(static_cast<ConstIfConst<TItem, osmium::Node>&>(item));
64  break;
66  handler.osm_object(static_cast<ConstIfConst<TItem, osmium::OSMObject>&>(item));
67  handler.way(static_cast<ConstIfConst<TItem, osmium::Way>&>(item));
68  break;
70  handler.osm_object(static_cast<ConstIfConst<TItem, osmium::OSMObject>&>(item));
71  handler.relation(static_cast<ConstIfConst<TItem, osmium::Relation>&>(item));
72  break;
74  handler.osm_object(static_cast<ConstIfConst<TItem, osmium::OSMObject>&>(item));
75  handler.area(static_cast<ConstIfConst<TItem, osmium::Area>&>(item));
76  break;
78  handler.changeset(static_cast<ConstIfConst<TItem, osmium::Changeset>&>(item));
79  break;
81  handler.tag_list(static_cast<ConstIfConst<TItem, osmium::TagList>&>(item));
82  break;
84  handler.way_node_list(static_cast<ConstIfConst<TItem, osmium::WayNodeList>&>(item));
85  break;
88  handler.relation_member_list(static_cast<ConstIfConst<TItem, osmium::RelationMemberList>&>(item));
89  break;
91  handler.outer_ring(static_cast<ConstIfConst<TItem, osmium::OuterRing>&>(item));
92  break;
94  handler.inner_ring(static_cast<ConstIfConst<TItem, osmium::InnerRing>&>(item));
95  break;
97  handler.changeset_discussion(static_cast<ConstIfConst<TItem, osmium::ChangesetDiscussion>&>(item));
98  break;
99  }
100  }
101 
102  template <typename THandler>
103  inline void apply_item_recurse(const osmium::OSMEntity& item, THandler& handler) {
104  switch (item.type()) {
106  handler.osm_object(static_cast<const osmium::OSMObject&>(item));
107  handler.node(static_cast<const osmium::Node&>(item));
108  break;
110  handler.osm_object(static_cast<const osmium::OSMObject&>(item));
111  handler.way(static_cast<const osmium::Way&>(item));
112  break;
114  handler.osm_object(static_cast<const osmium::OSMObject&>(item));
115  handler.relation(static_cast<const osmium::Relation&>(item));
116  break;
118  handler.osm_object(static_cast<const osmium::OSMObject&>(item));
119  handler.area(static_cast<const osmium::Area&>(item));
120  break;
122  handler.changeset(static_cast<const osmium::Changeset&>(item));
123  break;
124  default:
125  throw osmium::unknown_type();
126  }
127  }
128 
129  template <typename THandler>
130  inline void apply_item_recurse(osmium::OSMEntity& item, THandler& handler) {
131  switch (item.type()) {
133  handler.osm_object(static_cast<osmium::OSMObject&>(item));
134  handler.node(static_cast<osmium::Node&>(item));
135  break;
137  handler.osm_object(static_cast<osmium::OSMObject&>(item));
138  handler.way(static_cast<osmium::Way&>(item));
139  break;
141  handler.osm_object(static_cast<osmium::OSMObject&>(item));
142  handler.relation(static_cast<osmium::Relation&>(item));
143  break;
145  handler.osm_object(static_cast<osmium::OSMObject&>(item));
146  handler.area(static_cast<osmium::Area&>(item));
147  break;
149  handler.changeset(static_cast<osmium::Changeset&>(item));
150  break;
151  default:
152  throw osmium::unknown_type();
153  }
154  }
155 
156  template <typename THandler>
157  inline void apply_item_recurse(const osmium::OSMObject& item, THandler& handler) {
158  switch (item.type()) {
160  handler.osm_object(item);
161  handler.node(static_cast<const osmium::Node&>(item));
162  break;
164  handler.osm_object(item);
165  handler.way(static_cast<const osmium::Way&>(item));
166  break;
168  handler.osm_object(item);
169  handler.relation(static_cast<const osmium::Relation&>(item));
170  break;
172  handler.osm_object(item);
173  handler.area(static_cast<const osmium::Area&>(item));
174  break;
175  default:
176  throw osmium::unknown_type();
177  }
178  }
179 
180  template <typename THandler>
181  inline void apply_item_recurse(osmium::OSMObject& item, THandler& handler) {
182  switch (item.type()) {
184  handler.osm_object(item);
185  handler.node(static_cast<osmium::Node&>(item));
186  break;
188  handler.osm_object(item);
189  handler.way(static_cast<osmium::Way&>(item));
190  break;
192  handler.osm_object(item);
193  handler.relation(static_cast<osmium::Relation&>(item));
194  break;
196  handler.osm_object(item);
197  handler.area(static_cast<osmium::Area&>(item));
198  break;
199  default:
200  throw osmium::unknown_type();
201  }
202  }
203 
204  template <typename THandler, typename TItem, typename... TRest>
205  inline void apply_item_recurse(TItem& item, THandler& handler, TRest&... more) {
206  apply_item_recurse(item, handler);
207  apply_item_recurse(item, more...);
208  }
209 
210  template <typename THandler>
211  inline void flush_recurse(THandler& handler) {
212  handler.flush();
213  }
214 
215  template <typename THandler, typename... TRest>
216  inline void flush_recurse(THandler& handler, TRest&... more) {
217  flush_recurse(handler);
218  flush_recurse(more...);
219  }
220 
221  } // namespace detail
222 
223  template <typename... THandlers>
224  inline void apply_item(const osmium::memory::Item& item, THandlers&... handlers) {
225  detail::apply_item_recurse(item, handlers...);
226  }
227 
228  template <typename... THandlers>
229  inline void apply_item(osmium::memory::Item& item, THandlers&... handlers) {
230  detail::apply_item_recurse(item, handlers...);
231  }
232 
233  template <typename TIterator, typename... THandlers>
234  inline void apply(TIterator it, TIterator end, THandlers&... handlers) {
235  for (; it != end; ++it) {
236  detail::apply_item_recurse(*it, handlers...);
237  }
238  detail::flush_recurse(handlers...);
239  }
240 
241  template <typename TContainer, typename... THandlers>
242  inline void apply(TContainer& c, THandlers&... handlers) {
243  apply(std::begin(c), std::end(c), handlers...);
244  }
245 
246  template <typename... THandlers>
247  inline void apply(const osmium::memory::Buffer& buffer, THandlers&... handlers) {
248  apply(buffer.cbegin(), buffer.cend(), handlers...);
249  }
250 
251 } // namespace osmium
252 
253 #endif // OSMIUM_VISITOR_HPP
t_const_iterator< T > cend() const
Definition: buffer.hpp:626
Definition: item_type.hpp:198
type
Definition: entity_bits.hpp:60
OSMEntity is the abstract base class for the OSMObject and Changeset classes.
Definition: entity.hpp:64
void apply(TIterator it, TIterator end, THandlers &...handlers)
Definition: visitor.hpp:234
void apply_item(const osmium::memory::Item &item, THandlers &...handlers)
Definition: visitor.hpp:224
Definition: item.hpp:97
Namespace for everything in the Osmium library.
Definition: assembler.hpp:59
Definition: attr.hpp:298
osmium::io::InputIterator< osmium::io::Reader > end(osmium::io::Reader &)
Definition: reader_iterator.hpp:45
Definition: buffer.hpp:97
t_const_iterator< T > cbegin() const
Definition: buffer.hpp:604
osmium::io::InputIterator< osmium::io::Reader > begin(osmium::io::Reader &reader)
Definition: reader_iterator.hpp:41
item_type type() const noexcept
Definition: item.hpp:155
Definition: object.hpp:58