Libosmium  2.1.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 
6 This file is part of Osmium (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2015 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 #include <cstdlib>
39 #include <cstring>
40 #include <stdexcept>
41 
43 #include <osmium/memory/item.hpp>
45 #include <osmium/osm/entity.hpp>
46 #include <osmium/osm/item_type.hpp>
47 #include <osmium/osm/location.hpp>
48 #include <osmium/osm/tag.hpp>
49 #include <osmium/osm/timestamp.hpp>
50 #include <osmium/osm/types.hpp>
51 
52 namespace osmium {
53 
57  class OSMObject : public osmium::OSMEntity {
58 
60  bool m_deleted : 1;
65 
66  size_t sizeof_object() const noexcept {
67  return sizeof(OSMObject) + (type() == item_type::node ? sizeof(osmium::Location) : 0) + sizeof(string_size_type);
68  }
69 
70  unsigned char* user_position() noexcept {
71  return data() + sizeof_object() - sizeof(string_size_type);
72  }
73 
74  const unsigned char* user_position() const noexcept {
75  return data() + sizeof_object() - sizeof(string_size_type);
76  }
77 
78  string_size_type user_size() const noexcept {
79  return *reinterpret_cast<const string_size_type*>(user_position());
80  }
81 
82  unsigned char* subitems_position() {
84  }
85 
86  const unsigned char* subitems_position() const {
88  }
89 
90  protected:
91 
93  OSMEntity(size, type),
94  m_id(0),
95  m_deleted(false),
96  m_version(0),
97  m_timestamp(),
98  m_uid(0),
99  m_changeset(0) {
100  }
101 
103  *reinterpret_cast<string_size_type*>(user_position()) = size;
104  }
105 
106  public:
107 
109  object_id_type id() const noexcept {
110  return m_id;
111  }
112 
115  return static_cast<unsigned_object_id_type>(std::abs(m_id));
116  }
117 
124  m_id = id;
125  return *this;
126  }
127 
133  OSMObject& set_id(const char* id) {
135  }
136 
138  bool deleted() const noexcept {
139  return m_deleted;
140  }
141 
143  bool visible() const noexcept {
144  return !deleted();
145  }
146 
152  OSMObject& set_deleted(bool deleted) noexcept {
153  m_deleted = deleted;
154  return *this;
155  }
156 
162  OSMObject& set_visible(bool visible) noexcept {
163  m_deleted = !visible;
164  return *this;
165  }
166 
173  OSMObject& set_visible(const char* visible) {
174  if (!strcmp("true", visible)) {
175  set_visible(true);
176  } else if (!strcmp("false", visible)) {
177  set_visible(false);
178  } else {
179  throw std::invalid_argument("Unknown value for visible attribute (allowed is 'true' or 'false')");
180  }
181  return *this;
182  }
183 
185  object_version_type version() const noexcept {
186  return m_version;
187  }
188 
195  m_version = version;
196  return *this;
197  }
198 
204  OSMObject& set_version(const char* version) {
205  return set_version(string_to_object_version(version));
206  }
207 
209  changeset_id_type changeset() const noexcept {
210  return m_changeset;
211  }
212 
219  m_changeset = changeset;
220  return *this;
221  }
222 
229  return set_changeset(string_to_changeset_id(changeset));
230  }
231 
233  user_id_type uid() const noexcept {
234  return m_uid;
235  }
236 
243  m_uid = uid;
244  return *this;
245  }
246 
254  m_uid = uid < 0 ? 0 : static_cast<user_id_type>(uid);
255  return *this;
256  }
257 
263  OSMObject& set_uid(const char* uid) {
265  }
266 
268  bool user_is_anonymous() const noexcept {
269  return m_uid == 0;
270  }
271 
273  osmium::Timestamp timestamp() const noexcept {
274  return m_timestamp;
275  }
276 
284  m_timestamp = timestamp;
285  return *this;
286  }
287 
289  const char* user() const noexcept {
290  return reinterpret_cast<const char*>(data() + sizeof_object());
291  }
292 
294  const TagList& tags() const {
295  return osmium::detail::subitem_of_type<const TagList>(cbegin(), cend());
296  }
297 
304  const char* get_value_by_key(const char* key, const char* default_value = nullptr) const noexcept {
305  return tags().get_value_by_key(key, default_value);
306  }
307 
314  void set_attribute(const char* attr, const char* value) {
315  if (!strcmp(attr, "id")) {
316  set_id(value);
317  } else if (!strcmp(attr, "version")) {
318  set_version(value);
319  } else if (!strcmp(attr, "changeset")) {
320  set_changeset(value);
321  } else if (!strcmp(attr, "timestamp")) {
323  } else if (!strcmp(attr, "uid")) {
324  set_uid(value);
325  } else if (!strcmp(attr, "visible")) {
326  set_visible(value);
327  }
328  }
329 
332 
333  iterator begin() {
334  return iterator(subitems_position());
335  }
336 
337  iterator end() {
338  return iterator(next());
339  }
340 
341  const_iterator cbegin() const {
343  }
344 
345  const_iterator cend() const {
346  return const_iterator(next());
347  }
348 
349  const_iterator begin() const {
350  return cbegin();
351  }
352 
353  const_iterator end() const {
354  return cend();
355  }
356 
357  template <class T>
359 
360  template <class T>
362 
363  template <class T>
365  return t_iterator<T>(subitems_position(), next());
366  }
367 
368  template <class T>
370  return t_iterator<T>(next(), next());
371  }
372 
373  template <class T>
376  }
377 
378  template <class T>
380  return t_const_iterator<T>(next(), next());
381  }
382 
383  template <class T>
385  return cbegin<T>();
386  }
387 
388  template <class T>
390  return cend<T>();
391  }
392 
393  }; // class OSMObject
394 
395 
399  inline bool operator==(const OSMObject& lhs, const OSMObject& rhs) noexcept {
400  return lhs.type() == rhs.type() &&
401  lhs.id() == rhs.id() &&
402  lhs.version() == rhs.version();
403  }
404 
405  inline bool operator!=(const OSMObject& lhs, const OSMObject& rhs) noexcept {
406  return ! (lhs == rhs);
407  }
408 
414  inline bool operator<(const OSMObject& lhs, const OSMObject& rhs) noexcept {
415  if (lhs.type() != rhs.type()) {
416  return lhs.type() < rhs.type();
417  }
418  return (lhs.id() == rhs.id() && lhs.version() < rhs.version()) ||
419  lhs.positive_id() < rhs.positive_id();
420  }
421 
422  inline bool operator>(const OSMObject& lhs, const OSMObject& rhs) noexcept {
423  return rhs < lhs;
424  }
425 
426  inline bool operator<=(const OSMObject& lhs, const OSMObject& rhs) noexcept {
427  return ! (rhs < lhs);
428  }
429 
430  inline bool operator>=(const OSMObject& lhs, const OSMObject& rhs) noexcept {
431  return ! (lhs < rhs);
432  }
433 
434 } // namespace osmium
435 
436 #endif // OSMIUM_OSM_OBJECT_HPP
object_id_type m_id
Definition: object.hpp:59
Definition: collection.hpp:48
unsigned char * user_position() noexcept
Definition: object.hpp:70
object_version_type string_to_object_version(const char *string)
Definition: types.hpp:65
Definition: tag.hpp:105
uint64_t unsigned_object_id_type
Type for OSM object (node, way, or relation) IDs where we only allow positive IDs.
Definition: types.hpp:47
t_const_iterator< T > begin() const
Definition: object.hpp:384
bool operator<=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:324
bool m_deleted
Definition: object.hpp:60
OSMObject(osmium::memory::item_size_type size, osmium::item_type type)
Definition: object.hpp:92
object_id_type string_to_object_id(const char *string)
Definition: types.hpp:61
Definition: item_iterator.hpp:131
unsigned char * next() noexcept
Definition: item.hpp:140
string_size_type user_size() const noexcept
Definition: object.hpp:78
item_type
Definition: item_type.hpp:42
const_iterator cend() const
Definition: object.hpp:345
size_t sizeof_object() const noexcept
Definition: object.hpp:66
object_version_type m_version
Definition: object.hpp:61
t_const_iterator< T > cend() const
Definition: object.hpp:379
OSMObject & set_id(object_id_type id) noexcept
Definition: object.hpp:123
OSMEntity is the abstract base class for the OSMObject and Changeset classes.
Definition: entity.hpp:63
changeset_id_type string_to_changeset_id(const char *string)
Definition: types.hpp:69
OSMObject & set_changeset(const char *changeset)
Definition: object.hpp:228
const_iterator cbegin() const
Definition: object.hpp:341
T padded_length(T length) noexcept
Definition: item.hpp:57
void set_attribute(const char *attr, const char *value)
Definition: object.hpp:314
osmium::Timestamp m_timestamp
Definition: object.hpp:62
bool user_is_anonymous() const noexcept
Is this user anonymous?
Definition: object.hpp:268
iterator end()
Definition: object.hpp:337
OSMIUM_CONSTEXPR bool operator==(const Box &lhs, const Box &rhs) noexcept
Definition: box.hpp:219
OSMObject & set_visible(const char *visible)
Definition: object.hpp:173
t_const_iterator< T > cbegin() const
Definition: object.hpp:374
OSMObject & set_uid_from_signed(signed_user_id_type uid) noexcept
Definition: object.hpp:253
bool operator<(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:316
int64_t object_id_type
Type for OSM object (node, way, or relation) IDs.
Definition: types.hpp:46
Namespace for everything in the Osmium library.
Definition: assembler.hpp:55
changeset_id_type m_changeset
Definition: object.hpp:64
Definition: timestamp.hpp:52
uint16_t string_size_type
Definition: types.hpp:59
OSMObject & set_timestamp(const osmium::Timestamp timestamp) noexcept
Definition: object.hpp:283
const TagList & tags() const
Get the list of tags for this object.
Definition: object.hpp:294
user_id_type uid() const noexcept
Get user id of this object.
Definition: object.hpp:233
t_iterator< T > begin()
Definition: object.hpp:364
changeset_id_type changeset() const noexcept
Get changeset id of this object.
Definition: object.hpp:209
const_iterator begin() const
Definition: object.hpp:349
OSMObject & set_changeset(changeset_id_type changeset) noexcept
Definition: object.hpp:218
OSMObject & set_uid(const char *uid)
Definition: object.hpp:263
Definition: location.hpp:79
int32_t signed_user_id_type
Type for signed OSM user IDs.
Definition: types.hpp:51
OSMObject & set_id(const char *id)
Definition: object.hpp:133
bool deleted() const noexcept
Is this object marked as deleted?
Definition: object.hpp:138
bool operator>=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:328
object_id_type id() const noexcept
Get ID of this object.
Definition: object.hpp:109
OSMObject & set_version(object_version_type version) noexcept
Definition: object.hpp:194
object_version_type version() const noexcept
Get version of this object.
Definition: object.hpp:185
bool operator>(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:320
osmium::memory::CollectionIterator< const Item > const_iterator
Definition: object.hpp:331
OSMObject & set_visible(bool visible) noexcept
Definition: object.hpp:162
bool visible() const noexcept
Is this object marked visible (ie not deleted)?
Definition: object.hpp:143
uint32_t object_version_type
Type for OSM object version number.
Definition: types.hpp:48
unsigned char * subitems_position()
Definition: object.hpp:82
uint32_t item_size_type
Definition: item.hpp:51
t_iterator< T > end()
Definition: object.hpp:369
const_iterator end() const
Definition: object.hpp:353
user_id_type m_uid
Definition: object.hpp:63
unsigned_object_id_type positive_id() const noexcept
Get absolute value of the ID of this object.
Definition: object.hpp:114
OSMObject & set_deleted(bool deleted) noexcept
Definition: object.hpp:152
osmium::memory::CollectionIterator< Item > iterator
Definition: object.hpp:330
t_const_iterator< T > end() const
Definition: object.hpp:389
void set_user_size(string_size_type size)
Definition: object.hpp:102
uint32_t changeset_id_type
Type for OSM changeset IDs.
Definition: types.hpp:49
const unsigned char * subitems_position() const
Definition: object.hpp:86
const char * get_value_by_key(const char *key, const char *default_value=nullptr) const noexcept
Definition: object.hpp:304
const unsigned char * user_position() const noexcept
Definition: object.hpp:74
const char * user() const noexcept
Get user name for this object.
Definition: object.hpp:289
const char * get_value_by_key(const char *key, const char *default_value=nullptr) const noexcept
Definition: tag.hpp:119
signed_user_id_type string_to_user_id(const char *string)
Definition: types.hpp:73
OSMObject & set_version(const char *version)
Definition: object.hpp:204
item_type type() const noexcept
Definition: item.hpp:156
osmium::Timestamp timestamp() const noexcept
Get timestamp when this object last changed.
Definition: object.hpp:273
OSMObject & set_uid(user_id_type uid) noexcept
Definition: object.hpp:242
bool operator!=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:309
Definition: object.hpp:57
uint32_t user_id_type
Type for OSM user IDs.
Definition: types.hpp:50
iterator begin()
Definition: object.hpp:333