Libosmium  v2.23.0
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>
38
39#include <ostream>
40#include <stdexcept>
41#include <string>
42
43namespace osmium {
44
45 // NOLINTBEGIN(clang-analyzer-optin.core.EnumCastOutOfRange)
46
54
55 enum options : unsigned int {
56 md_none = 0x00,
57 md_version = 0x01,
60 md_uid = 0x08,
61 md_user = 0x10,
62 md_all = 0x1f
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") {
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") {
85 } else if (attr == "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) {
120 } else {
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) {
132 } else {
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) {
144 } else {
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 {
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) {
168 } else {
170 }
171 }
172
174 m_options = static_cast<options>(other.m_options & m_options);
175 return *this;
176 }
177
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
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
Definition object.hpp:64
Definition metadata_options.hpp:53
metadata_options operator&=(const metadata_options &other)
Definition metadata_options.hpp:173
options
Definition metadata_options.hpp:55
@ md_all
Definition metadata_options.hpp:62
@ md_version
Definition metadata_options.hpp:57
@ md_changeset
Definition metadata_options.hpp:59
@ md_uid
Definition metadata_options.hpp:60
@ md_timestamp
Definition metadata_options.hpp:58
@ md_user
Definition metadata_options.hpp:61
@ md_none
Definition metadata_options.hpp:56
bool changeset() const noexcept
Definition metadata_options.hpp:137
bool any() const noexcept
At least one metadata attribute should be stored.
Definition metadata_options.hpp:99
std::string to_string() const
Definition metadata_options.hpp:183
bool none() const noexcept
No metadata attributes should be stored.
Definition metadata_options.hpp:109
void set_user(bool flag) noexcept
Definition metadata_options.hpp:165
void set_timestamp(bool flag) noexcept
Definition metadata_options.hpp:129
void set_version(bool flag) noexcept
Definition metadata_options.hpp:117
enum osmium::metadata_options::options m_options
bool user() const noexcept
Definition metadata_options.hpp:161
metadata_options operator|=(const metadata_options &other)
Definition metadata_options.hpp:178
void set_uid(bool flag) noexcept
Definition metadata_options.hpp:153
void set_changeset(bool flag) noexcept
Definition metadata_options.hpp:141
bool all() const noexcept
All metadata attributes should be stored.
Definition metadata_options.hpp:104
bool uid() const noexcept
Definition metadata_options.hpp:149
bool version() const noexcept
Definition metadata_options.hpp:113
bool timestamp() const noexcept
Definition metadata_options.hpp:125
metadata_options() noexcept=default
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
std::vector< std::string > split_string(const std::string &str, const char sep, bool compact=false)
Definition string.hpp:50
osmium::metadata_options detect_available_metadata(const osmium::OSMObject &object)
Definition metadata_options.hpp:235
std::basic_ostream< TChar, TTraits > & operator<<(std::basic_ostream< TChar, TTraits > &out, const item_type item_type)
Definition item_type.hpp:187
Definition location.hpp:654