Libosmium  2.7.1
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 <cstddef>
39 #include <cstdint>
40 #include <cstring>
41 #include <new>
42 #include <string>
43 #include <type_traits>
44 
45 #include <osmium/memory/buffer.hpp>
46 #include <osmium/memory/item.hpp>
47 #include <osmium/osm/types.hpp>
48 #include <osmium/util/cast.hpp>
49 
50 namespace osmium {
51 
55  namespace builder {
56 
57  class Builder {
58 
61  size_t m_item_offset;
62 
63  Builder(const Builder&) = delete;
64  Builder(Builder&&) = delete;
65 
66  Builder& operator=(const Builder&) = delete;
67  Builder& operator=(Builder&&) = delete;
68 
69  protected:
70 
72  m_buffer(buffer),
73  m_parent(parent),
74  m_item_offset(buffer.written()) {
75  m_buffer.reserve_space(size);
76  assert(buffer.is_aligned());
77  if (m_parent) {
78  m_parent->add_size(size);
79  }
80  }
81 
82  ~Builder() = default;
83 
85  return *reinterpret_cast<osmium::memory::Item*>(m_buffer.data() + m_item_offset);
86  }
87 
88  public:
89 
103  void add_padding(bool self = false) {
105  if (padding != osmium::memory::align_bytes) {
106  std::fill_n(m_buffer.reserve_space(padding), padding, 0);
107  if (self) {
108  add_size(padding);
109  } else if (m_parent) {
110  m_parent->add_size(padding);
111  assert(m_parent->size() % osmium::memory::align_bytes == 0);
112  }
113  }
114  }
115 
116  void add_size(uint32_t size) {
117  item().add_size(size);
118  if (m_parent) {
119  m_parent->add_size(size);
120  }
121  }
122 
123  uint32_t size() const noexcept {
124  return item().byte_size();
125  }
126 
128  unsigned char* target = m_buffer.reserve_space(item->padded_size());
129  std::copy_n(reinterpret_cast<const unsigned char*>(item), item->padded_size(), target);
130  add_size(item->padded_size());
131  }
132 
137  template <typename T>
139  assert(m_buffer.is_aligned());
140  return reinterpret_cast<T*>(m_buffer.reserve_space(sizeof(T)));
141  }
142 
153  unsigned char* target = m_buffer.reserve_space(length);
154  std::copy_n(reinterpret_cast<const unsigned char*>(data), length, target);
155  return length;
156  }
157 
165  return append(str, static_cast<osmium::memory::item_size_type>(std::strlen(str) + 1));
166  }
167 
174  *m_buffer.reserve_space(1) = '\0';
175  return 1;
176  }
177 
180  return m_buffer;
181  }
182 
183  }; // class Builder
184 
185  template <typename TItem>
186  class ObjectBuilder : public Builder {
187 
188 
189  public:
190 
191  explicit ObjectBuilder(osmium::memory::Buffer& buffer, Builder* parent = nullptr) :
192  Builder(buffer, parent, sizeof(TItem)) {
193  new (&item()) TItem();
194  }
195 
196  TItem& object() noexcept {
197  return static_cast<TItem&>(item());
198  }
199 
206  void add_user(const char* user, const string_size_type length) {
207  object().set_user_size(length + 1);
208  add_size(append(user, length) + append_zero());
209  add_padding(true);
210  }
211 
217  void add_user(const char* user) {
218  add_user(user, static_cast_with_assert<string_size_type>(std::strlen(user)));
219  }
220 
226  void add_user(const std::string& user) {
227  add_user(user.data(), static_cast_with_assert<string_size_type>(user.size()));
228  }
229 
230  }; // class ObjectBuilder
231 
232  } // namespace builder
233 
234 } // namespace osmium
235 
236 #endif // OSMIUM_BUILDER_BUILDER_HPP
osmium::memory::Item & item() const
Definition: builder.hpp:84
Builder * m_parent
Definition: builder.hpp:60
Builder(osmium::memory::Buffer &buffer, Builder *parent, osmium::memory::item_size_type size)
Definition: builder.hpp:71
bool is_aligned() const noexcept
Definition: buffer.hpp:260
osmium::memory::Buffer & buffer() noexcept
Return the buffer this builder is using.
Definition: builder.hpp:179
osmium::memory::item_size_type append_zero()
Definition: builder.hpp:173
ObjectBuilder(osmium::memory::Buffer &buffer, Builder *parent=nullptr)
Definition: builder.hpp:191
uint32_t item_size_type
Definition: item.hpp:50
void add_user(const char *user, const string_size_type length)
Definition: builder.hpp:206
constexpr item_size_type align_bytes
Definition: item.hpp:53
uint16_t string_size_type
Definition: types.hpp:59
item_size_type padded_size() const
Definition: item.hpp:151
Definition: item.hpp:97
Namespace for everything in the Osmium library.
Definition: assembler.hpp:66
Item & add_size(const item_size_type size) noexcept
Definition: item.hpp:112
T * reserve_space_for()
Definition: builder.hpp:138
unsigned char * data() const noexcept
Definition: buffer.hpp:224
osmium::memory::item_size_type append(const char *str)
Definition: builder.hpp:164
unsigned char * reserve_space(const size_t size)
Definition: buffer.hpp:417
void add_user(const std::string &user)
Definition: builder.hpp:226
item_size_type byte_size() const noexcept
Definition: item.hpp:147
void add_user(TBuilder &builder, const TArgs &...args)
Definition: attr.hpp:584
osmium::memory::Buffer & m_buffer
Definition: builder.hpp:59
Definition: buffer.hpp:97
size_t m_item_offset
Definition: builder.hpp:61
Definition: builder.hpp:186
osmium::memory::item_size_type append(const char *data, const osmium::memory::item_size_type length)
Definition: builder.hpp:152
Builder(const Builder &)=delete
uint32_t size() const noexcept
Definition: builder.hpp:123
void add_size(uint32_t size)
Definition: builder.hpp:116
void add_item(const osmium::memory::Item *item)
Definition: builder.hpp:127
node, way, relation, or area object
Definition: entity_bits.hpp:72
TItem & object() noexcept
Definition: builder.hpp:196
void add_user(const char *user)
Definition: builder.hpp:217
Definition: builder.hpp:57
Builder & operator=(const Builder &)=delete
void add_padding(bool self=false)
Definition: builder.hpp:103