Libosmium  2.20.0
Fast and flexible C++ library for working with OpenStreetMap data
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
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 <iterator>
42#include <memory>
43#include <type_traits>
44
45namespace osmium {
46
47 namespace io {
48
54 template <typename TSource, typename TItem = osmium::memory::Item>
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;
63
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();
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;
84
85 explicit InputIterator(TSource& source) :
86 m_source(&source) {
88 }
89
90 // end iterator
91 InputIterator() noexcept :
92 m_source(nullptr) {
93 }
94
96 assert(m_source);
97 assert(m_buffer);
98 assert(m_iter);
99 ++m_iter;
100 if (m_iter == m_buffer->select<TItem>().end()) {
102 }
103 return *this;
104 }
105
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
123 assert(m_iter);
124 return static_cast<reference>(*m_iter);
125 }
126
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>
136
139
140 public:
141
144 m_begin(std::move(begin)),
145 m_end(std::move(end)) {
146 }
147
149 return m_begin;
150 }
151
153 return m_end;
154 }
155
157 return m_begin;
158 }
159
161 return m_end;
162 }
163
164 }; // class InputIteratorRange
165
166 template <typename TItem, typename TSource>
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
Definition: input_iterator.hpp:135
InputIteratorRange(InputIterator< TSource, TItem > &&begin, InputIterator< TSource, TItem > &&end)
Definition: input_iterator.hpp:142
InputIterator< TSource, TItem > m_end
Definition: input_iterator.hpp:138
InputIterator< TSource, TItem > cbegin() const noexcept
Definition: input_iterator.hpp:156
InputIterator< TSource, TItem > cend() const noexcept
Definition: input_iterator.hpp:160
InputIterator< TSource, TItem > m_begin
Definition: input_iterator.hpp:137
InputIterator< TSource, TItem > end() const noexcept
Definition: input_iterator.hpp:152
InputIterator< TSource, TItem > begin() const noexcept
Definition: input_iterator.hpp:148
Definition: input_iterator.hpp:55
item_iterator m_iter
Definition: input_iterator.hpp:62
ptrdiff_t difference_type
Definition: input_iterator.hpp:81
typename osmium::memory::Buffer::t_iterator< TItem > item_iterator
Definition: input_iterator.hpp:58
bool operator!=(const InputIterator &rhs) const noexcept
Definition: input_iterator.hpp:118
value_type * pointer
Definition: input_iterator.hpp:82
InputIterator() noexcept
Definition: input_iterator.hpp:91
value_type & reference
Definition: input_iterator.hpp:83
reference operator*() const
Definition: input_iterator.hpp:122
bool operator==(const InputIterator &rhs) const noexcept
Definition: input_iterator.hpp:112
void update_buffer()
Definition: input_iterator.hpp:64
InputIterator(TSource &source)
Definition: input_iterator.hpp:85
std::shared_ptr< osmium::memory::Buffer > m_buffer
Definition: input_iterator.hpp:61
pointer operator->() const
Definition: input_iterator.hpp:127
InputIterator & operator++()
Definition: input_iterator.hpp:95
std::input_iterator_tag iterator_category
Definition: input_iterator.hpp:79
InputIterator operator++(int)
Definition: input_iterator.hpp:106
TSource * m_source
Definition: input_iterator.hpp:60
TItem value_type
Definition: input_iterator.hpp:80
InputIterator< Reader > begin(Reader &reader)
Definition: reader_iterator.hpp:43
InputIteratorRange< TSource, TItem > make_input_iterator_range(TSource &source)
Definition: input_iterator.hpp:167
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
Definition: location.hpp:555