Libosmium  2.1.0
Fast and flexible C++ library for working with OpenStreetMap data
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
output_iterator.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_IO_OUTPUT_ITERATOR_HPP
2 #define OSMIUM_IO_OUTPUT_ITERATOR_HPP
3 
4 /*
5 
6 This file is part of Osmium (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2015 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 <cstddef>
37 #include <iterator>
38 #include <memory>
39 #include <utility>
40 
41 #include <osmium/memory/buffer.hpp>
43 
44 namespace osmium {
45 
46  namespace memory {
47  class Item;
48  } // namespace memory
49 
50  namespace io {
51 
52  template <class TDest>
53  class OutputIterator : public std::iterator<std::output_iterator_tag, osmium::memory::Item> {
54 
55  struct buffer_wrapper {
56 
58 
59  buffer_wrapper(size_t buffer_size) :
60  buffer(buffer_size, osmium::memory::Buffer::auto_grow::no) {
61  }
62 
63  }; // struct buffer_wrapper
64 
65  static constexpr size_t default_buffer_size = 10 * 1024 * 1024;
66 
67  TDest* m_destination;
68 
69  std::shared_ptr<buffer_wrapper> m_buffer_wrapper;
70 
71  public:
72 
73  explicit OutputIterator(TDest& destination, const size_t buffer_size = default_buffer_size) :
74  m_destination(&destination),
75  m_buffer_wrapper(std::make_shared<buffer_wrapper>(buffer_size)) {
76  }
77 
78  OutputIterator(const OutputIterator&) = default;
79  OutputIterator(OutputIterator&&) = default;
80 
81  OutputIterator& operator=(const OutputIterator&) = default;
83 
84  ~OutputIterator() = default;
85 
86  void flush() {
87  osmium::memory::Buffer buffer(m_buffer_wrapper->buffer.capacity(), osmium::memory::Buffer::auto_grow::no);
88  std::swap(m_buffer_wrapper->buffer, buffer);
89  (*m_destination)(std::move(buffer));
90  }
91 
93  try {
94  m_buffer_wrapper->buffer.push_back(item);
95  } catch (osmium::buffer_is_full&) {
96  flush();
97  m_buffer_wrapper->buffer.push_back(item);
98  }
99  return *this;
100  }
101 
103  return this->operator=(diff.curr());
104  }
105 
107  return *this;
108  }
109 
111  return *this;
112  }
113 
115  return *this;
116  }
117 
118  }; // class OutputIterator
119 
120  } // namespace io
121 
122 } // namespace osmium
123 
124 #endif // OSMIUM_IO_OUTPUT_ITERATOR_HPP
OutputIterator & operator=(const osmium::DiffObject &diff)
Definition: output_iterator.hpp:102
TDest * m_destination
Definition: output_iterator.hpp:67
Definition: diff_object.hpp:47
Definition: output_iterator.hpp:53
Definition: reader_iterator.hpp:39
Definition: output_iterator.hpp:55
buffer_wrapper(size_t buffer_size)
Definition: output_iterator.hpp:59
Definition: item.hpp:98
Namespace for everything in the Osmium library.
Definition: assembler.hpp:55
OutputIterator & operator++(int)
Definition: output_iterator.hpp:114
osmium::memory::Buffer buffer
Definition: output_iterator.hpp:57
std::shared_ptr< buffer_wrapper > m_buffer_wrapper
Definition: output_iterator.hpp:69
OutputIterator & operator*()
Definition: output_iterator.hpp:106
OutputIterator & operator=(const OutputIterator &)=default
void flush()
Definition: output_iterator.hpp:86
const osmium::OSMObject & curr() const noexcept
Definition: diff_object.hpp:79
static constexpr size_t default_buffer_size
Definition: output_iterator.hpp:65
Definition: buffer.hpp:95
Definition: buffer.hpp:58
OutputIterator(TDest &destination, const size_t buffer_size=default_buffer_size)
Definition: output_iterator.hpp:73
OutputIterator & operator=(const osmium::memory::Item &item)
Definition: output_iterator.hpp:92
OutputIterator & operator++()
Definition: output_iterator.hpp:110