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