Libosmium  2.19.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-2023 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>
38 
39 #include <algorithm>
40 #include <cassert>
41 #include <cstddef>
42 #include <cstdint>
43 #include <cstring>
44 
45 namespace osmium {
46 
50  namespace builder {
51 
56  class Builder {
57 
58  osmium::memory::Buffer& m_buffer;
60  std::size_t m_item_offset;
61 
62  protected:
63 
64  explicit Builder(osmium::memory::Buffer& buffer, Builder* parent, osmium::memory::item_size_type size) :
66  m_parent(parent),
67  m_item_offset(buffer.written() - buffer.committed()) {
69  assert(buffer.is_aligned());
70  if (m_parent) {
71  assert(m_buffer.builder_count() == 1 && "Only one sub-builder can be open at any time.");
73  } else {
74  assert(m_buffer.builder_count() == 0 && "Only one builder can be open at any time.");
75  }
76 #ifndef NDEBUG
77  m_buffer.increment_builder_count();
78 #endif
79  }
80 
81 #ifdef NDEBUG
82  ~Builder() noexcept = default;
83 #else
84  ~Builder() noexcept {
85  m_buffer.decrement_builder_count();
86  }
87 #endif
88 
89  unsigned char* item_pos() const noexcept {
90  return m_buffer.data() + m_buffer.committed() + m_item_offset;
91  }
92 
93  osmium::memory::Item& item() const noexcept {
94  return *reinterpret_cast<osmium::memory::Item*>(item_pos());
95  }
96 
97  unsigned char* reserve_space(std::size_t size) {
98  return m_buffer.reserve_space(size);
99  }
100 
114  void add_padding(bool self = false) {
115  // We know the padding is only a very small number, so it will
116  // always fit.
118  if (padding != osmium::memory::align_bytes) {
119  std::fill_n(reserve_space(padding), padding, 0);
120  if (self) {
121  add_size(padding);
122  } else if (m_parent) {
123  m_parent->add_size(padding);
124  assert(m_parent->size() % osmium::memory::align_bytes == 0);
125  }
126  }
127  }
128 
130  item().add_size(size);
131  if (m_parent) {
133  }
134  }
135 
136  uint32_t size() const noexcept {
137  return item().byte_size();
138  }
139 
144  template <typename T>
146  assert(m_buffer.is_aligned());
147  return reinterpret_cast<T*>(reserve_space(sizeof(T)));
148  }
149 
160  unsigned char* target = reserve_space(length);
161  std::copy_n(reinterpret_cast<const unsigned char*>(data), length, target);
162  return length;
163  }
164 
173  unsigned char* target = reserve_space(length + 1);
174  std::copy_n(reinterpret_cast<const unsigned char*>(data), length, target);
175  target[length] = '\0';
176  return length + 1;
177  }
178 
186  return append(str, static_cast<osmium::memory::item_size_type>(std::strlen(str) + 1));
187  }
188 
189  public:
190 
191  Builder(const Builder&) = delete;
192  Builder(Builder&&) = delete;
193 
194  Builder& operator=(const Builder&) = delete;
195  Builder& operator=(Builder&&) = delete;
196 
198  osmium::memory::Buffer& buffer() noexcept {
199  return m_buffer;
200  }
201 
207  m_buffer.add_item(item);
209  }
210 
211  }; // class Builder
212 
213  } // namespace builder
214 
215 } // namespace osmium
216 
217 #endif // OSMIUM_BUILDER_BUILDER_HPP
Definition: builder.hpp:56
osmium::memory::item_size_type append(const char *data, const osmium::memory::item_size_type length)
Definition: builder.hpp:159
uint32_t size() const noexcept
Definition: builder.hpp:136
osmium::memory::item_size_type append(const char *str)
Definition: builder.hpp:185
void add_padding(bool self=false)
Definition: builder.hpp:114
Builder(osmium::memory::Buffer &buffer, Builder *parent, osmium::memory::item_size_type size)
Definition: builder.hpp:64
void add_size(osmium::memory::item_size_type size)
Definition: builder.hpp:129
Builder(Builder &&)=delete
osmium::memory::Buffer & m_buffer
Definition: builder.hpp:58
unsigned char * reserve_space(std::size_t size)
Definition: builder.hpp:97
Builder & operator=(Builder &&)=delete
T * reserve_space_for()
Definition: builder.hpp:145
osmium::memory::Buffer & buffer() noexcept
Return the buffer this builder is using.
Definition: builder.hpp:198
osmium::memory::Item & item() const noexcept
Definition: builder.hpp:93
Builder(const Builder &)=delete
std::size_t m_item_offset
Definition: builder.hpp:60
osmium::memory::item_size_type append_with_zero(const char *data, const osmium::memory::item_size_type length)
Definition: builder.hpp:172
void add_item(const osmium::memory::Item &item)
Definition: builder.hpp:206
unsigned char * item_pos() const noexcept
Definition: builder.hpp:89
Builder * m_parent
Definition: builder.hpp:59
Builder & operator=(const Builder &)=delete
~Builder() noexcept
Definition: builder.hpp:84
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
@ 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