Loading...
Searching...
No Matches
callback_buffer.hpp
Go to the documentation of this file.
#ifndef OSMIUM_MEMORY_CALLBACK_BUFFER_HPP
#define OSMIUM_MEMORY_CALLBACK_BUFFER_HPP
/*
This file is part of Osmium (https://osmcode.org/libosmium).
Copyright 2013-2023 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 <osmium/memory/buffer.hpp>
#include <cstddef>
#include <functional>
#include <utility>
namespace osmium {
namespace memory {
class CallbackBuffer {
public:
using callback_func_type = std::function<void(osmium::memory::Buffer&&)>;
private:
enum {
default_initial_buffer_size = 1024UL * 1024UL
};
enum {
default_max_buffer_size = 800UL * 1024UL
};
osmium::memory::Buffer m_buffer;
std::size_t m_initial_buffer_size;
std::size_t m_max_buffer_size;
callback_func_type m_callback;
public:
explicit CallbackBuffer(std::size_t initial_buffer_size = default_initial_buffer_size, std::size_t max_buffer_size = default_max_buffer_size) :
m_buffer(initial_buffer_size, osmium::memory::Buffer::auto_grow::yes),
m_initial_buffer_size(initial_buffer_size),
m_max_buffer_size(max_buffer_size),
m_callback(nullptr) {
}
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) :
m_buffer(initial_buffer_size, osmium::memory::Buffer::auto_grow::yes),
m_initial_buffer_size(initial_buffer_size),
m_max_buffer_size(max_buffer_size),
m_callback(std::move(callback)) {
}
osmium::memory::Buffer& buffer() noexcept {
return m_buffer;
}
void set_callback(const callback_func_type& callback = nullptr) noexcept {
m_callback = callback;
}
void flush() {
if (m_callback && m_buffer.committed() > 0) {
m_callback(read());
}
}
void possibly_flush() {
if (m_buffer.committed() > m_max_buffer_size) {
flush();
}
}
osmium::memory::Buffer read() {
osmium::memory::Buffer new_buffer{m_initial_buffer_size, osmium::memory::Buffer::auto_grow::yes};
using std::swap;
swap(new_buffer, m_buffer);
return new_buffer;
}
}; // class CallbackBuffer
} // namespace memory
} // namespace osmium
#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