Libosmium  2.9.0
Fast and flexible C++ library for working with OpenStreetMap data
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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-2016 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 item_size_type align_bytes = 8;
63 
64  inline std::size_t padded_length(std::size_t length) noexcept {
65  return (length + align_bytes - 1) & ~(align_bytes - 1);
66  }
67 
71  namespace detail {
72 
77  class ItemHelper {
78 
79  protected:
80 
81  ItemHelper() = default;
82 
83  ~ItemHelper() = default;
84 
85  ItemHelper(const ItemHelper&) = default;
86  ItemHelper(ItemHelper&&) = default;
87 
88  ItemHelper& operator=(const ItemHelper&) = default;
89  ItemHelper& operator=(ItemHelper&&) = default;
90 
91  public:
92 
93  unsigned char* data() noexcept {
94  return reinterpret_cast<unsigned char*>(this);
95  }
96 
97  const unsigned char* data() const noexcept {
98  return reinterpret_cast<const unsigned char*>(this);
99  }
100 
101  }; // class ItemHelper
102 
103  } // namespace detail
104 
105  class Item : public osmium::memory::detail::ItemHelper {
106 
109  uint16_t m_removed : 1;
110  uint16_t m_diff : 2;
111  uint16_t m_padding : 13;
112 
113  template <typename TMember>
114  friend class CollectionIterator;
115 
116  template <typename TMember>
117  friend class ItemIterator;
118 
120 
121  Item& add_size(const item_size_type size) noexcept {
122  m_size += size;
123  return *this;
124  }
125 
126  protected:
127 
128  explicit Item(item_size_type size = 0, item_type type = item_type()) noexcept :
129  m_size(size),
130  m_type(type),
131  m_removed(false),
132  m_diff(0),
133  m_padding(0) {
134  }
135 
136  Item(const Item&) = delete;
137  Item(Item&&) = delete;
138 
139  Item& operator=(const Item&) = delete;
140  Item& operator=(Item&&) = delete;
141 
142  Item& set_type(const item_type item_type) noexcept {
143  m_type = item_type;
144  return *this;
145  }
146 
147  public:
148 
149  unsigned char* next() noexcept {
150  return data() + padded_size();
151  }
152 
153  const unsigned char* next() const noexcept {
154  return data() + padded_size();
155  }
156 
157  item_size_type byte_size() const noexcept {
158  return m_size;
159  }
160 
162  return static_cast_with_assert<item_size_type>(padded_length(m_size));
163  }
164 
165  item_type type() const noexcept {
166  return m_type;
167  }
168 
169  bool removed() const noexcept {
170  return m_removed;
171  }
172 
173  void set_removed(bool removed) noexcept {
174  m_removed = removed;
175  }
176 
177  diff_indicator_type diff() const noexcept {
178  return diff_indicator_type(m_diff);
179  }
180 
181  char diff_as_char() const noexcept {
182  static constexpr const char* diff_chars = "*-+ ";
183  return diff_chars[m_diff];
184  }
185 
187  m_diff = uint16_t(diff);
188  }
189 
190  }; // class Item
191 
192 
193  } // namespace memory
194 
195 } // namespace osmium
196 
197 #endif // OSMIUM_MEMORY_ITEM_HPP
Definition: collection.hpp:47
uint32_t item_size_type
Definition: item.hpp:59
Item & set_type(const item_type item_type) noexcept
Definition: item.hpp:142
Definition: item_iterator.hpp:132
unsigned char * next() noexcept
Definition: item.hpp:149
diff_indicator_type
Definition: item.hpp:50
item_type
Definition: item_type.hpp:43
constexpr item_size_type align_bytes
Definition: item.hpp:62
item_size_type padded_size() const
Definition: item.hpp:161
std::size_t padded_length(std::size_t length) noexcept
Definition: item.hpp:64
diff_indicator_type diff() const noexcept
Definition: item.hpp:177
Definition: item.hpp:105
Namespace for everything in the Osmium library.
Definition: assembler.hpp:73
uint16_t m_removed
Definition: item.hpp:109
Item & add_size(const item_size_type size) noexcept
Definition: item.hpp:121
const unsigned char * next() const noexcept
Definition: item.hpp:153
Definition: attr.hpp:333
uint16_t m_padding
Definition: item.hpp:111
void set_diff(diff_indicator_type diff) noexcept
Definition: item.hpp:186
uint16_t m_diff
Definition: item.hpp:110
item_size_type m_size
Definition: item.hpp:107
void set_removed(bool removed) noexcept
Definition: item.hpp:173
bool removed() const noexcept
Definition: item.hpp:169
item_size_type byte_size() const noexcept
Definition: item.hpp:157
Item(item_size_type size=0, item_type type=item_type()) noexcept
Definition: item.hpp:128
Item & operator=(const Item &)=delete
uint32_t size() const noexcept
Definition: builder.hpp:122
char diff_as_char() const noexcept
Definition: item.hpp:181
item_type m_type
Definition: item.hpp:108
item_type type() const noexcept
Definition: item.hpp:165
Definition: builder.hpp:56