Libosmium  2.6.1
Fast and flexible C++ library for working with OpenStreetMap data
crc.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_OSM_CRC_HPP
2 #define OSMIUM_OSM_CRC_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 <cstdint>
37 
38 #include <osmium/osm/area.hpp>
39 #include <osmium/osm/changeset.hpp>
40 #include <osmium/osm/location.hpp>
41 #include <osmium/osm/node.hpp>
43 #include <osmium/osm/relation.hpp>
44 #include <osmium/osm/way.hpp>
45 #include <osmium/util/endian.hpp>
46 
47 namespace osmium {
48 
49  namespace util {
50 
51  inline uint16_t byte_swap_16(uint16_t value) noexcept {
52 # if defined(__GNUC__) || defined(__clang__)
53  return __builtin_bswap16(value);
54 # else
55  return (value >> 8) | (value << 8);
56 # endif
57  }
58 
59  inline uint32_t byte_swap_32(uint32_t value) noexcept {
60 # if defined(__GNUC__) || defined(__clang__)
61  return __builtin_bswap32(value);
62 # else
63  return (value >> 24) |
64  ((value >> 8) & 0x0000FF00) |
65  ((value << 8) & 0x00FF0000) |
66  (value << 24);
67 # endif
68  }
69 
70  inline uint64_t byte_swap_64(uint64_t value) noexcept {
71 # if defined(__GNUC__) || defined(__clang__)
72  return __builtin_bswap64(value);
73 # else
74  uint64_t val1 = byte_swap_32(value & 0xFFFFFFFF);
75  uint64_t val2 = byte_swap_32(value >> 32);
76  return (val1 << 32) | val2;
77 # endif
78  }
79 
80  } // namespace util
81 
82  template <typename TCRC>
83  class CRC {
84 
85  TCRC m_crc;
86 
87  public:
88 
89  TCRC& operator()() {
90  return m_crc;
91  }
92 
93  const TCRC& operator()() const {
94  return m_crc;
95  }
96 
97  void update_bool(const bool value) {
98  m_crc.process_byte(value);
99  }
100 
101  void update_int8(const uint8_t value) {
102  m_crc.process_byte(value);
103  }
104 
105  void update_int16(const uint16_t value) {
106 #if __BYTE_ORDER == __LITTLE_ENDIAN
107  m_crc.process_bytes(&value, sizeof(uint16_t));
108 #else
109  uint16_t v = osmium::util::byte_swap_16(value);
110  m_crc.process_bytes(&v, sizeof(uint16_t));
111 #endif
112  }
113 
114  void update_int32(const uint32_t value) {
115 #if __BYTE_ORDER == __LITTLE_ENDIAN
116  m_crc.process_bytes(&value, sizeof(uint32_t));
117 #else
118  uint32_t v = osmium::util::byte_swap_32(value);
119  m_crc.process_bytes(&v, sizeof(uint32_t));
120 #endif
121  }
122 
123  void update_int64(const uint64_t value) {
124 #if __BYTE_ORDER == __LITTLE_ENDIAN
125  m_crc.process_bytes(&value, sizeof(uint64_t));
126 #else
127  uint64_t v = osmium::util::byte_swap_64(value);
128  m_crc.process_bytes(&v, sizeof(uint64_t));
129 #endif
130  }
131 
132  void update_string(const char* str) {
133  while (*str) {
134  m_crc.process_byte(*str++);
135  }
136  }
137 
138  void update(const Timestamp& timestamp) {
139  update_int32(uint32_t(timestamp));
140  }
141 
142  void update(const osmium::Location& location) {
143  update_int32(location.x());
144  update_int32(location.y());
145  }
146 
147  void update(const osmium::Box& box) {
148  update(box.bottom_left());
149  update(box.top_right());
150  }
151 
152  void update(const NodeRef& node_ref) {
153  update_int64(node_ref.ref());
154  }
155 
156  void update(const NodeRefList& node_refs) {
157  for (const NodeRef& node_ref : node_refs) {
158  update(node_ref);
159  }
160  }
161 
162  void update(const TagList& tags) {
163  for (const Tag& tag : tags) {
164  update_string(tag.key());
165  update_string(tag.value());
166  }
167  }
168 
169  void update(const osmium::RelationMember& member) {
170  update_int64(member.ref());
171  update_int16(uint16_t(member.type()));
172  update_string(member.role());
173  }
174 
175  void update(const osmium::RelationMemberList& members) {
176  for (const RelationMember& member : members) {
177  update(member);
178  }
179  }
180 
181  void update(const osmium::OSMObject& object) {
182  update_int64(object.id());
183  update_bool(object.visible());
184  update_int32(object.version());
185  update(object.timestamp());
186  update_int32(object.uid());
187  update_string(object.user());
188  update(object.tags());
189  }
190 
191  void update(const osmium::Node& node) {
192  update(static_cast<const osmium::OSMObject&>(node));
193  update(node.location());
194  }
195 
196  void update(const osmium::Way& way) {
197  update(static_cast<const osmium::OSMObject&>(way));
198  update(way.nodes());
199  }
200 
201  void update(const osmium::Relation& relation) {
202  update(static_cast<const osmium::OSMObject&>(relation));
203  update(relation.members());
204  }
205 
206  void update(const osmium::Area& area) {
207  update(static_cast<const osmium::OSMObject&>(area));
208  for (auto it = area.cbegin(); it != area.cend(); ++it) {
209  if (it->type() == osmium::item_type::outer_ring ||
210  it->type() == osmium::item_type::inner_ring) {
211  update(static_cast<const osmium::NodeRefList&>(*it));
212  }
213  }
214  }
215 
216  void update(const osmium::ChangesetDiscussion& discussion) {
217  for (const auto& comment : discussion) {
218  update(comment.date());
219  update_int32(comment.uid());
220  update_string(comment.user());
221  update_string(comment.text());
222  }
223  }
224 
225  void update(const osmium::Changeset& changeset) {
226  update_int64(changeset.id());
227  update(changeset.created_at());
228  update(changeset.closed_at());
229  update(changeset.bounds());
230  update_int32(changeset.num_changes());
231  update_int32(changeset.num_comments());
232  update_int32(changeset.uid());
233  update_string(changeset.user());
234  update(changeset.tags());
235  update(changeset.discussion());
236  }
237 
238  }; // class CRC
239 
240 } // namespace osmium
241 
242 #endif // OSMIUM_OSM_CRC
WayNodeList & nodes()
Definition: way.hpp:75
void update(const osmium::Relation &relation)
Definition: crc.hpp:201
void update_int8(const uint8_t value)
Definition: crc.hpp:101
Definition: tag.hpp:48
Definition: changeset.hpp:128
void update_int16(const uint16_t value)
Definition: crc.hpp:105
void update(const osmium::Way &way)
Definition: crc.hpp:196
Definition: tag.hpp:105
osmium::Box & bounds() noexcept
Definition: changeset.hpp:340
RelationMemberList & members()
Definition: relation.hpp:177
constexpr Location top_right() const noexcept
Definition: box.hpp:178
void update(const osmium::Changeset &changeset)
Definition: crc.hpp:225
void update_int64(const uint64_t value)
Definition: crc.hpp:123
osmium::Timestamp created_at() const noexcept
Get timestamp when this changeset was created.
Definition: changeset.hpp:257
user_id_type uid() const noexcept
Get user id.
Definition: changeset.hpp:214
osmium::Location location() const noexcept
Definition: node.hpp:61
const TagList & tags() const
Get the list of tags.
Definition: changeset.hpp:359
object_id_type ref() const noexcept
Definition: relation.hpp:108
Definition: relation.hpp:165
const_iterator cend() const
Definition: object.hpp:346
void update(const Timestamp &timestamp)
Definition: crc.hpp:138
const char * user() const
Get user name.
Definition: changeset.hpp:354
Definition: area.hpp:113
num_comments_type num_comments() const noexcept
Get the number of comments in this changeset.
Definition: changeset.hpp:320
Definition: relation.hpp:148
void update(const osmium::OSMObject &object)
Definition: crc.hpp:181
void update(const TagList &tags)
Definition: crc.hpp:162
item_type type() const noexcept
Definition: relation.hpp:126
const_iterator cbegin() const
Definition: object.hpp:342
TCRC m_crc
Definition: crc.hpp:85
Definition: way.hpp:65
void update(const osmium::RelationMember &member)
Definition: crc.hpp:169
Definition: relation.hpp:54
const char * role() const noexcept
Definition: relation.hpp:134
Namespace for everything in the Osmium library.
Definition: assembler.hpp:59
changeset_id_type id() const noexcept
Get ID of this changeset.
Definition: changeset.hpp:188
void update(const osmium::Area &area)
Definition: crc.hpp:206
void update(const osmium::Box &box)
Definition: crc.hpp:147
Definition: crc.hpp:83
void update(const osmium::Location &location)
Definition: crc.hpp:142
Definition: timestamp.hpp:56
constexpr osmium::object_id_type ref() const noexcept
Definition: node_ref.hpp:65
void update(const NodeRef &node_ref)
Definition: crc.hpp:152
const TCRC & operator()() const
Definition: crc.hpp:93
uint32_t byte_swap_32(uint32_t value) noexcept
Definition: crc.hpp:59
void update_bool(const bool value)
Definition: crc.hpp:97
Definition: location.hpp:79
Definition: box.hpp:50
uint64_t byte_swap_64(uint64_t value) noexcept
Definition: crc.hpp:70
ChangesetDiscussion & discussion()
Definition: changeset.hpp:413
Definition: node.hpp:47
constexpr int32_t x() const noexcept
Definition: location.hpp:163
Definition: node_ref_list.hpp:50
An OSM Changeset, a group of changes made by a single user over a short period of time...
Definition: changeset.hpp:154
void update(const osmium::Node &node)
Definition: crc.hpp:191
void update_int32(const uint32_t value)
Definition: crc.hpp:114
void update(const osmium::ChangesetDiscussion &discussion)
Definition: crc.hpp:216
void update(const osmium::RelationMemberList &members)
Definition: crc.hpp:175
Definition: node_ref.hpp:50
void update_string(const char *str)
Definition: crc.hpp:132
constexpr Location bottom_left() const noexcept
Definition: box.hpp:164
uint16_t byte_swap_16(uint16_t value) noexcept
Definition: crc.hpp:51
constexpr int32_t y() const noexcept
Definition: location.hpp:167
osmium::Timestamp closed_at() const noexcept
Definition: changeset.hpp:267
TCRC & operator()()
Definition: crc.hpp:89
void update(const NodeRefList &node_refs)
Definition: crc.hpp:156
Definition: object.hpp:58
num_changes_type num_changes() const noexcept
Get the number of changes in this changeset.
Definition: changeset.hpp:304