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