Libosmium  2.7.2
Fast and flexible C++ library for working with OpenStreetMap data
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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  update(node_ref.location());
155  }
156 
157  void update(const NodeRefList& node_refs) {
158  for (const NodeRef& node_ref : node_refs) {
159  update(node_ref);
160  }
161  }
162 
163  void update(const TagList& tags) {
164  for (const Tag& tag : tags) {
165  update_string(tag.key());
166  update_string(tag.value());
167  }
168  }
169 
170  void update(const osmium::RelationMember& member) {
171  update_int64(member.ref());
172  update_int16(uint16_t(member.type()));
173  update_string(member.role());
174  }
175 
176  void update(const osmium::RelationMemberList& members) {
177  for (const RelationMember& member : members) {
178  update(member);
179  }
180  }
181 
182  void update(const osmium::OSMObject& object) {
183  update_int64(object.id());
184  update_bool(object.visible());
185  update_int32(object.version());
186  update(object.timestamp());
187  update_int32(object.uid());
188  update_string(object.user());
189  update(object.tags());
190  }
191 
192  void update(const osmium::Node& node) {
193  update(static_cast<const osmium::OSMObject&>(node));
194  update(node.location());
195  }
196 
197  void update(const osmium::Way& way) {
198  update(static_cast<const osmium::OSMObject&>(way));
199  update(way.nodes());
200  }
201 
203  update(static_cast<const osmium::OSMObject&>(relation));
204  update(relation.members());
205  }
206 
207  void update(const osmium::Area& area) {
208  update(static_cast<const osmium::OSMObject&>(area));
209  for (auto it = area.cbegin(); it != area.cend(); ++it) {
210  if (it->type() == osmium::item_type::outer_ring ||
211  it->type() == osmium::item_type::inner_ring) {
212  update(static_cast<const osmium::NodeRefList&>(*it));
213  }
214  }
215  }
216 
217  void update(const osmium::ChangesetDiscussion& discussion) {
218  for (const auto& comment : discussion) {
219  update(comment.date());
220  update_int32(comment.uid());
221  update_string(comment.user());
222  update_string(comment.text());
223  }
224  }
225 
227  update_int64(changeset.id());
228  update(changeset.created_at());
229  update(changeset.closed_at());
230  update(changeset.bounds());
231  update_int32(changeset.num_changes());
232  update_int32(changeset.num_comments());
233  update_int32(changeset.uid());
234  update_string(changeset.user());
235  update(changeset.tags());
236  update(changeset.discussion());
237  }
238 
239  }; // class CRC
240 
241 } // namespace osmium
242 
243 #endif // OSMIUM_OSM_CRC
WayNodeList & nodes()
Definition: way.hpp:75
void update(const osmium::Relation &relation)
Definition: crc.hpp:202
void update_int8(const uint8_t value)
Definition: crc.hpp:101
Definition: tag.hpp:49
Definition: changeset.hpp:128
void update_int16(const uint16_t value)
Definition: crc.hpp:105
void update(const osmium::Way &way)
Definition: crc.hpp:197
num_comments_type num_comments() const noexcept
Get the number of comments in this changeset.
Definition: changeset.hpp:320
Definition: tag.hpp:106
osmium::Box & bounds() noexcept
Definition: changeset.hpp:340
RelationMemberList & members()
Definition: relation.hpp:177
void update(const osmium::Changeset &changeset)
Definition: crc.hpp:226
void update_int64(const uint64_t value)
Definition: crc.hpp:123
user_id_type uid() const noexcept
Get user id.
Definition: changeset.hpp:214
const TagList & tags() const
Get the list of tags.
Definition: changeset.hpp:359
constexpr Location top_right() const noexcept
Definition: box.hpp:178
Definition: relation.hpp:165
const_iterator cend() const
Definition: object.hpp:348
void update(const Timestamp &timestamp)
Definition: crc.hpp:138
const char * user() const
Get user name.
Definition: changeset.hpp:354
changeset_id_type id() const noexcept
Get ID of this changeset.
Definition: changeset.hpp:188
Definition: area.hpp:114
Definition: relation.hpp:148
void update(const osmium::OSMObject &object)
Definition: crc.hpp:182
void update(const TagList &tags)
Definition: crc.hpp:163
const_iterator cbegin() const
Definition: object.hpp:344
TCRC m_crc
Definition: crc.hpp:85
Definition: way.hpp:65
void update(const osmium::RelationMember &member)
Definition: crc.hpp:170
osmium::Timestamp closed_at() const noexcept
Definition: changeset.hpp:267
constexpr osmium::object_id_type ref() const noexcept
Definition: node_ref.hpp:65
Definition: relation.hpp:54
Namespace for everything in the Osmium library.
Definition: assembler.hpp:66
num_changes_type num_changes() const noexcept
Get the number of changes in this changeset.
Definition: changeset.hpp:304
void update(const osmium::Area &area)
Definition: crc.hpp:207
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 int32_t y() const noexcept
Definition: location.hpp:302
void update(const NodeRef &node_ref)
Definition: crc.hpp:152
const TCRC & operator()() const
Definition: crc.hpp:93
item_type type() const noexcept
Definition: relation.hpp:126
uint32_t byte_swap_32(uint32_t value) noexcept
Definition: crc.hpp:59
void update_bool(const bool value)
Definition: crc.hpp:97
osmium::Timestamp created_at() const noexcept
Get timestamp when this changeset was created.
Definition: changeset.hpp:257
Definition: location.hpp:216
osmium::Location & location() noexcept
Definition: node_ref.hpp:79
Definition: box.hpp:50
object_id_type ref() const noexcept
Definition: relation.hpp:108
osmium::Location location() const noexcept
Definition: node.hpp:61
const char * role() const noexcept
Definition: relation.hpp:134
uint64_t byte_swap_64(uint64_t value) noexcept
Definition: crc.hpp:70
constexpr Location bottom_left() const noexcept
Definition: box.hpp:164
ChangesetDiscussion & discussion()
Definition: changeset.hpp:413
Definition: node.hpp:47
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:192
void update_int32(const uint32_t value)
Definition: crc.hpp:114
constexpr int32_t x() const noexcept
Definition: location.hpp:298
void update(const osmium::ChangesetDiscussion &discussion)
Definition: crc.hpp:217
void update(const osmium::RelationMemberList &members)
Definition: crc.hpp:176
Definition: node_ref.hpp:50
void update_string(const char *str)
Definition: crc.hpp:132
uint16_t byte_swap_16(uint16_t value) noexcept
Definition: crc.hpp:51
TCRC & operator()()
Definition: crc.hpp:89
void update(const NodeRefList &node_refs)
Definition: crc.hpp:157
Definition: object.hpp:60