Libosmium  2.9.0
Fast and flexible C++ library for working with OpenStreetMap data
builder.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_BUILDER_BUILDER_HPP
2 #define OSMIUM_BUILDER_BUILDER_HPP
3 
4 /*
5 
6 This file is part of Osmium (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2016 Jochen Topf <jochen@topf.org> and others (see README).
9 
10 Boost Software License - Version 1.0 - August 17th, 2003
11 
12 Permission is hereby granted, free of charge, to any person or organization
13 obtaining a copy of the software and accompanying documentation covered by
14 this license (the "Software") to use, reproduce, display, distribute,
15 execute, and transmit the Software, and to prepare derivative works of the
16 Software, and to permit third-parties to whom the Software is furnished to
17 do so, all subject to the following:
18 
19 The copyright notices in the Software and this entire statement, including
20 the above license grant, this restriction and the following disclaimer,
21 must be included in all copies of the Software, in whole or in part, and
22 all derivative works of the Software, unless such copies or derivative
23 works are solely in the form of machine-executable object code generated by
24 a source language processor.
25 
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 DEALINGS IN THE SOFTWARE.
33 
34 */
35 
36 #include <algorithm>
37 #include <cassert>
38 #include <cstdint>
39 #include <cstring>
40 #include <new>
41 #include <string>
42 #include <type_traits>
43 
44 #include <osmium/memory/buffer.hpp>
45 #include <osmium/memory/item.hpp>
46 #include <osmium/osm/types.hpp>
47 #include <osmium/util/cast.hpp>
48 
49 namespace osmium {
50 
54  namespace builder {
55 
56  class Builder {
57 
60  size_t m_item_offset;
61 
62  Builder(const Builder&) = delete;
63  Builder(Builder&&) = delete;
64 
65  Builder& operator=(const Builder&) = delete;
66  Builder& operator=(Builder&&) = delete;
67 
68  protected:
69 
71  m_buffer(buffer),
72  m_parent(parent),
73  m_item_offset(buffer.written()) {
74  m_buffer.reserve_space(size);
75  assert(buffer.is_aligned());
76  if (m_parent) {
77  m_parent->add_size(size);
78  }
79  }
80 
81  ~Builder() = default;
82 
84  return *reinterpret_cast<osmium::memory::Item*>(m_buffer.data() + m_item_offset);
85  }
86 
87  public:
88 
102  void add_padding(bool self = false) {
103  const auto padding = osmium::memory::align_bytes - (size() % osmium::memory::align_bytes);
104  if (padding != osmium::memory::align_bytes) {
105  std::fill_n(m_buffer.reserve_space(padding), padding, 0);
106  if (self) {
107  add_size(padding);
108  } else if (m_parent) {
109  m_parent->add_size(padding);
110  assert(m_parent->size() % osmium::memory::align_bytes == 0);
111  }
112  }
113  }
114 
115  void add_size(uint32_t size) {
116  item().add_size(size);
117  if (m_parent) {
118  m_parent->add_size(size);
119  }
120  }
121 
122  uint32_t size() const noexcept {
123  return item().byte_size();
124  }
125 
127  unsigned char* target = m_buffer.reserve_space(item->padded_size());
128  std::copy_n(reinterpret_cast<const unsigned char*>(item), item->padded_size(), target);
129  add_size(item->padded_size());
130  }
131 
136  template <typename T>
138  assert(m_buffer.is_aligned());
139  return reinterpret_cast<T*>(m_buffer.reserve_space(sizeof(T)));
140  }
141 
152  unsigned char* target = m_buffer.reserve_space(length);
153  std::copy_n(reinterpret_cast<const unsigned char*>(data), length, target);
154  return length;
155  }
156 
164  return append(str, static_cast<osmium::memory::item_size_type>(std::strlen(str) + 1));
165  }
166 
173  *m_buffer.reserve_space(1) = '\0';
174  return 1;
175  }
176 
179  return m_buffer;
180  }
181 
182  }; // class Builder
183 
184  template <typename TItem>
185  class ObjectBuilder : public Builder {
186 
187 
188  public:
189 
190  explicit ObjectBuilder(osmium::memory::Buffer& buffer, Builder* parent = nullptr) :
191  Builder(buffer, parent, sizeof(TItem)) {
192  new (&item()) TItem();
193  }
194 
195  TItem& object() noexcept {
196  return static_cast<TItem&>(item());
197  }
198 
205  void add_user(const char* user, const string_size_type length) {
206  object().set_user_size(length + 1);
207  add_size(append(user, length) + append_zero());
208  add_padding(true);
209  }
210 
216  void add_user(const char* user) {
217  add_user(user, static_cast_with_assert<string_size_type>(std::strlen(user)));
218  }
219 
225  void add_user(const std::string& user) {
226  add_user(user.data(), static_cast_with_assert<string_size_type>(user.size()));
227  }
228 
229  }; // class ObjectBuilder
230 
231  } // namespace builder
232 
233 } // namespace osmium
234 
235 #endif // OSMIUM_BUILDER_BUILDER_HPP
Builder * m_parent
Definition: builder.hpp:59
Builder(osmium::memory::Buffer &buffer, Builder *parent, osmium::memory::item_size_type size)
Definition: builder.hpp:70
bool is_aligned() const noexcept
Definition: buffer.hpp:260
osmium::memory::Buffer & buffer() noexcept
Return the buffer this builder is using.
Definition: builder.hpp:178
osmium::memory::item_size_type append_zero()
Definition: builder.hpp:172
ObjectBuilder(osmium::memory::Buffer &buffer, Builder *parent=nullptr)
Definition: builder.hpp:190
uint32_t item_size_type
Definition: item.hpp:59
void add_user(const char *user, const string_size_type length)
Definition: builder.hpp:205
constexpr item_size_type align_bytes
Definition: item.hpp:62
uint16_t string_size_type
Definition: types.hpp:59
item_size_type padded_size() const
Definition: item.hpp:161
Definition: item.hpp:105
Namespace for everything in the Osmium library.
Definition: assembler.hpp:73
Item & add_size(const item_size_type size) noexcept
Definition: item.hpp:121
T * reserve_space_for()
Definition: builder.hpp:137
unsigned char * data() const noexcept
Definition: buffer.hpp:224
osmium::memory::item_size_type append(const char *str)
Definition: builder.hpp:163
unsigned char * reserve_space(const size_t size)
Definition: buffer.hpp:417
void add_user(const std::string &user)
Definition: builder.hpp:225
void add_user(TBuilder &builder, const TArgs &... args)
Definition: attr.hpp:619
item_size_type byte_size() const noexcept
Definition: item.hpp:157
osmium::memory::Buffer & m_buffer
Definition: builder.hpp:58
Definition: buffer.hpp:97
size_t m_item_offset
Definition: builder.hpp:60
Definition: builder.hpp:185
osmium::memory::item_size_type append(const char *data, const osmium::memory::item_size_type length)
Definition: builder.hpp:151
Builder(const Builder &)=delete
uint32_t size() const noexcept
Definition: builder.hpp:122
void add_size(uint32_t size)
Definition: builder.hpp:115
void add_item(const osmium::memory::Item *item)
Definition: builder.hpp:126
node, way, relation, or area object
Definition: entity_bits.hpp:72
TItem & object() noexcept
Definition: builder.hpp:195
void add_user(const char *user)
Definition: builder.hpp:216
Definition: builder.hpp:56
Builder & operator=(const Builder &)=delete
void add_padding(bool self=false)
Definition: builder.hpp:102
osmium::memory::Item & item() const
Definition: builder.hpp:83