Libosmium  2.19.0
Fast and flexible C++ library for working with OpenStreetMap data
object.hpp
Go to the documentation of this file.
1#ifndef OSMIUM_OSM_OBJECT_HPP
2#define OSMIUM_OSM_OBJECT_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
39#include <osmium/osm/entity.hpp>
42#include <osmium/osm/tag.hpp>
44#include <osmium/osm/types.hpp>
46#include <osmium/util/misc.hpp>
47
48#include <cstdlib>
49#include <cstring>
50#include <stdexcept>
51#include <tuple>
52#include <type_traits>
53
54namespace osmium {
55
56 namespace builder {
57 template <typename TDerived, typename T>
58 class OSMObjectBuilder;
59 } // namespace builder
60
65
66 template <typename TDerived, typename T>
68
70 bool m_deleted : 1;
75
76 size_t sizeof_object() const noexcept {
77 return sizeof(OSMObject) + (type() == item_type::node ? sizeof(osmium::Location) : 0) + sizeof(string_size_type);
78 }
79
80 unsigned char* user_position() noexcept {
81 return data() + sizeof_object() - sizeof(string_size_type);
82 }
83
84 const unsigned char* user_position() const noexcept {
85 return data() + sizeof_object() - sizeof(string_size_type);
86 }
87
88 string_size_type user_size() const noexcept {
89 return *reinterpret_cast<const string_size_type*>(user_position());
90 }
91
92 unsigned char* subitems_position() {
94 }
95
96 const unsigned char* subitems_position() const {
98 }
99
100 protected:
101
104 m_deleted(false),
105 m_version(0) {
106 }
107
108 public:
109
110 constexpr static bool is_compatible_to(osmium::item_type t) noexcept {
111 return t == osmium::item_type::node ||
115 }
116
118 object_id_type id() const noexcept {
119 return m_id;
120 }
121
124 return static_cast<unsigned_object_id_type>(std::abs(m_id));
125 }
126
133 m_id = id;
134 return *this;
135 }
136
142 OSMObject& set_id(const char* id) {
144 }
145
147 bool deleted() const noexcept {
148 return m_deleted;
149 }
150
152 bool visible() const noexcept {
153 return !deleted();
154 }
155
161 OSMObject& set_deleted(bool deleted) noexcept {
163 return *this;
164 }
165
171 OSMObject& set_visible(bool visible) noexcept {
173 return *this;
174 }
175
183 if (!std::strcmp("true", visible)) {
184 set_visible(true);
185 } else if (!std::strcmp("false", visible)) {
186 set_visible(false);
187 } else {
188 throw std::invalid_argument{"Unknown value for visible attribute (allowed is 'true' or 'false')"};
189 }
190 return *this;
191 }
192
194 object_version_type version() const noexcept {
195 return m_version;
196 }
197
205 return *this;
206 }
207
215 }
216
218 changeset_id_type changeset() const noexcept {
219 return m_changeset;
220 }
221
229 return *this;
230 }
231
239 }
240
242 user_id_type uid() const noexcept {
243 return m_uid;
244 }
245
252 m_uid = uid;
253 return *this;
254 }
255
263 m_uid = uid < 0 ? 0 : static_cast<user_id_type>(uid);
264 return *this;
265 }
266
272 OSMObject& set_uid(const char* uid) {
274 return *this;
275 }
276
278 bool user_is_anonymous() const noexcept {
279 return m_uid == 0;
280 }
281
283 osmium::Timestamp timestamp() const noexcept {
284 return m_timestamp;
285 }
286
295 return *this;
296 }
297
309 assert(timestamp);
310 m_timestamp = detail::parse_timestamp(timestamp);
311 if (timestamp[20] != '\0') {
312 throw std::invalid_argument{"can not parse timestamp: garbage after timestamp"};
313 }
314 return *this;
315 }
316
318 const char* user() const noexcept {
319 return reinterpret_cast<const char*>(data() + sizeof_object());
320 }
321
323 void clear_user() noexcept {
324 std::memset(data() + sizeof_object(), 0, user_size());
325 }
326
328 const TagList& tags() const {
329 return osmium::detail::subitem_of_type<const TagList>(cbegin(), cend());
330 }
331
338 const char* get_value_by_key(const char* key, const char* default_value = nullptr) const noexcept {
339 return tags().get_value_by_key(key, default_value);
340 }
341
349 OSMObject& set_attribute(const char* attr, const char* value) {
350 if (!std::strcmp(attr, "id")) {
351 set_id(value);
352 } else if (!std::strcmp(attr, "version")) {
353 set_version(value);
354 } else if (!std::strcmp(attr, "changeset")) {
355 set_changeset(value);
356 } else if (!std::strcmp(attr, "timestamp")) {
357 set_timestamp(value);
358 } else if (!std::strcmp(attr, "uid")) {
359 set_uid(value);
360 } else if (!std::strcmp(attr, "visible")) {
361 set_visible(value);
362 }
363
364 return *this;
365 }
366
369
376 void remove_tags() noexcept {
377 for (auto& subitem : *this) {
378 if (subitem.type() == osmium::item_type::tag_list) {
379 subitem.set_removed(true);
380 }
381 }
382 }
383
385 return iterator(subitems_position());
386 }
387
389 return iterator(next());
390 }
391
394 }
395
397 return const_iterator(next());
398 }
399
401 return cbegin();
402 }
403
405 return cend();
406 }
407
413 template <typename T>
416 }
417
423 template <typename T>
426 }
427
428 template <typename T>
430
431 template <typename T>
433
434 template <typename T>
437 }
438
439 template <typename T>
441 return t_iterator<T>(next(), next());
442 }
443
444 template <typename T>
447 }
448
449 template <typename T>
451 return t_const_iterator<T>(next(), next());
452 }
453
454 template <typename T>
456 return cbegin<T>();
457 }
458
459 template <typename T>
461 return cend<T>();
462 }
463
464 }; // class OSMObject
465
466
470 inline bool operator==(const OSMObject& lhs, const OSMObject& rhs) noexcept {
471 return lhs.type() == rhs.type() &&
472 lhs.id() == rhs.id() &&
473 lhs.version() == rhs.version();
474 }
475
476 inline bool operator!=(const OSMObject& lhs, const OSMObject& rhs) noexcept {
477 return !(lhs == rhs);
478 }
479
495 inline bool operator<(const OSMObject& lhs, const OSMObject& rhs) noexcept {
496 return const_tie(lhs.type(), lhs.id() > 0, lhs.positive_id(), lhs.version(),
497 ((lhs.timestamp().valid() && rhs.timestamp().valid()) ? lhs.timestamp() : osmium::Timestamp())) <
498 const_tie(rhs.type(), rhs.id() > 0, rhs.positive_id(), rhs.version(),
499 ((lhs.timestamp().valid() && rhs.timestamp().valid()) ? rhs.timestamp() : osmium::Timestamp()));
500 }
501
502 inline bool operator>(const OSMObject& lhs, const OSMObject& rhs) noexcept {
503 return rhs < lhs;
504 }
505
506 inline bool operator<=(const OSMObject& lhs, const OSMObject& rhs) noexcept {
507 return !(rhs < lhs);
508 }
509
510 inline bool operator>=(const OSMObject& lhs, const OSMObject& rhs) noexcept {
511 return !(lhs < rhs);
512 }
513
514} // namespace osmium
515
516#endif // OSMIUM_OSM_OBJECT_HPP
Definition: location.hpp:271
OSMEntity is the abstract base class for the OSMObject and Changeset classes.
Definition: entity.hpp:64
Definition: object.hpp:64
const_iterator begin() const
Definition: object.hpp:400
const char * user() const noexcept
Get user name for this object.
Definition: object.hpp:318
OSMObject & set_deleted(bool deleted) noexcept
Definition: object.hpp:161
const TagList & tags() const
Get the list of tags for this object.
Definition: object.hpp:328
OSMObject & set_visible(const char *visible)
Definition: object.hpp:182
const_iterator cend() const
Definition: object.hpp:396
bool visible() const noexcept
Is this object marked visible (ie not deleted)?
Definition: object.hpp:152
osmium::Timestamp m_timestamp
Definition: object.hpp:72
bool user_is_anonymous() const noexcept
Is this user anonymous?
Definition: object.hpp:278
static constexpr bool is_compatible_to(osmium::item_type t) noexcept
Definition: object.hpp:110
OSMObject & set_visible(bool visible) noexcept
Definition: object.hpp:171
OSMObject & set_uid(const char *uid)
Definition: object.hpp:272
object_version_type m_version
Definition: object.hpp:71
void clear_user() noexcept
Clear user name.
Definition: object.hpp:323
object_version_type version() const noexcept
Get version of this object.
Definition: object.hpp:194
OSMObject & set_id(const char *id)
Definition: object.hpp:142
osmium::Timestamp timestamp() const noexcept
Get timestamp when this object last changed.
Definition: object.hpp:283
string_size_type user_size() const noexcept
Definition: object.hpp:88
bool deleted() const noexcept
Is this object marked as deleted?
Definition: object.hpp:147
user_id_type uid() const noexcept
Get user id of this object.
Definition: object.hpp:242
iterator end()
Definition: object.hpp:388
OSMObject & set_uid(user_id_type uid) noexcept
Definition: object.hpp:251
OSMObject & set_timestamp(const char *timestamp)
Definition: object.hpp:308
const char * get_value_by_key(const char *key, const char *default_value=nullptr) const noexcept
Definition: object.hpp:338
t_const_iterator< T > cend() const
Definition: object.hpp:450
unsigned char * subitems_position()
Definition: object.hpp:92
OSMObject & set_id(object_id_type id) noexcept
Definition: object.hpp:132
changeset_id_type changeset() const noexcept
Get changeset id of this object.
Definition: object.hpp:218
object_id_type m_id
Definition: object.hpp:69
const unsigned char * user_position() const noexcept
Definition: object.hpp:84
void remove_tags() noexcept
Definition: object.hpp:376
OSMObject(osmium::memory::item_size_type size, osmium::item_type type)
Definition: object.hpp:102
const_iterator end() const
Definition: object.hpp:404
object_id_type id() const noexcept
Get ID of this object.
Definition: object.hpp:118
unsigned_object_id_type positive_id() const noexcept
Get absolute value of the ID of this object.
Definition: object.hpp:123
osmium::memory::CollectionIterator< Item > iterator
Definition: object.hpp:367
t_const_iterator< T > end() const
Definition: object.hpp:460
t_iterator< T > begin()
Definition: object.hpp:435
size_t sizeof_object() const noexcept
Definition: object.hpp:76
const unsigned char * subitems_position() const
Definition: object.hpp:96
t_const_iterator< T > cbegin() const
Definition: object.hpp:445
osmium::memory::CollectionIterator< const Item > const_iterator
Definition: object.hpp:368
bool m_deleted
Definition: object.hpp:70
unsigned char * user_position() noexcept
Definition: object.hpp:80
OSMObject & set_version(const char *version)
Definition: object.hpp:213
OSMObject & set_uid_from_signed(signed_user_id_type uid) noexcept
Definition: object.hpp:262
t_const_iterator< T > begin() const
Definition: object.hpp:455
user_id_type m_uid
Definition: object.hpp:73
OSMObject & set_changeset(const char *changeset)
Definition: object.hpp:237
osmium::memory::ItemIteratorRange< const T > subitems() const
Definition: object.hpp:424
iterator begin()
Definition: object.hpp:384
changeset_id_type m_changeset
Definition: object.hpp:74
const_iterator cbegin() const
Definition: object.hpp:392
osmium::memory::ItemIteratorRange< T > subitems()
Definition: object.hpp:414
t_iterator< T > end()
Definition: object.hpp:440
OSMObject & set_timestamp(const osmium::Timestamp &timestamp) noexcept
Definition: object.hpp:293
OSMObject & set_version(object_version_type version) noexcept
Definition: object.hpp:203
OSMObject & set_attribute(const char *attr, const char *value)
Definition: object.hpp:349
OSMObject & set_changeset(changeset_id_type changeset) noexcept
Definition: object.hpp:227
Definition: tag.hpp:119
const char * get_value_by_key(const char *key, const char *default_value=nullptr) const noexcept
Definition: tag.hpp:137
Definition: timestamp.hpp:146
uint32_t size() const noexcept
Definition: builder.hpp:136
Definition: osm_object_builder.hpp:401
Definition: collection.hpp:47
Definition: item_iterator.hpp:175
Definition: item_iterator.hpp:59
item_type type() const noexcept
Definition: item.hpp:171
unsigned char * next() noexcept
Definition: item.hpp:155
constexpr std::size_t padded_length(std::size_t length) noexcept
Definition: item.hpp:64
uint32_t item_size_type
Definition: item.hpp:57
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
changeset_id_type string_to_changeset_id(const char *input)
Definition: types_from_string.hpp:148
user_id_type string_to_uid(const char *input)
Definition: types_from_string.hpp:162
object_id_type string_to_object_id(const char *input)
Definition: types_from_string.hpp:59
std::tuple< const Ts &... > const_tie(const Ts &... args) noexcept
Definition: misc.hpp:52
bool operator==(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:440
uint32_t object_version_type
Type for OSM object version number.
Definition: types.hpp:47
int32_t signed_user_id_type
Type for signed OSM user IDs.
Definition: types.hpp:50
uint32_t user_id_type
Type for OSM user IDs.
Definition: types.hpp:49
bool operator<=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:459
uint32_t changeset_id_type
Type for OSM changeset IDs.
Definition: types.hpp:48
bool operator>(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:455
int64_t object_id_type
Type for OSM object (node, way, or relation) IDs.
Definition: types.hpp:45
uint16_t string_size_type
Definition: types.hpp:59
item_type
Definition: item_type.hpp:45
object_version_type string_to_object_version(const char *input)
Definition: types_from_string.hpp:134
bool operator>=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:463
bool operator!=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:444
uint64_t unsigned_object_id_type
Type for OSM object (node, way, or relation) IDs where we only allow positive IDs.
Definition: types.hpp:46
bool operator<(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:451