Libosmium  2.17.2
Fast and flexible C++ library for working with OpenStreetMap data
changeset.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_OSM_CHANGESET_HPP
2 #define OSMIUM_OSM_CHANGESET_HPP
3 
4 /*
5 
6 This file is part of Osmium (https://osmcode.org/libosmium).
7 
8 Copyright 2013-2021 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 
37 #include <osmium/memory/item.hpp>
38 #include <osmium/osm/box.hpp>
39 #include <osmium/osm/entity.hpp>
40 #include <osmium/osm/item_type.hpp>
41 #include <osmium/osm/tag.hpp>
42 #include <osmium/osm/timestamp.hpp>
43 #include <osmium/osm/types.hpp>
45 
46 #include <cstdint>
47 #include <cstring>
48 #include <iterator>
49 
50 namespace osmium {
51 
52  namespace builder {
53  class ChangesetDiscussionBuilder;
54  class ChangesetBuilder;
55  } // namespace builder
56 
57  class Changeset;
58 
59  class ChangesetComment : public osmium::memory::detail::ItemHelper {
60 
62 
67 
68  unsigned char* endpos() {
70  }
71 
72  const unsigned char* endpos() const {
74  }
75 
76  template <typename TMember>
78 
79  unsigned char* next() {
80  return endpos();
81  }
82 
83  const unsigned char* next() const {
84  return endpos();
85  }
86 
87  void set_user_size(string_size_type size) noexcept {
88  m_user_size = size;
89  }
90 
92  m_text_size = size;
93  }
94 
95  public:
96 
98 
100  m_date(date),
101  m_uid(uid),
102  m_text_size(0),
103  m_user_size(0) {
104  }
105 
108 
111 
112  ~ChangesetComment() noexcept = default;
113 
114  osmium::Timestamp date() const noexcept {
115  return m_date;
116  }
117 
118  osmium::user_id_type uid() const noexcept {
119  return m_uid;
120  }
121 
122  const char* user() const noexcept {
123  return reinterpret_cast<const char*>(data() + sizeof(ChangesetComment));
124  }
125 
126  const char* text() const noexcept {
127  return reinterpret_cast<const char*>(data() + sizeof(ChangesetComment) + m_user_size);
128  }
129 
130  }; // class ChangesetComment
131 
132  class ChangesetDiscussion : public osmium::memory::Collection<ChangesetComment, osmium::item_type::changeset_discussion> {
133 
134  public:
135 
136  ChangesetDiscussion() noexcept = default;
137 
138  }; // class ChangesetDiscussion
139 
140 
148  class Changeset : public osmium::OSMEntity {
149 
151 
152  osmium::Box m_bounds;
156  num_changes_type m_num_changes = 0;
157  num_comments_type m_num_comments = 0;
158  user_id_type m_uid = 0;
159  string_size_type m_user_size = 0;
160  int16_t m_padding1 = 0;
161  int32_t m_padding2 = 0;
162 
164  OSMEntity(sizeof(Changeset), osmium::item_type::changeset) {
165  }
166 
168  m_user_size = size;
169  }
170 
171  string_size_type user_size() const noexcept {
172  return m_user_size;
173  }
174 
175  unsigned char* subitems_position() {
176  return data() + osmium::memory::padded_length(sizeof(Changeset) + m_user_size);
177  }
178 
179  const unsigned char* subitems_position() const {
180  return data() + osmium::memory::padded_length(sizeof(Changeset) + m_user_size);
181  }
182 
183  public:
184 
186 
187  constexpr static bool is_compatible_to(osmium::item_type t) noexcept {
188  return t == itemtype;
189  }
190 
191  // Dummy to avoid warning because of unused private fields. Do not use.
192  int32_t do_not_use() const noexcept {
193  return m_padding1 + m_padding2;
194  }
195 
197  changeset_id_type id() const noexcept {
198  return m_id;
199  }
200 
208  m_id = id;
209  return *this;
210  }
211 
218  Changeset& set_id(const char* id) {
219  return set_id(osmium::string_to_changeset_id(id));
220  }
221 
223  user_id_type uid() const noexcept {
224  return m_uid;
225  }
226 
233  Changeset& set_uid(user_id_type uid) noexcept {
234  m_uid = uid;
235  return *this;
236  }
237 
246  m_uid = uid < 0 ? 0 : static_cast<user_id_type>(uid);
247  return *this;
248  }
249 
256  Changeset& set_uid(const char* uid) {
257  m_uid = string_to_uid(uid);
258  return *this;
259  }
260 
262  bool user_is_anonymous() const noexcept {
263  return m_uid == 0;
264  }
265 
267  osmium::Timestamp created_at() const noexcept {
268  return m_created_at;
269  }
270 
277  osmium::Timestamp closed_at() const noexcept {
278  return m_closed_at;
279  }
280 
282  bool open() const noexcept {
283  return m_closed_at == osmium::Timestamp();
284  }
285 
287  bool closed() const noexcept {
288  return !open();
289  }
290 
298  m_created_at = timestamp;
299  return *this;
300  }
301 
309  m_closed_at = timestamp;
310  return *this;
311  }
312 
314  num_changes_type num_changes() const noexcept {
315  return m_num_changes;
316  }
317 
319  Changeset& set_num_changes(num_changes_type num_changes) noexcept {
320  m_num_changes = num_changes;
321  return *this;
322  }
323 
325  Changeset& set_num_changes(const char* num_changes) {
326  return set_num_changes(osmium::string_to_num_changes(num_changes));
327  }
328 
330  num_comments_type num_comments() const noexcept {
331  return m_num_comments;
332  }
333 
335  Changeset& set_num_comments(num_comments_type num_comments) noexcept {
336  m_num_comments = num_comments;
337  return *this;
338  }
339 
341  Changeset& set_num_comments(const char* num_comments) {
342  return set_num_comments(osmium::string_to_num_comments(num_comments));
343  }
344 
350  osmium::Box& bounds() noexcept {
351  return m_bounds;
352  }
353 
359  const osmium::Box& bounds() const noexcept {
360  return m_bounds;
361  }
362 
364  const char* user() const {
365  return reinterpret_cast<const char*>(data() + sizeof(Changeset));
366  }
367 
369  void clear_user() noexcept {
370  std::memset(data() + sizeof(Changeset), 0, user_size());
371  }
372 
374  const TagList& tags() const {
375  return osmium::detail::subitem_of_type<const TagList>(cbegin(), cend());
376  }
377 
385  void set_attribute(const char* attr, const char* value) {
386  if (!std::strcmp(attr, "id")) {
387  set_id(value);
388  } else if (!std::strcmp(attr, "num_changes")) {
389  set_num_changes(value);
390  } else if (!std::strcmp(attr, "comments_count")) {
391  set_num_comments(value);
392  } else if (!std::strcmp(attr, "created_at")) {
393  set_created_at(osmium::Timestamp(value));
394  } else if (!std::strcmp(attr, "closed_at")) {
395  set_closed_at(osmium::Timestamp(value));
396  } else if (!std::strcmp(attr, "uid")) {
397  set_uid(value);
398  }
399  }
400 
403 
405  return iterator(subitems_position());
406  }
407 
409  return iterator(data() + padded_size());
410  }
411 
413  return const_iterator(subitems_position());
414  }
415 
417  return const_iterator(data() + padded_size());
418  }
419 
421  return cbegin();
422  }
423 
424  const_iterator end() const {
425  return cend();
426  }
427 
429  return osmium::detail::subitem_of_type<ChangesetDiscussion>(begin(), end());
430  }
431 
433  return osmium::detail::subitem_of_type<const ChangesetDiscussion>(cbegin(), cend());
434  }
435 
436  }; // class Changeset
437 
438 
442  inline bool operator==(const Changeset& lhs, const Changeset& rhs) {
443  return lhs.id() == rhs.id();
444  }
445 
446  inline bool operator!=(const Changeset& lhs, const Changeset& rhs) {
447  return !(lhs == rhs);
448  }
449 
453  inline bool operator<(const Changeset& lhs, const Changeset& rhs) {
454  return lhs.id() < rhs.id();
455  }
456 
457  inline bool operator>(const Changeset& lhs, const Changeset& rhs) {
458  return rhs < lhs;
459  }
460 
461  inline bool operator<=(const Changeset& lhs, const Changeset& rhs) {
462  return !(rhs < lhs);
463  }
464 
465  inline bool operator>=(const Changeset& lhs, const Changeset& rhs) {
466  return !(lhs < rhs);
467  }
468 
469 } // namespace osmium
470 
471 #endif // OSMIUM_OSM_CHANGESET_HPP
Definition: changeset.hpp:59
ChangesetComment & operator=(ChangesetComment &&)=delete
void set_text_size(changeset_comment_size_type size) noexcept
Definition: changeset.hpp:91
ChangesetComment(ChangesetComment &&)=delete
const unsigned char * endpos() const
Definition: changeset.hpp:72
unsigned char * endpos()
Definition: changeset.hpp:68
osmium::Timestamp m_date
Definition: changeset.hpp:63
osmium::Timestamp date() const noexcept
Definition: changeset.hpp:114
const unsigned char * next() const
Definition: changeset.hpp:83
ChangesetComment(const ChangesetComment &)=delete
osmium::user_id_type m_uid
Definition: changeset.hpp:64
string_size_type m_user_size
Definition: changeset.hpp:66
~ChangesetComment() noexcept=default
const char * user() const noexcept
Definition: changeset.hpp:122
ChangesetComment(osmium::Timestamp date, osmium::user_id_type uid) noexcept
Definition: changeset.hpp:99
const char * text() const noexcept
Definition: changeset.hpp:126
unsigned char * next()
Definition: changeset.hpp:79
osmium::user_id_type uid() const noexcept
Definition: changeset.hpp:118
void set_user_size(string_size_type size) noexcept
Definition: changeset.hpp:87
changeset_comment_size_type m_text_size
Definition: changeset.hpp:65
ChangesetComment & operator=(const ChangesetComment &)=delete
static constexpr item_type collection_type
Definition: changeset.hpp:97
Definition: changeset.hpp:132
ChangesetDiscussion() noexcept=default
An OSM Changeset, a group of changes made by a single user over a short period of time.
Definition: changeset.hpp:148
num_comments_type num_comments() const noexcept
Get the number of comments in this changeset.
Definition: changeset.hpp:330
void set_user_size(string_size_type size) noexcept
Definition: changeset.hpp:167
const_iterator end() const
Definition: changeset.hpp:424
user_id_type uid() const noexcept
Get user id.
Definition: changeset.hpp:223
const_iterator cbegin() const
Definition: changeset.hpp:412
osmium::Box m_bounds
Definition: changeset.hpp:152
void set_attribute(const char *attr, const char *value)
Definition: changeset.hpp:385
osmium::Timestamp closed_at() const noexcept
Definition: changeset.hpp:277
int32_t do_not_use() const noexcept
Definition: changeset.hpp:192
Changeset & set_num_changes(const char *num_changes)
Set the number of changes in this changeset.
Definition: changeset.hpp:325
constexpr static bool is_compatible_to(osmium::item_type t) noexcept
Definition: changeset.hpp:187
const unsigned char * subitems_position() const
Definition: changeset.hpp:179
Changeset & set_id(changeset_id_type id) noexcept
Definition: changeset.hpp:207
const char * user() const
Get user name.
Definition: changeset.hpp:364
Changeset & set_closed_at(const osmium::Timestamp &timestamp)
Definition: changeset.hpp:308
const TagList & tags() const
Get the list of tags.
Definition: changeset.hpp:374
ChangesetDiscussion & discussion()
Definition: changeset.hpp:428
const_iterator begin() const
Definition: changeset.hpp:420
Changeset()
Definition: changeset.hpp:163
unsigned char * subitems_position()
Definition: changeset.hpp:175
string_size_type user_size() const noexcept
Definition: changeset.hpp:171
Changeset & set_num_changes(num_changes_type num_changes) noexcept
Set the number of changes in this changeset.
Definition: changeset.hpp:319
bool open() const noexcept
Is this changeset open?
Definition: changeset.hpp:282
osmium::Timestamp created_at() const noexcept
Get timestamp when this changeset was created.
Definition: changeset.hpp:267
const osmium::Box & bounds() const noexcept
Definition: changeset.hpp:359
Changeset & set_uid_from_signed(signed_user_id_type uid) noexcept
Definition: changeset.hpp:245
iterator begin()
Definition: changeset.hpp:404
osmium::Timestamp m_closed_at
Definition: changeset.hpp:154
Changeset & set_uid(user_id_type uid) noexcept
Definition: changeset.hpp:233
bool user_is_anonymous() const noexcept
Is this user anonymous?
Definition: changeset.hpp:262
Changeset & set_id(const char *id)
Definition: changeset.hpp:218
osmium::Box & bounds() noexcept
Definition: changeset.hpp:350
Changeset & set_num_comments(const char *num_comments)
Set the number of comments in this changeset.
Definition: changeset.hpp:341
Changeset & set_created_at(const osmium::Timestamp &timestamp)
Definition: changeset.hpp:297
changeset_id_type id() const noexcept
Get ID of this changeset.
Definition: changeset.hpp:197
Changeset & set_num_comments(num_comments_type num_comments) noexcept
Set the number of comments in this changeset.
Definition: changeset.hpp:335
void clear_user() noexcept
Clear user name.
Definition: changeset.hpp:369
const_iterator cend() const
Definition: changeset.hpp:416
Changeset & set_uid(const char *uid)
Definition: changeset.hpp:256
osmium::Timestamp m_created_at
Definition: changeset.hpp:153
const ChangesetDiscussion & discussion() const
Definition: changeset.hpp:432
iterator end()
Definition: changeset.hpp:408
bool closed() const noexcept
Is this changeset closed?
Definition: changeset.hpp:287
num_changes_type num_changes() const noexcept
Get the number of changes in this changeset.
Definition: changeset.hpp:314
OSMEntity is the abstract base class for the OSMObject and Changeset classes.
Definition: entity.hpp:64
Definition: tag.hpp:119
Definition: timestamp.hpp:147
Definition: osm_object_builder.hpp:603
Definition: osm_object_builder.hpp:326
Definition: collection.hpp:47
unsigned char * data() const noexcept
Definition: collection.hpp:91
Definition: collection.hpp:181
static constexpr osmium::item_type itemtype
Definition: collection.hpp:192
CollectionIterator< ChangesetComment > iterator
Definition: collection.hpp:188
CollectionIterator< const ChangesetComment > const_iterator
Definition: collection.hpp:189
const_iterator cend() const noexcept
Definition: collection.hpp:232
const_iterator cbegin() const noexcept
Definition: collection.hpp:228
item_size_type padded_size() const
Definition: item.hpp:167
constexpr std::size_t padded_length(std::size_t length) noexcept
Definition: item.hpp:64
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:149
uint32_t changeset_comment_size_type
Definition: types.hpp:66
user_id_type string_to_uid(const char *input)
Definition: types_from_string.hpp:186
bool operator==(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:442
num_comments_type string_to_num_comments(const char *input)
Definition: types_from_string.hpp:214
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:461
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:457
uint16_t string_size_type
Definition: types.hpp:59
item_type
Definition: item_type.hpp:45
bool operator>=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:465
bool operator!=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:446
uint32_t num_comments_type
Type for changeset num_comments.
Definition: types.hpp:52
num_changes_type string_to_num_changes(const char *input)
Definition: types_from_string.hpp:200
uint32_t num_changes_type
Type for changeset num_changes.
Definition: types.hpp:51
bool operator<(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:453