Libosmium  2.15.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 (https://osmcode.org/libosmium).
7 
8 Copyright 2013-2018 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 <osmium/fwd.hpp>
37 #include <osmium/io/reader_iterator.hpp> // IWYU pragma: keep
38 #include <osmium/memory/buffer.hpp>
39 #include <osmium/osm.hpp>
40 #include <osmium/osm/entity.hpp>
41 #include <osmium/osm/item_type.hpp>
42 
43 #include <type_traits>
44 #include <utility>
45 
46 namespace osmium {
47 
48  namespace detail {
49 
50  template <typename T, typename U>
51  using ConstIfConst = typename std::conditional<std::is_const<T>::value, typename std::add_const<U>::type, U>::type;
52 
53  template <typename THandler, typename TItem>
54  inline void apply_item_impl(TItem& item, THandler&& handler) {
55  switch (item.type()) {
57  break;
59  std::forward<THandler>(handler).osm_object(static_cast<ConstIfConst<TItem, osmium::OSMObject>&>(item));
60  std::forward<THandler>(handler).node(static_cast<ConstIfConst<TItem, osmium::Node>&>(item));
61  break;
63  std::forward<THandler>(handler).osm_object(static_cast<ConstIfConst<TItem, osmium::OSMObject>&>(item));
64  std::forward<THandler>(handler).way(static_cast<ConstIfConst<TItem, osmium::Way>&>(item));
65  break;
67  std::forward<THandler>(handler).osm_object(static_cast<ConstIfConst<TItem, osmium::OSMObject>&>(item));
68  std::forward<THandler>(handler).relation(static_cast<ConstIfConst<TItem, osmium::Relation>&>(item));
69  break;
71  std::forward<THandler>(handler).osm_object(static_cast<ConstIfConst<TItem, osmium::OSMObject>&>(item));
72  std::forward<THandler>(handler).area(static_cast<ConstIfConst<TItem, osmium::Area>&>(item));
73  break;
75  std::forward<THandler>(handler).changeset(static_cast<ConstIfConst<TItem, osmium::Changeset>&>(item));
76  break;
78  std::forward<THandler>(handler).tag_list(static_cast<ConstIfConst<TItem, osmium::TagList>&>(item));
79  break;
81  std::forward<THandler>(handler).way_node_list(static_cast<ConstIfConst<TItem, osmium::WayNodeList>&>(item));
82  break;
85  std::forward<THandler>(handler).relation_member_list(static_cast<ConstIfConst<TItem, osmium::RelationMemberList>&>(item));
86  break;
88  std::forward<THandler>(handler).outer_ring(static_cast<ConstIfConst<TItem, osmium::OuterRing>&>(item));
89  break;
91  std::forward<THandler>(handler).inner_ring(static_cast<ConstIfConst<TItem, osmium::InnerRing>&>(item));
92  break;
94  std::forward<THandler>(handler).changeset_discussion(static_cast<ConstIfConst<TItem, osmium::ChangesetDiscussion>&>(item));
95  break;
96  }
97  }
98 
99  template <typename THandler>
100  inline void apply_item_impl(const osmium::OSMEntity& item, THandler&& handler) {
101  switch (item.type()) {
103  std::forward<THandler>(handler).osm_object(static_cast<const osmium::OSMObject&>(item));
104  std::forward<THandler>(handler).node(static_cast<const osmium::Node&>(item));
105  break;
107  std::forward<THandler>(handler).osm_object(static_cast<const osmium::OSMObject&>(item));
108  std::forward<THandler>(handler).way(static_cast<const osmium::Way&>(item));
109  break;
111  std::forward<THandler>(handler).osm_object(static_cast<const osmium::OSMObject&>(item));
112  std::forward<THandler>(handler).relation(static_cast<const osmium::Relation&>(item));
113  break;
115  std::forward<THandler>(handler).osm_object(static_cast<const osmium::OSMObject&>(item));
116  std::forward<THandler>(handler).area(static_cast<const osmium::Area&>(item));
117  break;
119  std::forward<THandler>(handler).changeset(static_cast<const osmium::Changeset&>(item));
120  break;
121  default:
122  throw osmium::unknown_type{};
123  }
124  }
125 
126  template <typename THandler>
127  inline void apply_item_impl(osmium::OSMEntity& item, THandler&& handler) {
128  switch (item.type()) {
130  std::forward<THandler>(handler).osm_object(static_cast<osmium::OSMObject&>(item));
131  std::forward<THandler>(handler).node(static_cast<osmium::Node&>(item));
132  break;
134  std::forward<THandler>(handler).osm_object(static_cast<osmium::OSMObject&>(item));
135  std::forward<THandler>(handler).way(static_cast<osmium::Way&>(item));
136  break;
138  std::forward<THandler>(handler).osm_object(static_cast<osmium::OSMObject&>(item));
139  std::forward<THandler>(handler).relation(static_cast<osmium::Relation&>(item));
140  break;
142  std::forward<THandler>(handler).osm_object(static_cast<osmium::OSMObject&>(item));
143  std::forward<THandler>(handler).area(static_cast<osmium::Area&>(item));
144  break;
146  std::forward<THandler>(handler).changeset(static_cast<osmium::Changeset&>(item));
147  break;
148  default:
149  throw osmium::unknown_type{};
150  }
151  }
152 
153  template <typename THandler>
154  inline void apply_item_impl(const osmium::OSMObject& item, THandler&& handler) {
155  switch (item.type()) {
157  std::forward<THandler>(handler).osm_object(item);
158  std::forward<THandler>(handler).node(static_cast<const osmium::Node&>(item));
159  break;
161  std::forward<THandler>(handler).osm_object(item);
162  std::forward<THandler>(handler).way(static_cast<const osmium::Way&>(item));
163  break;
165  std::forward<THandler>(handler).osm_object(item);
166  std::forward<THandler>(handler).relation(static_cast<const osmium::Relation&>(item));
167  break;
169  std::forward<THandler>(handler).osm_object(item);
170  std::forward<THandler>(handler).area(static_cast<const osmium::Area&>(item));
171  break;
172  default:
173  throw osmium::unknown_type{};
174  }
175  }
176 
177  template <typename THandler>
178  inline void apply_item_impl(osmium::OSMObject& item, THandler&& handler) {
179  switch (item.type()) {
181  std::forward<THandler>(handler).osm_object(item);
182  std::forward<THandler>(handler).node(static_cast<osmium::Node&>(item));
183  break;
185  std::forward<THandler>(handler).osm_object(item);
186  std::forward<THandler>(handler).way(static_cast<osmium::Way&>(item));
187  break;
189  std::forward<THandler>(handler).osm_object(item);
190  std::forward<THandler>(handler).relation(static_cast<osmium::Relation&>(item));
191  break;
193  std::forward<THandler>(handler).osm_object(item);
194  std::forward<THandler>(handler).area(static_cast<osmium::Area&>(item));
195  break;
196  default:
197  throw osmium::unknown_type{};
198  }
199  }
200 
201  } // namespace detail
202 
203  template <typename TItem, typename... THandlers>
204  inline void apply_item(TItem& item, THandlers&&... handlers) {
205  (void)std::initializer_list<int>{
206  (detail::apply_item_impl(item, std::forward<THandlers>(handlers)), 0)...
207  };
208  }
209 
210  template <typename... THandlers>
211  inline void apply_flush(THandlers&&... handlers) {
212  (void)std::initializer_list<int>{
213  (std::forward<THandlers>(handlers).flush(), 0)...
214  };
215  }
216 
217  template <typename TIterator, typename... THandlers>
218  inline void apply(TIterator it, TIterator end, THandlers&&... handlers) {
219  for (; it != end; ++it) {
220  apply_item(*it, std::forward<THandlers>(handlers)...);
221  }
222  apply_flush(std::forward<THandlers>(handlers)...);
223  }
224 
225  template <typename TContainer, typename... THandlers>
226  inline void apply(TContainer& c, THandlers&&... handlers) {
227  using std::begin;
228  using std::end;
229  apply(begin(c), end(c), std::forward<THandlers>(handlers)...);
230  }
231 
232  template <typename... THandlers>
233  inline void apply(const osmium::memory::Buffer& buffer, THandlers&&... handlers) {
234  apply(buffer.cbegin(), buffer.cend(), std::forward<THandlers>(handlers)...);
235  }
236 
237 } // namespace osmium
238 
239 #endif // OSMIUM_VISITOR_HPP
Definition: item_type.hpp:195
type
Definition: entity_bits.hpp:63
t_const_iterator< T > cend() const
Definition: buffer.hpp:784
InputIterator< Reader > end(Reader &)
Definition: reader_iterator.hpp:47
OSMEntity is the abstract base class for the OSMObject and Changeset classes.
Definition: entity.hpp:64
void apply_item(TItem &item, THandlers &&... handlers)
Definition: visitor.hpp:204
InputIterator< Reader > begin(Reader &reader)
Definition: reader_iterator.hpp:43
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
Definition: attr.hpp:333
void apply(TIterator it, TIterator end, THandlers &&... handlers)
Definition: visitor.hpp:218
void apply_flush(THandlers &&... handlers)
Definition: visitor.hpp:211
Definition: buffer.hpp:97
t_const_iterator< T > cbegin() const
Definition: buffer.hpp:762
item_type type() const noexcept
Definition: item.hpp:171
Definition: object.hpp:64