1 #ifndef OSMIUM_MEMORY_BUFFER_HPP
2 #define OSMIUM_MEMORY_BUFFER_HPP
61 std::runtime_error(
"Osmium buffer is full") {
154 throw std::invalid_argument(
"buffer size needs to be multiple of alignment");
172 m_capacity(capacity),
173 m_written(committed),
174 m_committed(committed) {
176 throw std::invalid_argument(
"buffer capacity needs to be multiple of alignment");
179 throw std::invalid_argument(
"buffer parameter 'committed' needs to be multiple of alignment");
199 m_data(m_memory.
data()),
200 m_capacity(capacity),
205 throw std::invalid_argument(
"buffer capacity needs to be multiple of alignment");
224 unsigned char*
data() const noexcept {
308 if (m_memory.empty()) {
309 throw std::logic_error(
"Can't grow Buffer if it doesn't use internal memory management.");
311 if (m_capacity < size) {
313 throw std::invalid_argument(
"buffer capacity needs to be multiple of alignment");
315 m_memory.resize(size);
316 m_data = m_memory.data();
374 template <
typename T>
375 T&
get(
const size_t offset)
const {
377 return *
reinterpret_cast<T*
>(&m_data[offset]);
416 if (m_written + size > m_capacity && m_full) {
420 if (m_written + size > m_capacity) {
423 size_t new_capacity = m_capacity * 2;
424 while (m_written + size > new_capacity) {
452 template <
typename T>
456 std::copy_n(reinterpret_cast<const unsigned char*>(&item), item.padded_size(), target);
457 return *
reinterpret_cast<T*
>(target);
471 assert(m_data && buffer);
494 template <
typename T>
501 template <
typename T>
524 template <
typename T>
540 return iterator(m_data, m_data + m_committed);
552 template <
typename T>
569 return iterator(m_data + offset, m_data + m_committed);
580 template <
typename T>
596 return iterator(m_data + m_committed, m_data + m_committed);
599 template <
typename T>
610 template <
typename T>
621 template <
typename T>
629 return const_iterator(m_data + m_committed, m_data + m_committed);
632 template <
typename T>
641 template <
typename T>
653 explicit operator bool() const noexcept {
654 return m_data !=
nullptr;
685 template <
typename TCallbackClass>
696 next = std::next(it_read);
697 if (!it_read->removed()) {
698 if (it_read != it_write) {
699 assert(it_read.data() >=
data());
701 size_t old_offset =
static_cast<size_t>(it_read.data() -
data());
702 size_t new_offset =
static_cast<size_t>(it_write.
data() -
data());
703 callback->moving_in_buffer(old_offset, new_offset);
704 std::memmove(it_write.
data(), it_read.data(), it_read->padded_size());
711 m_written =
static_cast<size_t>(it_write.
data() -
data());
728 return lhs.data() == rhs.data() && lhs.capacity() == rhs.capacity() && lhs.committed() == rhs.committed();
732 return ! (lhs == rhs);
739 #endif // OSMIUM_MEMORY_BUFFER_HPP
size_t m_written
Definition: buffer.hpp:114
t_const_iterator< T > cend() const
Definition: buffer.hpp:622
const_iterator begin() const
Definition: buffer.hpp:637
size_t clear()
Definition: buffer.hpp:357
#define OSMIUM_DEPRECATED
Definition: compatibility.hpp:50
OSMIUM_DEPRECATED void set_full_callback(std::function< void(Buffer &)> full)
Definition: buffer.hpp:285
iterator get_iterator(size_t offset)
Definition: buffer.hpp:567
bool operator!=(const Buffer &lhs, const Buffer &rhs) noexcept
Definition: buffer.hpp:731
void grow(size_t size)
Definition: buffer.hpp:306
const_iterator end() const
Definition: buffer.hpp:646
Definition: item_iterator.hpp:119
unsigned char * m_data
Definition: buffer.hpp:112
constexpr item_size_type align_bytes
Definition: item.hpp:53
size_t capacity() const noexcept
Definition: buffer.hpp:233
Buffer(unsigned char *data, size_t capacity, size_t committed)
Definition: buffer.hpp:169
Definition: reader_iterator.hpp:39
ItemIterator< TMember > & advance_once()
Definition: item_iterator.hpp:168
Buffer(size_t capacity, auto_grow auto_grow=auto_grow::yes)
Definition: buffer.hpp:197
unsigned char * data() const
Definition: item_iterator.hpp:189
bool operator==(const Buffer &lhs, const Buffer &rhs) noexcept
Definition: buffer.hpp:724
t_iterator< T > end()
Definition: buffer.hpp:581
Namespace for everything in the Osmium library.
Definition: assembler.hpp:59
T & add_item(const T &item)
Definition: buffer.hpp:453
void purge_removed(TCallbackClass *callback)
Definition: buffer.hpp:686
t_iterator< T > begin()
Definition: buffer.hpp:525
void add_buffer(const Buffer &buffer)
Definition: buffer.hpp:470
friend void swap(Buffer &lhs, Buffer &rhs)
Definition: buffer.hpp:657
const_iterator cbegin() const
Definition: buffer.hpp:605
size_t m_committed
Definition: buffer.hpp:115
bool is_aligned() const noexcept
Definition: buffer.hpp:260
t_iterator< osmium::OSMEntity > iterator
Definition: buffer.hpp:508
Buffer() noexcept
Definition: buffer.hpp:129
unsigned char * reserve_space(const size_t size)
Definition: buffer.hpp:413
auto_grow m_auto_grow
Definition: buffer.hpp:116
void push_back(const osmium::memory::Item &item)
Definition: buffer.hpp:484
iterator end()
Definition: buffer.hpp:594
size_t committed() const noexcept
Definition: buffer.hpp:241
iterator begin()
Definition: buffer.hpp:538
const_iterator cend() const
Definition: buffer.hpp:627
size_t m_capacity
Definition: buffer.hpp:113
t_const_iterator< T > get_iterator(size_t offset) const
Definition: buffer.hpp:611
size_t written() const noexcept
Definition: buffer.hpp:250
Buffer(unsigned char *data, size_t size)
Definition: buffer.hpp:147
t_const_iterator< T > end() const
Definition: buffer.hpp:642
Definition: buffer.hpp:97
unsigned char * data() const noexcept
Definition: buffer.hpp:224
Definition: buffer.hpp:58
const_iterator get_iterator(size_t offset) const
Definition: buffer.hpp:616
t_const_iterator< T > cbegin() const
Definition: buffer.hpp:600
t_const_iterator< T > begin() const
Definition: buffer.hpp:633
auto_grow
Definition: buffer.hpp:104
std::vector< unsigned char > m_memory
Definition: buffer.hpp:111
buffer_is_full()
Definition: buffer.hpp:60
void rollback()
Definition: buffer.hpp:345
t_iterator< T > get_iterator(size_t offset)
Definition: buffer.hpp:553
std::function< void(Buffer &)> m_full
Definition: buffer.hpp:117
Buffer & operator=(const Buffer &)=delete
size_t commit()
Definition: buffer.hpp:331
t_const_iterator< osmium::OSMEntity > const_iterator
Definition: buffer.hpp:514