Libosmium 2.22.0 Fast and flexible C++ library for working with OpenStreetMap data

Loading...
Searching...
No Matches
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-2025 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 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 = std::conditional_t<std::is_const<TMember>::value, const unsigned char*, unsigned char*>;
66
67 data_type m_data;
68 data_type m_end;
69
70 void advance_to_next_item_of_right_type() noexcept {
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*;
83 using reference = value_type&;
84
85 ItemIterator() noexcept :
86 m_data(nullptr),
87 m_end(nullptr) {
88 }
89
90 ItemIterator(data_type data, data_type end) noexcept :
91 m_data(data),
92 m_end(end) {
93 advance_to_next_item_of_right_type();
94 }
95
96 template <typename T>
97 ItemIterator<T> cast() const noexcept {
98 return ItemIterator<T>(m_data, m_end);
99 }
100
101 ItemIterator<TMember>& operator++() noexcept {
102 assert(m_data);
103 assert(m_data != m_end);
104 m_data = reinterpret_cast<TMember*>(m_data)->next();
105 advance_to_next_item_of_right_type();
106 return *static_cast<ItemIterator<TMember>*>(this);
107 }
108
114 ItemIterator<TMember>& advance_once() noexcept {
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
121 ItemIterator<TMember> operator++(int) noexcept {
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>
175 class ItemIteratorRange {
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 = std::conditional_t<std::is_const<T>::value, const unsigned char*, unsigned char*>;
181
182 data_type m_begin;
183 data_type m_end;
184
185 public:
186
187 using iterator = ItemIterator<T>;
188 using const_iterator = ItemIterator<const T>;
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
osmium::memory::ItemIteratorRange
Definition item_iterator.hpp:175
osmium::memory::ItemIteratorRange::size
std::size_t size() const noexcept
Definition item_iterator.hpp:224
osmium::memory::ItemIteratorRange::empty
bool empty() const noexcept
Definition item_iterator.hpp:236
osmium::memory::ItemIteratorRange::end
const_iterator end() const noexcept
Definition item_iterator.hpp:215
osmium::memory::ItemIteratorRange::end
iterator end() noexcept
Definition item_iterator.hpp:199
osmium::memory::ItemIteratorRange::begin
const_iterator begin() const noexcept
Definition item_iterator.hpp:211
osmium::memory::ItemIteratorRange::cbegin
const_iterator cbegin() const noexcept
Definition item_iterator.hpp:203
osmium::memory::ItemIteratorRange::m_end
data_type m_end
Definition item_iterator.hpp:183
osmium::memory::ItemIteratorRange::data_type
std::conditional_t< std::is_const< T >::value, const unsigned char *, unsigned char * > data_type
Definition item_iterator.hpp:180
osmium::memory::ItemIteratorRange::m_begin
data_type m_begin
Definition item_iterator.hpp:182
osmium::memory::ItemIteratorRange::ItemIteratorRange
ItemIteratorRange(data_type first, data_type last) noexcept
Definition item_iterator.hpp:190
osmium::memory::ItemIteratorRange::begin
iterator begin() noexcept
Definition item_iterator.hpp:195
osmium::memory::ItemIteratorRange::cend
const_iterator cend() const noexcept
Definition item_iterator.hpp:207
osmium::memory::ItemIterator
Definition item_iterator.hpp:59
osmium::memory::ItemIterator::difference_type
std::ptrdiff_t difference_type
Definition item_iterator.hpp:81
osmium::memory::ItemIterator::data
const unsigned char * data() const noexcept
Definition item_iterator.hpp:140
osmium::memory::ItemIterator::ItemIterator
ItemIterator() noexcept
Definition item_iterator.hpp:85
osmium::memory::ItemIterator::operator==
bool operator==(const ItemIterator< TMember > &rhs) const noexcept
Definition item_iterator.hpp:127
osmium::memory::ItemIterator::m_end
data_type m_end
Definition item_iterator.hpp:68
osmium::memory::ItemIterator::ItemIterator
ItemIterator(data_type data, data_type end) noexcept
Definition item_iterator.hpp:90
osmium::memory::ItemIterator::operator*
TMember & operator*() const noexcept
Definition item_iterator.hpp:145
osmium::memory::ItemIterator::operator++
ItemIterator< TMember > operator++(int) noexcept
Definition item_iterator.hpp:121
osmium::memory::ItemIterator::operator!=
bool operator!=(const ItemIterator< TMember > &rhs) const noexcept
Definition item_iterator.hpp:131
osmium::memory::ItemIterator::operator->
TMember * operator->() const noexcept
Definition item_iterator.hpp:151
osmium::memory::ItemIterator::cast
ItemIterator< T > cast() const noexcept
Definition item_iterator.hpp:97
osmium::memory::ItemIterator::reference
value_type & reference
Definition item_iterator.hpp:83
osmium::memory::ItemIterator::operator++
ItemIterator< TMember > & operator++() noexcept
Definition item_iterator.hpp:101
osmium::memory::ItemIterator::value_type
TMember value_type
Definition item_iterator.hpp:80
osmium::memory::ItemIterator::print
void print(std::basic_ostream< TChar, TTraits > &out) const
Definition item_iterator.hpp:162
osmium::memory::ItemIterator::data_type
std::conditional_t< std::is_const< TMember >::value, const unsigned char *, unsigned char * > data_type
Definition item_iterator.hpp:65
osmium::memory::ItemIterator::m_data
data_type m_data
Definition item_iterator.hpp:67
osmium::memory::ItemIterator::advance_once
ItemIterator< TMember > & advance_once() noexcept
Definition item_iterator.hpp:114
osmium::memory::ItemIterator::pointer
value_type * pointer
Definition item_iterator.hpp:82
osmium::memory::ItemIterator::advance_to_next_item_of_right_type
void advance_to_next_item_of_right_type() noexcept
Definition item_iterator.hpp:70
osmium::memory::ItemIterator::iterator_category
std::forward_iterator_tag iterator_category
Definition item_iterator.hpp:79
osmium::memory::ItemIterator::data
data_type data() noexcept
Definition item_iterator.hpp:135
osmium::memory::Item
Definition item.hpp:105
item.hpp
item_type.hpp
detail
Definition attr.hpp:342
osmium::memory::operator<<
std::basic_ostream< TChar, TTraits > & operator<<(std::basic_ostream< TChar, TTraits > &out, const CollectionIterator< TMember > &iter)
Definition collection.hpp:111
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
osmium::item_type
item_type
Definition item_type.hpp:45