Libosmium  2.20.0
Fast and flexible C++ library for working with OpenStreetMap data
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-2023 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
52
53 enum options : unsigned int {
54 md_none = 0x00,
55 md_version = 0x01,
58 md_uid = 0x08,
59 md_user = 0x10,
60 md_all = 0x1f
62
63 public:
64
65 metadata_options() noexcept = default;
66
67 explicit metadata_options(const std::string& attributes) {
68 if (attributes.empty() || attributes == "all" || attributes == "true" || attributes == "yes") {
69 return;
70 }
71 if (attributes == "none" || attributes == "false" || attributes == "no") {
72 m_options = options::md_none;
73 return;
74 }
75
76 const auto attrs = osmium::split_string(attributes, '+', true);
77 unsigned int opts = 0;
78 for (const auto& attr : attrs) {
79 if (attr == "version") {
80 opts |= options::md_version;
81 } else if (attr == "timestamp") {
82 opts |= options::md_timestamp;
83 } else if (attr == "changeset") {
84 opts |= options::md_changeset;
85 } else if (attr == "uid") {
86 opts |= options::md_uid;
87 } else if (attr == "user") {
88 opts |= options::md_user;
89 } else {
90 throw std::invalid_argument{std::string{"Unknown OSM object metadata attribute: '"} + attr + "'"};
91 }
92 }
93 m_options = static_cast<options>(opts);
94 }
95
97 bool any() const noexcept {
98 return m_options != 0;
99 }
100
102 bool all() const noexcept {
103 return m_options == options::md_all;
104 }
105
107 bool none() const noexcept {
108 return m_options == 0;
109 }
110
111 bool version() const noexcept {
112 return (m_options & options::md_version) != 0;
113 }
114
115 void set_version(bool flag) noexcept {
116 if (flag) {
117 m_options = static_cast<options>(m_options | options::md_version);
118 } else {
120 }
121 }
122
123 bool timestamp() const noexcept {
124 return (m_options & options::md_timestamp) != 0;
125 }
126
127 void set_timestamp(bool flag) noexcept {
128 if (flag) {
129 m_options = static_cast<options>(m_options | options::md_timestamp);
130 } else {
132 }
133 }
134
135 bool changeset() const noexcept {
136 return (m_options & options::md_changeset) != 0;
137 }
138
139 void set_changeset(bool flag) noexcept {
140 if (flag) {
141 m_options = static_cast<options>(m_options | options::md_changeset);
142 } else {
144 }
145 }
146
147 bool uid() const noexcept {
148 return (m_options & options::md_uid) != 0;
149 }
150
151 void set_uid(bool flag) noexcept {
152 if (flag) {
153 m_options = static_cast<options>(m_options | options::md_uid);
154 } else {
156 }
157 }
158
159 bool user() const noexcept {
160 return (m_options & options::md_user) != 0;
161 }
162
163 void set_user(bool flag) noexcept {
164 if (flag) {
165 m_options = static_cast<options>(m_options | options::md_user);
166 } else {
168 }
169 }
170
172 m_options = static_cast<options>(other.m_options & m_options);
173 return *this;
174 }
175
177 m_options = static_cast<options>(other.m_options | m_options);
178 return *this;
179 }
180
181 std::string to_string() const {
182 std::string result;
183
184 if (none()) {
185 result = "none";
186 return result;
187 }
188
189 if (all()) {
190 result = "all";
191 return result;
192 }
193
194 if (version()) {
195 result += "version+";
196 }
197
198 if (timestamp()) {
199 result += "timestamp+";
200 }
201
202 if (changeset()) {
203 result += "changeset+";
204 }
205
206 if (uid()) {
207 result += "uid+";
208 }
209
210 if (user()) {
211 result += "user+";
212 }
213
214 // remove last '+' character
215 result.pop_back();
216
217 return result;
218 }
219
220 }; // class metadata_options
221
222 template <typename TChar, typename TTraits>
223 inline std::basic_ostream<TChar, TTraits>& operator<<(std::basic_ostream<TChar, TTraits>& out, const metadata_options& options) {
224 return out << options.to_string();
225 }
226
233
234 opts.set_version(object.version() > 0);
235 opts.set_timestamp(object.timestamp().valid());
236 opts.set_changeset(object.changeset() > 0);
237
238 // Objects by anonymous users don't have these attributes set. There is no way
239 // to distinguish them from objects with a reduced number of metadata fields.
240 opts.set_uid(object.uid() > 0);
241 opts.set_user(object.user()[0] != '\0');
242
243 return opts;
244 }
245
246} // namespace osmium
247
248#endif // OSMIUM_OSM_METADATA_OPTIONS_HPP
Definition: object.hpp:64
Definition: metadata_options.hpp:51
metadata_options operator&=(const metadata_options &other)
Definition: metadata_options.hpp:171
options
Definition: metadata_options.hpp:53
@ md_all
Definition: metadata_options.hpp:60
@ md_version
Definition: metadata_options.hpp:55
@ md_changeset
Definition: metadata_options.hpp:57
@ md_uid
Definition: metadata_options.hpp:58
@ md_timestamp
Definition: metadata_options.hpp:56
@ md_user
Definition: metadata_options.hpp:59
@ md_none
Definition: metadata_options.hpp:54
bool changeset() const noexcept
Definition: metadata_options.hpp:135
bool any() const noexcept
At least one metadata attribute should be stored.
Definition: metadata_options.hpp:97
std::string to_string() const
Definition: metadata_options.hpp:181
bool none() const noexcept
No metadata attributes should be stored.
Definition: metadata_options.hpp:107
void set_user(bool flag) noexcept
Definition: metadata_options.hpp:163
void set_timestamp(bool flag) noexcept
Definition: metadata_options.hpp:127
void set_version(bool flag) noexcept
Definition: metadata_options.hpp:115
enum osmium::metadata_options::options m_options
bool user() const noexcept
Definition: metadata_options.hpp:159
metadata_options operator|=(const metadata_options &other)
Definition: metadata_options.hpp:176
void set_uid(bool flag) noexcept
Definition: metadata_options.hpp:151
void set_changeset(bool flag) noexcept
Definition: metadata_options.hpp:139
bool all() const noexcept
All metadata attributes should be stored.
Definition: metadata_options.hpp:102
bool uid() const noexcept
Definition: metadata_options.hpp:147
bool version() const noexcept
Definition: metadata_options.hpp:111
bool timestamp() const noexcept
Definition: metadata_options.hpp:123
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:231
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:555