Libosmium  2.20.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
6This file is part of Osmium (https://osmcode.org/libosmium).
7
8Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
9
10Boost Software License - Version 1.0 - August 17th, 2003
11
12Permission is hereby granted, free of charge, to any person or organization
13obtaining a copy of the software and accompanying documentation covered by
14this license (the "Software") to use, reproduce, display, distribute,
15execute, and transmit the Software, and to prepare derivative works of the
16Software, and to permit third-parties to whom the Software is furnished to
17do so, all subject to the following:
18
19The copyright notices in the Software and this entire statement, including
20the above license grant, this restriction and the following disclaimer,
21must be included in all copies of the Software, in whole or in part, and
22all derivative works of the Software, unless such copies or derivative
23works are solely in the form of machine-executable object code generated by
24a source language processor.
25
26THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32DEALINGS IN THE SOFTWARE.
33
34*/
35
36#include <cstddef>
37#include <cstdint>
38
39namespace 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
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 enum : std::size_t {
61 align_bytes = 8UL
62 };
63
64 inline constexpr 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() noexcept = default;
82
83 ItemHelper(const ItemHelper&) noexcept = default;
84 ItemHelper(ItemHelper&&) noexcept = default;
85
86 ItemHelper& operator=(const ItemHelper&) noexcept = default;
87 ItemHelper& operator=(ItemHelper&&) noexcept = default;
88
89 ~ItemHelper() noexcept = 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& set_type(const item_type item_type) noexcept {
138 return *this;
139 }
140
141 public:
142
143 Item(const Item&) = delete;
144 Item& operator=(const Item&) = delete;
145
146 Item(Item&&) = delete;
147 Item& operator=(Item&&) = delete;
148
149 ~Item() noexcept = default;
150
151 constexpr static bool is_compatible_to(osmium::item_type /*t*/) noexcept {
152 return true;
153 }
154
155 unsigned char* next() noexcept {
156 return data() + padded_size();
157 }
158
159 const unsigned char* next() const noexcept {
160 return data() + padded_size();
161 }
162
163 item_size_type byte_size() const noexcept {
164 return m_size;
165 }
166
168 return static_cast<item_size_type>(padded_length(m_size));
169 }
170
171 item_type type() const noexcept {
172 return m_type;
173 }
174
175 bool removed() const noexcept {
176 return m_removed;
177 }
178
179 void set_removed(const bool removed) noexcept {
181 }
182
183 diff_indicator_type diff() const noexcept {
184 return static_cast<diff_indicator_type>(m_diff);
185 }
186
187 char diff_as_char() const noexcept {
188 static constexpr const char* diff_chars = "*-+ ";
189 return diff_chars[m_diff];
190 }
191
192 void set_diff(const diff_indicator_type diff) noexcept {
193 m_diff = static_cast<uint16_t>(diff);
194 }
195
196 }; // class Item
197
198
199 } // namespace memory
200
201} // namespace osmium
202
203#endif // OSMIUM_MEMORY_ITEM_HPP
Definition: builder.hpp:56
uint32_t size() const noexcept
Definition: builder.hpp:136
Definition: collection.hpp:47
Definition: item_iterator.hpp:59
Definition: item.hpp:105
char diff_as_char() const noexcept
Definition: item.hpp:187
Item & set_type(const item_type item_type) noexcept
Definition: item.hpp:136
item_type type() const noexcept
Definition: item.hpp:171
static constexpr bool is_compatible_to(osmium::item_type) noexcept
Definition: item.hpp:151
uint16_t m_diff
Definition: item.hpp:110
item_size_type m_size
Definition: item.hpp:107
Item(Item &&)=delete
Item(const Item &)=delete
diff_indicator_type diff() const noexcept
Definition: item.hpp:183
uint16_t m_removed
Definition: item.hpp:109
bool removed() const noexcept
Definition: item.hpp:175
~Item() noexcept=default
void set_removed(const bool removed) noexcept
Definition: item.hpp:179
Item(item_size_type size=0, item_type type=item_type{}) noexcept
Definition: item.hpp:128
item_size_type byte_size() const noexcept
Definition: item.hpp:163
Item & operator=(Item &&)=delete
Item & operator=(const Item &)=delete
const unsigned char * next() const noexcept
Definition: item.hpp:159
Item & add_size(const item_size_type size) noexcept
Definition: item.hpp:121
unsigned char * next() noexcept
Definition: item.hpp:155
void set_diff(const diff_indicator_type diff) noexcept
Definition: item.hpp:192
uint16_t m_padding
Definition: item.hpp:111
item_type m_type
Definition: item.hpp:108
item_size_type padded_size() const
Definition: item.hpp:167
Definition: attr.hpp:342
constexpr std::size_t padded_length(std::size_t length) noexcept
Definition: item.hpp:64
@ 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
diff_indicator_type
Definition: item.hpp:48
item_type
Definition: item_type.hpp:45