Libosmium  2.16.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 (https://osmcode.org/libosmium).
7 
8 Copyright 2013-2021 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 <osmium/memory/buffer.hpp>
37 #include <osmium/memory/item.hpp>
39 
40 #include <algorithm>
41 #include <cassert>
42 #include <cstddef>
43 #include <cstdint>
44 #include <cstring>
45 
46 namespace osmium {
47 
51  namespace builder {
52 
57  class Builder {
58 
61  std::size_t m_item_offset;
62 
63  protected:
64 
67  m_parent(parent),
68  m_item_offset(buffer.written() - buffer.committed()) {
70  assert(buffer.is_aligned());
71  if (m_parent) {
72  assert(m_buffer.builder_count() == 1 && "Only one sub-builder can be open at any time.");
74  } else {
75  assert(m_buffer.builder_count() == 0 && "Only one builder can be open at any time.");
76  }
77 #ifndef NDEBUG
79 #endif
80  }
81 
82 #ifdef NDEBUG
83  ~Builder() noexcept = default;
84 #else
85  ~Builder() noexcept {
87  }
88 #endif
89 
91  return *reinterpret_cast<osmium::memory::Item*>(m_buffer.data() + m_buffer.committed() + m_item_offset);
92  }
93 
94  unsigned char* reserve_space(std::size_t size) {
95  return m_buffer.reserve_space(size);
96  }
97 
111  void add_padding(bool self = false) {
112  // We know the padding is only a very small number, so it will
113  // always fit.
115  if (padding != osmium::memory::align_bytes) {
116  std::fill_n(reserve_space(padding), padding, 0);
117  if (self) {
118  add_size(padding);
119  } else if (m_parent) {
120  m_parent->add_size(padding);
121  assert(m_parent->size() % osmium::memory::align_bytes == 0);
122  }
123  }
124  }
125 
127  item().add_size(size);
128  if (m_parent) {
130  }
131  }
132 
133  uint32_t size() const noexcept {
134  return item().byte_size();
135  }
136 
141  template <typename T>
143  assert(m_buffer.is_aligned());
144  return reinterpret_cast<T*>(reserve_space(sizeof(T)));
145  }
146 
157  unsigned char* target = reserve_space(length);
158  std::copy_n(reinterpret_cast<const unsigned char*>(data), length, target);
159  return length;
160  }
161 
170  unsigned char* target = reserve_space(length + 1);
171  std::copy_n(reinterpret_cast<const unsigned char*>(data), length, target);
172  target[length] = '\0';
173  return length + 1;
174  }
175 
183  return append(str, static_cast<osmium::memory::item_size_type>(std::strlen(str) + 1));
184  }
185 
194  *reserve_space(1) = '\0';
195  return 1;
196  }
197 
198  public:
199 
200  Builder(const Builder&) = delete;
201  Builder(Builder&&) = delete;
202 
203  Builder& operator=(const Builder&) = delete;
204  Builder& operator=(Builder&&) = delete;
205 
208  return m_buffer;
209  }
210 
218  }
219 
225  assert(item);
226  add_item(*item);
227  }
228 
229  }; // class Builder
230 
231  } // namespace builder
232 
233 } // namespace osmium
234 
235 #endif // OSMIUM_BUILDER_BUILDER_HPP
Definition: builder.hpp:57
osmium::memory::item_size_type append(const char *data, const osmium::memory::item_size_type length)
Definition: builder.hpp:156
uint32_t size() const noexcept
Definition: builder.hpp:133
osmium::memory::item_size_type append(const char *str)
Definition: builder.hpp:182
osmium::memory::Item & item() const
Definition: builder.hpp:90
void add_padding(bool self=false)
Definition: builder.hpp:111
OSMIUM_DEPRECATED void add_item(const osmium::memory::Item *item)
Definition: builder.hpp:224
Builder(osmium::memory::Buffer &buffer, Builder *parent, osmium::memory::item_size_type size)
Definition: builder.hpp:65
void add_size(osmium::memory::item_size_type size)
Definition: builder.hpp:126
Builder(Builder &&)=delete
osmium::memory::Buffer & m_buffer
Definition: builder.hpp:59
unsigned char * reserve_space(std::size_t size)
Definition: builder.hpp:94
Builder & operator=(Builder &&)=delete
T * reserve_space_for()
Definition: builder.hpp:142
osmium::memory::Buffer & buffer() noexcept
Return the buffer this builder is using.
Definition: builder.hpp:207
OSMIUM_DEPRECATED osmium::memory::item_size_type append_zero()
Definition: builder.hpp:193
Builder(const Builder &)=delete
std::size_t m_item_offset
Definition: builder.hpp:61
osmium::memory::item_size_type append_with_zero(const char *data, const osmium::memory::item_size_type length)
Definition: builder.hpp:169
void add_item(const osmium::memory::Item &item)
Definition: builder.hpp:215
Builder * m_parent
Definition: builder.hpp:60
Builder & operator=(const Builder &)=delete
~Builder() noexcept
Definition: builder.hpp:85
Definition: buffer.hpp:97
uint8_t builder_count() const noexcept
Definition: buffer.hpp:329
unsigned char * data() const noexcept
Definition: buffer.hpp:339
T & add_item(const T &item)
Definition: buffer.hpp:601
std::size_t committed() const noexcept
Definition: buffer.hpp:356
unsigned char * reserve_space(const std::size_t size)
Definition: buffer.hpp:557
bool is_aligned() const noexcept
Definition: buffer.hpp:375
void decrement_builder_count() noexcept
Definition: buffer.hpp:324
void increment_builder_count() noexcept
Definition: buffer.hpp:320
Definition: item.hpp:105
Item & add_size(const item_size_type size) noexcept
Definition: item.hpp:121
item_size_type byte_size() const noexcept
Definition: item.hpp:163
item_size_type padded_size() const
Definition: item.hpp:167
#define OSMIUM_DEPRECATED
Definition: compatibility.hpp:51
@ align_bytes
Definition: item.hpp:61
uint32_t item_size_type
Definition: item.hpp:57
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53