Libosmium  2.10.3
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 
55  namespace builder {
56  template <typename TDerived, typename T>
57  class OSMObjectBuilder;
58  } // namespace builder
59 
63  class OSMObject : public osmium::OSMEntity {
64 
65  template <typename TDerived, typename T>
67 
69  bool m_deleted : 1;
70  object_version_type m_version : 31;
74 
75  size_t sizeof_object() const noexcept {
76  return sizeof(OSMObject) + (type() == item_type::node ? sizeof(osmium::Location) : 0) + sizeof(string_size_type);
77  }
78 
79  unsigned char* user_position() noexcept {
80  return data() + sizeof_object() - sizeof(string_size_type);
81  }
82 
83  const unsigned char* user_position() const noexcept {
84  return data() + sizeof_object() - sizeof(string_size_type);
85  }
86 
87  string_size_type user_size() const noexcept {
88  return *reinterpret_cast<const string_size_type*>(user_position());
89  }
90 
91  unsigned char* subitems_position() {
92  return data() + osmium::memory::padded_length(sizeof_object() + user_size());
93  }
94 
95  const unsigned char* subitems_position() const {
96  return data() + osmium::memory::padded_length(sizeof_object() + user_size());
97  }
98 
99  protected:
100 
102  OSMEntity(size, type),
103  m_id(0),
104  m_deleted(false),
105  m_version(0),
106  m_timestamp(),
107  m_uid(0),
108  m_changeset(0) {
109  }
110 
112  *reinterpret_cast<string_size_type*>(user_position()) = size;
113  }
114 
115  public:
116 
118  object_id_type id() const noexcept {
119  return m_id;
120  }
121 
124  return static_cast<unsigned_object_id_type>(std::abs(m_id));
125  }
126 
133  m_id = id;
134  return *this;
135  }
136 
142  OSMObject& set_id(const char* id) {
143  return set_id(osmium::string_to_object_id(id));
144  }
145 
147  bool deleted() const noexcept {
148  return m_deleted;
149  }
150 
152  bool visible() const noexcept {
153  return !deleted();
154  }
155 
161  OSMObject& set_deleted(bool deleted) noexcept {
162  m_deleted = deleted;
163  return *this;
164  }
165 
171  OSMObject& set_visible(bool visible) noexcept {
172  m_deleted = !visible;
173  return *this;
174  }
175 
182  OSMObject& set_visible(const char* visible) {
183  if (!std::strcmp("true", visible)) {
184  set_visible(true);
185  } else if (!std::strcmp("false", visible)) {
186  set_visible(false);
187  } else {
188  throw std::invalid_argument("Unknown value for visible attribute (allowed is 'true' or 'false')");
189  }
190  return *this;
191  }
192 
194  object_version_type version() const noexcept {
195  return m_version;
196  }
197 
204  m_version = version;
205  return *this;
206  }
207 
213  OSMObject& set_version(const char* version) {
214  return set_version(string_to_object_version(version));
215  }
216 
218  changeset_id_type changeset() const noexcept {
219  return m_changeset;
220  }
221 
228  m_changeset = changeset;
229  return *this;
230  }
231 
237  OSMObject& set_changeset(const char* changeset) {
238  return set_changeset(string_to_changeset_id(changeset));
239  }
240 
242  user_id_type uid() const noexcept {
243  return m_uid;
244  }
245 
251  OSMObject& set_uid(user_id_type uid) noexcept {
252  m_uid = uid;
253  return *this;
254  }
255 
263  m_uid = uid < 0 ? 0 : static_cast<user_id_type>(uid);
264  return *this;
265  }
266 
272  OSMObject& set_uid(const char* uid) {
273  return set_uid_from_signed(string_to_user_id(uid));
274  }
275 
277  bool user_is_anonymous() const noexcept {
278  return m_uid == 0;
279  }
280 
282  osmium::Timestamp timestamp() const noexcept {
283  return m_timestamp;
284  }
285 
292  OSMObject& set_timestamp(const osmium::Timestamp& timestamp) noexcept {
293  m_timestamp = timestamp;
294  return *this;
295  }
296 
303  OSMObject& set_timestamp(const char* timestamp) {
304  m_timestamp = detail::parse_timestamp(timestamp);
305  if (timestamp[20] != '\0') {
306  throw std::invalid_argument{"can not parse timestamp"};
307  }
308  return *this;
309  }
310 
312  const char* user() const noexcept {
313  return reinterpret_cast<const char*>(data() + sizeof_object());
314  }
315 
317  const TagList& tags() const {
318  return osmium::detail::subitem_of_type<const TagList>(cbegin(), cend());
319  }
320 
327  const char* get_value_by_key(const char* key, const char* default_value = nullptr) const noexcept {
328  return tags().get_value_by_key(key, default_value);
329  }
330 
338  OSMObject& set_attribute(const char* attr, const char* value) {
339  if (!std::strcmp(attr, "id")) {
340  set_id(value);
341  } else if (!std::strcmp(attr, "version")) {
342  set_version(value);
343  } else if (!std::strcmp(attr, "changeset")) {
344  set_changeset(value);
345  } else if (!std::strcmp(attr, "timestamp")) {
346  set_timestamp(value);
347  } else if (!std::strcmp(attr, "uid")) {
348  set_uid(value);
349  } else if (!std::strcmp(attr, "visible")) {
350  set_visible(value);
351  }
352 
353  return *this;
354  }
355 
358 
360  return iterator(subitems_position());
361  }
362 
364  return iterator(next());
365  }
366 
368  return const_iterator(subitems_position());
369  }
370 
372  return const_iterator(next());
373  }
374 
376  return cbegin();
377  }
378 
379  const_iterator end() const {
380  return cend();
381  }
382 
388  template <typename T>
390  return osmium::memory::ItemIteratorRange<T>{subitems_position(), next()};
391  }
392 
398  template <typename T>
400  return osmium::memory::ItemIteratorRange<const T>{subitems_position(), next()};
401  }
402 
403  template <typename T>
405 
406  template <typename T>
408 
409  template <typename T>
411  return t_iterator<T>(subitems_position(), next());
412  }
413 
414  template <typename T>
416  return t_iterator<T>(next(), next());
417  }
418 
419  template <typename T>
421  return t_const_iterator<T>(subitems_position(), next());
422  }
423 
424  template <typename T>
426  return t_const_iterator<T>(next(), next());
427  }
428 
429  template <typename T>
431  return cbegin<T>();
432  }
433 
434  template <typename T>
436  return cend<T>();
437  }
438 
439  }; // class OSMObject
440 
441 
445  inline bool operator==(const OSMObject& lhs, const OSMObject& rhs) noexcept {
446  return lhs.type() == rhs.type() &&
447  lhs.id() == rhs.id() &&
448  lhs.version() == rhs.version();
449  }
450 
451  inline bool operator!=(const OSMObject& lhs, const OSMObject& rhs) noexcept {
452  return ! (lhs == rhs);
453  }
454 
469  inline bool operator<(const OSMObject& lhs, const OSMObject& rhs) noexcept {
470  return const_tie(lhs.type(), lhs.positive_id(), lhs.id() < 0, lhs.version(), lhs.timestamp()) <
471  const_tie(rhs.type(), rhs.positive_id(), rhs.id() < 0, rhs.version(), rhs.timestamp());
472  }
473 
474  inline bool operator>(const OSMObject& lhs, const OSMObject& rhs) noexcept {
475  return rhs < lhs;
476  }
477 
478  inline bool operator<=(const OSMObject& lhs, const OSMObject& rhs) noexcept {
479  return ! (rhs < lhs);
480  }
481 
482  inline bool operator>=(const OSMObject& lhs, const OSMObject& rhs) noexcept {
483  return ! (lhs < rhs);
484  }
485 
486 } // namespace osmium
487 
488 #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
Definition: osm_object_builder.hpp:376
object_id_type m_id
Definition: object.hpp:68
osmium::memory::ItemIteratorRange< T > subitems()
Definition: object.hpp:389
t_const_iterator< T > end() const
Definition: object.hpp:435
Definition: collection.hpp:47
unsigned char * user_position() noexcept
Definition: object.hpp:79
const TagList & tags() const
Get the list of tags for this object.
Definition: object.hpp:317
Definition: tag.hpp:107
type
Definition: entity_bits.hpp:63
Definition: item_iterator.hpp:248
bool operator<=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:449
OSMObject & set_timestamp(const osmium::Timestamp &timestamp) noexcept
Definition: object.hpp:292
uint32_t item_size_type
Definition: item.hpp:59
OSMObject(osmium::memory::item_size_type size, osmium::item_type type)
Definition: object.hpp:101
Definition: item_iterator.hpp:132
string_size_type user_size() const noexcept
Definition: object.hpp:87
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:399
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:75
std::tuple< const Ts &... > const_tie(const Ts &... args) noexcept
Definition: misc.hpp:46
OSMObject & set_id(object_id_type id) noexcept
Definition: object.hpp:132
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:237
constexpr std::size_t padded_length(std::size_t length) noexcept
Definition: item.hpp:64
const_iterator cbegin() const
Definition: object.hpp:367
osmium::Timestamp m_timestamp
Definition: object.hpp:71
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:277
iterator end()
Definition: object.hpp:363
OSMObject & set_visible(const char *visible)
Definition: object.hpp:182
OSMObject & set_uid_from_signed(signed_user_id_type uid) noexcept
Definition: object.hpp:262
bool operator<(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:441
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:430
Namespace for everything in the Osmium library.
Definition: assembler.hpp:73
changeset_id_type m_changeset
Definition: object.hpp:73
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:371
user_id_type uid() const noexcept
Get user id of this object.
Definition: object.hpp:242
t_iterator< T > begin()
Definition: object.hpp:410
changeset_id_type changeset() const noexcept
Get changeset id of this object.
Definition: object.hpp:218
OSMObject & set_changeset(changeset_id_type changeset) noexcept
Definition: object.hpp:227
OSMObject & set_uid(const char *uid)
Definition: object.hpp:272
Definition: location.hpp:266
OSMObject & set_id(const char *id)
Definition: object.hpp:142
bool deleted() const noexcept
Is this object marked as deleted?
Definition: object.hpp:147
bool operator>=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:453
object_id_type id() const noexcept
Get ID of this object.
Definition: object.hpp:118
OSMObject & set_version(object_version_type version) noexcept
Definition: object.hpp:203
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:338
object_version_type version() const noexcept
Get version of this object.
Definition: object.hpp:194
bool operator>(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:445
t_const_iterator< T > cend() const
Definition: object.hpp:425
OSMObject & set_visible(bool visible) noexcept
Definition: object.hpp:171
uint32_t size() const noexcept
Definition: builder.hpp:141
bool visible() const noexcept
Is this object marked visible (ie not deleted)?
Definition: object.hpp:152
t_const_iterator< T > cbegin() const
Definition: object.hpp:420
unsigned char * subitems_position()
Definition: object.hpp:91
t_iterator< T > end()
Definition: object.hpp:415
user_id_type m_uid
Definition: object.hpp:72
unsigned_object_id_type positive_id() const noexcept
Get absolute value of the ID of this object.
Definition: object.hpp:123
OSMObject & set_deleted(bool deleted) noexcept
Definition: object.hpp:161
const unsigned char * subitems_position() const
Definition: object.hpp:95
void set_user_size(string_size_type size)
Definition: object.hpp:111
const_iterator end() const
Definition: object.hpp:379
const char * get_value_by_key(const char *key, const char *default_value=nullptr) const noexcept
Definition: object.hpp:327
const unsigned char * user_position() const noexcept
Definition: object.hpp:83
const char * user() const noexcept
Get user name for this object.
Definition: object.hpp:312
const_iterator begin() const
Definition: object.hpp:375
OSMObject & set_version(const char *version)
Definition: object.hpp:213
osmium::Timestamp timestamp() const noexcept
Get timestamp when this object last changed.
Definition: object.hpp:282
OSMObject & set_uid(user_id_type uid) noexcept
Definition: object.hpp:251
bool operator!=(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:434
OSMObject & set_timestamp(const char *timestamp)
Definition: object.hpp:303
Definition: object.hpp:63
iterator begin()
Definition: object.hpp:359