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