Loading...
Searching...
No Matches
builder.hpp
Go to the documentation of this file.
#ifndef OSMIUM_BUILDER_BUILDER_HPP
#define OSMIUM_BUILDER_BUILDER_HPP
/*
This file is part of Osmium (https://osmcode.org/libosmium).
Copyright 2013-2025 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 <osmium/memory/item.hpp>
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <cstring>
namespace osmium {
namespace builder {
class Builder {
osmium::memory::Buffer& m_buffer;
Builder* m_parent;
std::size_t m_item_offset;
protected:
explicit Builder(osmium::memory::Buffer& buffer, Builder* parent, osmium::memory::item_size_type size) :
m_buffer(buffer),
m_parent(parent),
m_item_offset(buffer.written() - buffer.committed()) {
reserve_space(size);
assert(buffer.is_aligned());
if (m_parent) {
assert(m_buffer.builder_count() == 1 && "Only one sub-builder can be open at any time.");
m_parent->add_size(size);
} else {
assert(m_buffer.builder_count() == 0 && "Only one builder can be open at any time.");
}
#ifndef NDEBUG
m_buffer.increment_builder_count();
#endif
}
#ifdef NDEBUG
~Builder() noexcept = default;
#else
~Builder() noexcept {
m_buffer.decrement_builder_count();
}
#endif
unsigned char* item_pos() const noexcept {
return m_buffer.data() + m_buffer.committed() + m_item_offset;
}
osmium::memory::Item& item() const noexcept {
return *reinterpret_cast<osmium::memory::Item*>(item_pos());
}
unsigned char* reserve_space(std::size_t size) {
return m_buffer.reserve_space(size);
}
void add_padding(bool self = false) {
// We know the padding is only a very small number, so it will
// always fit.
const auto padding = static_cast<osmium::memory::item_size_type>(osmium::memory::align_bytes - (size() % osmium::memory::align_bytes));
if (padding != osmium::memory::align_bytes) {
std::fill_n(reserve_space(padding), padding, 0);
if (self) {
add_size(padding);
} else if (m_parent) {
m_parent->add_size(padding);
assert(m_parent->size() % osmium::memory::align_bytes == 0);
}
}
}
void add_size(osmium::memory::item_size_type size) {
item().add_size(size);
if (m_parent) {
m_parent->add_size(size);
}
}
uint32_t size() const noexcept {
return item().byte_size();
}
template <typename T>
T* reserve_space_for() {
assert(m_buffer.is_aligned());
return reinterpret_cast<T*>(reserve_space(sizeof(T)));
}
osmium::memory::item_size_type append(const char* data, const osmium::memory::item_size_type length) {
unsigned char* target = reserve_space(length);
std::copy_n(reinterpret_cast<const unsigned char*>(data), length, target);
return length;
}
osmium::memory::item_size_type append_with_zero(const char* data, const osmium::memory::item_size_type length) {
unsigned char* target = reserve_space(length + 1);
std::copy_n(reinterpret_cast<const unsigned char*>(data), length, target);
target[length] = '\0';
return length + 1;
}
osmium::memory::item_size_type append(const char* str) {
return append(str, static_cast<osmium::memory::item_size_type>(std::strlen(str) + 1));
}
public:
Builder(const Builder&) = delete;
Builder(Builder&&) = delete;
Builder& operator=(const Builder&) = delete;
Builder& operator=(Builder&&) = delete;
osmium::memory::Buffer& buffer() noexcept {
return m_buffer;
}
void add_item(const osmium::memory::Item& item) {
m_buffer.add_item(item);
add_size(item.padded_size());
}
}; // class Builder
} // namespace builder
} // namespace osmium
#endif // OSMIUM_BUILDER_BUILDER_HPP
buffer.hpp
osmium::builder::Builder
Definition builder.hpp:56
osmium::builder::Builder::reserve_space
unsigned char * reserve_space(std::size_t size)
Definition builder.hpp:97
osmium::builder::Builder::append
osmium::memory::item_size_type append(const char *data, const osmium::memory::item_size_type length)
Definition builder.hpp:159
osmium::builder::Builder::size
uint32_t size() const noexcept
Definition builder.hpp:136
osmium::builder::Builder::reserve_space_for
T * reserve_space_for()
Definition builder.hpp:145
osmium::builder::Builder::append
osmium::memory::item_size_type append(const char *str)
Definition builder.hpp:185
osmium::builder::Builder::add_padding
void add_padding(bool self=false)
Definition builder.hpp:114
osmium::builder::Builder::Builder
Builder(osmium::memory::Buffer &buffer, Builder *parent, osmium::memory::item_size_type size)
Definition builder.hpp:64
osmium::builder::Builder::add_size
void add_size(osmium::memory::item_size_type size)
Definition builder.hpp:129
osmium::builder::Builder::Builder
Builder(Builder &&)=delete
osmium::builder::Builder::operator=
Builder & operator=(const Builder &)=delete
osmium::builder::Builder::m_buffer
osmium::memory::Buffer & m_buffer
Definition builder.hpp:58
osmium::builder::Builder::item_pos
unsigned char * item_pos() const noexcept
Definition builder.hpp:89
osmium::builder::Builder::buffer
osmium::memory::Buffer & buffer() noexcept
Return the buffer this builder is using.
Definition builder.hpp:198
osmium::builder::Builder::Builder
Builder(const Builder &)=delete
osmium::builder::Builder::m_item_offset
std::size_t m_item_offset
Definition builder.hpp:60
osmium::builder::Builder::operator=
Builder & operator=(Builder &&)=delete
osmium::builder::Builder::item
osmium::memory::Item & item() const noexcept
Definition builder.hpp:93
osmium::builder::Builder::append_with_zero
osmium::memory::item_size_type append_with_zero(const char *data, const osmium::memory::item_size_type length)
Definition builder.hpp:172
osmium::builder::Builder::add_item
void add_item(const osmium::memory::Item &item)
Definition builder.hpp:206
osmium::builder::Builder::m_parent
Builder * m_parent
Definition builder.hpp:59
osmium::builder::Builder::~Builder
~Builder() noexcept
Definition builder.hpp:84
osmium::memory::Item
Definition item.hpp:105
osmium::memory::Item::byte_size
item_size_type byte_size() const noexcept
Definition item.hpp:163
osmium::memory::Item::add_size
Item & add_size(const item_size_type size) noexcept
Definition item.hpp:121
osmium::memory::Item::padded_size
item_size_type padded_size() const
Definition item.hpp:167
item.hpp
osmium::memory::align_bytes
@ align_bytes
Definition item.hpp:61
osmium::memory::item_size_type
uint32_t item_size_type
Definition item.hpp:57
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53