Libosmium  2.9.0
Fast and flexible C++ library for working with OpenStreetMap data
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
item_iterator.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_ITEM_ITERATOR_HPP
2 #define OSMIUM_ITEM_ITERATOR_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 <cassert>
37 #include <cstddef>
38 #include <iterator>
39 #include <iosfwd>
40 #include <type_traits>
41 
42 #include <osmium/memory/item.hpp>
43 #include <osmium/osm/item_type.hpp>
44 
45 namespace osmium {
46 
47  class Area;
48  class Changeset;
49  class InnerRing;
50  class Node;
51  class OSMEntity;
52  class OSMObject;
53  class OuterRing;
54  class Relation;
55  class RelationMemberList;
56  class TagList;
57  class Way;
58  class WayNodeList;
59 
60  namespace memory {
61 
62  namespace detail {
63 
64  template <typename T>
65  inline bool type_is_compatible(osmium::item_type) noexcept {
66  return true;
67  }
68 
69  template <>
70  inline bool type_is_compatible<osmium::Node>(osmium::item_type t) noexcept {
71  return t == osmium::item_type::node;
72  }
73 
74  template <>
75  inline bool type_is_compatible<osmium::Way>(osmium::item_type t) noexcept {
76  return t == osmium::item_type::way;
77  }
78 
79  template <>
80  inline bool type_is_compatible<osmium::Relation>(osmium::item_type t) noexcept {
81  return t == osmium::item_type::relation;
82  }
83 
84  template <>
85  inline bool type_is_compatible<osmium::Area>(osmium::item_type t) noexcept {
86  return t == osmium::item_type::area;
87  }
88 
89  template <>
90  inline bool type_is_compatible<osmium::Changeset>(osmium::item_type t) noexcept {
91  return t == osmium::item_type::changeset;
92  }
93 
94  template <>
95  inline bool type_is_compatible<osmium::OSMObject>(osmium::item_type t) noexcept {
97  }
98 
99  template <>
100  inline bool type_is_compatible<osmium::OSMEntity>(osmium::item_type t) noexcept {
102  }
103 
104  template <>
105  inline bool type_is_compatible<osmium::TagList>(osmium::item_type t) noexcept {
106  return t == osmium::item_type::tag_list;
107  }
108 
109  template <>
110  inline bool type_is_compatible<osmium::WayNodeList>(osmium::item_type t) noexcept {
112  }
113 
114  template <>
115  inline bool type_is_compatible<osmium::RelationMemberList>(osmium::item_type t) noexcept {
117  }
118 
119  template <>
120  inline bool type_is_compatible<osmium::OuterRing>(osmium::item_type t) noexcept {
121  return t == osmium::item_type::outer_ring;
122  }
123 
124  template <>
125  inline bool type_is_compatible<osmium::InnerRing>(osmium::item_type t) noexcept {
126  return t == osmium::item_type::inner_ring;
127  }
128 
129  } // namespace detail
130 
131  template <typename TMember>
132  class ItemIterator {
133 
134 
135  // This data_type is either 'unsigned char*' or 'const unsigned char*' depending
136  // on whether TMember is const. This allows this class to be used as an iterator and
137  // as a const_iterator.
138  using data_type = typename std::conditional<std::is_const<TMember>::value, const unsigned char*, unsigned char*>::type;
139 
142 
144  while (m_data != m_end &&
145  !detail::type_is_compatible<typename std::remove_const<TMember>::type>(reinterpret_cast<const osmium::memory::Item*>(m_data)->type())) {
146  m_data = reinterpret_cast<TMember*>(m_data)->next();
147  }
148  }
149 
150  public:
151 
152  using iterator_category = std::forward_iterator_tag;
153  using value_type = TMember;
154  using difference_type = std::ptrdiff_t;
155  using pointer = value_type*;
157 
158  ItemIterator() noexcept :
159  m_data(nullptr),
160  m_end(nullptr) {
161  }
162 
164  m_data(data),
165  m_end(end) {
167  }
168 
169  template <typename T>
170  ItemIterator<T> cast() const noexcept {
171  return ItemIterator<T>(m_data, m_end);
172  }
173 
175  assert(m_data);
176  assert(m_data != m_end);
177  m_data = reinterpret_cast<TMember*>(m_data)->next();
179  return *static_cast<ItemIterator<TMember>*>(this);
180  }
181 
188  assert(m_data);
189  assert(m_data != m_end);
190  m_data = reinterpret_cast<TMember*>(m_data)->next();
191  return *static_cast<ItemIterator<TMember>*>(this);
192  }
193 
195  ItemIterator<TMember> tmp(*this);
196  operator++();
197  return tmp;
198  }
199 
200  bool operator==(const ItemIterator<TMember>& rhs) const noexcept {
201  return m_data == rhs.m_data && m_end == rhs.m_end;
202  }
203 
204  bool operator!=(const ItemIterator<TMember>& rhs) const noexcept {
205  return !(*this == rhs);
206  }
207 
208  data_type data() noexcept {
209  assert(m_data);
210  return m_data;
211  }
212 
213  const unsigned char* data() const noexcept {
214  assert(m_data);
215  return m_data;
216  }
217 
218  TMember& operator*() const noexcept {
219  assert(m_data);
220  assert(m_data != m_end);
221  return *reinterpret_cast<TMember*>(m_data);
222  }
223 
224  TMember* operator->() const noexcept {
225  assert(m_data);
226  assert(m_data != m_end);
227  return reinterpret_cast<TMember*>(m_data);
228  }
229 
230  explicit operator bool() const noexcept {
231  return (m_data != nullptr) && (m_data != m_end);
232  }
233 
234  template <typename TChar, typename TTraits>
235  void print(std::basic_ostream<TChar, TTraits>& out) const {
236  out << static_cast<const void*>(m_data);
237  }
238 
239  }; // class ItemIterator
240 
241  template <typename TChar, typename TTraits, typename TMember>
242  inline std::basic_ostream<TChar, TTraits>& operator<<(std::basic_ostream<TChar, TTraits>& out, const ItemIterator<TMember>& iter) {
243  iter.print(out);
244  return out;
245  }
246 
247  template <typename T>
249 
250 
251  // This data_type is either 'unsigned char*' or
252  // 'const unsigned char*' depending on whether T is const.
253  using data_type = typename std::conditional<std::is_const<T>::value, const unsigned char*, unsigned char*>::type;
254 
257 
258  public:
259 
262 
263  ItemIteratorRange(data_type first, data_type last) noexcept :
264  m_begin(first),
265  m_end(last) {
266  }
267 
268  iterator begin() noexcept {
269  return iterator{m_begin, m_end};
270  }
271 
272  iterator end() noexcept {
273  return iterator{m_end, m_end};
274  }
275 
276  const_iterator cbegin() const noexcept {
277  return const_iterator{m_begin, m_end};
278  }
279 
280  const_iterator cend() const noexcept {
281  return const_iterator{m_end, m_end};
282  }
283 
284  const_iterator begin() const noexcept {
285  return cbegin();
286  }
287 
288  const_iterator end() const noexcept {
289  return cend();
290  }
291 
298  size_t size() const {
299  if (m_begin == m_end) {
300  return 0;
301  }
302  return std::distance(cbegin(), cend());
303  }
304 
311  bool empty() const {
312  return size() == 0;
313  }
314 
315  }; // class ItemIteratorRange
316 
317  } // namespace memory
318 
319 } // namespace osmium
320 
321 #endif // OSMIUM_ITEM_ITERATOR_HPP
data_type m_end
Definition: item_iterator.hpp:256
type
Definition: entity_bits.hpp:63
Definition: item_iterator.hpp:248
Definition: item_iterator.hpp:132
ItemIteratorRange(data_type first, data_type last) noexcept
Definition: item_iterator.hpp:263
const_iterator begin() const noexcept
Definition: item_iterator.hpp:284
item_type
Definition: item_type.hpp:43
bool empty() const
Definition: item_iterator.hpp:311
data_type m_begin
Definition: item_iterator.hpp:255
const_iterator end() const noexcept
Definition: item_iterator.hpp:288
ItemIterator< TMember > operator++(int) noexcept
Definition: item_iterator.hpp:194
double distance(const osmium::geom::Coordinates &c1, const osmium::geom::Coordinates &c2)
Definition: haversine.hpp:64
TMember & operator*() const noexcept
Definition: item_iterator.hpp:218
void advance_to_next_item_of_right_type() noexcept
Definition: item_iterator.hpp:143
const unsigned char * data() const noexcept
Definition: item_iterator.hpp:213
value_type & reference
Definition: item_iterator.hpp:156
Namespace for everything in the Osmium library.
Definition: assembler.hpp:73
typename std::conditional< std::is_const< TItem >::value, const unsigned char *, unsigned char * >::type data_type
Definition: item_iterator.hpp:138
Definition: attr.hpp:333
size_t size() const
Definition: item_iterator.hpp:298
ItemIterator(data_type data, data_type end) noexcept
Definition: item_iterator.hpp:163
ItemIterator< TMember > & advance_once() noexcept
Definition: item_iterator.hpp:187
osmium::io::InputIterator< osmium::io::Reader > end(osmium::io::Reader &)
Definition: reader_iterator.hpp:45
bool operator!=(const ItemIterator< TMember > &rhs) const noexcept
Definition: item_iterator.hpp:204
TItem value_type
Definition: item_iterator.hpp:153
ItemIterator() noexcept
Definition: item_iterator.hpp:158
data_type m_end
Definition: item_iterator.hpp:141
iterator end() noexcept
Definition: item_iterator.hpp:272
const_iterator cend() const noexcept
Definition: item_iterator.hpp:280
data_type m_data
Definition: item_iterator.hpp:140
std::forward_iterator_tag iterator_category
Definition: item_iterator.hpp:152
iterator begin() noexcept
Definition: item_iterator.hpp:268
const_iterator cbegin() const noexcept
Definition: item_iterator.hpp:276
std::ptrdiff_t difference_type
Definition: item_iterator.hpp:154
ItemIterator< TMember > & operator++() noexcept
Definition: item_iterator.hpp:174
data_type data() noexcept
Definition: item_iterator.hpp:208
ItemIterator< T > cast() const noexcept
Definition: item_iterator.hpp:170
value_type * pointer
Definition: item_iterator.hpp:155
typename std::conditional< std::is_const< T >::value, const unsigned char *, unsigned char * >::type data_type
Definition: item_iterator.hpp:253
TMember * operator->() const noexcept
Definition: item_iterator.hpp:224
bool operator==(const ItemIterator< TMember > &rhs) const noexcept
Definition: item_iterator.hpp:200
void print(std::basic_ostream< TChar, TTraits > &out) const
Definition: item_iterator.hpp:235