Libosmium  2.6.1
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 
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>
52 
53 namespace osmium {
54 
58  class OSMObject : public osmium::OSMEntity {
59 
61  bool m_deleted : 1;
66 
67  size_t sizeof_object() const noexcept {
68  return sizeof(OSMObject) + (type() == item_type::node ? sizeof(osmium::Location) : 0) + sizeof(string_size_type);
69  }
70 
71  unsigned char* user_position() noexcept {
72  return data() + sizeof_object() - sizeof(string_size_type);
73  }
74 
75  const unsigned char* user_position() const noexcept {
76  return data() + sizeof_object() - sizeof(string_size_type);
77  }
78 
79  string_size_type user_size() const noexcept {
80  return *reinterpret_cast<const string_size_type*>(user_position());
81  }
82 
83  unsigned char* subitems_position() {
85  }
86 
87  const unsigned char* subitems_position() const {
89  }
90 
91  protected:
92 
94  OSMEntity(size, type),
95  m_id(0),
96  m_deleted(false),
97  m_version(0),
98  m_timestamp(),
99  m_uid(0),
100  m_changeset(0) {
101  }
102 
104  *reinterpret_cast<string_size_type*>(user_position()) = size;
105  }
106 
107  public:
108 
110  object_id_type id() const noexcept {
111  return m_id;
112  }
113 
116  return static_cast<unsigned_object_id_type>(std::abs(m_id));
117  }
118 
125  m_id = id;
126  return *this;
127  }
128 
134  OSMObject& set_id(const char* id) {
136  }
137 
139  bool deleted() const noexcept {
140  return m_deleted;
141  }
142 
144  bool visible() const noexcept {
145  return !deleted();
146  }
147 
153  OSMObject& set_deleted(bool deleted) noexcept {
154  m_deleted = deleted;
155  return *this;
156  }
157 
163  OSMObject& set_visible(bool visible) noexcept {
164  m_deleted = !visible;
165  return *this;
166  }
167 
174  OSMObject& set_visible(const char* visible) {
175  if (!strcmp("true", visible)) {
176  set_visible(true);
177  } else if (!strcmp("false", visible)) {
178  set_visible(false);
179  } else {
180  throw std::invalid_argument("Unknown value for visible attribute (allowed is 'true' or 'false')");
181  }
182  return *this;
183  }
184 
186  object_version_type version() const noexcept {
187  return m_version;
188  }
189 
196  m_version = version;
197  return *this;
198  }
199 
205  OSMObject& set_version(const char* version) {
206  return set_version(string_to_object_version(version));
207  }
208 
210  changeset_id_type changeset() const noexcept {
211  return m_changeset;
212  }
213 
220  m_changeset = changeset;
221  return *this;
222  }
223 
230  return set_changeset(string_to_changeset_id(changeset));
231  }
232 
234  user_id_type uid() const noexcept {
235  return m_uid;
236  }
237 
244  m_uid = uid;
245  return *this;
246  }
247 
255  m_uid = uid < 0 ? 0 : static_cast<user_id_type>(uid);
256  return *this;
257  }
258 
264  OSMObject& set_uid(const char* uid) {
266  }
267 
269  bool user_is_anonymous() const noexcept {
270  return m_uid == 0;
271  }
272 
274  osmium::Timestamp timestamp() const noexcept {
275  return m_timestamp;
276  }
277 
285  m_timestamp = timestamp;
286  return *this;
287  }
288 
290  const char* user() const noexcept {
291  return reinterpret_cast<const char*>(data() + sizeof_object());
292  }
293 
295  const TagList& tags() const {
296  return osmium::detail::subitem_of_type<const TagList>(cbegin(), cend());
297  }
298 
305  const char* get_value_by_key(const char* key, const char* default_value = nullptr) const noexcept {
306  return tags().get_value_by_key(key, default_value);
307  }
308 
315  void set_attribute(const char* attr, const char* value) {
316  if (!strcmp(attr, "id")) {
317  set_id(value);
318  } else if (!strcmp(attr, "version")) {
319  set_version(value);
320  } else if (!strcmp(attr, "changeset")) {
321  set_changeset(value);
322  } else if (!strcmp(attr, "timestamp")) {
324  } else if (!strcmp(attr, "uid")) {
325  set_uid(value);
326  } else if (!strcmp(attr, "visible")) {
327  set_visible(value);
328  }
329  }
330 
333 
334  iterator begin() {
335  return iterator(subitems_position());
336  }
337 
338  iterator end() {
339  return iterator(next());
340  }
341 
342  const_iterator cbegin() const {
344  }
345 
346  const_iterator cend() const {
347  return const_iterator(next());
348  }
349 
350  const_iterator begin() const {
351  return cbegin();
352  }
353 
354  const_iterator end() const {
355  return cend();
356  }
357 
358  template <typename T>
360 
361  template <typename T>
363 
364  template <typename T>
366  return t_iterator<T>(subitems_position(), next());
367  }
368 
369  template <typename T>
371  return t_iterator<T>(next(), next());
372  }
373 
374  template <typename T>
377  }
378 
379  template <typename T>
381  return t_const_iterator<T>(next(), next());
382  }
383 
384  template <typename T>
386  return cbegin<T>();
387  }
388 
389  template <typename T>
391  return cend<T>();
392  }
393 
394  }; // class OSMObject
395 
396 
400  inline bool operator==(const OSMObject& lhs, const OSMObject& rhs) noexcept {
401  return lhs.type() == rhs.type() &&
402  lhs.id() == rhs.id() &&
403  lhs.version() == rhs.version();
404  }
405 
406  inline bool operator!=(const OSMObject& lhs, const OSMObject& rhs) noexcept {
407  return ! (lhs == rhs);
408  }
409 
415  inline bool operator<(const OSMObject& lhs, const OSMObject& rhs) noexcept {
416  if (lhs.type() != rhs.type()) {
417  return lhs.type() < rhs.type();
418  }
419  return (lhs.id() == rhs.id() && lhs.version() < rhs.version()) ||
420  lhs.positive_id() < rhs.positive_id();
421  }
422 
423  inline bool operator>(const OSMObject& lhs, const OSMObject& rhs) noexcept {
424  return rhs < lhs;
425  }
426 
427  inline bool operator<=(const OSMObject& lhs, const OSMObject& rhs) noexcept {
428  return ! (rhs < lhs);
429  }
430 
431  inline bool operator>=(const OSMObject& lhs, const OSMObject& rhs) noexcept {
432  return ! (lhs < rhs);
433  }
434 
435 } // namespace osmium
436 
437 #endif // OSMIUM_OSM_OBJECT_HPP
const char * get_value_by_key(const char *key, const char *default_value=nullptr) const noexcept
Definition: tag.hpp:119
object_id_type m_id
Definition: object.hpp:60
changeset_id_type changeset() const noexcept
Get changeset id of this object.
Definition: object.hpp:210
Definition: collection.hpp:47
unsigned char * user_position() noexcept
Definition: object.hpp:71
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:46
t_const_iterator< T > begin() const
Definition: object.hpp:385
bool operator<=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:446
OSMObject & set_timestamp(const osmium::Timestamp &timestamp) noexcept
Definition: object.hpp:284
bool m_deleted
Definition: object.hpp:61
OSMObject(osmium::memory::item_size_type size, osmium::item_type type)
Definition: object.hpp:93
Definition: item_iterator.hpp:119
unsigned char * next() noexcept
Definition: item.hpp:139
constexpr bool operator==(const Box &lhs, const Box &rhs) noexcept
Definition: box.hpp:222
item_type
Definition: item_type.hpp:43
const_iterator cend() const
Definition: object.hpp:346
object_version_type m_version
Definition: object.hpp:62
t_const_iterator< T > cend() const
Definition: object.hpp:380
OSMObject & set_id(object_id_type id) noexcept
Definition: object.hpp:124
OSMEntity is the abstract base class for the OSMObject and Changeset classes.
Definition: entity.hpp:64
OSMObject & set_changeset(const char *changeset)
Definition: object.hpp:229
const_iterator cbegin() const
Definition: object.hpp:342
T padded_length(T length) noexcept
Definition: item.hpp:56
void set_attribute(const char *attr, const char *value)
Definition: object.hpp:315
osmium::Timestamp m_timestamp
Definition: object.hpp:63
object_version_type string_to_object_version(const char *input)
Definition: types_from_string.hpp:123
iterator end()
Definition: object.hpp:338
object_version_type version() const noexcept
Get version of this object.
Definition: object.hpp:186
const char * get_value_by_key(const char *key, const char *default_value=nullptr) const noexcept
Definition: object.hpp:305
OSMObject & set_visible(const char *visible)
Definition: object.hpp:174
t_const_iterator< T > cbegin() const
Definition: object.hpp:375
OSMObject & set_uid_from_signed(signed_user_id_type uid) noexcept
Definition: object.hpp:254
bool visible() const noexcept
Is this object marked visible (ie not deleted)?
Definition: object.hpp:144
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
int64_t object_id_type
Type for OSM object (node, way, or relation) IDs.
Definition: types.hpp:45
Namespace for everything in the Osmium library.
Definition: assembler.hpp:59
changeset_id_type m_changeset
Definition: object.hpp:65
unsigned_object_id_type positive_id() const noexcept
Get absolute value of the ID of this object.
Definition: object.hpp:115
osmium::Timestamp timestamp() const noexcept
Get timestamp when this object last changed.
Definition: object.hpp:274
Definition: timestamp.hpp:56
user_id_type uid() const noexcept
Get user id of this object.
Definition: object.hpp:234
uint16_t string_size_type
Definition: types.hpp:59
changeset_id_type string_to_changeset_id(const char *input)
Definition: types_from_string.hpp:137
const TagList & tags() const
Get the list of tags for this object.
Definition: object.hpp:295
t_iterator< T > begin()
Definition: object.hpp:365
const char * user() const noexcept
Get user name for this object.
Definition: object.hpp:290
const_iterator begin() const
Definition: object.hpp:350
OSMObject & set_changeset(changeset_id_type changeset) noexcept
Definition: object.hpp:219
OSMObject & set_uid(const char *uid)
Definition: object.hpp:264
Definition: location.hpp:79
int32_t signed_user_id_type
Type for signed OSM user IDs.
Definition: types.hpp:50
OSMObject & set_id(const char *id)
Definition: object.hpp:134
bool operator>=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:450
const unsigned char * user_position() const noexcept
Definition: object.hpp:75
OSMObject & set_version(object_version_type version) noexcept
Definition: object.hpp:195
signed_user_id_type string_to_user_id(const char *input)
Definition: types_from_string.hpp:151
bool operator>(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:442
osmium::memory::CollectionIterator< const Item > const_iterator
Definition: object.hpp:332
bool deleted() const noexcept
Is this object marked as deleted?
Definition: object.hpp:139
OSMObject & set_visible(bool visible) noexcept
Definition: object.hpp:163
object_id_type id() const noexcept
Get ID of this object.
Definition: object.hpp:110
uint32_t object_version_type
Type for OSM object version number.
Definition: types.hpp:47
bool user_is_anonymous() const noexcept
Is this user anonymous?
Definition: object.hpp:269
unsigned char * subitems_position()
Definition: object.hpp:83
uint32_t item_size_type
Definition: item.hpp:50
t_iterator< T > end()
Definition: object.hpp:370
const_iterator end() const
Definition: object.hpp:354
user_id_type m_uid
Definition: object.hpp:64
OSMObject & set_deleted(bool deleted) noexcept
Definition: object.hpp:153
osmium::memory::CollectionIterator< Item > iterator
Definition: object.hpp:331
t_const_iterator< T > end() const
Definition: object.hpp:390
string_size_type user_size() const noexcept
Definition: object.hpp:79
void set_user_size(string_size_type size)
Definition: object.hpp:103
uint32_t changeset_id_type
Type for OSM changeset IDs.
Definition: types.hpp:48
const unsigned char * subitems_position() const
Definition: object.hpp:87
item_type type() const noexcept
Definition: item.hpp:155
size_t sizeof_object() const noexcept
Definition: object.hpp:67
OSMObject & set_version(const char *version)
Definition: object.hpp:205
OSMObject & set_uid(user_id_type uid) noexcept
Definition: object.hpp:243
bool operator!=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:431
Definition: object.hpp:58
uint32_t user_id_type
Type for OSM user IDs.
Definition: types.hpp:49
iterator begin()
Definition: object.hpp:334