Libosmium 2.22.0 Fast and flexible C++ library for working with OpenStreetMap data

Loading...
Searching...
No Matches
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 (https://osmcode.org/libosmium).
7
8 Copyright 2013-2025 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 enum : std::size_t {
61 align_bytes = 8UL
62 };
63
64 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
107 item_size_type m_size;
108 item_type m_type;
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
119 friend class osmium::builder::Builder;
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 {
137 m_type = item_type;
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
167 item_size_type padded_size() const {
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 {
180 m_removed = removed;
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
osmium::builder::Builder
Definition builder.hpp:56
osmium::memory::CollectionIterator
Definition collection.hpp:47
osmium::memory::ItemIterator
Definition item_iterator.hpp:59
osmium::memory::Item
Definition item.hpp:105
osmium::memory::Item::diff_as_char
char diff_as_char() const noexcept
Definition item.hpp:187
osmium::memory::Item::set_type
Item & set_type(const item_type item_type) noexcept
Definition item.hpp:136
osmium::memory::Item::type
item_type type() const noexcept
Definition item.hpp:171
osmium::memory::Item::is_compatible_to
static constexpr bool is_compatible_to(osmium::item_type) noexcept
Definition item.hpp:151
osmium::memory::Item::m_diff
uint16_t m_diff
Definition item.hpp:110
osmium::memory::Item::m_size
item_size_type m_size
Definition item.hpp:107
osmium::memory::Item::Item
Item(Item &&)=delete
osmium::memory::Item::Item
Item(const Item &)=delete
osmium::memory::Item::diff
diff_indicator_type diff() const noexcept
Definition item.hpp:183
osmium::memory::Item::m_removed
uint16_t m_removed
Definition item.hpp:109
osmium::memory::Item::removed
bool removed() const noexcept
Definition item.hpp:175
osmium::memory::Item::~Item
~Item() noexcept=default
osmium::memory::Item::set_removed
void set_removed(const bool removed) noexcept
Definition item.hpp:179
osmium::memory::Item::Item
Item(item_size_type size=0, item_type type=item_type{}) noexcept
Definition item.hpp:128
osmium::memory::Item::byte_size
item_size_type byte_size() const noexcept
Definition item.hpp:163
osmium::memory::Item::operator=
Item & operator=(Item &&)=delete
osmium::memory::Item::operator=
Item & operator=(const Item &)=delete
osmium::memory::Item::next
const unsigned char * next() const noexcept
Definition item.hpp:159
osmium::memory::Item::add_size
Item & add_size(const item_size_type size) noexcept
Definition item.hpp:121
osmium::memory::Item::next
unsigned char * next() noexcept
Definition item.hpp:155
osmium::memory::Item::set_diff
void set_diff(const diff_indicator_type diff) noexcept
Definition item.hpp:192
osmium::memory::Item::m_padding
uint16_t m_padding
Definition item.hpp:111
osmium::memory::Item::m_type
item_type m_type
Definition item.hpp:108
osmium::memory::Item::padded_size
item_size_type padded_size() const
Definition item.hpp:167
detail
Definition attr.hpp:342
osmium::memory::padded_length
constexpr std::size_t padded_length(std::size_t length) noexcept
Definition item.hpp:64
osmium::memory::item_size_type
uint32_t item_size_type
Definition item.hpp:57
osmium::memory::align_bytes
@ align_bytes
Definition item.hpp:61
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
osmium::diff_indicator_type
diff_indicator_type
Definition item.hpp:48
osmium::diff_indicator_type::none
@ none
osmium::diff_indicator_type::right
@ right
osmium::diff_indicator_type::left
@ left
osmium::diff_indicator_type::both
@ both
osmium::item_type
item_type
Definition item_type.hpp:45