Libosmium  2.7.2
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-2016 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 #include <tuple>
42 
44 #include <osmium/memory/item.hpp>
46 #include <osmium/osm/entity.hpp>
47 #include <osmium/osm/item_type.hpp>
48 #include <osmium/osm/location.hpp>
49 #include <osmium/osm/tag.hpp>
50 #include <osmium/osm/timestamp.hpp>
51 #include <osmium/osm/types.hpp>
53 #include <osmium/util/misc.hpp>
54 
55 namespace osmium {
56 
60  class OSMObject : public osmium::OSMEntity {
61 
63  bool m_deleted : 1;
68 
69  size_t sizeof_object() const noexcept {
70  return sizeof(OSMObject) + (type() == item_type::node ? sizeof(osmium::Location) : 0) + sizeof(string_size_type);
71  }
72 
73  unsigned char* user_position() noexcept {
74  return data() + sizeof_object() - sizeof(string_size_type);
75  }
76 
77  const unsigned char* user_position() const noexcept {
78  return data() + sizeof_object() - sizeof(string_size_type);
79  }
80 
81  string_size_type user_size() const noexcept {
82  return *reinterpret_cast<const string_size_type*>(user_position());
83  }
84 
85  unsigned char* subitems_position() {
87  }
88 
89  const unsigned char* subitems_position() const {
91  }
92 
93  protected:
94 
96  OSMEntity(size, type),
97  m_id(0),
98  m_deleted(false),
99  m_version(0),
100  m_timestamp(),
101  m_uid(0),
102  m_changeset(0) {
103  }
104 
106  *reinterpret_cast<string_size_type*>(user_position()) = size;
107  }
108 
109  public:
110 
112  object_id_type id() const noexcept {
113  return m_id;
114  }
115 
118  return static_cast<unsigned_object_id_type>(std::abs(m_id));
119  }
120 
127  m_id = id;
128  return *this;
129  }
130 
136  OSMObject& set_id(const char* id) {
138  }
139 
141  bool deleted() const noexcept {
142  return m_deleted;
143  }
144 
146  bool visible() const noexcept {
147  return !deleted();
148  }
149 
155  OSMObject& set_deleted(bool deleted) noexcept {
156  m_deleted = deleted;
157  return *this;
158  }
159 
165  OSMObject& set_visible(bool visible) noexcept {
166  m_deleted = !visible;
167  return *this;
168  }
169 
176  OSMObject& set_visible(const char* visible) {
177  if (!std::strcmp("true", visible)) {
178  set_visible(true);
179  } else if (!std::strcmp("false", visible)) {
180  set_visible(false);
181  } else {
182  throw std::invalid_argument("Unknown value for visible attribute (allowed is 'true' or 'false')");
183  }
184  return *this;
185  }
186 
188  object_version_type version() const noexcept {
189  return m_version;
190  }
191 
198  m_version = version;
199  return *this;
200  }
201 
207  OSMObject& set_version(const char* version) {
208  return set_version(string_to_object_version(version));
209  }
210 
212  changeset_id_type changeset() const noexcept {
213  return m_changeset;
214  }
215 
222  m_changeset = changeset;
223  return *this;
224  }
225 
232  return set_changeset(string_to_changeset_id(changeset));
233  }
234 
236  user_id_type uid() const noexcept {
237  return m_uid;
238  }
239 
246  m_uid = uid;
247  return *this;
248  }
249 
257  m_uid = uid < 0 ? 0 : static_cast<user_id_type>(uid);
258  return *this;
259  }
260 
266  OSMObject& set_uid(const char* uid) {
268  }
269 
271  bool user_is_anonymous() const noexcept {
272  return m_uid == 0;
273  }
274 
276  osmium::Timestamp timestamp() const noexcept {
277  return m_timestamp;
278  }
279 
287  m_timestamp = timestamp;
288  return *this;
289  }
290 
292  const char* user() const noexcept {
293  return reinterpret_cast<const char*>(data() + sizeof_object());
294  }
295 
297  const TagList& tags() const {
298  return osmium::detail::subitem_of_type<const TagList>(cbegin(), cend());
299  }
300 
307  const char* get_value_by_key(const char* key, const char* default_value = nullptr) const noexcept {
308  return tags().get_value_by_key(key, default_value);
309  }
310 
317  void set_attribute(const char* attr, const char* value) {
318  if (!std::strcmp(attr, "id")) {
319  set_id(value);
320  } else if (!std::strcmp(attr, "version")) {
321  set_version(value);
322  } else if (!std::strcmp(attr, "changeset")) {
323  set_changeset(value);
324  } else if (!std::strcmp(attr, "timestamp")) {
326  } else if (!std::strcmp(attr, "uid")) {
327  set_uid(value);
328  } else if (!std::strcmp(attr, "visible")) {
329  set_visible(value);
330  }
331  }
332 
335 
337  return iterator(subitems_position());
338  }
339 
341  return iterator(next());
342  }
343 
346  }
347 
349  return const_iterator(next());
350  }
351 
353  return cbegin();
354  }
355 
356  const_iterator end() const {
357  return cend();
358  }
359 
365  template <typename T>
368  }
369 
375  template <typename T>
378  }
379 
380  template <typename T>
382 
383  template <typename T>
385 
386  template <typename T>
388  return t_iterator<T>(subitems_position(), next());
389  }
390 
391  template <typename T>
393  return t_iterator<T>(next(), next());
394  }
395 
396  template <typename T>
399  }
400 
401  template <typename T>
403  return t_const_iterator<T>(next(), next());
404  }
405 
406  template <typename T>
408  return cbegin<T>();
409  }
410 
411  template <typename T>
413  return cend<T>();
414  }
415 
416  }; // class OSMObject
417 
418 
422  inline bool operator==(const OSMObject& lhs, const OSMObject& rhs) noexcept {
423  return lhs.type() == rhs.type() &&
424  lhs.id() == rhs.id() &&
425  lhs.version() == rhs.version();
426  }
427 
428  inline bool operator!=(const OSMObject& lhs, const OSMObject& rhs) noexcept {
429  return ! (lhs == rhs);
430  }
431 
446  inline bool operator<(const OSMObject& lhs, const OSMObject& rhs) noexcept {
447  return const_tie(lhs.type(), lhs.positive_id(), lhs.id() < 0, lhs.version(), lhs.timestamp()) <
448  const_tie(rhs.type(), rhs.positive_id(), rhs.id() < 0, rhs.version(), rhs.timestamp());
449  }
450 
451  inline bool operator>(const OSMObject& lhs, const OSMObject& rhs) noexcept {
452  return rhs < lhs;
453  }
454 
455  inline bool operator<=(const OSMObject& lhs, const OSMObject& rhs) noexcept {
456  return ! (rhs < lhs);
457  }
458 
459  inline bool operator>=(const OSMObject& lhs, const OSMObject& rhs) noexcept {
460  return ! (lhs < rhs);
461  }
462 
463 } // namespace osmium
464 
465 #endif // OSMIUM_OSM_OBJECT_HPP
uint32_t object_version_type
Type for OSM object version number.
Definition: types.hpp:47
uint32_t user_id_type
Type for OSM user IDs.
Definition: types.hpp:49
object_id_type m_id
Definition: object.hpp:62
osmium::memory::ItemIteratorRange< T > subitems()
Definition: object.hpp:366
Definition: collection.hpp:47
unsigned char * user_position() noexcept
Definition: object.hpp:73
Definition: tag.hpp:106
Definition: item_iterator.hpp:235
std::tuple< const Ts &... > const_tie(const Ts &...args) noexcept
Definition: misc.hpp:46
t_const_iterator< T > begin() const
Definition: object.hpp:407
bool operator<=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:446
OSMObject & set_timestamp(const osmium::Timestamp &timestamp) noexcept
Definition: object.hpp:286
uint32_t item_size_type
Definition: item.hpp:50
bool m_deleted
Definition: object.hpp:63
OSMObject(osmium::memory::item_size_type size, osmium::item_type type)
Definition: object.hpp:95
Definition: item_iterator.hpp:119
unsigned char * next() noexcept
Definition: item.hpp:139
string_size_type user_size() const noexcept
Definition: object.hpp:81
constexpr bool operator==(const Box &lhs, const Box &rhs) noexcept
Definition: box.hpp:222
item_type
Definition: item_type.hpp:43
osmium::memory::CollectionIterator< Item > iterator
Definition: object.hpp:333
const_iterator cend() const
Definition: object.hpp:348
uint16_t string_size_type
Definition: types.hpp:59
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
size_t sizeof_object() const noexcept
Definition: object.hpp:69
object_version_type m_version
Definition: object.hpp:64
t_const_iterator< T > cend() const
Definition: object.hpp:402
OSMObject & set_id(object_id_type id) noexcept
Definition: object.hpp:126
OSMEntity is the abstract base class for the OSMObject and Changeset classes.
Definition: entity.hpp:64
osmium::memory::CollectionIterator< const Item > const_iterator
Definition: object.hpp:334
OSMObject & set_changeset(const char *changeset)
Definition: object.hpp:231
const_iterator cbegin() const
Definition: object.hpp:344
T padded_length(T length) noexcept
Definition: item.hpp:56
void set_attribute(const char *attr, const char *value)
Definition: object.hpp:317
osmium::Timestamp m_timestamp
Definition: object.hpp:65
object_version_type string_to_object_version(const char *input)
Definition: types_from_string.hpp:129
bool user_is_anonymous() const noexcept
Is this user anonymous?
Definition: object.hpp:271
iterator end()
Definition: object.hpp:340
OSMObject & set_visible(const char *visible)
Definition: object.hpp:176
t_const_iterator< T > cbegin() const
Definition: object.hpp:397
OSMObject & set_uid_from_signed(signed_user_id_type uid) noexcept
Definition: object.hpp:256
bool operator<(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:438
object_id_type string_to_object_id(const char *input)
Definition: types_from_string.hpp:59
Namespace for everything in the Osmium library.
Definition: assembler.hpp:66
changeset_id_type m_changeset
Definition: object.hpp:67
int32_t signed_user_id_type
Type for signed OSM user IDs.
Definition: types.hpp:50
Definition: timestamp.hpp:56
uint32_t changeset_id_type
Type for OSM changeset IDs.
Definition: types.hpp:48
int64_t object_id_type
Type for OSM object (node, way, or relation) IDs.
Definition: types.hpp:45
changeset_id_type string_to_changeset_id(const char *input)
Definition: types_from_string.hpp:143
const TagList & tags() const
Get the list of tags for this object.
Definition: object.hpp:297
user_id_type uid() const noexcept
Get user id of this object.
Definition: object.hpp:236
t_iterator< T > begin()
Definition: object.hpp:387
changeset_id_type changeset() const noexcept
Get changeset id of this object.
Definition: object.hpp:212
const_iterator begin() const
Definition: object.hpp:352
OSMObject & set_changeset(changeset_id_type changeset) noexcept
Definition: object.hpp:221
OSMObject & set_uid(const char *uid)
Definition: object.hpp:266
Definition: location.hpp:216
OSMObject & set_id(const char *id)
Definition: object.hpp:136
bool deleted() const noexcept
Is this object marked as deleted?
Definition: object.hpp:141
bool operator>=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:450
object_id_type id() const noexcept
Get ID of this object.
Definition: object.hpp:112
OSMObject & set_version(object_version_type version) noexcept
Definition: object.hpp:197
signed_user_id_type string_to_user_id(const char *input)
Definition: types_from_string.hpp:157
object_version_type version() const noexcept
Get version of this object.
Definition: object.hpp:188
bool operator>(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:442
OSMObject & set_visible(bool visible) noexcept
Definition: object.hpp:165
bool visible() const noexcept
Is this object marked visible (ie not deleted)?
Definition: object.hpp:146
unsigned char * subitems_position()
Definition: object.hpp:85
t_iterator< T > end()
Definition: object.hpp:392
const_iterator end() const
Definition: object.hpp:356
user_id_type m_uid
Definition: object.hpp:66
unsigned_object_id_type positive_id() const noexcept
Get absolute value of the ID of this object.
Definition: object.hpp:117
OSMObject & set_deleted(bool deleted) noexcept
Definition: object.hpp:155
t_const_iterator< T > end() const
Definition: object.hpp:412
void set_user_size(string_size_type size)
Definition: object.hpp:105
const unsigned char * subitems_position() const
Definition: object.hpp:89
const char * get_value_by_key(const char *key, const char *default_value=nullptr) const noexcept
Definition: object.hpp:307
const unsigned char * user_position() const noexcept
Definition: object.hpp:77
const char * user() const noexcept
Get user name for this object.
Definition: object.hpp:292
const char * get_value_by_key(const char *key, const char *default_value=nullptr) const noexcept
Definition: tag.hpp:135
osmium::memory::ItemIteratorRange< const T > subitems() const
Definition: object.hpp:376
OSMObject & set_version(const char *version)
Definition: object.hpp:207
item_type type() const noexcept
Definition: item.hpp:155
osmium::Timestamp timestamp() const noexcept
Get timestamp when this object last changed.
Definition: object.hpp:276
OSMObject & set_uid(user_id_type uid) noexcept
Definition: object.hpp:245
bool operator!=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:431
Definition: object.hpp:60
iterator begin()
Definition: object.hpp:336