40c4a48 Release v2.10.0 6addb2e Search for protozero also in the place where libosmium was found. 9c0d4bb Consistently use lhs and rhs as parameter names in operators etc. 6179759 Update change log. 894eb7d Explicitly use size_t to get no narrowing conversions. e549e73 Fix CMake config for index_lookup. ccf0bc2 Fix bug where some compilers deduce the wrong type. fc3507d Cleaned and documented index example and renamed to osmium_index_lookup. d3c3036 Rename serdump example to dump_internal and document it. 0e9822e Also forward set_uid_from_signed() function. 29ef95c Rename build_taglist function because it was to similar to build_tag_list. c088dd0 Move static constant out of class so clang will compile it. 606cdc4 Fix CMake build script: Path for finding libosmium. 65f91fe Removed unused typedef. 07174f5 Use condition_variable to tell producer when thread queue is not full. b35e957 Some code cleanup in XML parser. c703dff Fix problem with MemberMeta. 1ccbbef Refactoring CompressionFactory. 6561bd6 Use our own exception type for map factory errors. 56e31fa Throw not_found error directly instead of using helper function. 28230c3 Test empty value for node location store, reorganize tests. 2ba316c Reorganize and fix projection tests. f949485 Don't run quite as many tests with projections because they are slow. 1bad16d Add information about build environment to benchmark output. 99617bb Change proj_geojson benchmark into mercator benchmark. 553b946 Allow optional parameters on Reader in any order. dcc3d8f Factor out some common code. 004d8cd Fix forwarding constructor. 9702978 Fix metadata check. 6cfb6c4 Faster implementation of decode_dense_nodes without reading metdata. 4ba4638 Add additional read_metadata parameter to other Reader constructors. d005937 Optionally ignore metadata when reading file. 63961da Mark all CRC32 update functions as noexcept. cc4ca75 Refactor set_user() function to speed it up. 38d19dc Update comments with file sizes. OSM has grown... f7d0824 Add new benchmark that shows performance when main thread is busy. 25070dc Use const ref params. 88e8d96 Mark add_user() as deprecated. f58d9db Refactor some low-level collection code to clean up code. 4680def Add example showing how to create OSM data out of thin air. d42fd50 Add an example showing how tags in OSM files can be changed. 49bf5bc Add additional constructors to Builders taking a reference to parent. 7b91d63 Change Builder::add_item() to take a reference instead of pointer. 2957e48 Some cleanup of examples. d0b458d Calculate size of object at compile time. 3fbb6e7 Use explicit cast. 1851f3d Remove a test that depends on math details. f6a0802 Various cleanups of example programs. ba4921f Rename add_user() to set_user(). d7637c9 Various cleanups related to builders. 07827bc Fix add_user(). 9a5b395 Also refactor OSMObjectBuilder like ChangesetBuilder before. b1f423c Use call chaining on the builder. e49473d Get rid of ObjectBuilder class. 67d70b9 Refactor ChangesetBuilder::add_user(). 8199c33 Make ChangesetBuilder derive directly from Builder. 61d1b67 Simplify some code. d38467a Change derivation hierarchy of some builder classes. b52f8af Refactor Builder code. d012bfa Refactored some code setting attributes on objects using builder instead. 6a05f60 Also forward set_removed function from builders. 8d63b7d Return *this from Builder setter functions and test it. 72a1266 Update catch.hpp to newest version. 3424a74 Check GEOS version is <= 3.5. aee9f9d Cleanup test code. aef198c Improved asserts in Buffer. a98b9bf Code cleanup in tests. a150466 Use GDAL/OGR instead of GEOS to test our WKB implementation. b04a525 Refactor test. 39aa932 Refactored test_factory_with_projection so it works without GEOS. 648f43a Remove unused dependency on geos from tile test. f1748ae Add setters to Builder classes forwarding to underlying objects. 8166879 In debug mode check that Builders are correctly called and destructed. 1c4257e Call commit() on buffer only after all builders are destructed. 2618636 Add functions to check availability of relation members. b45a4d9 Mark RelationMember::set_ref() as deprecated. 7886771 Move "basic" and "buffer" tests to "osm" and "memory", respectively. b664685 Use functions instead of macros in location test for faster compile. b4929ac Add more tests for number parser. 02662a7 Merge pull request #171 from lonvia/fix-long-exponentials 5344a6c fix parsing of numbers in e-notiation with many post-comma digits 3aeaff3 Add some typedefs to NodeRefList and memory::Collection. e750665 Add iterators to IdSetSmall and add docs and tests to IdSet classes. 50ecb2a Add more features to IdSetDense, including unset and iterator. e3dec78 Make IdSet virtual base class with two implementations. 8ea0153 Use C array instead of std::array in IdSet and clear explicitly. 3ba9905 Deprecate osmium::index::BoolVector in favour of new IdSet. 453d1ca Add osmium::index::IdSet. c78254e Add function to (temporarily) disable the progress bar. 4d88a9f Better document osmium::io::Header class. 320e3af Look for protozero includes in CMake config. 838d25e Allow optional checking for libosmium version number in CMake config. 6ce60c1 Fix entity_bits static_assert() tests. f054731 Update change log. 77ac4c2 Make sleep duration for full queues much smaller. 7e39c01 Make some entity_bits functions constexpr. 69ea72f Fix ~ operator on entity_bits and more tests for entity bits. dafe4cf Update embedded Catch unit test header to version 1.5.7. a41c832 Fixed parsing of location coordinates starting with decimal dot. 6523bae README cosmetics. 229acac Add tests for some examples. f1e753d Merge pull request #163 from sebastic/executable-not-elf-or-script ccea2d5 Remove executable bit from .cpp files. af77fb4 Changelog formatting fixes. git-subtree-dir: third_party/libosmium git-subtree-split: 40c4a48f88d25edace6f0b9e079c306308c7760b
658 lines
26 KiB
C++
658 lines
26 KiB
C++
#ifndef OSMIUM_BUILDER_OSM_OBJECT_BUILDER_HPP
|
|
#define OSMIUM_BUILDER_OSM_OBJECT_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 <cassert>
|
|
#include <cstring>
|
|
#include <initializer_list>
|
|
#include <limits>
|
|
#include <new>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <utility>
|
|
|
|
#include <osmium/builder/builder.hpp>
|
|
#include <osmium/osm/item_type.hpp>
|
|
#include <osmium/osm/location.hpp>
|
|
#include <osmium/osm/node.hpp>
|
|
#include <osmium/osm/node_ref.hpp>
|
|
#include <osmium/osm/object.hpp>
|
|
#include <osmium/osm/tag.hpp>
|
|
#include <osmium/osm/types.hpp>
|
|
#include <osmium/memory/item.hpp>
|
|
#include <osmium/osm/area.hpp>
|
|
#include <osmium/osm/changeset.hpp>
|
|
#include <osmium/osm/relation.hpp>
|
|
#include <osmium/osm/timestamp.hpp>
|
|
#include <osmium/osm/way.hpp>
|
|
#include <osmium/util/compatibility.hpp>
|
|
|
|
namespace osmium {
|
|
|
|
class Node;
|
|
|
|
namespace memory {
|
|
class Buffer;
|
|
} // namespace memory
|
|
|
|
namespace builder {
|
|
|
|
class TagListBuilder : public Builder {
|
|
|
|
public:
|
|
|
|
explicit TagListBuilder(osmium::memory::Buffer& buffer, Builder* parent = nullptr) :
|
|
Builder(buffer, parent, sizeof(TagList)) {
|
|
new (&item()) TagList();
|
|
}
|
|
|
|
explicit TagListBuilder(Builder& parent) :
|
|
Builder(parent.buffer(), &parent, sizeof(TagList)) {
|
|
new (&item()) TagList();
|
|
}
|
|
|
|
~TagListBuilder() {
|
|
add_padding();
|
|
}
|
|
|
|
/**
|
|
* Add tag to buffer.
|
|
*
|
|
* @param key Tag key (0-terminated string).
|
|
* @param value Tag value (0-terminated string).
|
|
*/
|
|
void add_tag(const char* key, const char* value) {
|
|
if (std::strlen(key) > osmium::max_osm_string_length) {
|
|
throw std::length_error("OSM tag key is too long");
|
|
}
|
|
if (std::strlen(value) > osmium::max_osm_string_length) {
|
|
throw std::length_error("OSM tag value is too long");
|
|
}
|
|
add_size(append(key) + append(value));
|
|
}
|
|
|
|
/**
|
|
* Add tag to buffer.
|
|
*
|
|
* @param key Pointer to tag key.
|
|
* @param key_length Length of key (not including the \0 byte).
|
|
* @param value Pointer to tag value.
|
|
* @param value_length Length of value (not including the \0 byte).
|
|
*/
|
|
void add_tag(const char* key, const size_t key_length, const char* value, const size_t value_length) {
|
|
if (key_length > osmium::max_osm_string_length) {
|
|
throw std::length_error("OSM tag key is too long");
|
|
}
|
|
if (value_length > osmium::max_osm_string_length) {
|
|
throw std::length_error("OSM tag value is too long");
|
|
}
|
|
add_size(append(key, osmium::memory::item_size_type(key_length)) + append_zero() +
|
|
append(value, osmium::memory::item_size_type(value_length)) + append_zero());
|
|
}
|
|
|
|
/**
|
|
* Add tag to buffer.
|
|
*
|
|
* @param key Tag key.
|
|
* @param value Tag value.
|
|
*/
|
|
void add_tag(const std::string& key, const std::string& value) {
|
|
if (key.size() > osmium::max_osm_string_length) {
|
|
throw std::length_error("OSM tag key is too long");
|
|
}
|
|
if (value.size() > osmium::max_osm_string_length) {
|
|
throw std::length_error("OSM tag value is too long");
|
|
}
|
|
add_size(append(key.data(), osmium::memory::item_size_type(key.size()) + 1) +
|
|
append(value.data(), osmium::memory::item_size_type(value.size()) + 1));
|
|
}
|
|
|
|
/**
|
|
* Add tag to buffer.
|
|
*
|
|
* @param tag Tag.
|
|
*/
|
|
void add_tag(const osmium::Tag& tag) {
|
|
add_size(append(tag.key()) + append(tag.value()));
|
|
}
|
|
|
|
/**
|
|
* Add tag to buffer.
|
|
*
|
|
* @param tag Pair of key/value 0-terminated strings.
|
|
*/
|
|
void add_tag(const std::pair<const char* const, const char* const>& tag) {
|
|
add_tag(tag.first, tag.second);
|
|
}
|
|
void add_tag(const std::pair<const char* const, const char*>& tag) {
|
|
add_tag(tag.first, tag.second);
|
|
}
|
|
void add_tag(const std::pair<const char*, const char* const>& tag) {
|
|
add_tag(tag.first, tag.second);
|
|
}
|
|
void add_tag(const std::pair<const char*, const char*>& tag) {
|
|
add_tag(tag.first, tag.second);
|
|
}
|
|
|
|
/**
|
|
* Add tag to buffer.
|
|
*
|
|
* @param tag Pair of std::string references.
|
|
*/
|
|
void add_tag(const std::pair<const std::string&, const std::string&>& tag) {
|
|
add_tag(tag.first, tag.second);
|
|
}
|
|
|
|
}; // class TagListBuilder
|
|
|
|
template <typename T>
|
|
class NodeRefListBuilder : public Builder {
|
|
|
|
public:
|
|
|
|
explicit NodeRefListBuilder(osmium::memory::Buffer& buffer, Builder* parent = nullptr) :
|
|
Builder(buffer, parent, sizeof(T)) {
|
|
new (&item()) T();
|
|
}
|
|
|
|
explicit NodeRefListBuilder(Builder& parent) :
|
|
Builder(parent.buffer(), &parent, sizeof(T)) {
|
|
new (&item()) T();
|
|
}
|
|
|
|
~NodeRefListBuilder() {
|
|
add_padding();
|
|
}
|
|
|
|
void add_node_ref(const NodeRef& node_ref) {
|
|
new (reserve_space_for<osmium::NodeRef>()) osmium::NodeRef(node_ref);
|
|
add_size(sizeof(osmium::NodeRef));
|
|
}
|
|
|
|
void add_node_ref(const object_id_type ref, const osmium::Location& location = Location{}) {
|
|
add_node_ref(NodeRef(ref, location));
|
|
}
|
|
|
|
}; // class NodeRefListBuilder
|
|
|
|
using WayNodeListBuilder = NodeRefListBuilder<WayNodeList>;
|
|
using OuterRingBuilder = NodeRefListBuilder<OuterRing>;
|
|
using InnerRingBuilder = NodeRefListBuilder<InnerRing>;
|
|
|
|
class RelationMemberListBuilder : public Builder {
|
|
|
|
/**
|
|
* Add role to buffer.
|
|
*
|
|
* @param member Relation member object where the length of the role
|
|
* will be set.
|
|
* @param role The role.
|
|
* @param length Length of role (without \0 termination).
|
|
* @throws std:length_error If role is longer than osmium::max_osm_string_length
|
|
*/
|
|
void add_role(osmium::RelationMember& member, const char* role, const size_t length) {
|
|
if (length > osmium::max_osm_string_length) {
|
|
throw std::length_error("OSM relation member role is too long");
|
|
}
|
|
member.set_role_size(osmium::string_size_type(length) + 1);
|
|
add_size(append(role, osmium::memory::item_size_type(length)) + append_zero());
|
|
add_padding(true);
|
|
}
|
|
|
|
public:
|
|
|
|
explicit RelationMemberListBuilder(osmium::memory::Buffer& buffer, Builder* parent = nullptr) :
|
|
Builder(buffer, parent, sizeof(RelationMemberList)) {
|
|
new (&item()) RelationMemberList();
|
|
}
|
|
|
|
explicit RelationMemberListBuilder(Builder& parent) :
|
|
Builder(parent.buffer(), &parent, sizeof(RelationMemberList)) {
|
|
new (&item()) RelationMemberList();
|
|
}
|
|
|
|
~RelationMemberListBuilder() {
|
|
add_padding();
|
|
}
|
|
|
|
/**
|
|
* Add a member to the relation.
|
|
*
|
|
* @param type The type (node, way, or relation).
|
|
* @param ref The ID of the member.
|
|
* @param role The role of the member.
|
|
* @param role_length Length of the role (without \0 termination).
|
|
* @param full_member Optional pointer to the member object. If it
|
|
* is available a copy will be added to the
|
|
* relation.
|
|
* @throws std:length_error If role_length is greater than
|
|
* osmium::max_osm_string_length
|
|
*/
|
|
void add_member(osmium::item_type type, object_id_type ref, const char* role, const size_t role_length, const osmium::OSMObject* full_member = nullptr) {
|
|
osmium::RelationMember* member = reserve_space_for<osmium::RelationMember>();
|
|
new (member) osmium::RelationMember(ref, type, full_member != nullptr);
|
|
add_size(sizeof(RelationMember));
|
|
add_role(*member, role, role_length);
|
|
if (full_member) {
|
|
add_item(*full_member);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Add a member to the relation.
|
|
*
|
|
* @param type The type (node, way, or relation).
|
|
* @param ref The ID of the member.
|
|
* @param role The role of the member (\0 terminated string).
|
|
* @param full_member Optional pointer to the member object. If it
|
|
* is available a copy will be added to the
|
|
* relation.
|
|
* @throws std:length_error If role is longer than osmium::max_osm_string_length
|
|
*/
|
|
void add_member(osmium::item_type type, object_id_type ref, const char* role, const osmium::OSMObject* full_member = nullptr) {
|
|
add_member(type, ref, role, std::strlen(role), full_member);
|
|
}
|
|
|
|
/**
|
|
* Add a member to the relation.
|
|
*
|
|
* @param type The type (node, way, or relation).
|
|
* @param ref The ID of the member.
|
|
* @param role The role of the member.
|
|
* @param full_member Optional pointer to the member object. If it
|
|
* is available a copy will be added to the
|
|
* relation.
|
|
* @throws std:length_error If role is longer than osmium::max_osm_string_length
|
|
*/
|
|
void add_member(osmium::item_type type, object_id_type ref, const std::string& role, const osmium::OSMObject* full_member = nullptr) {
|
|
add_member(type, ref, role.data(), role.size(), full_member);
|
|
}
|
|
|
|
}; // class RelationMemberListBuilder
|
|
|
|
class ChangesetDiscussionBuilder : public Builder {
|
|
|
|
osmium::ChangesetComment* m_comment = nullptr;
|
|
|
|
void add_user(osmium::ChangesetComment& comment, const char* user, const size_t length) {
|
|
if (length > osmium::max_osm_string_length) {
|
|
throw std::length_error("OSM user name is too long");
|
|
}
|
|
comment.set_user_size(osmium::string_size_type(length) + 1);
|
|
add_size(append(user, osmium::memory::item_size_type(length)) + append_zero());
|
|
}
|
|
|
|
void add_text(osmium::ChangesetComment& comment, const char* text, const size_t length) {
|
|
// XXX There is no limit on the length of a comment text. We
|
|
// limit it here to 2^16-2 characters, because that's all that
|
|
// will fit into our internal data structure. This is not ideal,
|
|
// and will have to be discussed and cleared up.
|
|
if (length > std::numeric_limits<osmium::string_size_type>::max() - 1) {
|
|
throw std::length_error("OSM changeset comment is too long");
|
|
}
|
|
comment.set_text_size(osmium::string_size_type(length) + 1);
|
|
add_size(append(text, osmium::memory::item_size_type(length)) + append_zero());
|
|
add_padding(true);
|
|
}
|
|
|
|
public:
|
|
|
|
explicit ChangesetDiscussionBuilder(osmium::memory::Buffer& buffer, Builder* parent = nullptr) :
|
|
Builder(buffer, parent, sizeof(ChangesetDiscussion)) {
|
|
new (&item()) ChangesetDiscussion();
|
|
}
|
|
|
|
explicit ChangesetDiscussionBuilder(Builder& parent) :
|
|
Builder(parent.buffer(), &parent, sizeof(ChangesetDiscussion)) {
|
|
new (&item()) ChangesetDiscussion();
|
|
}
|
|
|
|
~ChangesetDiscussionBuilder() {
|
|
assert(!m_comment && "You have to always call both add_comment() and then add_comment_text() in that order for each comment!");
|
|
add_padding();
|
|
}
|
|
|
|
void add_comment(osmium::Timestamp date, osmium::user_id_type uid, const char* user) {
|
|
assert(!m_comment && "You have to always call both add_comment() and then add_comment_text() in that order for each comment!");
|
|
m_comment = reserve_space_for<osmium::ChangesetComment>();
|
|
new (m_comment) osmium::ChangesetComment(date, uid);
|
|
add_size(sizeof(ChangesetComment));
|
|
add_user(*m_comment, user, std::strlen(user));
|
|
}
|
|
|
|
void add_comment_text(const char* text) {
|
|
assert(m_comment && "You have to always call both add_comment() and then add_comment_text() in that order for each comment!");
|
|
add_text(*m_comment, text, std::strlen(text));
|
|
m_comment = nullptr;
|
|
}
|
|
|
|
void add_comment_text(const std::string& text) {
|
|
assert(m_comment && "You have to always call both add_comment() and then add_comment_text() in that order for each comment!");
|
|
add_text(*m_comment, text.c_str(), text.size());
|
|
m_comment = nullptr;
|
|
}
|
|
|
|
}; // class ChangesetDiscussionBuilder
|
|
|
|
#define OSMIUM_FORWARD(setter) \
|
|
template <typename... TArgs> \
|
|
type& setter(TArgs&&... args) { \
|
|
object().setter(std::forward<TArgs>(args)...); \
|
|
return static_cast<type&>(*this); \
|
|
}
|
|
|
|
template <typename TDerived, typename T>
|
|
class OSMObjectBuilder : public Builder {
|
|
|
|
using type = TDerived;
|
|
|
|
constexpr static const size_t min_size_for_user = osmium::memory::padded_length(sizeof(string_size_type) + 1);
|
|
|
|
public:
|
|
|
|
explicit OSMObjectBuilder(osmium::memory::Buffer& buffer, Builder* parent = nullptr) :
|
|
Builder(buffer, parent, sizeof(T) + min_size_for_user) {
|
|
new (&item()) T();
|
|
add_size(min_size_for_user);
|
|
std::fill_n(object().data() + sizeof(T), min_size_for_user, 0);
|
|
object().set_user_size(1);
|
|
}
|
|
|
|
/**
|
|
* Get a reference to the object buing built.
|
|
*
|
|
* Note that this reference will be invalidated by every action
|
|
* on the builder that might make the buffer grow. This includes
|
|
* calls to set_user() and any time a new sub-builder is created.
|
|
*/
|
|
T& object() noexcept {
|
|
return static_cast<T&>(item());
|
|
}
|
|
|
|
/**
|
|
* Set user name.
|
|
*
|
|
* @param user Pointer to user name.
|
|
* @param length Length of user name (without \0 termination).
|
|
*/
|
|
TDerived& set_user(const char* user, const string_size_type length) {
|
|
const auto size_of_object = sizeof(T) + sizeof(string_size_type);
|
|
assert(object().user_size() == 1 && (size() <= size_of_object + osmium::memory::padded_length(1))
|
|
&& "set_user() must be called at most once and before any sub-builders");
|
|
const auto available_space = min_size_for_user - sizeof(string_size_type) - 1;
|
|
if (length > available_space) {
|
|
const auto space_needed = osmium::memory::padded_length(length - available_space);
|
|
reserve_space(space_needed);
|
|
add_size(static_cast<uint32_t>(space_needed));
|
|
}
|
|
std::copy_n(user, length, object().data() + size_of_object);
|
|
std::fill_n(object().data() + size_of_object + length, osmium::memory::padded_length(length + 1) - length, 0);
|
|
object().set_user_size(length + 1);
|
|
|
|
return static_cast<TDerived&>(*this);
|
|
}
|
|
|
|
/**
|
|
* Set user name.
|
|
*
|
|
* @param user Pointer to \0-terminated user name.
|
|
*/
|
|
TDerived& set_user(const char* user) {
|
|
return set_user(user, static_cast_with_assert<string_size_type>(std::strlen(user)));
|
|
}
|
|
|
|
/**
|
|
* Set user name.
|
|
*
|
|
* @param user User name.
|
|
*/
|
|
TDerived& set_user(const std::string& user) {
|
|
return set_user(user.data(), static_cast_with_assert<string_size_type>(user.size()));
|
|
}
|
|
|
|
/// @deprecated Use set_user(...) instead.
|
|
template <typename... TArgs>
|
|
OSMIUM_DEPRECATED void add_user(TArgs&&... args) {
|
|
set_user(std::forward<TArgs>(args)...);
|
|
}
|
|
|
|
OSMIUM_FORWARD(set_id)
|
|
OSMIUM_FORWARD(set_visible)
|
|
OSMIUM_FORWARD(set_deleted)
|
|
OSMIUM_FORWARD(set_version)
|
|
OSMIUM_FORWARD(set_changeset)
|
|
OSMIUM_FORWARD(set_uid)
|
|
OSMIUM_FORWARD(set_uid_from_signed)
|
|
OSMIUM_FORWARD(set_timestamp)
|
|
OSMIUM_FORWARD(set_attribute)
|
|
OSMIUM_FORWARD(set_removed)
|
|
|
|
void add_tags(const std::initializer_list<std::pair<const char*, const char*>>& tags) {
|
|
osmium::builder::TagListBuilder tl_builder{buffer(), this};
|
|
for (const auto& p : tags) {
|
|
tl_builder.add_tag(p.first, p.second);
|
|
}
|
|
}
|
|
|
|
}; // class OSMObjectBuilder
|
|
|
|
class NodeBuilder : public OSMObjectBuilder<NodeBuilder, Node> {
|
|
|
|
using type = NodeBuilder;
|
|
|
|
public:
|
|
|
|
explicit NodeBuilder(osmium::memory::Buffer& buffer, Builder* parent = nullptr) :
|
|
OSMObjectBuilder<NodeBuilder, Node>(buffer, parent) {
|
|
}
|
|
|
|
explicit NodeBuilder(Builder& parent) :
|
|
OSMObjectBuilder<NodeBuilder, Node>(parent.buffer(), &parent) {
|
|
}
|
|
|
|
OSMIUM_FORWARD(set_location)
|
|
|
|
}; // class NodeBuilder
|
|
|
|
class WayBuilder : public OSMObjectBuilder<WayBuilder, Way> {
|
|
|
|
using type = WayBuilder;
|
|
|
|
public:
|
|
|
|
explicit WayBuilder(osmium::memory::Buffer& buffer, Builder* parent = nullptr) :
|
|
OSMObjectBuilder<WayBuilder, Way>(buffer, parent) {
|
|
}
|
|
|
|
explicit WayBuilder(Builder& parent) :
|
|
OSMObjectBuilder<WayBuilder, Way>(parent.buffer(), &parent) {
|
|
}
|
|
|
|
void add_node_refs(const std::initializer_list<osmium::NodeRef>& nodes) {
|
|
osmium::builder::WayNodeListBuilder builder{buffer(), this};
|
|
for (const auto& node_ref : nodes) {
|
|
builder.add_node_ref(node_ref);
|
|
}
|
|
}
|
|
|
|
}; // class WayBuilder
|
|
|
|
class RelationBuilder : public OSMObjectBuilder<RelationBuilder, Relation> {
|
|
|
|
using type = RelationBuilder;
|
|
|
|
public:
|
|
|
|
explicit RelationBuilder(osmium::memory::Buffer& buffer, Builder* parent = nullptr) :
|
|
OSMObjectBuilder<RelationBuilder, Relation>(buffer, parent) {
|
|
}
|
|
|
|
explicit RelationBuilder(Builder& parent) :
|
|
OSMObjectBuilder<RelationBuilder, Relation>(parent.buffer(), &parent) {
|
|
}
|
|
|
|
}; // class RelationBuilder
|
|
|
|
class AreaBuilder : public OSMObjectBuilder<AreaBuilder, Area> {
|
|
|
|
using type = AreaBuilder;
|
|
|
|
public:
|
|
|
|
explicit AreaBuilder(osmium::memory::Buffer& buffer, Builder* parent = nullptr) :
|
|
OSMObjectBuilder<AreaBuilder, Area>(buffer, parent) {
|
|
}
|
|
|
|
explicit AreaBuilder(Builder& parent) :
|
|
OSMObjectBuilder<AreaBuilder, Area>(parent.buffer(), &parent) {
|
|
}
|
|
|
|
/**
|
|
* Initialize area attributes from the attributes of the given object.
|
|
*/
|
|
void initialize_from_object(const osmium::OSMObject& source) {
|
|
set_id(osmium::object_id_to_area_id(source.id(), source.type()));
|
|
set_version(source.version());
|
|
set_changeset(source.changeset());
|
|
set_timestamp(source.timestamp());
|
|
set_visible(source.visible());
|
|
set_uid(source.uid());
|
|
set_user(source.user());
|
|
}
|
|
|
|
}; // class AreaBuilder
|
|
|
|
class ChangesetBuilder : public Builder {
|
|
|
|
using type = ChangesetBuilder;
|
|
|
|
constexpr static const size_t min_size_for_user = osmium::memory::padded_length(1);
|
|
|
|
public:
|
|
|
|
explicit ChangesetBuilder(osmium::memory::Buffer& buffer, Builder* parent = nullptr) :
|
|
Builder(buffer, parent, sizeof(Changeset) + min_size_for_user) {
|
|
new (&item()) Changeset();
|
|
add_size(min_size_for_user);
|
|
std::fill_n(object().data() + sizeof(Changeset), min_size_for_user, 0);
|
|
object().set_user_size(1);
|
|
}
|
|
|
|
/**
|
|
* Get a reference to the changeset buing built.
|
|
*
|
|
* Note that this reference will be invalidated by every action
|
|
* on the builder that might make the buffer grow. This includes
|
|
* calls to set_user() and any time a new sub-builder is created.
|
|
*/
|
|
Changeset& object() noexcept {
|
|
return static_cast<Changeset&>(item());
|
|
}
|
|
|
|
OSMIUM_FORWARD(set_id)
|
|
OSMIUM_FORWARD(set_uid)
|
|
OSMIUM_FORWARD(set_uid_from_signed)
|
|
OSMIUM_FORWARD(set_created_at)
|
|
OSMIUM_FORWARD(set_closed_at)
|
|
OSMIUM_FORWARD(set_num_changes)
|
|
OSMIUM_FORWARD(set_num_comments)
|
|
OSMIUM_FORWARD(set_attribute)
|
|
OSMIUM_FORWARD(set_removed)
|
|
|
|
// @deprecated Use set_bounds() instead.
|
|
OSMIUM_DEPRECATED osmium::Box& bounds() noexcept {
|
|
return object().bounds();
|
|
}
|
|
|
|
ChangesetBuilder& set_bounds(const osmium::Box& box) noexcept {
|
|
object().bounds() = box;
|
|
return *this;
|
|
}
|
|
|
|
/**
|
|
* Set user name.
|
|
*
|
|
* @param user Pointer to user name.
|
|
* @param length Length of user name (without \0 termination).
|
|
*/
|
|
ChangesetBuilder& set_user(const char* user, const string_size_type length) {
|
|
assert(object().user_size() == 1 && (size() <= sizeof(Changeset) + osmium::memory::padded_length(1))
|
|
&& "set_user() must be called at most once and before any sub-builders");
|
|
const auto available_space = min_size_for_user - 1;
|
|
if (length > available_space) {
|
|
const auto space_needed = osmium::memory::padded_length(length - available_space);
|
|
reserve_space(space_needed);
|
|
add_size(static_cast<uint32_t>(space_needed));
|
|
}
|
|
std::copy_n(user, length, object().data() + sizeof(Changeset));
|
|
std::fill_n(object().data() + sizeof(Changeset) + length, osmium::memory::padded_length(length + 1) - length, 0);
|
|
object().set_user_size(length + 1);
|
|
|
|
return *this;
|
|
}
|
|
|
|
/**
|
|
* Set user name.
|
|
*
|
|
* @param user Pointer to \0-terminated user name.
|
|
*/
|
|
ChangesetBuilder& set_user(const char* user) {
|
|
return set_user(user, static_cast_with_assert<string_size_type>(std::strlen(user)));
|
|
}
|
|
|
|
/**
|
|
* Set user name.
|
|
*
|
|
* @param user User name.
|
|
*/
|
|
ChangesetBuilder& set_user(const std::string& user) {
|
|
return set_user(user.data(), static_cast_with_assert<string_size_type>(user.size()));
|
|
}
|
|
|
|
/// @deprecated Use set_user(...) instead.
|
|
template <typename... TArgs>
|
|
OSMIUM_DEPRECATED void add_user(TArgs&&... args) {
|
|
set_user(std::forward<TArgs>(args)...);
|
|
}
|
|
|
|
}; // class ChangesetBuilder
|
|
|
|
#undef OSMIUM_FORWARD
|
|
|
|
} // namespace builder
|
|
|
|
} // namespace osmium
|
|
|
|
#endif // OSMIUM_BUILDER_OSM_OBJECT_BUILDER_HPP
|