Libosmium: include/osmium/osm/metadata_options.hpp Source File

Libosmium Fast and flexible C++ library for working with OpenStreetMap data

Loading...
Searching...
No Matches
metadata_options.hpp
Go to the documentation of this file.
1#ifndef OSMIUM_OSM_METADATA_OPTIONS_HPP
2#define OSMIUM_OSM_METADATA_OPTIONS_HPP
3
4/*
5
6This file is part of Osmium (https://osmcode.org/libosmium).
7
8Copyright 2013-2026 Jochen Topf <jochen@topf.org> and others (see README).
9
10Boost Software License - Version 1.0 - August 17th, 2003
11
12Permission is hereby granted, free of charge, to any person or organization
13obtaining a copy of the software and accompanying documentation covered by
14this license (the "Software") to use, reproduce, display, distribute,
15execute, and transmit the Software, and to prepare derivative works of the
16Software, and to permit third-parties to whom the Software is furnished to
17do so, all subject to the following:
18
19The copyright notices in the Software and this entire statement, including
20the above license grant, this restriction and the following disclaimer,
21must be included in all copies of the Software, in whole or in part, and
22all derivative works of the Software, unless such copies or derivative
23works are solely in the form of machine-executable object code generated by
24a source language processor.
25
26THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32DEALINGS IN THE SOFTWARE.
33
34*/
35
36#include <osmium/osm/object.hpp>
37#include <osmium/util/string.hpp>
38
39#include <ostream>
40#include <stdexcept>
41#include <string>
42
43namespace osmium {
44
45 // NOLINTBEGIN(clang-analyzer-optin.core.EnumCastOutOfRange)
46
53 class metadata_options {
54
55 enum options : unsigned int {
56 md_none = 0x00,
57 md_version = 0x01,
58 md_timestamp = 0x02,
59 md_changeset = 0x04,
60 md_uid = 0x08,
61 md_user = 0x10,
62 md_all = 0x1f
63 } m_options = md_all;
64
65 public:
66
67 metadata_options() noexcept = default;
68
69 explicit metadata_options(const std::string& attributes) {
70 if (attributes.empty() || attributes == "all" || attributes == "true" || attributes == "yes") {
71 return;
72 }
73 if (attributes == "none" || attributes == "false" || attributes == "no") {
74 m_options = options::md_none;
75 return;
76 }
77
78 const auto attrs = osmium::split_string(attributes, '+', true);
79 unsigned int opts = 0;
80 for (const auto& attr : attrs) {
81 if (attr == "version") {
82 opts |= options::md_version;
83 } else if (attr == "timestamp") {
84 opts |= options::md_timestamp;
85 } else if (attr == "changeset") {
86 opts |= options::md_changeset;
87 } else if (attr == "uid") {
88 opts |= options::md_uid;
89 } else if (attr == "user") {
90 opts |= options::md_user;
91 } else {
92 throw std::invalid_argument{std::string{"Unknown OSM object metadata attribute: '"} + attr + "'"};
93 }
94 }
95 m_options = static_cast<options>(opts);
96 }
97
99 bool any() const noexcept {
100 return m_options != 0;
101 }
102
104 bool all() const noexcept {
105 return m_options == options::md_all;
106 }
107
109 bool none() const noexcept {
110 return m_options == 0;
111 }
112
113 bool version() const noexcept {
114 return (m_options & options::md_version) != 0;
115 }
116
117 void set_version(bool flag) noexcept {
118 if (flag) {
119 m_options = static_cast<options>(m_options | options::md_version);
120 } else {
121 m_options = static_cast<options>(m_options & ~options::md_version);
122 }
123 }
124
125 bool timestamp() const noexcept {
126 return (m_options & options::md_timestamp) != 0;
127 }
128
129 void set_timestamp(bool flag) noexcept {
130 if (flag) {
131 m_options = static_cast<options>(m_options | options::md_timestamp);
132 } else {
133 m_options = static_cast<options>(m_options & ~options::md_timestamp);
134 }
135 }
136
137 bool changeset() const noexcept {
138 return (m_options & options::md_changeset) != 0;
139 }
140
141 void set_changeset(bool flag) noexcept {
142 if (flag) {
143 m_options = static_cast<options>(m_options | options::md_changeset);
144 } else {
145 m_options = static_cast<options>(m_options & ~options::md_changeset);
146 }
147 }
148
149 bool uid() const noexcept {
150 return (m_options & options::md_uid) != 0;
151 }
152
153 void set_uid(bool flag) noexcept {
154 if (flag) {
155 m_options = static_cast<options>(m_options | options::md_uid);
156 } else {
157 m_options = static_cast<options>(m_options & ~options::md_uid);
158 }
159 }
160
161 bool user() const noexcept {
162 return (m_options & options::md_user) != 0;
163 }
164
165 void set_user(bool flag) noexcept {
166 if (flag) {
167 m_options = static_cast<options>(m_options | options::md_user);
168 } else {
169 m_options = static_cast<options>(m_options & ~options::md_user);
170 }
171 }
172
173 metadata_options operator&=(const metadata_options& other) {
174 m_options = static_cast<options>(other.m_options & m_options);
175 return *this;
176 }
177
178 metadata_options operator|=(const metadata_options& other) {
179 m_options = static_cast<options>(other.m_options | m_options);
180 return *this;
181 }
182
183 std::string to_string() const {
184 std::string result;
185
186 if (none()) {
187 result = "none";
188 return result;
189 }
190
191 if (all()) {
192 result = "all";
193 return result;
194 }
195
196 if (version()) {
197 result += "version+";
198 }
199
200 if (timestamp()) {
201 result += "timestamp+";
202 }
203
204 if (changeset()) {
205 result += "changeset+";
206 }
207
208 if (uid()) {
209 result += "uid+";
210 }
211
212 if (user()) {
213 result += "user+";
214 }
215
216 // remove last '+' character
217 result.pop_back();
218
219 return result;
220 }
221
222 }; // class metadata_options
223
224 // NOLINTEND(clang-analyzer-optin.core.EnumCastOutOfRange)
225
226 template <typename TChar, typename TTraits>
227 inline std::basic_ostream<TChar, TTraits>& operator<<(std::basic_ostream<TChar, TTraits>& out, const metadata_options& options) {
228 return out << options.to_string();
229 }
230
235 inline osmium::metadata_options detect_available_metadata(const osmium::OSMObject& object) {
236 osmium::metadata_options opts;
237
238 opts.set_version(object.version() > 0);
239 opts.set_timestamp(object.timestamp().valid());
240 opts.set_changeset(object.changeset() > 0);
241
242 // Objects by anonymous users don't have these attributes set. There is no way
243 // to distinguish them from objects with a reduced number of metadata fields.
244 opts.set_uid(object.uid() > 0);
245 opts.set_user(object.user()[0] != '\0');
246
247 return opts;
248 }
249
250} // namespace osmium
251
252#endif // OSMIUM_OSM_METADATA_OPTIONS_HPP
osmium::OSMObject
Definition object.hpp:64
osmium::metadata_options
Definition metadata_options.hpp:53
osmium::metadata_options::operator&=
metadata_options operator&=(const metadata_options &other)
Definition metadata_options.hpp:173
osmium::metadata_options::options
options
Definition metadata_options.hpp:55
osmium::metadata_options::md_all
@ md_all
Definition metadata_options.hpp:62
osmium::metadata_options::md_version
@ md_version
Definition metadata_options.hpp:57
osmium::metadata_options::md_changeset
@ md_changeset
Definition metadata_options.hpp:59
osmium::metadata_options::md_uid
@ md_uid
Definition metadata_options.hpp:60
osmium::metadata_options::md_timestamp
@ md_timestamp
Definition metadata_options.hpp:58
osmium::metadata_options::md_user
@ md_user
Definition metadata_options.hpp:61
osmium::metadata_options::md_none
@ md_none
Definition metadata_options.hpp:56
osmium::metadata_options::changeset
bool changeset() const noexcept
Definition metadata_options.hpp:137
osmium::metadata_options::any
bool any() const noexcept
At least one metadata attribute should be stored.
Definition metadata_options.hpp:99
osmium::metadata_options::to_string
std::string to_string() const
Definition metadata_options.hpp:183
osmium::metadata_options::none
bool none() const noexcept
No metadata attributes should be stored.
Definition metadata_options.hpp:109
osmium::metadata_options::set_user
void set_user(bool flag) noexcept
Definition metadata_options.hpp:165
osmium::metadata_options::set_timestamp
void set_timestamp(bool flag) noexcept
Definition metadata_options.hpp:129
osmium::metadata_options::set_version
void set_version(bool flag) noexcept
Definition metadata_options.hpp:117
osmium::metadata_options::m_options
enum osmium::metadata_options::options m_options
osmium::metadata_options::user
bool user() const noexcept
Definition metadata_options.hpp:161
osmium::metadata_options::operator|=
metadata_options operator|=(const metadata_options &other)
Definition metadata_options.hpp:178
osmium::metadata_options::set_uid
void set_uid(bool flag) noexcept
Definition metadata_options.hpp:153
osmium::metadata_options::set_changeset
void set_changeset(bool flag) noexcept
Definition metadata_options.hpp:141
osmium::metadata_options::all
bool all() const noexcept
All metadata attributes should be stored.
Definition metadata_options.hpp:104
osmium::metadata_options::uid
bool uid() const noexcept
Definition metadata_options.hpp:149
osmium::metadata_options::version
bool version() const noexcept
Definition metadata_options.hpp:113
osmium::metadata_options::timestamp
bool timestamp() const noexcept
Definition metadata_options.hpp:125
osmium::metadata_options::metadata_options
metadata_options() noexcept=default
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
osmium::split_string
std::vector< std::string > split_string(const std::string &str, const char sep, bool compact=false)
Definition string.hpp:50
osmium::detect_available_metadata
osmium::metadata_options detect_available_metadata(const osmium::OSMObject &object)
Definition metadata_options.hpp:235
osmium::item_type::changeset
@ changeset
Definition item_type.hpp:52
osmium::operator<<
std::basic_ostream< TChar, TTraits > & operator<<(std::basic_ostream< TChar, TTraits > &out, const item_type item_type)
Definition item_type.hpp:187
std
Definition location.hpp:654
object.hpp
string.hpp