Libosmium  2.9.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-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 <cstdlib>
37 #include <cstring>
38 #include <stdexcept>
39 #include <tuple>
40 
42 #include <osmium/memory/item.hpp>
44 #include <osmium/osm/entity.hpp>
45 #include <osmium/osm/item_type.hpp>
46 #include <osmium/osm/location.hpp>
47 #include <osmium/osm/tag.hpp>
48 #include <osmium/osm/timestamp.hpp>
49 #include <osmium/osm/types.hpp>
51 #include <osmium/util/misc.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 (!std::strcmp("true", visible)) {
176  set_visible(true);
177  } else if (!std::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 
296  m_timestamp = detail::parse_timestamp(timestamp);
297  if (timestamp[20] != '\0') {
298  throw std::invalid_argument{"can not parse timestamp"};
299  }
300  return *this;
301  }
302 
304  const char* user() const noexcept {
305  return reinterpret_cast<const char*>(data() + sizeof_object());
306  }
307 
309  const TagList& tags() const {
310  return osmium::detail::subitem_of_type<const TagList>(cbegin(), cend());
311  }
312 
319  const char* get_value_by_key(const char* key, const char* default_value = nullptr) const noexcept {
320  return tags().get_value_by_key(key, default_value);
321  }
322 
330  OSMObject& set_attribute(const char* attr, const char* value) {
331  if (!std::strcmp(attr, "id")) {
332  set_id(value);
333  } else if (!std::strcmp(attr, "version")) {
334  set_version(value);
335  } else if (!std::strcmp(attr, "changeset")) {
336  set_changeset(value);
337  } else if (!std::strcmp(attr, "timestamp")) {
338  set_timestamp(value);
339  } else if (!std::strcmp(attr, "uid")) {
340  set_uid(value);
341  } else if (!std::strcmp(attr, "visible")) {
342  set_visible(value);
343  }
344 
345  return *this;
346  }
347 
350 
352  return iterator(subitems_position());
353  }
354 
356  return iterator(next());
357  }
358 
361  }
362 
364  return const_iterator(next());
365  }
366 
368  return cbegin();
369  }
370 
371  const_iterator end() const {
372  return cend();
373  }
374 
380  template <typename T>
383  }
384 
390  template <typename T>
393  }
394 
395  template <typename T>
397 
398  template <typename T>
400 
401  template <typename T>
403  return t_iterator<T>(subitems_position(), next());
404  }
405 
406  template <typename T>
408  return t_iterator<T>(next(), next());
409  }
410 
411  template <typename T>
414  }
415 
416  template <typename T>
418  return t_const_iterator<T>(next(), next());
419  }
420 
421  template <typename T>
423  return cbegin<T>();
424  }
425 
426  template <typename T>
428  return cend<T>();
429  }
430 
431  }; // class OSMObject
432 
433 
437  inline bool operator==(const OSMObject& lhs, const OSMObject& rhs) noexcept {
438  return lhs.type() == rhs.type() &&
439  lhs.id() == rhs.id() &&
440  lhs.version() == rhs.version();
441  }
442 
443  inline bool operator!=(const OSMObject& lhs, const OSMObject& rhs) noexcept {
444  return ! (lhs == rhs);
445  }
446 
461  inline bool operator<(const OSMObject& lhs, const OSMObject& rhs) noexcept {
462  return const_tie(lhs.type(), lhs.positive_id(), lhs.id() < 0, lhs.version(), lhs.timestamp()) <
463  const_tie(rhs.type(), rhs.positive_id(), rhs.id() < 0, rhs.version(), rhs.timestamp());
464  }
465 
466  inline bool operator>(const OSMObject& lhs, const OSMObject& rhs) noexcept {
467  return rhs < lhs;
468  }
469 
470  inline bool operator<=(const OSMObject& lhs, const OSMObject& rhs) noexcept {
471  return ! (rhs < lhs);
472  }
473 
474  inline bool operator>=(const OSMObject& lhs, const OSMObject& rhs) noexcept {
475  return ! (lhs < rhs);
476  }
477 
478 } // namespace osmium
479 
480 #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:60
osmium::memory::ItemIteratorRange< T > subitems()
Definition: object.hpp:381
t_const_iterator< T > end() const
Definition: object.hpp:427
Definition: collection.hpp:47
unsigned char * user_position() noexcept
Definition: object.hpp:71
const TagList & tags() const
Get the list of tags for this object.
Definition: object.hpp:309
Definition: tag.hpp:105
Definition: item_iterator.hpp:248
bool operator<=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:453
OSMObject & set_timestamp(const osmium::Timestamp &timestamp) noexcept
Definition: object.hpp:284
uint32_t item_size_type
Definition: item.hpp:59
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:132
unsigned char * next() noexcept
Definition: item.hpp:149
string_size_type user_size() const noexcept
Definition: object.hpp:79
constexpr bool operator==(const Box &lhs, const Box &rhs) noexcept
Definition: box.hpp:221
item_type
Definition: item_type.hpp:43
osmium::memory::ItemIteratorRange< const T > subitems() const
Definition: object.hpp:391
osmium::memory::CollectionIterator< Item > iterator
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:67
object_version_type m_version
Definition: object.hpp:62
std::tuple< const Ts &... > const_tie(const Ts &... args) noexcept
Definition: misc.hpp:46
std::size_t padded_length(std::size_t length) noexcept
Definition: item.hpp:64
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
osmium::memory::CollectionIterator< const Item > const_iterator
Definition: object.hpp:349
OSMObject & set_changeset(const char *changeset)
Definition: object.hpp:229
const_iterator cbegin() const
Definition: object.hpp:359
osmium::Timestamp m_timestamp
Definition: object.hpp:63
object_version_type string_to_object_version(const char *input)
Definition: types_from_string.hpp:130
bool user_is_anonymous() const noexcept
Is this user anonymous?
Definition: object.hpp:269
iterator end()
Definition: object.hpp:355
OSMObject & set_visible(const char *visible)
Definition: object.hpp:174
OSMObject & set_uid_from_signed(signed_user_id_type uid) noexcept
Definition: object.hpp:254
bool operator<(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:445
object_id_type string_to_object_id(const char *input)
Definition: types_from_string.hpp:60
t_const_iterator< T > begin() const
Definition: object.hpp:422
Namespace for everything in the Osmium library.
Definition: assembler.hpp:73
changeset_id_type m_changeset
Definition: object.hpp:65
int32_t signed_user_id_type
Type for signed OSM user IDs.
Definition: types.hpp:50
Definition: timestamp.hpp:115
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:144
const_iterator cend() const
Definition: object.hpp:363
user_id_type uid() const noexcept
Get user id of this object.
Definition: object.hpp:234
t_iterator< T > begin()
Definition: object.hpp:402
changeset_id_type changeset() const noexcept
Get changeset id of this object.
Definition: object.hpp:210
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:256
OSMObject & set_id(const char *id)
Definition: object.hpp:134
bool deleted() const noexcept
Is this object marked as deleted?
Definition: object.hpp:139
bool operator>=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:457
object_id_type id() const noexcept
Get ID of this object.
Definition: object.hpp:110
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:158
OSMObject & set_attribute(const char *attr, const char *value)
Definition: object.hpp:330
object_version_type version() const noexcept
Get version of this object.
Definition: object.hpp:186
bool operator>(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:449
t_const_iterator< T > cend() const
Definition: object.hpp:417
OSMObject & set_visible(bool visible) noexcept
Definition: object.hpp:163
bool visible() const noexcept
Is this object marked visible (ie not deleted)?
Definition: object.hpp:144
t_const_iterator< T > cbegin() const
Definition: object.hpp:412
unsigned char * subitems_position()
Definition: object.hpp:83
t_iterator< T > end()
Definition: object.hpp:407
user_id_type m_uid
Definition: object.hpp:64
unsigned_object_id_type positive_id() const noexcept
Get absolute value of the ID of this object.
Definition: object.hpp:115
OSMObject & set_deleted(bool deleted) noexcept
Definition: object.hpp:153
const unsigned char * subitems_position() const
Definition: object.hpp:87
void set_user_size(string_size_type size)
Definition: object.hpp:103
const_iterator end() const
Definition: object.hpp:371
const char * get_value_by_key(const char *key, const char *default_value=nullptr) const noexcept
Definition: object.hpp:319
const unsigned char * user_position() const noexcept
Definition: object.hpp:75
const char * user() const noexcept
Get user name for this object.
Definition: object.hpp:304
const_iterator begin() const
Definition: object.hpp:367
const char * get_value_by_key(const char *key, const char *default_value=nullptr) const noexcept
Definition: tag.hpp:134
OSMObject & set_version(const char *version)
Definition: object.hpp:205
item_type type() const noexcept
Definition: item.hpp:165
osmium::Timestamp timestamp() const noexcept
Get timestamp when this object last changed.
Definition: object.hpp:274
OSMObject & set_uid(user_id_type uid) noexcept
Definition: object.hpp:243
bool operator!=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:438
OSMObject & set_timestamp(const char *timestamp)
Definition: object.hpp:295
Definition: object.hpp:58
iterator begin()
Definition: object.hpp:351