Libosmium  2.1.0
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-2015 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/io/reader_iterator.hpp> // IWYU pragma: keep
39 #include <osmium/memory/buffer.hpp>
40 #include <osmium/osm.hpp>
41 #include <osmium/osm/entity.hpp>
42 #include <osmium/osm/item_type.hpp>
43 
44 namespace osmium {
45 
46  class TagList;
47  class WayNodeList;
48  class RelationMemberList;
49  class OuterRing;
50  class InnerRing;
51 
52  namespace memory {
53  class Item;
54  }
55 
56  namespace detail {
57 
58  template <typename T, typename U>
59  using ConstIfConst = typename std::conditional<std::is_const<T>::value, typename std::add_const<U>::type, U>::type;
60 
61  template <class THandler, class TItem>
62  inline void apply_item_recurse(TItem& item, THandler& handler) {
63  switch (item.type()) {
65  break;
67  handler.osm_object(static_cast<ConstIfConst<TItem, osmium::OSMObject>&>(item));
68  handler.node(static_cast<ConstIfConst<TItem, osmium::Node>&>(item));
69  break;
71  handler.osm_object(static_cast<ConstIfConst<TItem, osmium::OSMObject>&>(item));
72  handler.way(static_cast<ConstIfConst<TItem, osmium::Way>&>(item));
73  break;
75  handler.osm_object(static_cast<ConstIfConst<TItem, osmium::OSMObject>&>(item));
76  handler.relation(static_cast<ConstIfConst<TItem, osmium::Relation>&>(item));
77  break;
79  handler.osm_object(static_cast<ConstIfConst<TItem, osmium::OSMObject>&>(item));
80  handler.area(static_cast<ConstIfConst<TItem, osmium::Area>&>(item));
81  break;
83  handler.changeset(static_cast<ConstIfConst<TItem, osmium::Changeset>&>(item));
84  break;
86  handler.tag_list(static_cast<ConstIfConst<TItem, osmium::TagList>&>(item));
87  break;
89  handler.way_node_list(static_cast<ConstIfConst<TItem, osmium::WayNodeList>&>(item));
90  break;
93  handler.relation_member_list(static_cast<ConstIfConst<TItem, osmium::RelationMemberList>&>(item));
94  break;
96  handler.outer_ring(static_cast<ConstIfConst<TItem, osmium::OuterRing>&>(item));
97  break;
99  handler.inner_ring(static_cast<ConstIfConst<TItem, osmium::InnerRing>&>(item));
100  break;
101  }
102  }
103 
104  template <class THandler>
105  inline void apply_item_recurse(const osmium::OSMEntity& item, THandler& handler) {
106  switch (item.type()) {
108  handler.osm_object(static_cast<const osmium::OSMObject&>(item));
109  handler.node(static_cast<const osmium::Node&>(item));
110  break;
112  handler.osm_object(static_cast<const osmium::OSMObject&>(item));
113  handler.way(static_cast<const osmium::Way&>(item));
114  break;
116  handler.osm_object(static_cast<const osmium::OSMObject&>(item));
117  handler.relation(static_cast<const osmium::Relation&>(item));
118  break;
120  handler.osm_object(static_cast<const osmium::OSMObject&>(item));
121  handler.area(static_cast<const osmium::Area&>(item));
122  break;
124  handler.changeset(static_cast<const osmium::Changeset&>(item));
125  break;
126  default:
127  throw osmium::unknown_type();
128  }
129  }
130 
131  template <class THandler>
132  inline void apply_item_recurse(osmium::OSMEntity& item, THandler& handler) {
133  switch (item.type()) {
135  handler.osm_object(static_cast<osmium::OSMObject&>(item));
136  handler.node(static_cast<osmium::Node&>(item));
137  break;
139  handler.osm_object(static_cast<osmium::OSMObject&>(item));
140  handler.way(static_cast<osmium::Way&>(item));
141  break;
143  handler.osm_object(static_cast<osmium::OSMObject&>(item));
144  handler.relation(static_cast<osmium::Relation&>(item));
145  break;
147  handler.osm_object(static_cast<osmium::OSMObject&>(item));
148  handler.area(static_cast<osmium::Area&>(item));
149  break;
151  handler.changeset(static_cast<osmium::Changeset&>(item));
152  break;
153  default:
154  throw osmium::unknown_type();
155  }
156  }
157 
158  template <class THandler>
159  inline void apply_item_recurse(const osmium::OSMObject& item, THandler& handler) {
160  switch (item.type()) {
162  handler.osm_object(item);
163  handler.node(static_cast<const osmium::Node&>(item));
164  break;
166  handler.osm_object(item);
167  handler.way(static_cast<const osmium::Way&>(item));
168  break;
170  handler.osm_object(item);
171  handler.relation(static_cast<const osmium::Relation&>(item));
172  break;
174  handler.osm_object(item);
175  handler.area(static_cast<const osmium::Area&>(item));
176  break;
177  default:
178  throw osmium::unknown_type();
179  }
180  }
181 
182  template <class THandler>
183  inline void apply_item_recurse(osmium::OSMObject& item, THandler& handler) {
184  switch (item.type()) {
186  handler.osm_object(item);
187  handler.node(static_cast<osmium::Node&>(item));
188  break;
190  handler.osm_object(item);
191  handler.way(static_cast<osmium::Way&>(item));
192  break;
194  handler.osm_object(item);
195  handler.relation(static_cast<osmium::Relation&>(item));
196  break;
198  handler.osm_object(item);
199  handler.area(static_cast<osmium::Area&>(item));
200  break;
201  default:
202  throw osmium::unknown_type();
203  }
204  }
205 
206  template <class THandler, class TItem, class ...TRest>
207  inline void apply_item_recurse(TItem& item, THandler& handler, TRest&... more) {
208  apply_item_recurse(item, handler);
209  apply_item_recurse(item, more...);
210  }
211 
212  template <class THandler>
213  inline void flush_recurse(THandler& handler) {
214  handler.flush();
215  }
216 
217  template <class THandler, class ...TRest>
218  inline void flush_recurse(THandler& handler, TRest&... more) {
219  flush_recurse(handler);
220  flush_recurse(more...);
221  }
222 
223  } // namespace detail
224 
225  template <class ...THandlers>
226  inline void apply_item(const osmium::memory::Item& item, THandlers&... handlers) {
227  detail::apply_item_recurse(item, handlers...);
228  }
229 
230  template <class ...THandlers>
231  inline void apply_item(osmium::memory::Item& item, THandlers&... handlers) {
232  detail::apply_item_recurse(item, handlers...);
233  }
234 
235  template <class TIterator, class ...THandlers>
236  inline void apply(TIterator it, TIterator end, THandlers&... handlers) {
237  for (; it != end; ++it) {
238  detail::apply_item_recurse(*it, handlers...);
239  }
240  detail::flush_recurse(handlers...);
241  }
242 
243  template <class TContainer, class ...THandlers>
244  inline void apply(TContainer& c, THandlers&... handlers) {
245  apply(std::begin(c), std::end(c), handlers...);
246  }
247 
248  template <class ...THandlers>
249  inline void apply(const osmium::memory::Buffer& buffer, THandlers&... handlers) {
250  apply(buffer.cbegin(), buffer.cend(), handlers...);
251  }
252 
253 } // namespace osmium
254 
255 #endif // OSMIUM_VISITOR_HPP
t_const_iterator< T > cend() const
Definition: buffer.hpp:435
Definition: item_type.hpp:163
type
Definition: entity_bits.hpp:60
OSMEntity is the abstract base class for the OSMObject and Changeset classes.
Definition: entity.hpp:63
void apply(TIterator it, TIterator end, THandlers &...handlers)
Definition: visitor.hpp:236
void apply_item(const osmium::memory::Item &item, THandlers &...handlers)
Definition: visitor.hpp:226
Definition: item.hpp:98
Namespace for everything in the Osmium library.
Definition: assembler.hpp:55
osmium::io::InputIterator< osmium::io::Reader > end(osmium::io::Reader &)
Definition: reader_iterator.hpp:45
Definition: buffer.hpp:95
t_const_iterator< T > cbegin() const
Definition: buffer.hpp:426
osmium::io::InputIterator< osmium::io::Reader > begin(osmium::io::Reader &reader)
Definition: reader_iterator.hpp:41
item_type type() const noexcept
Definition: item.hpp:156
Definition: object.hpp:57