Libosmium  2.14.0
Fast and flexible C++ library for working with OpenStreetMap data
item.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_MEMORY_ITEM_HPP
2 #define OSMIUM_MEMORY_ITEM_HPP
3 
4 /*
5 
6 This file is part of Osmium (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2018 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 <cstddef>
37 #include <cstdint>
38 
39 namespace osmium {
40 
41  // forward declaration, see osmium/osm/item_type.hpp for declaration
42  enum class item_type : uint16_t;
43 
44  namespace builder {
45  class Builder;
46  } // namespace builder
47 
48  enum class diff_indicator_type {
49  none = 0,
50  left = 1,
51  right = 2,
52  both = 3
53  }; // diff_indicator_type
54 
55  namespace memory {
56 
57  using item_size_type = uint32_t;
58 
59  // align datastructures to this many bytes
60  constexpr const std::size_t align_bytes = 8;
61 
62  inline constexpr std::size_t padded_length(std::size_t length) noexcept {
63  return (length + align_bytes - 1) & ~(align_bytes - 1);
64  }
65 
69  namespace detail {
70 
75  class ItemHelper {
76 
77  protected:
78 
79  ItemHelper() noexcept = default;
80 
81  ItemHelper(const ItemHelper&) noexcept = default;
82  ItemHelper(ItemHelper&&) noexcept = default;
83 
84  ItemHelper& operator=(const ItemHelper&) noexcept = default;
85  ItemHelper& operator=(ItemHelper&&) noexcept = default;
86 
87  ~ItemHelper() noexcept = default;
88 
89  public:
90 
91  unsigned char* data() noexcept {
92  return reinterpret_cast<unsigned char*>(this);
93  }
94 
95  const unsigned char* data() const noexcept {
96  return reinterpret_cast<const unsigned char*>(this);
97  }
98 
99  }; // class ItemHelper
100 
101  } // namespace detail
102 
103  class Item : public osmium::memory::detail::ItemHelper {
104 
107  uint16_t m_removed : 1;
108  uint16_t m_diff : 2;
109  uint16_t m_padding : 13;
110 
111  template <typename TMember>
112  friend class CollectionIterator;
113 
114  template <typename TMember>
115  friend class ItemIterator;
116 
118 
119  Item& add_size(const item_size_type size) noexcept {
120  m_size += size;
121  return *this;
122  }
123 
124  protected:
125 
126  explicit Item(item_size_type size = 0, item_type type = item_type()) noexcept :
127  m_size(size),
128  m_type(type),
129  m_removed(false),
130  m_diff(0),
131  m_padding(0) {
132  }
133 
134  Item& set_type(const item_type item_type) noexcept {
135  m_type = item_type;
136  return *this;
137  }
138 
139  public:
140 
141  Item(const Item&) = delete;
142  Item& operator=(const Item&) = delete;
143 
144  Item(Item&&) = delete;
145  Item& operator=(Item&&) = delete;
146 
147  ~Item() noexcept = default;
148 
149  constexpr static bool is_compatible_to(osmium::item_type /*t*/) noexcept {
150  return true;
151  }
152 
153  unsigned char* next() noexcept {
154  return data() + padded_size();
155  }
156 
157  const unsigned char* next() const noexcept {
158  return data() + padded_size();
159  }
160 
161  item_size_type byte_size() const noexcept {
162  return m_size;
163  }
164 
166  return static_cast<item_size_type>(padded_length(m_size));
167  }
168 
169  item_type type() const noexcept {
170  return m_type;
171  }
172 
173  bool removed() const noexcept {
174  return m_removed;
175  }
176 
177  void set_removed(bool removed) noexcept {
178  m_removed = removed;
179  }
180 
181  diff_indicator_type diff() const noexcept {
182  return diff_indicator_type(m_diff);
183  }
184 
185  char diff_as_char() const noexcept {
186  static constexpr const char* diff_chars = "*-+ ";
187  return diff_chars[m_diff];
188  }
189 
190  void set_diff(diff_indicator_type diff) noexcept {
191  m_diff = uint16_t(diff);
192  }
193 
194  }; // class Item
195 
196 
197  } // namespace memory
198 
199 } // namespace osmium
200 
201 #endif // OSMIUM_MEMORY_ITEM_HPP
Definition: collection.hpp:47
type
Definition: entity_bits.hpp:63
uint32_t item_size_type
Definition: item.hpp:57
Item & set_type(const item_type item_type) noexcept
Definition: item.hpp:134
Definition: item_iterator.hpp:59
unsigned char * next() noexcept
Definition: item.hpp:153
diff_indicator_type
Definition: item.hpp:48
item_type
Definition: item_type.hpp:43
constexpr std::size_t padded_length(std::size_t length) noexcept
Definition: item.hpp:62
static constexpr bool is_compatible_to(osmium::item_type) noexcept
Definition: item.hpp:149
constexpr const std::size_t align_bytes
Definition: item.hpp:60
item_size_type padded_size() const
Definition: item.hpp:165
diff_indicator_type diff() const noexcept
Definition: item.hpp:181
Definition: item.hpp:103
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
uint16_t m_removed
Definition: item.hpp:107
Item & add_size(const item_size_type size) noexcept
Definition: item.hpp:119
const unsigned char * next() const noexcept
Definition: item.hpp:157
Definition: attr.hpp:333
uint16_t m_padding
Definition: item.hpp:109
void set_diff(diff_indicator_type diff) noexcept
Definition: item.hpp:190
uint16_t m_diff
Definition: item.hpp:108
item_size_type m_size
Definition: item.hpp:105
void set_removed(bool removed) noexcept
Definition: item.hpp:177
bool removed() const noexcept
Definition: item.hpp:173
item_size_type byte_size() const noexcept
Definition: item.hpp:161
Item(item_size_type size=0, item_type type=item_type()) noexcept
Definition: item.hpp:126
uint32_t size() const noexcept
Definition: builder.hpp:133
char diff_as_char() const noexcept
Definition: item.hpp:185
item_type m_type
Definition: item.hpp:106
item_type type() const noexcept
Definition: item.hpp:169
Definition: builder.hpp:57
Builder & operator=(const Builder &)=delete