1 #ifndef OSMIUM_MEMORY_BUFFER_HPP
2 #define OSMIUM_MEMORY_BUFFER_HPP
61 std::runtime_error(
"Osmium buffer is full") {
157 throw std::invalid_argument(
"buffer size needs to be multiple of alignment");
175 m_capacity(capacity),
176 m_written(committed),
177 m_committed(committed) {
179 throw std::invalid_argument(
"buffer capacity needs to be multiple of alignment");
182 throw std::invalid_argument(
"buffer parameter 'committed' needs to be multiple of alignment");
201 m_memory(new unsigned char[capacity]),
202 m_data(m_memory.
get()),
203 m_capacity(capacity),
208 throw std::invalid_argument(
"buffer capacity needs to be multiple of alignment");
242 unsigned char*
data() const noexcept {
243 assert(m_data &&
"This must be a valid buffer");
279 assert(m_data &&
"This must be a valid buffer");
304 assert(m_data &&
"This must be a valid buffer");
325 assert(m_data &&
"This must be a valid buffer");
327 throw std::logic_error(
"Can't grow Buffer if it doesn't use internal memory management.");
329 if (m_capacity < size) {
331 throw std::invalid_argument(
"buffer capacity needs to be multiple of alignment");
333 std::unique_ptr<unsigned char[]> memory(
new unsigned char[size]);
334 std::copy_n(m_memory.get(),
m_capacity, memory.get());
336 swap(m_memory, memory);
337 m_data = m_memory.get();
355 assert(m_data &&
"This must be a valid buffer");
356 assert(
m_builder_count == 0 &&
"Make sure there are no Builder objects still in scope");
371 assert(m_data &&
"This must be a valid buffer");
372 assert(
m_builder_count == 0 &&
"Make sure there are no Builder objects still in scope");
386 assert(
m_builder_count == 0 &&
"Make sure there are no Builder objects still in scope");
403 template <
typename T>
404 T&
get(
const size_t offset)
const {
405 assert(m_data &&
"This must be a valid buffer");
406 return *
reinterpret_cast<T*
>(&m_data[offset]);
443 assert(m_data &&
"This must be a valid buffer");
445 if (m_written + size > m_capacity && m_full) {
449 if (m_written + size > m_capacity) {
452 size_t new_capacity = m_capacity * 2;
453 while (m_written + size > new_capacity) {
481 template <
typename T>
483 assert(m_data &&
"This must be a valid buffer");
485 std::copy_n(reinterpret_cast<const unsigned char*>(&item), item.padded_size(), target);
486 return *
reinterpret_cast<T*
>(target);
501 assert(m_data &&
"This must be a valid buffer");
502 assert(buffer &&
"Buffer parameter must be a valid buffer");
503 assert(
m_builder_count == 0 &&
"Make sure there are no Builder objects still in scope");
518 assert(m_data &&
"This must be a valid buffer");
519 assert(
m_builder_count == 0 &&
"Make sure there are no Builder objects still in scope");
528 template <
typename T>
535 template <
typename T>
550 template <
typename T>
555 template <
typename T>
568 template <
typename T>
570 assert(m_data &&
"This must be a valid buffer");
583 assert(m_data &&
"This must be a valid buffer");
584 return iterator(m_data, m_data + m_committed);
596 template <
typename T>
598 assert(m_data &&
"This must be a valid buffer");
612 assert(m_data &&
"This must be a valid buffer");
613 return iterator(m_data + offset, m_data + m_committed);
624 template <
typename T>
626 assert(m_data &&
"This must be a valid buffer");
639 assert(m_data &&
"This must be a valid buffer");
640 return iterator(m_data + m_committed, m_data + m_committed);
643 template <
typename T>
645 assert(m_data &&
"This must be a valid buffer");
650 assert(m_data &&
"This must be a valid buffer");
654 template <
typename T>
656 assert(m_data &&
"This must be a valid buffer");
661 assert(m_data &&
"This must be a valid buffer");
665 template <
typename T>
667 assert(m_data &&
"This must be a valid buffer");
672 assert(m_data &&
"This must be a valid buffer");
673 return const_iterator(m_data + m_committed, m_data + m_committed);
676 template <
typename T>
685 template <
typename T>
697 explicit operator bool() const noexcept {
698 return m_data !=
nullptr;
729 template <
typename TCallbackClass>
731 assert(m_data &&
"This must be a valid buffer");
740 next = std::next(it_read);
741 if (!it_read->removed()) {
742 if (it_read != it_write) {
743 assert(it_read.data() >=
data());
745 size_t old_offset =
static_cast<size_t>(it_read.data() -
data());
746 size_t new_offset =
static_cast<size_t>(it_write.
data() -
data());
747 callback->moving_in_buffer(old_offset, new_offset);
748 std::memmove(it_write.
data(), it_read.data(), it_read->padded_size());
755 m_written =
static_cast<size_t>(it_write.
data() -
data());
776 return lhs.data() == rhs.data() && lhs.capacity() == rhs.capacity() && lhs.committed() == rhs.committed();
780 return ! (lhs == rhs);
787 #endif // OSMIUM_MEMORY_BUFFER_HPP
size_t m_written
Definition: buffer.hpp:114
void swap(Buffer &other)
Definition: buffer.hpp:701
t_const_iterator< T > cend() const
Definition: buffer.hpp:666
const_iterator begin() const
Definition: buffer.hpp:681
size_t clear()
Definition: buffer.hpp:385
#define OSMIUM_DEPRECATED
Definition: compatibility.hpp:50
bool is_aligned() const noexcept
Definition: buffer.hpp:278
size_t written() const noexcept
Definition: buffer.hpp:268
OSMIUM_DEPRECATED void set_full_callback(std::function< void(Buffer &)> full)
Definition: buffer.hpp:303
Definition: item_iterator.hpp:175
iterator get_iterator(size_t offset)
Definition: buffer.hpp:611
bool operator!=(const Buffer &lhs, const Buffer &rhs) noexcept
Definition: buffer.hpp:779
void grow(size_t size)
Definition: buffer.hpp:324
const_iterator end() const
Definition: buffer.hpp:690
Definition: item_iterator.hpp:59
unsigned char * m_data
Definition: buffer.hpp:112
ItemIteratorRange< const T > select() const
Definition: buffer.hpp:556
void increment_builder_count() noexcept
Definition: buffer.hpp:223
Buffer(unsigned char *data, size_t capacity, size_t committed)
Definition: buffer.hpp:172
Definition: reader_iterator.hpp:39
void swap(Buffer &lhs, Buffer &rhs)
Definition: buffer.hpp:761
Buffer(size_t capacity, auto_grow auto_grow=auto_grow::yes)
Definition: buffer.hpp:200
bool operator==(const Buffer &lhs, const Buffer &rhs) noexcept
Definition: buffer.hpp:772
t_iterator< T > end()
Definition: buffer.hpp:625
uint8_t m_builder_count
Definition: buffer.hpp:117
Namespace for everything in the Osmium library.
Definition: assembler.hpp:73
T & add_item(const T &item)
Definition: buffer.hpp:482
void purge_removed(TCallbackClass *callback)
Definition: buffer.hpp:730
t_iterator< T > begin()
Definition: buffer.hpp:569
void add_buffer(const Buffer &buffer)
Definition: buffer.hpp:500
uint8_t builder_count() const noexcept
Definition: buffer.hpp:232
const_iterator cbegin() const
Definition: buffer.hpp:649
size_t m_committed
Definition: buffer.hpp:115
ItemIterator< TMember > & advance_once() noexcept
Definition: item_iterator.hpp:114
t_iterator< osmium::OSMEntity > iterator
Definition: buffer.hpp:542
unsigned char * data() const noexcept
Definition: buffer.hpp:242
Buffer() noexcept
Definition: buffer.hpp:132
size_t capacity() const noexcept
Definition: buffer.hpp:251
unsigned char * reserve_space(const size_t size)
Definition: buffer.hpp:442
auto_grow m_auto_grow
Definition: buffer.hpp:119
void push_back(const osmium::memory::Item &item)
Definition: buffer.hpp:517
constexpr const item_size_type align_bytes
Definition: item.hpp:62
iterator end()
Definition: buffer.hpp:638
iterator begin()
Definition: buffer.hpp:582
const_iterator cend() const
Definition: buffer.hpp:671
size_t m_capacity
Definition: buffer.hpp:113
t_const_iterator< T > get_iterator(size_t offset) const
Definition: buffer.hpp:655
size_t committed() const noexcept
Definition: buffer.hpp:259
Buffer(unsigned char *data, size_t size)
Definition: buffer.hpp:150
t_const_iterator< T > end() const
Definition: buffer.hpp:686
Definition: buffer.hpp:97
T & get(const size_t offset) const
Definition: buffer.hpp:404
Definition: buffer.hpp:58
void decrement_builder_count() noexcept
Definition: buffer.hpp:227
const_iterator get_iterator(size_t offset) const
Definition: buffer.hpp:660
t_const_iterator< T > cbegin() const
Definition: buffer.hpp:644
t_const_iterator< T > begin() const
Definition: buffer.hpp:677
auto_grow
Definition: buffer.hpp:104
data_type data() noexcept
Definition: item_iterator.hpp:135
buffer_is_full()
Definition: buffer.hpp:60
ItemIteratorRange< T > select()
Definition: buffer.hpp:551
void rollback()
Definition: buffer.hpp:370
std::unique_ptr< unsigned char[]> m_memory
Definition: buffer.hpp:111
t_iterator< T > get_iterator(size_t offset)
Definition: buffer.hpp:597
std::function< void(Buffer &)> m_full
Definition: buffer.hpp:120
Buffer & operator=(const Buffer &)=delete
size_t commit()
Definition: buffer.hpp:354
t_const_iterator< osmium::OSMEntity > const_iterator
Definition: buffer.hpp:548