osrm-backend/include/osmium/builder/builder.hpp
Dennis Luxen c0402345f7 Squashed 'third_party/libosmium/' changes from f074d94..8bcd4ea
8bcd4ea Add explicit cast operator from osmium::Timestamp to uint32_t.
0b24814 Fixed the full_queue_sleep_duration const (again).
5e19dd2 Try different workaround for windows...
df5d6c9 Fix build error on Windows.
1553adf Workaround for missing support for u8 string literals on Windows.
aa5e44a Do not build benchmarks in Appveyor to speed up build.
2b22b31 Remove warning generated by assert by casting to largest type.
148c5e3 Fix inclusion of our own css file into doxygen documentation.
13bce6f Split out test thats fails on Windows and do not run it on appveyor.
ca04757 Make CMake add_unit_test() function more flexible.
6c04a63 Tell CMake to output json file with compile commands.
c0dd848 Fix indentation in some tests.
dfa7e4b Formatting: Consistently use spaces around equal signs.
08fe6db Add change log, release version 2.1.0.
667192e Add XML tests.
28acfc7 Make sorting the PBF stringtables optional.
8184781 Fix PBFInputFormat. Use member variable instead of static variable.
2b48945 Fix comment.
6d37054 Rename m_done to m_quit_input_thread to clarify what it is for.
bc23083 Fix race condition in PBF reader.
7fc380e Add various docs, noexcepts, asserts, and tests.
aeaf4d7 Not a good idea to how overbroad patterns in .gitignore.
c1ef2f9 Bugfix: Multipolygon collector was accessing non-existent NodeRef.
0ef9a13 Add noexcept and docs to some functions in NodeRefList class.
da4d764 Workaround for Doxygen bug.
e67d5d9 use absolute paths to osmium in YouCompleteMe configuration
38abeac remove template parameter from NodeRefList
c47adf0 Add check that osm xml file has <osm> or <osmChange> as top-level element.
5e4af90 Updated version number to 2.0.0.
5b2bc3e Workaround in cmake test for sparsehash size.

git-subtree-dir: third_party/libosmium
git-subtree-split: 8bcd4ea771696812bbb08ebc58e3ee22d0538070
2015-04-13 15:44:38 +02:00

224 lines
7.8 KiB
C++

#ifndef OSMIUM_BUILDER_BUILDER_HPP
#define OSMIUM_BUILDER_BUILDER_HPP
/*
This file is part of Osmium (http://osmcode.org/libosmium).
Copyright 2013-2015 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 {
/**
* @brief Classes for building OSM objects and other items in buffers
*/
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:
/**
* Add padding to buffer (if needed) to align data properly.
*
* This calculates how many padding bytes are needed and adds
* as many zero bytes to the buffer. It also adds this number
* to the size of the current item (if the "self" param is
* true) and recursively to all the parent items.
*
* @param self If true add number of padding bytes to size
* of current item. Size is always added to
* parent item (if any).
*
*/
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());
}
/**
* Reserve space for an object of class T in buffer and return
* pointer to it.
*/
template <class T>
T* reserve_space_for() {
assert(m_buffer.is_aligned());
return reinterpret_cast<T*>(m_buffer.reserve_space(sizeof(T)));
}
/**
* Append data to buffer.
*
* @param data Pointer to data.
* @param length Length of data in bytes. If data is a
* \0-terminated string, length must contain the
* \0 byte.
*/
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;
}
/**
* Append \0-terminated string to buffer.
*/
osmium::memory::item_size_type append(const char* str) {
return append(str, static_cast<osmium::memory::item_size_type>(std::strlen(str) + 1));
}
/// Return the buffer this builder is using.
osmium::memory::Buffer& buffer() noexcept {
return m_buffer;
}
}; // class Builder
template <class TItem>
class ObjectBuilder : public Builder {
static_assert(std::is_base_of<osmium::memory::Item, TItem>::value, "ObjectBuilder can only build objects derived from osmium::memory::Item");
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());
}
/**
* Add user name to buffer.
*
* @param user Pointer to user name.
* @param length Length of user name including \0 byte.
*/
void add_user(const char* user, const string_size_type length) {
object().set_user_size(length);
add_size(append(user, length));
add_padding(true);
}
/**
* Add user name to buffer.
*
* @param user Pointer to \0-terminated user name.
*/
void add_user(const char* user) {
add_user(user, static_cast_with_assert<string_size_type>(std::strlen(user) + 1));
}
/**
* Add user name to buffer.
*
* @param user User name.
*/
void add_user(const std::string& user) {
add_user(user.data(), static_cast_with_assert<string_size_type>(user.size() + 1));
}
}; // class ObjectBuilder
} // namespace builder
} // namespace osmium
#endif // OSMIUM_BUILDER_BUILDER_HPP