Libosmium  2.14.2
Fast and flexible C++ library for working with OpenStreetMap data
callback_buffer.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_MEMORY_CALLBACK_BUFFER_HPP
2 #define OSMIUM_MEMORY_CALLBACK_BUFFER_HPP
3 
4 /*
5 
6 This file is part of Osmium (https://osmcode.org/libosmium).
7 
8 Copyright 2013-2018 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 
38 #include <cstddef>
39 #include <functional>
40 #include <utility>
41 
42 namespace osmium {
43 
44  namespace memory {
45 
71 
72  public:
73 
75  using callback_func_type = std::function<void(osmium::memory::Buffer&&)>;
76 
77  private:
78 
79  static constexpr const std::size_t default_initial_buffer_size = 1024 * 1024;
80  static constexpr const std::size_t default_max_buffer_size = 800 * 1024;
81 
83  std::size_t m_initial_buffer_size;
84  std::size_t m_max_buffer_size;
86 
87  public:
88 
98  explicit CallbackBuffer(std::size_t initial_buffer_size = default_initial_buffer_size, std::size_t max_buffer_size = default_max_buffer_size) :
99  m_buffer(initial_buffer_size, osmium::memory::Buffer::auto_grow::yes),
100  m_initial_buffer_size(initial_buffer_size),
101  m_max_buffer_size(max_buffer_size),
102  m_callback(nullptr) {
103  }
104 
115  explicit CallbackBuffer(callback_func_type callback, std::size_t initial_buffer_size = default_initial_buffer_size, std::size_t max_buffer_size = default_max_buffer_size) :
116  m_buffer(initial_buffer_size, osmium::memory::Buffer::auto_grow::yes),
117  m_initial_buffer_size(initial_buffer_size),
118  m_max_buffer_size(max_buffer_size),
119  m_callback(std::move(callback)) {
120  }
121 
129  return m_buffer;
130  }
131 
139  void set_callback(const callback_func_type& callback = nullptr) noexcept {
140  m_callback = callback;
141  }
142 
151  void flush() {
152  if (m_callback && m_buffer.committed() > 0) {
153  m_callback(read());
154  }
155  }
156 
165  void possibly_flush() {
167  flush();
168  }
169  }
170 
178  using std::swap;
179  swap(buffer, m_buffer);
180  return buffer;
181  }
182 
183  }; // class CallbackBuffer
184 
185  } // namespace memory
186 
187 } // namespace osmium
188 
189 #endif // OSMIUM_MEMORY_CALLBACK_BUFFER_HPP
void set_callback(const callback_func_type &callback=nullptr) noexcept
Definition: callback_buffer.hpp:139
callback_func_type m_callback
Definition: callback_buffer.hpp:85
osmium::memory::Buffer & buffer() noexcept
Definition: callback_buffer.hpp:128
Definition: location.hpp:550
void possibly_flush()
Definition: callback_buffer.hpp:165
void swap(Buffer &lhs, Buffer &rhs)
Definition: buffer.hpp:755
std::size_t committed() const noexcept
Definition: buffer.hpp:261
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
osmium::memory::Buffer read()
Definition: callback_buffer.hpp:176
CallbackBuffer(callback_func_type callback, std::size_t initial_buffer_size=default_initial_buffer_size, std::size_t max_buffer_size=default_max_buffer_size)
Definition: callback_buffer.hpp:115
osmium::memory::Buffer m_buffer
Definition: callback_buffer.hpp:82
static constexpr const std::size_t default_max_buffer_size
Definition: callback_buffer.hpp:80
std::size_t m_max_buffer_size
Definition: callback_buffer.hpp:84
void flush()
Definition: callback_buffer.hpp:151
Definition: buffer.hpp:97
Definition: callback_buffer.hpp:70
std::function< void(osmium::memory::Buffer &&)> callback_func_type
The type for the callback function.
Definition: callback_buffer.hpp:75
CallbackBuffer(std::size_t initial_buffer_size=default_initial_buffer_size, std::size_t max_buffer_size=default_max_buffer_size)
Definition: callback_buffer.hpp:98
std::size_t m_initial_buffer_size
Definition: callback_buffer.hpp:83
static constexpr const std::size_t default_initial_buffer_size
Definition: callback_buffer.hpp:79