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

Loading...
Searching...
No Matches
input_iterator.hpp
Go to the documentation of this file.
1#ifndef OSMIUM_IO_INPUT_ITERATOR_HPP
2#define OSMIUM_IO_INPUT_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/buffer.hpp>
37#include <osmium/memory/item.hpp>
38
39#include <cassert>
40#include <cstddef>
41#include <iterator>
42#include <memory>
43#include <type_traits>
44
45 namespace osmium {
46
47 namespace io {
48
54 template <typename TSource, typename TItem = osmium::memory::Item>
55 class InputIterator {
56
57
58 using item_iterator = typename osmium::memory::Buffer::t_iterator<TItem>;
59
60 TSource* m_source;
61 std::shared_ptr<osmium::memory::Buffer> m_buffer;
62 item_iterator m_iter{};
63
64 void update_buffer() {
65 do {
66 m_buffer = std::make_shared<osmium::memory::Buffer>(std::move(m_source->read()));
67 if (!m_buffer || !*m_buffer) { // end of input
68 m_source = nullptr;
69 m_buffer.reset();
70 m_iter = item_iterator{};
71 return;
72 }
73 m_iter = m_buffer->select<TItem>().begin();
74 } while (m_iter == m_buffer->select<TItem>().end());
75 }
76
77 public:
78
79 using iterator_category = std::input_iterator_tag;
80 using value_type = TItem;
81 using difference_type = ptrdiff_t;
82 using pointer = value_type*;
83 using reference = value_type&;
84
85 explicit InputIterator(TSource& source) :
86 m_source(&source) {
87 update_buffer();
88 }
89
90 // end iterator
91 InputIterator() noexcept :
92 m_source(nullptr) {
93 }
94
95 InputIterator& operator++() {
96 assert(m_source);
97 assert(m_buffer);
98 assert(m_iter);
99 ++m_iter;
100 if (m_iter == m_buffer->select<TItem>().end()) {
101 update_buffer();
102 }
103 return *this;
104 }
105
106 InputIterator operator++(int) {
107 InputIterator tmp{*this};
108 operator++();
109 return tmp;
110 }
111
112 bool operator==(const InputIterator& rhs) const noexcept {
113 return m_source == rhs.m_source &&
114 m_buffer == rhs.m_buffer &&
115 m_iter == rhs.m_iter;
116 }
117
118 bool operator!=(const InputIterator& rhs) const noexcept {
119 return !(*this == rhs);
120 }
121
122 reference operator*() const {
123 assert(m_iter);
124 return static_cast<reference>(*m_iter);
125 }
126
127 pointer operator->() const {
128 assert(m_iter);
129 return &static_cast<reference>(*m_iter);
130 }
131
132 }; // class InputIterator
133
134 template <typename TSource, typename TItem = osmium::memory::Item>
135 class InputIteratorRange {
136
137 InputIterator<TSource, TItem> m_begin;
138 InputIterator<TSource, TItem> m_end;
139
140 public:
141
142 InputIteratorRange(InputIterator<TSource, TItem>&& begin,
143 InputIterator<TSource, TItem>&& end) :
144 m_begin(std::move(begin)),
145 m_end(std::move(end)) {
146 }
147
148 InputIterator<TSource, TItem> begin() const noexcept {
149 return m_begin;
150 }
151
152 InputIterator<TSource, TItem> end() const noexcept {
153 return m_end;
154 }
155
156 InputIterator<TSource, TItem> cbegin() const noexcept {
157 return m_begin;
158 }
159
160 InputIterator<TSource, TItem> cend() const noexcept {
161 return m_end;
162 }
163
164 }; // class InputIteratorRange
165
166 template <typename TItem, typename TSource>
167 InputIteratorRange<TSource, TItem> make_input_iterator_range(TSource& source) {
168 using it_type = InputIterator<TSource, TItem>;
169 return InputIteratorRange<TSource, TItem>{it_type{source}, it_type{}};
170 }
171
172 } // namespace io
173
174} // namespace osmium
175
176#endif // OSMIUM_IO_INPUT_ITERATOR_HPP
buffer.hpp
osmium::io::InputIteratorRange
Definition input_iterator.hpp:135
osmium::io::InputIteratorRange::InputIteratorRange
InputIteratorRange(InputIterator< TSource, TItem > &&begin, InputIterator< TSource, TItem > &&end)
Definition input_iterator.hpp:142
osmium::io::InputIteratorRange::m_end
InputIterator< TSource, TItem > m_end
Definition input_iterator.hpp:138
osmium::io::InputIteratorRange::cbegin
InputIterator< TSource, TItem > cbegin() const noexcept
Definition input_iterator.hpp:156
osmium::io::InputIteratorRange::cend
InputIterator< TSource, TItem > cend() const noexcept
Definition input_iterator.hpp:160
osmium::io::InputIteratorRange::m_begin
InputIterator< TSource, TItem > m_begin
Definition input_iterator.hpp:137
osmium::io::InputIteratorRange::end
InputIterator< TSource, TItem > end() const noexcept
Definition input_iterator.hpp:152
osmium::io::InputIteratorRange::begin
InputIterator< TSource, TItem > begin() const noexcept
Definition input_iterator.hpp:148
osmium::io::InputIterator
Definition input_iterator.hpp:55
osmium::io::InputIterator::m_iter
item_iterator m_iter
Definition input_iterator.hpp:62
osmium::io::InputIterator::difference_type
ptrdiff_t difference_type
Definition input_iterator.hpp:81
osmium::io::InputIterator::item_iterator
typename osmium::memory::Buffer::t_iterator< TItem > item_iterator
Definition input_iterator.hpp:58
osmium::io::InputIterator::operator!=
bool operator!=(const InputIterator &rhs) const noexcept
Definition input_iterator.hpp:118
osmium::io::InputIterator::pointer
value_type * pointer
Definition input_iterator.hpp:82
osmium::io::InputIterator::InputIterator
InputIterator() noexcept
Definition input_iterator.hpp:91
osmium::io::InputIterator::reference
value_type & reference
Definition input_iterator.hpp:83
osmium::io::InputIterator::operator*
reference operator*() const
Definition input_iterator.hpp:122
osmium::io::InputIterator::operator==
bool operator==(const InputIterator &rhs) const noexcept
Definition input_iterator.hpp:112
osmium::io::InputIterator::update_buffer
void update_buffer()
Definition input_iterator.hpp:64
osmium::io::InputIterator::InputIterator
InputIterator(TSource &source)
Definition input_iterator.hpp:85
osmium::io::InputIterator::m_buffer
std::shared_ptr< osmium::memory::Buffer > m_buffer
Definition input_iterator.hpp:61
osmium::io::InputIterator::operator->
pointer operator->() const
Definition input_iterator.hpp:127
osmium::io::InputIterator::operator++
InputIterator & operator++()
Definition input_iterator.hpp:95
osmium::io::InputIterator::iterator_category
std::input_iterator_tag iterator_category
Definition input_iterator.hpp:79
osmium::io::InputIterator::operator++
InputIterator operator++(int)
Definition input_iterator.hpp:106
osmium::io::InputIterator::m_source
TSource * m_source
Definition input_iterator.hpp:60
osmium::io::InputIterator::value_type
TItem value_type
Definition input_iterator.hpp:80
item.hpp
osmium::io::begin
InputIterator< Reader > begin(Reader &reader)
Definition reader_iterator.hpp:43
osmium::io::make_input_iterator_range
InputIteratorRange< TSource, TItem > make_input_iterator_range(TSource &source)
Definition input_iterator.hpp:167
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
std
Definition location.hpp:555