Libosmium  2.20.0
Fast and flexible C++ library for working with OpenStreetMap data
item_iterator.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_MEMORY_ITEM_ITERATOR_HPP
2 #define OSMIUM_MEMORY_ITEM_ITERATOR_HPP
3 
4 /*
5 
6 This file is part of Osmium (https://osmcode.org/libosmium).
7 
8 Copyright 2013-2023 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/memory/item.hpp>
37 #include <osmium/osm/item_type.hpp>
38 
39 #include <cassert>
40 #include <cstddef>
41 #include <iosfwd>
42 #include <iterator>
43 #include <type_traits>
44 
45 namespace osmium {
46 
47  namespace memory {
48 
49  namespace detail {
50 
51  template <typename T>
52  constexpr inline bool type_is_compatible(const osmium::item_type t) noexcept {
53  return T::is_compatible_to(t);
54  }
55 
56  } // namespace detail
57 
58  template <typename TMember>
59  class ItemIterator {
60 
61 
62  // This data_type is either 'unsigned char*' or 'const unsigned char*' depending
63  // on whether TMember is const. This allows this class to be used as an iterator and
64  // as a const_iterator.
65  using data_type = typename std::conditional<std::is_const<TMember>::value, const unsigned char*, unsigned char*>::type;
66 
69 
71  while (m_data != m_end &&
72  !detail::type_is_compatible<TMember>(reinterpret_cast<const osmium::memory::Item*>(m_data)->type())) {
73  m_data = reinterpret_cast<TMember*>(m_data)->next();
74  }
75  }
76 
77  public:
78 
79  using iterator_category = std::forward_iterator_tag;
80  using value_type = TMember;
81  using difference_type = std::ptrdiff_t;
82  using pointer = value_type*;
84 
85  ItemIterator() noexcept :
86  m_data(nullptr),
87  m_end(nullptr) {
88  }
89 
91  m_data(data),
92  m_end(end) {
94  }
95 
96  template <typename T>
97  ItemIterator<T> cast() const noexcept {
98  return ItemIterator<T>(m_data, m_end);
99  }
100 
102  assert(m_data);
103  assert(m_data != m_end);
104  m_data = reinterpret_cast<TMember*>(m_data)->next();
106  return *static_cast<ItemIterator<TMember>*>(this);
107  }
108 
115  assert(m_data);
116  assert(m_data != m_end);
117  m_data = reinterpret_cast<TMember*>(m_data)->next();
118  return *static_cast<ItemIterator<TMember>*>(this);
119  }
120 
122  ItemIterator<TMember> tmp{*this};
123  operator++();
124  return tmp;
125  }
126 
127  bool operator==(const ItemIterator<TMember>& rhs) const noexcept {
128  return m_data == rhs.m_data && m_end == rhs.m_end;
129  }
130 
131  bool operator!=(const ItemIterator<TMember>& rhs) const noexcept {
132  return !(*this == rhs);
133  }
134 
135  data_type data() noexcept {
136  assert(m_data);
137  return m_data;
138  }
139 
140  const unsigned char* data() const noexcept {
141  assert(m_data);
142  return m_data;
143  }
144 
145  TMember& operator*() const noexcept {
146  assert(m_data);
147  assert(m_data != m_end);
148  return *reinterpret_cast<TMember*>(m_data);
149  }
150 
151  TMember* operator->() const noexcept {
152  assert(m_data);
153  assert(m_data != m_end);
154  return reinterpret_cast<TMember*>(m_data);
155  }
156 
157  explicit operator bool() const noexcept {
158  return (m_data != nullptr) && (m_data != m_end);
159  }
160 
161  template <typename TChar, typename TTraits>
162  void print(std::basic_ostream<TChar, TTraits>& out) const {
163  out << static_cast<const void*>(m_data);
164  }
165 
166  }; // class ItemIterator
167 
168  template <typename TChar, typename TTraits, typename TMember>
169  inline std::basic_ostream<TChar, TTraits>& operator<<(std::basic_ostream<TChar, TTraits>& out, const ItemIterator<TMember>& iter) {
170  iter.print(out);
171  return out;
172  }
173 
174  template <typename T>
176 
177 
178  // This data_type is either 'unsigned char*' or
179  // 'const unsigned char*' depending on whether T is const.
180  using data_type = typename std::conditional<std::is_const<T>::value, const unsigned char*, unsigned char*>::type;
181 
184 
185  public:
186 
189 
190  ItemIteratorRange(data_type first, data_type last) noexcept :
191  m_begin(first),
192  m_end(last) {
193  }
194 
195  iterator begin() noexcept {
196  return iterator{m_begin, m_end};
197  }
198 
199  iterator end() noexcept {
200  return iterator{m_end, m_end};
201  }
202 
203  const_iterator cbegin() const noexcept {
204  return const_iterator{m_begin, m_end};
205  }
206 
207  const_iterator cend() const noexcept {
208  return const_iterator{m_end, m_end};
209  }
210 
211  const_iterator begin() const noexcept {
212  return cbegin();
213  }
214 
215  const_iterator end() const noexcept {
216  return cend();
217  }
218 
224  std::size_t size() const noexcept {
225  if (m_begin == m_end) {
226  return 0;
227  }
228  return std::distance(cbegin(), cend());
229  }
230 
236  bool empty() const noexcept {
237  return size() == 0;
238  }
239 
240  }; // class ItemIteratorRange
241 
242  } // namespace memory
243 
244 } // namespace osmium
245 
246 #endif // OSMIUM_MEMORY_ITEM_ITERATOR_HPP
Definition: item_iterator.hpp:175
std::size_t size() const noexcept
Definition: item_iterator.hpp:224
bool empty() const noexcept
Definition: item_iterator.hpp:236
const_iterator end() const noexcept
Definition: item_iterator.hpp:215
iterator end() noexcept
Definition: item_iterator.hpp:199
const_iterator begin() const noexcept
Definition: item_iterator.hpp:211
const_iterator cbegin() const noexcept
Definition: item_iterator.hpp:203
data_type m_end
Definition: item_iterator.hpp:183
data_type m_begin
Definition: item_iterator.hpp:182
ItemIteratorRange(data_type first, data_type last) noexcept
Definition: item_iterator.hpp:190
iterator begin() noexcept
Definition: item_iterator.hpp:195
typename std::conditional< std::is_const< T >::value, const unsigned char *, unsigned char * >::type data_type
Definition: item_iterator.hpp:180
const_iterator cend() const noexcept
Definition: item_iterator.hpp:207
Definition: item_iterator.hpp:59
std::ptrdiff_t difference_type
Definition: item_iterator.hpp:81
TMember * operator->() const noexcept
Definition: item_iterator.hpp:151
ItemIterator< TMember > operator++(int) noexcept
Definition: item_iterator.hpp:121
ItemIterator() noexcept
Definition: item_iterator.hpp:85
bool operator==(const ItemIterator< TMember > &rhs) const noexcept
Definition: item_iterator.hpp:127
data_type m_end
Definition: item_iterator.hpp:68
ItemIterator(data_type data, data_type end) noexcept
Definition: item_iterator.hpp:90
bool operator!=(const ItemIterator< TMember > &rhs) const noexcept
Definition: item_iterator.hpp:131
value_type & reference
Definition: item_iterator.hpp:83
ItemIterator< TMember > & advance_once() noexcept
Definition: item_iterator.hpp:114
TMember value_type
Definition: item_iterator.hpp:80
void print(std::basic_ostream< TChar, TTraits > &out) const
Definition: item_iterator.hpp:162
typename std::conditional< std::is_const< TMember >::value, const unsigned char *, unsigned char * >::type data_type
Definition: item_iterator.hpp:65
ItemIterator< T > cast() const noexcept
Definition: item_iterator.hpp:97
data_type m_data
Definition: item_iterator.hpp:67
TMember & operator*() const noexcept
Definition: item_iterator.hpp:145
value_type * pointer
Definition: item_iterator.hpp:82
void advance_to_next_item_of_right_type() noexcept
Definition: item_iterator.hpp:70
const unsigned char * data() const noexcept
Definition: item_iterator.hpp:140
ItemIterator< TMember > & operator++() noexcept
Definition: item_iterator.hpp:101
std::forward_iterator_tag iterator_category
Definition: item_iterator.hpp:79
data_type data() noexcept
Definition: item_iterator.hpp:135
Definition: item.hpp:105
Definition: attr.hpp:342
double distance(const osmium::geom::Coordinates &c1, const osmium::geom::Coordinates &c2) noexcept
Definition: haversine.hpp:66
InputIterator< Reader > end(Reader &)
Definition: reader_iterator.hpp:47
std::basic_ostream< TChar, TTraits > & operator<<(std::basic_ostream< TChar, TTraits > &out, const CollectionIterator< TMember > &iter)
Definition: collection.hpp:111
type
Definition: entity_bits.hpp:63
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
item_type
Definition: item_type.hpp:45