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 (http://osmcode.org/libosmium).
Copyright 2013-2016 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 <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <new>
#include <string>
#include <type_traits>
#include <osmium/memory/buffer.hpp>
#include <osmium/memory/item.hpp>
#include <osmium/osm/types.hpp>
#include <osmium/util/cast.hpp>
namespace osmium {
namespace builder {
class Builder {
osmium::memory::Buffer& m_buffer;
Builder* m_parent;
size_t m_item_offset;
Builder(const Builder&) = delete;
Builder(Builder&&) = delete;
Builder& operator=(const Builder&) = delete;
Builder& operator=(Builder&&) = delete;
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()) {
m_buffer.reserve_space(size);
assert(buffer.is_aligned());
if (m_parent) {
m_parent->add_size(size);
}
}
~Builder() = default;
osmium::memory::Item& item() const {
return *reinterpret_cast<osmium::memory::Item*>(m_buffer.data() + m_item_offset);
}
public:
void add_padding(bool self = false) {
auto padding = osmium::memory::align_bytes - (size() % osmium::memory::align_bytes);
if (padding != osmium::memory::align_bytes) {
std::fill_n(m_buffer.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(uint32_t size) {
item().add_size(size);
if (m_parent) {
m_parent->add_size(size);
}
}
uint32_t size() const noexcept {
return item().byte_size();
}
void add_item(const osmium::memory::Item* item) {
unsigned char* target = m_buffer.reserve_space(item->padded_size());
std::copy_n(reinterpret_cast<const unsigned char*>(item), item->padded_size(), target);
add_size(item->padded_size());
}
template <typename T>
T* reserve_space_for() {
assert(m_buffer.is_aligned());
return reinterpret_cast<T*>(m_buffer.reserve_space(sizeof(T)));
}
osmium::memory::item_size_type append(const char* data, const osmium::memory::item_size_type length) {
unsigned char* target = m_buffer.reserve_space(length);
std::copy_n(reinterpret_cast<const unsigned char*>(data), length, target);
return length;
}
osmium::memory::item_size_type append(const char* str) {
return append(str, static_cast<osmium::memory::item_size_type>(std::strlen(str) + 1));
}
osmium::memory::item_size_type append_zero() {
*m_buffer.reserve_space(1) = '\0';
return 1;
}
osmium::memory::Buffer& buffer() noexcept {
return m_buffer;
}
}; // class Builder
template <typename TItem>
class ObjectBuilder : public Builder {
public:
explicit ObjectBuilder(osmium::memory::Buffer& buffer, Builder* parent = nullptr) :
Builder(buffer, parent, sizeof(TItem)) {
new (&item()) TItem();
}
TItem& object() noexcept {
return static_cast<TItem&>(item());
}
void add_user(const char* user, const string_size_type length) {
object().set_user_size(length + 1);
add_size(append(user, length) + append_zero());
add_padding(true);
}
void add_user(const char* user) {
add_user(user, static_cast_with_assert<string_size_type>(std::strlen(user)));
}
void add_user(const std::string& user) {
add_user(user.data(), static_cast_with_assert<string_size_type>(user.size()));
}
}; // class ObjectBuilder
} // namespace builder
} // namespace osmium
#endif // OSMIUM_BUILDER_BUILDER_HPP
osmium::builder::Builder::item
osmium::memory::Item & item() const
Definition: builder.hpp:84
osmium::builder::Builder::m_parent
Builder * m_parent
Definition: builder.hpp:60
osmium::builder::Builder::Builder
Builder(osmium::memory::Buffer &buffer, Builder *parent, osmium::memory::item_size_type size)
Definition: builder.hpp:71
osmium::memory::Buffer::is_aligned
bool is_aligned() const noexcept
Definition: buffer.hpp:260
osmium::builder::Builder::buffer
osmium::memory::Buffer & buffer() noexcept
Return the buffer this builder is using.
Definition: builder.hpp:179
osmium::builder::Builder::append_zero
osmium::memory::item_size_type append_zero()
Definition: builder.hpp:173
osmium::builder::ObjectBuilder::ObjectBuilder
ObjectBuilder(osmium::memory::Buffer &buffer, Builder *parent=nullptr)
Definition: builder.hpp:191
osmium::builder::ObjectBuilder::add_user
void add_user(const char *user, const string_size_type length)
Definition: builder.hpp:206
osmium::memory::align_bytes
constexpr item_size_type align_bytes
Definition: item.hpp:53
osmium::memory::Item::padded_size
item_size_type padded_size() const
Definition: item.hpp:151
types.hpp
osmium::builder::Builder::~Builder
~Builder()=default
osmium::memory::Item
Definition: item.hpp:97
osmium
Namespace for everything in the Osmium library.
Definition: assembler.hpp:59
osmium::memory::Item::add_size
Item & add_size(const item_size_type size) noexcept
Definition: item.hpp:112
osmium::builder::Builder::reserve_space_for
T * reserve_space_for()
Definition: builder.hpp:138
osmium::memory::Buffer::data
unsigned char * data() const noexcept
Definition: buffer.hpp:224
osmium::builder::Builder::append
osmium::memory::item_size_type append(const char *str)
Definition: builder.hpp:164
osmium::string_size_type
uint16_t string_size_type
Definition: types.hpp:59
osmium::memory::Buffer::reserve_space
unsigned char * reserve_space(const size_t size)
Definition: buffer.hpp:417
osmium::builder::ObjectBuilder::add_user
void add_user(const std::string &user)
Definition: builder.hpp:226
cast.hpp
osmium::memory::Item::byte_size
item_size_type byte_size() const noexcept
Definition: item.hpp:147
item.hpp
detail::add_user
void add_user(TBuilder &builder, const TArgs &...args)
Definition: attr.hpp:584
osmium::builder::Builder::m_buffer
osmium::memory::Buffer & m_buffer
Definition: builder.hpp:59
osmium::memory::Buffer
Definition: buffer.hpp:97
osmium::builder::Builder::m_item_offset
size_t m_item_offset
Definition: builder.hpp:61
osmium::builder::ObjectBuilder
Definition: builder.hpp:186
buffer.hpp
osmium::builder::Builder::append
osmium::memory::item_size_type append(const char *data, const osmium::memory::item_size_type length)
Definition: builder.hpp:152
osmium::builder::Builder::Builder
Builder(const Builder &)=delete
osmium::builder::Builder::size
uint32_t size() const noexcept
Definition: builder.hpp:123
osmium::builder::Builder::add_size
void add_size(uint32_t size)
Definition: builder.hpp:116
osmium::memory::item_size_type
uint32_t item_size_type
Definition: item.hpp:50
osmium::builder::Builder::add_item
void add_item(const osmium::memory::Item *item)
Definition: builder.hpp:127
osmium::osm_entity_bits::object
node, way, relation, or area object
Definition: entity_bits.hpp:69
osmium::builder::ObjectBuilder::object
TItem & object() noexcept
Definition: builder.hpp:196
osmium::builder::ObjectBuilder::add_user
void add_user(const char *user)
Definition: builder.hpp:217
osmium::builder::Builder
Definition: builder.hpp:57
osmium::builder::Builder::operator=
Builder & operator=(const Builder &)=delete
osmium::builder::Builder::add_padding
void add_padding(bool self=false)
Definition: builder.hpp:103