Libosmium  2.3.0
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-2015 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  template <class TCRC>
50  class CRC {
51 
52  static inline uint16_t byte_swap_16(uint16_t value) noexcept {
53 # if defined(__GNUC__) || defined(__clang__)
54  return __builtin_bswap16(value);
55 # else
56  return (value >> 8) | (value << 8);
57 # endif
58  }
59 
60  static inline uint32_t byte_swap_32(uint32_t value) noexcept {
61 # if defined(__GNUC__) || defined(__clang__)
62  return __builtin_bswap32(value);
63 # else
64  return (value >> 24) |
65  ((value >> 8) & 0x0000FF00) |
66  ((value << 8) & 0x00FF0000) |
67  (value << 24);
68 # endif
69  }
70 
71  static inline uint64_t byte_swap_64(uint64_t value) noexcept {
72 # if defined(__GNUC__) || defined(__clang__)
73  return __builtin_bswap64(value);
74 # else
75  uint64_t val1 = byte_swap_32(value & 0xFFFFFFFF);
76  uint64_t val2 = byte_swap_32(value >> 32);
77  return (val1 << 32) & val2;
78 # endif
79  }
80 
81  TCRC m_crc;
82 
83  public:
84 
85  TCRC& operator()() {
86  return m_crc;
87  }
88 
89  const TCRC& operator()() const {
90  return m_crc;
91  }
92 
93  void update_bool(bool value) {
94  m_crc.process_byte(value);
95  }
96 
97  void update_int8(uint8_t value) {
98  m_crc.process_byte(value);
99  }
100 
101  void update_int16(uint16_t value) {
102 #if __BYTE_ORDER == __LITTLE_ENDIAN
103  m_crc.process_bytes(&value, sizeof(uint16_t));
104 #else
105  uint16_t v = byte_swap_16(value);
106  m_crc.process_bytes(&v, sizeof(uint16_t));
107 #endif
108  }
109 
110  void update_int32(uint32_t value) {
111 #if __BYTE_ORDER == __LITTLE_ENDIAN
112  m_crc.process_bytes(&value, sizeof(uint32_t));
113 #else
114  uint32_t v = byte_swap_32(value);
115  m_crc.process_bytes(&v, sizeof(uint32_t));
116 #endif
117  }
118 
119  void update_int64(uint64_t value) {
120 #if __BYTE_ORDER == __LITTLE_ENDIAN
121  m_crc.process_bytes(&value, sizeof(uint64_t));
122 #else
123  uint64_t v = byte_swap_64(value);
124  m_crc.process_bytes(&v, sizeof(uint64_t));
125 #endif
126  }
127 
128  void update_string(const char* str) {
129  while (*str) {
130  m_crc.process_byte(*str++);
131  }
132  }
133 
134  void update(const Timestamp& timestamp) {
135  update_int32(uint32_t(timestamp));
136  }
137 
138  void update(const osmium::Location& location) {
139  update_int32(location.x());
140  update_int32(location.y());
141  }
142 
143  void update(const osmium::Box& box) {
144  update(box.bottom_left());
145  update(box.top_right());
146  }
147 
148  void update(const NodeRef& node_ref) {
149  update_int64(node_ref.ref());
150  }
151 
152  void update(const NodeRefList& node_refs) {
153  for (const NodeRef& node_ref : node_refs) {
154  update(node_ref);
155  }
156  }
157 
158  void update(const TagList& tags) {
159  m_crc.process_bytes(tags.data(), tags.byte_size());
160  }
161 
162  void update(const osmium::RelationMember& member) {
163  update_int64(member.ref());
164  update_int16(uint16_t(member.type()));
165  update_string(member.role());
166  }
167 
168  void update(const osmium::RelationMemberList& members) {
169  for (const RelationMember& member : members) {
170  update(member);
171  }
172  }
173 
174  void update(const osmium::OSMObject& object) {
175  update_int64(object.id());
176  update_bool(object.visible());
177  update_int32(object.version());
178  update(object.timestamp());
179  update_int32(object.uid());
180  update_string(object.user());
181  update(object.tags());
182  }
183 
184  void update(const osmium::Node& node) {
185  update(static_cast<const osmium::OSMObject&>(node));
186  update(node.location());
187  }
188 
189  void update(const osmium::Way& way) {
190  update(static_cast<const osmium::OSMObject&>(way));
191  update(way.nodes());
192  }
193 
195  update(static_cast<const osmium::OSMObject&>(relation));
196  update(relation.members());
197  }
198 
199  void update(const osmium::Area& area) {
200  update(static_cast<const osmium::OSMObject&>(area));
201  for (auto it = area.cbegin(); it != area.cend(); ++it) {
202  if (it->type() == osmium::item_type::outer_ring ||
203  it->type() == osmium::item_type::inner_ring) {
204  update(static_cast<const osmium::NodeRefList&>(*it));
205  }
206  }
207  }
208 
210  update_int64(changeset.id());
211  update(changeset.created_at());
212  update(changeset.closed_at());
213  update(changeset.bounds());
214  update_int32(changeset.num_changes());
215  update_int32(changeset.uid());
216  update_string(changeset.user());
217  }
218 
219  }; // class CRC
220 
221 } // namespace osmium
222 
223 #endif // OSMIUM_OSM_CRC
WayNodeList & nodes()
Definition: way.hpp:75
void update_int32(uint32_t value)
Definition: crc.hpp:110
void update(const osmium::Relation &relation)
Definition: crc.hpp:194
void update(const osmium::Way &way)
Definition: crc.hpp:189
Definition: tag.hpp:105
osmium::Box & bounds() noexcept
Definition: changeset.hpp:228
RelationMemberList & members()
Definition: relation.hpp:179
void update(const osmium::Changeset &changeset)
Definition: crc.hpp:209
static uint32_t byte_swap_32(uint32_t value) noexcept
Definition: crc.hpp:60
void update_int64(uint64_t value)
Definition: crc.hpp:119
user_id_type uid() const noexcept
Get user id.
Definition: changeset.hpp:118
static uint16_t byte_swap_16(uint16_t value) noexcept
Definition: crc.hpp:52
Definition: relation.hpp:167
const_iterator cend() const
Definition: object.hpp:346
void update(const Timestamp &timestamp)
Definition: crc.hpp:134
const char * user() const
Get user name.
Definition: changeset.hpp:242
changeset_id_type id() const noexcept
Get ID of this changeset.
Definition: changeset.hpp:92
Definition: area.hpp:113
Definition: relation.hpp:150
void update(const osmium::OSMObject &object)
Definition: crc.hpp:174
void update(const TagList &tags)
Definition: crc.hpp:158
const_iterator cbegin() const
Definition: object.hpp:342
TCRC m_crc
Definition: crc.hpp:81
Definition: way.hpp:65
osmium::object_id_type ref() const noexcept
Definition: node_ref.hpp:62
OSMIUM_CONSTEXPR Location bottom_left() const noexcept
Definition: box.hpp:164
void update(const osmium::RelationMember &member)
Definition: crc.hpp:162
osmium::Timestamp closed_at() const noexcept
Definition: changeset.hpp:171
Definition: relation.hpp:54
Namespace for everything in the Osmium library.
Definition: assembler.hpp:55
void update_int16(uint16_t value)
Definition: crc.hpp:101
OSMIUM_CONSTEXPR Location top_right() const noexcept
Definition: box.hpp:178
num_changes_type num_changes() const noexcept
Get the number of changes in this changeset.
Definition: changeset.hpp:208
void update(const osmium::Area &area)
Definition: crc.hpp:199
void update(const osmium::Box &box)
Definition: crc.hpp:143
Definition: crc.hpp:50
void update(const osmium::Location &location)
Definition: crc.hpp:138
Definition: timestamp.hpp:52
void update_bool(bool value)
Definition: crc.hpp:93
constexpr int32_t y() const noexcept
Definition: location.hpp:167
void update(const NodeRef &node_ref)
Definition: crc.hpp:148
const TCRC & operator()() const
Definition: crc.hpp:89
item_type type() const noexcept
Definition: relation.hpp:128
osmium::Timestamp created_at() const noexcept
Get timestamp when this changeset was created.
Definition: changeset.hpp:161
Definition: location.hpp:79
item_size_type byte_size() const noexcept
Definition: item.hpp:147
Definition: box.hpp:50
object_id_type ref() const noexcept
Definition: relation.hpp:110
osmium::Location location() const noexcept
Definition: node.hpp:61
const char * role() const noexcept
Definition: relation.hpp:136
static uint64_t byte_swap_64(uint64_t value) noexcept
Definition: crc.hpp:71
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:61
void update(const osmium::Node &node)
Definition: crc.hpp:184
constexpr int32_t x() const noexcept
Definition: location.hpp:163
void update_int8(uint8_t value)
Definition: crc.hpp:97
void update(const osmium::RelationMemberList &members)
Definition: crc.hpp:168
Definition: node_ref.hpp:50
void update_string(const char *str)
Definition: crc.hpp:128
TCRC & operator()()
Definition: crc.hpp:85
void update(const NodeRefList &node_refs)
Definition: crc.hpp:152
Definition: object.hpp:58