input_iterator.hpp
Go to the documentation of this file.
#ifndef OSMIUM_IO_INPUT_ITERATOR_HPP
#define OSMIUM_IO_INPUT_ITERATOR_HPP
/*
This file is part of Osmium (http://osmcode.org/libosmium).
Copyright 2013-2016 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <cassert>
#include <cstddef>
#include <iterator>
#include <memory>
#include <type_traits>
#include <osmium/memory/buffer.hpp>
#include <osmium/memory/item.hpp>
#include <osmium/util/compatibility.hpp>
namespace osmium {
namespace io {
template <typename TSource, typename TItem = osmium::memory::Item>
class InputIterator {
typedef typename osmium::memory::Buffer::t_iterator<TItem> item_iterator;
TSource* m_source;
std::shared_ptr<osmium::memory::Buffer> m_buffer;
item_iterator m_iter {};
void update_buffer() {
do {
m_buffer = std::make_shared<osmium::memory::Buffer>(std::move(m_source->read()));
if (!m_buffer || !*m_buffer) { // end of input
m_source = nullptr;
m_buffer.reset();
m_iter = item_iterator();
return;
}
m_iter = m_buffer->begin<TItem>();
} while (m_iter == m_buffer->end<TItem>());
}
public:
typedef std::input_iterator_tag iterator_category;
typedef TItem value_type;
typedef ptrdiff_t difference_type;
typedef TItem* pointer;
typedef TItem& reference;
explicit InputIterator(TSource& source) :
m_source(&source) {
update_buffer();
}
// end iterator
InputIterator() noexcept :
m_source(nullptr) {
}
InputIterator& operator++() {
assert(m_source);
assert(m_buffer);
assert(m_iter);
++m_iter;
if (m_iter == m_buffer->end<TItem>()) {
update_buffer();
}
return *this;
}
InputIterator operator++(int) {
InputIterator tmp(*this);
operator++();
return tmp;
}
bool operator==(const InputIterator& rhs) const noexcept {
return m_source == rhs.m_source &&
m_buffer == rhs.m_buffer &&
m_iter == rhs.m_iter;
}
bool operator!=(const InputIterator& rhs) const noexcept {
return !(*this == rhs);
}
reference operator*() const {
assert(m_iter);
return static_cast<reference>(*m_iter);
}
pointer operator->() const {
assert(m_iter);
return &static_cast<reference>(*m_iter);
}
}; // class InputIterator
template <typename TSource, typename TItem = osmium::memory::Item>
class InputIteratorRange {
InputIterator<TSource, TItem> m_begin;
InputIterator<TSource, TItem> m_end;
public:
InputIteratorRange(InputIterator<TSource, TItem>&& begin,
InputIterator<TSource, TItem>&& end) :
m_begin(std::move(begin)),
m_end(std::move(end)) {
}
InputIterator<TSource, TItem> begin() const noexcept {
return m_begin;
}
InputIterator<TSource, TItem> end() const noexcept {
return m_end;
}
const InputIterator<TSource, TItem> cbegin() const noexcept {
return m_begin;
}
const InputIterator<TSource, TItem> cend() const noexcept {
return m_end;
}
}; // class InputIteratorRange
template <typename TItem, typename TSource>
InputIteratorRange<TSource, TItem> make_input_iterator_range(TSource& source) {
using it_type = InputIterator<TSource, TItem>;
return InputIteratorRange<TSource, TItem>(it_type{source}, it_type{});
}
} // namespace io
} // namespace osmium
#endif // OSMIUM_IO_INPUT_ITERATOR_HPP
osmium::io::InputIterator::update_buffer
void update_buffer()
Definition: input_iterator.hpp:65
osmium::io::InputIterator::InputIterator
InputIterator() noexcept
Definition: input_iterator.hpp:92
osmium::io::InputIteratorRange::m_begin
InputIterator< TSource, TItem > m_begin
Definition: input_iterator.hpp:138
osmium::memory::ItemIterator
Definition: item_iterator.hpp:119
osmium::io::InputIteratorRange::cbegin
const InputIterator< TSource, TItem > cbegin() const noexcept
Definition: input_iterator.hpp:157
osmium::io::InputIterator::operator*
reference operator*() const
Definition: input_iterator.hpp:123
osmium::io::InputIterator::value_type
TItem value_type
Definition: input_iterator.hpp:81
std
Definition: reader_iterator.hpp:39
osmium::io::InputIterator::reference
TItem & reference
Definition: input_iterator.hpp:84
osmium::io::InputIteratorRange
Definition: input_iterator.hpp:136
osmium::io::InputIterator::operator++
InputIterator operator++(int)
Definition: input_iterator.hpp:107
osmium::io::InputIterator::operator->
pointer operator->() const
Definition: input_iterator.hpp:128
osmium
Namespace for everything in the Osmium library.
Definition: assembler.hpp:59
osmium::io::InputIteratorRange::m_end
InputIterator< TSource, TItem > m_end
Definition: input_iterator.hpp:139
osmium::io::InputIterator
Definition: input_iterator.hpp:56
osmium::io::InputIterator::m_iter
item_iterator m_iter
Definition: input_iterator.hpp:63
osmium::io::InputIterator::operator==
bool operator==(const InputIterator &rhs) const noexcept
Definition: input_iterator.hpp:113
osmium::io::InputIterator::pointer
TItem * pointer
Definition: input_iterator.hpp:83
std::end
osmium::io::InputIterator< osmium::io::Reader > end(osmium::io::Reader &)
Definition: reader_iterator.hpp:45
osmium::io::InputIteratorRange::cend
const InputIterator< TSource, TItem > cend() const noexcept
Definition: input_iterator.hpp:161
osmium::io::InputIteratorRange::InputIteratorRange
InputIteratorRange(InputIterator< TSource, TItem > &&begin, InputIterator< TSource, TItem > &&end)
Definition: input_iterator.hpp:143
osmium::io::InputIterator::difference_type
ptrdiff_t difference_type
Definition: input_iterator.hpp:82
osmium::io::InputIterator::operator!=
bool operator!=(const InputIterator &rhs) const noexcept
Definition: input_iterator.hpp:119
osmium::io::InputIterator::item_iterator
osmium::memory::Buffer::t_iterator< TItem > item_iterator
Definition: input_iterator.hpp:59
item.hpp
buffer.hpp
osmium::io::InputIterator::InputIterator
InputIterator(TSource &source)
Definition: input_iterator.hpp:86
osmium::io::InputIterator::m_buffer
std::shared_ptr< osmium::memory::Buffer > m_buffer
Definition: input_iterator.hpp:62
osmium::io::make_input_iterator_range
InputIteratorRange< TSource, TItem > make_input_iterator_range(TSource &source)
Definition: input_iterator.hpp:168
osmium::io::InputIteratorRange::begin
InputIterator< TSource, TItem > begin() const noexcept
Definition: input_iterator.hpp:149
osmium::io::InputIteratorRange::end
InputIterator< TSource, TItem > end() const noexcept
Definition: input_iterator.hpp:153
std::begin
osmium::io::InputIterator< osmium::io::Reader > begin(osmium::io::Reader &reader)
Definition: reader_iterator.hpp:41
osmium::io::InputIterator::m_source
TSource * m_source
Definition: input_iterator.hpp:61
osmium::io::InputIterator::iterator_category
std::input_iterator_tag iterator_category
Definition: input_iterator.hpp:80
osmium::io::InputIterator::operator++
InputIterator & operator++()
Definition: input_iterator.hpp:96
compatibility.hpp