Libosmium  2.16.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 
6 This file is part of Osmium (https://osmcode.org/libosmium).
7 
8 Copyright 2013-2021 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 
59 
60  TSource* m_source;
61  std::shared_ptr<osmium::memory::Buffer> m_buffer;
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();
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*;
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 
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 
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 
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>
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
InputIterator< TSource, TItem > cend() const noexcept
Definition: input_iterator.hpp:160
InputIteratorRange(InputIterator< TSource, TItem > &&begin, InputIterator< TSource, TItem > &&end)
Definition: input_iterator.hpp:142
InputIterator< TSource, TItem > begin() const noexcept
Definition: input_iterator.hpp:148
InputIterator< TSource, TItem > m_end
Definition: input_iterator.hpp:138
InputIterator< TSource, TItem > cbegin() const noexcept
Definition: input_iterator.hpp:156
InputIterator< TSource, TItem > m_begin
Definition: input_iterator.hpp:137
InputIterator< TSource, TItem > end() const noexcept
Definition: input_iterator.hpp:152
Definition: input_iterator.hpp:55
item_iterator m_iter
Definition: input_iterator.hpp:62
ptrdiff_t difference_type
Definition: input_iterator.hpp:81
InputIterator & operator++()
Definition: input_iterator.hpp:95
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
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
Definition: item_iterator.hpp:59
InputIteratorRange< TSource, TItem > make_input_iterator_range(TSource &source)
Definition: input_iterator.hpp:167
InputIterator< Reader > begin(Reader &reader)
Definition: reader_iterator.hpp:43
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
Definition: location.hpp:551