Libosmium  2.13.0
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 (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2017 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 <cstdint>
37 #include <cstring>
38 #include <iterator>
39 
41 #include <osmium/memory/item.hpp>
42 #include <osmium/osm/box.hpp>
43 #include <osmium/osm/entity.hpp>
44 #include <osmium/osm/item_type.hpp>
45 #include <osmium/osm/tag.hpp>
46 #include <osmium/osm/timestamp.hpp>
47 #include <osmium/osm/types.hpp>
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  ChangesetComment(const ChangesetComment&) = delete;
70 
73 
74  unsigned char* endpos() {
75  return data() + osmium::memory::padded_length(sizeof(ChangesetComment) + m_user_size + m_text_size);
76  }
77 
78  const unsigned char* endpos() const {
79  return data() + osmium::memory::padded_length(sizeof(ChangesetComment) + m_user_size + m_text_size);
80  }
81 
82  template <typename TMember>
84 
85  unsigned char* next() {
86  return endpos();
87  }
88 
89  unsigned const char* next() const {
90  return endpos();
91  }
92 
93  void set_user_size(string_size_type size) noexcept {
94  m_user_size = size;
95  }
96 
98  m_text_size = size;
99  }
100 
101  public:
102 
103  static constexpr item_type collection_type = item_type::changeset_discussion;
104 
106  m_date(date),
107  m_uid(uid),
108  m_text_size(0),
109  m_user_size(0) {
110  }
111 
112  osmium::Timestamp date() const noexcept {
113  return m_date;
114  }
115 
116  osmium::user_id_type uid() const noexcept {
117  return m_uid;
118  }
119 
120  const char* user() const noexcept {
121  return reinterpret_cast<const char*>(data() + sizeof(ChangesetComment));
122  }
123 
124  const char* text() const noexcept {
125  return reinterpret_cast<const char*>(data() + sizeof(ChangesetComment) + m_user_size);
126  }
127 
128  }; // class ChangesetComment
129 
130  class ChangesetDiscussion : public osmium::memory::Collection<ChangesetComment, osmium::item_type::changeset_discussion> {
131 
132  public:
133 
135  osmium::memory::Collection<ChangesetComment, osmium::item_type::changeset_discussion>() {
136  }
137 
138  }; // class ChangesetDiscussion
139 
140 
148  class Changeset : public osmium::OSMEntity {
149 
151 
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 
185  static constexpr osmium::item_type itemtype = osmium::item_type::changeset;
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  return set_uid_from_signed(string_to_user_id(uid));
258  }
259 
261  bool user_is_anonymous() const noexcept {
262  return m_uid == 0;
263  }
264 
266  osmium::Timestamp created_at() const noexcept {
267  return m_created_at;
268  }
269 
276  osmium::Timestamp closed_at() const noexcept {
277  return m_closed_at;
278  }
279 
281  bool open() const noexcept {
282  return m_closed_at == osmium::Timestamp();
283  }
284 
286  bool closed() const noexcept {
287  return !open();
288  }
289 
297  m_created_at = timestamp;
298  return *this;
299  }
300 
308  m_closed_at = timestamp;
309  return *this;
310  }
311 
313  num_changes_type num_changes() const noexcept {
314  return m_num_changes;
315  }
316 
318  Changeset& set_num_changes(num_changes_type num_changes) noexcept {
319  m_num_changes = num_changes;
320  return *this;
321  }
322 
324  Changeset& set_num_changes(const char* num_changes) {
325  return set_num_changes(osmium::string_to_num_changes(num_changes));
326  }
327 
329  num_comments_type num_comments() const noexcept {
330  return m_num_comments;
331  }
332 
334  Changeset& set_num_comments(num_comments_type num_comments) noexcept {
335  m_num_comments = num_comments;
336  return *this;
337  }
338 
340  Changeset& set_num_comments(const char* num_comments) {
341  return set_num_comments(osmium::string_to_num_comments(num_comments));
342  }
343 
349  osmium::Box& bounds() noexcept {
350  return m_bounds;
351  }
352 
358  const osmium::Box& bounds() const noexcept {
359  return m_bounds;
360  }
361 
363  const char* user() const {
364  return reinterpret_cast<const char*>(data() + sizeof(Changeset));
365  }
366 
368  const TagList& tags() const {
369  return osmium::detail::subitem_of_type<const TagList>(cbegin(), cend());
370  }
371 
379  void set_attribute(const char* attr, const char* value) {
380  if (!std::strcmp(attr, "id")) {
381  set_id(value);
382  } else if (!std::strcmp(attr, "num_changes")) {
383  set_num_changes(value);
384  } else if (!std::strcmp(attr, "comments_count")) {
385  set_num_comments(value);
386  } else if (!std::strcmp(attr, "created_at")) {
387  set_created_at(osmium::Timestamp(value));
388  } else if (!std::strcmp(attr, "closed_at")) {
389  set_closed_at(osmium::Timestamp(value));
390  } else if (!std::strcmp(attr, "uid")) {
391  set_uid(value);
392  }
393  }
394 
397 
399  return iterator(subitems_position());
400  }
401 
403  return iterator(data() + padded_size());
404  }
405 
407  return const_iterator(subitems_position());
408  }
409 
411  return const_iterator(data() + padded_size());
412  }
413 
415  return cbegin();
416  }
417 
418  const_iterator end() const {
419  return cend();
420  }
421 
423  return osmium::detail::subitem_of_type<ChangesetDiscussion>(begin(), end());
424  }
425 
427  return osmium::detail::subitem_of_type<const ChangesetDiscussion>(cbegin(), cend());
428  }
429 
430  }; // class Changeset
431 
432 
436  inline bool operator==(const Changeset& lhs, const Changeset& rhs) {
437  return lhs.id() == rhs.id();
438  }
439 
440  inline bool operator!=(const Changeset& lhs, const Changeset& rhs) {
441  return ! (lhs == rhs);
442  }
443 
447  inline bool operator<(const Changeset& lhs, const Changeset& rhs) {
448  return lhs.id() < rhs.id();
449  }
450 
451  inline bool operator>(const Changeset& lhs, const Changeset& rhs) {
452  return rhs < lhs;
453  }
454 
455  inline bool operator<=(const Changeset& lhs, const Changeset& rhs) {
456  return ! (rhs < lhs);
457  }
458 
459  inline bool operator>=(const Changeset& lhs, const Changeset& rhs) {
460  return ! (lhs < rhs);
461  }
462 
463 } // namespace osmium
464 
465 #endif // OSMIUM_OSM_CHANGESET_HPP
uint32_t user_id_type
Type for OSM user IDs.
Definition: types.hpp:49
osmium::Timestamp m_closed_at
Definition: changeset.hpp:154
const_iterator begin() const
Definition: changeset.hpp:414
Definition: changeset.hpp:130
string_size_type user_size() const noexcept
Definition: changeset.hpp:171
const char * text() const noexcept
Definition: changeset.hpp:124
Definition: collection.hpp:47
num_comments_type num_comments() const noexcept
Get the number of comments in this changeset.
Definition: changeset.hpp:329
Definition: tag.hpp:107
bool operator<=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:455
osmium::Box & bounds() noexcept
Definition: changeset.hpp:349
bool open() const noexcept
Is this changeset open?
Definition: changeset.hpp:281
constexpr bool operator==(const Box &lhs, const Box &rhs) noexcept
Definition: box.hpp:221
void set_attribute(const char *attr, const char *value)
Definition: changeset.hpp:379
user_id_type uid() const noexcept
Get user id.
Definition: changeset.hpp:223
string_size_type m_user_size
Definition: changeset.hpp:66
item_type
Definition: item_type.hpp:43
Changeset & set_uid_from_signed(signed_user_id_type uid) noexcept
Definition: changeset.hpp:245
uint16_t string_size_type
Definition: types.hpp:59
const TagList & tags() const
Get the list of tags.
Definition: changeset.hpp:368
changeset_id_type id() const noexcept
Get ID of this changeset.
Definition: changeset.hpp:197
uint32_t changeset_comment_size_type
Definition: types.hpp:66
OSMEntity is the abstract base class for the OSMObject and Changeset classes.
Definition: entity.hpp:64
uint32_t num_changes_type
Type for changeset num_changes.
Definition: types.hpp:51
Changeset & set_num_changes(const char *num_changes)
Set the number of changes in this changeset.
Definition: changeset.hpp:324
constexpr std::size_t padded_length(std::size_t length) noexcept
Definition: item.hpp:64
Definition: changeset.hpp:59
Definition: osm_object_builder.hpp:309
const_iterator end() const
Definition: changeset.hpp:418
iterator begin()
Definition: changeset.hpp:398
Changeset & set_id(changeset_id_type id) noexcept
Definition: changeset.hpp:207
osmium::Timestamp closed_at() const noexcept
Definition: changeset.hpp:276
bool operator<(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:447
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
Changeset & set_uid(const char *uid)
Definition: changeset.hpp:256
void set_text_size(changeset_comment_size_type size) noexcept
Definition: changeset.hpp:97
void set_user_size(string_size_type size) noexcept
Definition: changeset.hpp:167
Changeset & set_num_changes(num_changes_type num_changes) noexcept
Set the number of changes in this changeset.
Definition: changeset.hpp:318
const_iterator cbegin() const
Definition: changeset.hpp:406
uint32_t num_comments_type
Type for changeset num_comments.
Definition: types.hpp:52
num_changes_type num_changes() const noexcept
Get the number of changes in this changeset.
Definition: changeset.hpp:313
Definition: collection.hpp:117
int32_t signed_user_id_type
Type for signed OSM user IDs.
Definition: types.hpp:50
Definition: timestamp.hpp:115
unsigned char * next()
Definition: changeset.hpp:85
osmium::io::InputIterator< osmium::io::Reader > end(osmium::io::Reader &)
Definition: reader_iterator.hpp:45
osmium::Timestamp m_created_at
Definition: changeset.hpp:153
uint32_t changeset_id_type
Type for OSM changeset IDs.
Definition: types.hpp:48
changeset_id_type string_to_changeset_id(const char *input)
Definition: types_from_string.hpp:146
const_iterator cend() const
Definition: changeset.hpp:410
bool user_is_anonymous() const noexcept
Is this user anonymous?
Definition: changeset.hpp:261
Changeset & set_num_comments(num_comments_type num_comments) noexcept
Set the number of comments in this changeset.
Definition: changeset.hpp:334
osmium::Box m_bounds
Definition: changeset.hpp:152
osmium::Timestamp created_at() const noexcept
Get timestamp when this changeset was created.
Definition: changeset.hpp:266
Changeset()
Definition: changeset.hpp:163
osmium::user_id_type uid() const noexcept
Definition: changeset.hpp:116
unsigned char * data() const noexcept
Definition: collection.hpp:91
Definition: box.hpp:49
unsigned char * endpos()
Definition: changeset.hpp:74
iterator end()
Definition: changeset.hpp:402
bool operator>=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:459
signed_user_id_type string_to_user_id(const char *input)
Definition: types_from_string.hpp:160
ChangesetComment(osmium::Timestamp date, osmium::user_id_type uid) noexcept
Definition: changeset.hpp:105
Changeset & set_num_comments(const char *num_comments)
Set the number of comments in this changeset.
Definition: changeset.hpp:340
bool operator>(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:451
void set_user_size(string_size_type size) noexcept
Definition: changeset.hpp:93
osmium::Timestamp date() const noexcept
Definition: changeset.hpp:112
const ChangesetDiscussion & discussion() const
Definition: changeset.hpp:426
const unsigned char * endpos() const
Definition: changeset.hpp:78
unsigned char * subitems_position()
Definition: changeset.hpp:175
ChangesetDiscussion & discussion()
Definition: changeset.hpp:422
int32_t do_not_use() const noexcept
Definition: changeset.hpp:192
uint32_t size() const noexcept
Definition: builder.hpp:139
const char * user() const
Get user name.
Definition: changeset.hpp:363
Changeset & set_created_at(const osmium::Timestamp &timestamp)
Definition: changeset.hpp:296
An OSM Changeset, a group of changes made by a single user over a short period of time...
Definition: changeset.hpp:148
const osmium::Box & bounds() const noexcept
Definition: changeset.hpp:358
Changeset & set_closed_at(const osmium::Timestamp &timestamp)
Definition: changeset.hpp:307
Definition: osm_object_builder.hpp:569
bool closed() const noexcept
Is this changeset closed?
Definition: changeset.hpp:286
const unsigned char * subitems_position() const
Definition: changeset.hpp:179
osmium::io::InputIterator< osmium::io::Reader > begin(osmium::io::Reader &reader)
Definition: reader_iterator.hpp:41
unsigned const char * next() const
Definition: changeset.hpp:89
ChangesetDiscussion()
Definition: changeset.hpp:134
static constexpr bool is_compatible_to(osmium::item_type t) noexcept
Definition: changeset.hpp:187
num_comments_type string_to_num_comments(const char *input)
Definition: types_from_string.hpp:191
changeset_comment_size_type m_text_size
Definition: changeset.hpp:65
osmium::Timestamp m_date
Definition: changeset.hpp:63
bool operator!=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:440
Changeset & set_id(const char *id)
Definition: changeset.hpp:218
num_changes_type string_to_num_changes(const char *input)
Definition: types_from_string.hpp:177
Builder & operator=(const Builder &)=delete
Changeset & set_uid(user_id_type uid) noexcept
Definition: changeset.hpp:233
const char * user() const noexcept
Definition: changeset.hpp:120