Libosmium 2.22.0 Fast and flexible C++ library for working with OpenStreetMap data

Loading...
Searching...
No Matches
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-2025 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
70 class CallbackBuffer {
71
72 public:
73
75 using callback_func_type = std::function<void(osmium::memory::Buffer&&)>;
76
77 private:
78
79 enum {
80 default_initial_buffer_size = 1024UL * 1024UL
81 };
82
83 enum {
84 default_max_buffer_size = 800UL * 1024UL
85 };
86
87 osmium::memory::Buffer m_buffer;
88 std::size_t m_initial_buffer_size;
89 std::size_t m_max_buffer_size;
90 callback_func_type m_callback;
91
92 public:
93
103 explicit CallbackBuffer(std::size_t initial_buffer_size = default_initial_buffer_size, std::size_t max_buffer_size = default_max_buffer_size) :
104 m_buffer(initial_buffer_size, osmium::memory::Buffer::auto_grow::yes),
105 m_initial_buffer_size(initial_buffer_size),
106 m_max_buffer_size(max_buffer_size),
107 m_callback(nullptr) {
108 }
109
120 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) :
121 m_buffer(initial_buffer_size, osmium::memory::Buffer::auto_grow::yes),
122 m_initial_buffer_size(initial_buffer_size),
123 m_max_buffer_size(max_buffer_size),
124 m_callback(std::move(callback)) {
125 }
126
133 osmium::memory::Buffer& buffer() noexcept {
134 return m_buffer;
135 }
136
144 void set_callback(const callback_func_type& callback = nullptr) noexcept {
145 m_callback = callback;
146 }
147
156 void flush() {
157 if (m_callback && m_buffer.committed() > 0) {
158 m_callback(read());
159 }
160 }
161
170 void possibly_flush() {
171 if (m_buffer.committed() > m_max_buffer_size) {
172 flush();
173 }
174 }
175
181 osmium::memory::Buffer read() {
182 osmium::memory::Buffer new_buffer{m_initial_buffer_size, osmium::memory::Buffer::auto_grow::yes};
183 using std::swap;
184 swap(new_buffer, m_buffer);
185 return new_buffer;
186 }
187
188 }; // class CallbackBuffer
189
190 } // namespace memory
191
192} // namespace osmium
193
194#endif // OSMIUM_MEMORY_CALLBACK_BUFFER_HPP
buffer.hpp
osmium::memory::CallbackBuffer
Definition callback_buffer.hpp:70
osmium::memory::CallbackBuffer::buffer
osmium::memory::Buffer & buffer() noexcept
Definition callback_buffer.hpp:133
osmium::memory::CallbackBuffer::default_max_buffer_size
@ default_max_buffer_size
Definition callback_buffer.hpp:84
osmium::memory::CallbackBuffer::CallbackBuffer
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:103
osmium::memory::CallbackBuffer::m_max_buffer_size
std::size_t m_max_buffer_size
Definition callback_buffer.hpp:89
osmium::memory::CallbackBuffer::CallbackBuffer
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:120
osmium::memory::CallbackBuffer::set_callback
void set_callback(const callback_func_type &callback=nullptr) noexcept
Definition callback_buffer.hpp:144
osmium::memory::CallbackBuffer::read
osmium::memory::Buffer read()
Definition callback_buffer.hpp:181
osmium::memory::CallbackBuffer::callback_func_type
std::function< void(osmium::memory::Buffer &&)> callback_func_type
The type for the callback function.
Definition callback_buffer.hpp:75
osmium::memory::CallbackBuffer::m_buffer
osmium::memory::Buffer m_buffer
Definition callback_buffer.hpp:87
osmium::memory::CallbackBuffer::possibly_flush
void possibly_flush()
Definition callback_buffer.hpp:170
osmium::memory::CallbackBuffer::flush
void flush()
Definition callback_buffer.hpp:156
osmium::memory::CallbackBuffer::default_initial_buffer_size
@ default_initial_buffer_size
Definition callback_buffer.hpp:80
osmium::memory::CallbackBuffer::m_callback
callback_func_type m_callback
Definition callback_buffer.hpp:90
osmium::memory::CallbackBuffer::m_initial_buffer_size
std::size_t m_initial_buffer_size
Definition callback_buffer.hpp:88
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
std
Definition location.hpp:555