Libosmium  2.11.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-2017 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>
49 
50 namespace osmium {
51 
55  namespace builder {
56 
61  class Builder {
62 
65  size_t m_item_offset;
66 
67  Builder(const Builder&) = delete;
68  Builder(Builder&&) = delete;
69 
70  Builder& operator=(const Builder&) = delete;
71  Builder& operator=(Builder&&) = delete;
72 
73  protected:
74 
76  m_buffer(buffer),
77  m_parent(parent),
78  m_item_offset(buffer.written()) {
79  reserve_space(size);
80  assert(buffer.is_aligned());
81  if (m_parent) {
82  assert(m_buffer.builder_count() == 1 && "Only one sub-builder can be open at any time.");
83  m_parent->add_size(size);
84  } else {
85  assert(m_buffer.builder_count() == 0 && "Only one builder can be open at any time.");
86  }
87 #ifndef NDEBUG
88  m_buffer.increment_builder_count();
89 #endif
90  }
91 
92 #ifdef NDEBUG
93  ~Builder() = default;
94 #else
95  ~Builder() noexcept {
96  m_buffer.decrement_builder_count();
97  }
98 #endif
99 
101  return *reinterpret_cast<osmium::memory::Item*>(m_buffer.data() + m_item_offset);
102  }
103 
104  unsigned char* reserve_space(size_t size) {
105  return m_buffer.reserve_space(size);
106  }
107 
121  void add_padding(bool self = false) {
122  const auto padding = osmium::memory::align_bytes - (size() % osmium::memory::align_bytes);
123  if (padding != osmium::memory::align_bytes) {
124  std::fill_n(reserve_space(padding), padding, 0);
125  if (self) {
126  add_size(padding);
127  } else if (m_parent) {
128  m_parent->add_size(padding);
129  assert(m_parent->size() % osmium::memory::align_bytes == 0);
130  }
131  }
132  }
133 
134  void add_size(uint32_t size) {
135  item().add_size(size);
136  if (m_parent) {
137  m_parent->add_size(size);
138  }
139  }
140 
141  uint32_t size() const noexcept {
142  return item().byte_size();
143  }
144 
149  template <typename T>
151  assert(m_buffer.is_aligned());
152  return reinterpret_cast<T*>(reserve_space(sizeof(T)));
153  }
154 
165  unsigned char* target = reserve_space(length);
166  std::copy_n(reinterpret_cast<const unsigned char*>(data), length, target);
167  return length;
168  }
169 
177  return append(str, static_cast<osmium::memory::item_size_type>(std::strlen(str) + 1));
178  }
179 
186  *reserve_space(1) = '\0';
187  return 1;
188  }
189 
190  public:
191 
194  return m_buffer;
195  }
196 
202  m_buffer.add_item(item);
203  add_size(item.padded_size());
204  }
205 
211  assert(item);
212  add_item(*item);
213  }
214 
215  }; // class Builder
216 
217  } // namespace builder
218 
219 } // namespace osmium
220 
221 #endif // OSMIUM_BUILDER_BUILDER_HPP
Builder * m_parent
Definition: builder.hpp:64
Builder(osmium::memory::Buffer &buffer, Builder *parent, osmium::memory::item_size_type size)
Definition: builder.hpp:75
#define OSMIUM_DEPRECATED
Definition: compatibility.hpp:50
bool is_aligned() const noexcept
Definition: buffer.hpp:278
osmium::memory::Buffer & buffer() noexcept
Return the buffer this builder is using.
Definition: builder.hpp:193
void add_item(const osmium::memory::Item &item)
Definition: builder.hpp:201
osmium::memory::item_size_type append_zero()
Definition: builder.hpp:185
uint32_t item_size_type
Definition: item.hpp:59
void increment_builder_count() noexcept
Definition: buffer.hpp:223
OSMIUM_DEPRECATED void add_item(const osmium::memory::Item *item)
Definition: builder.hpp:210
unsigned char * reserve_space(size_t size)
Definition: builder.hpp:104
item_size_type padded_size() const
Definition: item.hpp:165
Definition: item.hpp:105
Namespace for everything in the Osmium library.
Definition: assembler.hpp:73
T & add_item(const T &item)
Definition: buffer.hpp:482
Item & add_size(const item_size_type size) noexcept
Definition: item.hpp:121
T * reserve_space_for()
Definition: builder.hpp:150
uint8_t builder_count() const noexcept
Definition: buffer.hpp:232
unsigned char * data() const noexcept
Definition: buffer.hpp:242
osmium::memory::item_size_type append(const char *str)
Definition: builder.hpp:176
unsigned char * reserve_space(const size_t size)
Definition: buffer.hpp:442
constexpr const item_size_type align_bytes
Definition: item.hpp:62
item_size_type byte_size() const noexcept
Definition: item.hpp:161
~Builder() noexcept
Definition: builder.hpp:95
osmium::memory::Buffer & m_buffer
Definition: builder.hpp:63
Definition: buffer.hpp:97
size_t m_item_offset
Definition: builder.hpp:65
void decrement_builder_count() noexcept
Definition: buffer.hpp:227
osmium::memory::item_size_type append(const char *data, const osmium::memory::item_size_type length)
Definition: builder.hpp:164
Builder(const Builder &)=delete
uint32_t size() const noexcept
Definition: builder.hpp:141
void add_size(uint32_t size)
Definition: builder.hpp:134
Definition: builder.hpp:61
Builder & operator=(const Builder &)=delete
void add_padding(bool self=false)
Definition: builder.hpp:121
osmium::memory::Item & item() const
Definition: builder.hpp:100