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