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
6This file is part of Osmium (https://osmcode.org/libosmium).
7
8Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
9
10Boost Software License - Version 1.0 - August 17th, 2003
11
12Permission is hereby granted, free of charge, to any person or organization
13obtaining a copy of the software and accompanying documentation covered by
14this license (the "Software") to use, reproduce, display, distribute,
15execute, and transmit the Software, and to prepare derivative works of the
16Software, and to permit third-parties to whom the Software is furnished to
17do so, all subject to the following:
18
19The copyright notices in the Software and this entire statement, including
20the above license grant, this restriction and the following disclaimer,
21must be included in all copies of the Software, in whole or in part, and
22all derivative works of the Software, unless such copies or derivative
23works are solely in the form of machine-executable object code generated by
24a source language processor.
25
26THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32DEALINGS IN THE SOFTWARE.
33
34*/
35
38
39#include <cassert>
40#include <cstddef>
41#include <iosfwd>
42#include <iterator>
43#include <type_traits>
44
45namespace 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>
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;
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 {
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 {
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
const unsigned char * data() const noexcept
Definition: item_iterator.hpp:140
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
TMember & operator*() const noexcept
Definition: item_iterator.hpp:145
ItemIterator< TMember > operator++(int) noexcept
Definition: item_iterator.hpp:121
bool operator!=(const ItemIterator< TMember > &rhs) const noexcept
Definition: item_iterator.hpp:131
TMember * operator->() const noexcept
Definition: item_iterator.hpp:151
ItemIterator< T > cast() const noexcept
Definition: item_iterator.hpp:97
value_type & reference
Definition: item_iterator.hpp:83
ItemIterator< TMember > & operator++() noexcept
Definition: item_iterator.hpp:101
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
data_type m_data
Definition: item_iterator.hpp:67
ItemIterator< TMember > & advance_once() noexcept
Definition: item_iterator.hpp:114
value_type * pointer
Definition: item_iterator.hpp:82
void advance_to_next_item_of_right_type() noexcept
Definition: item_iterator.hpp:70
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