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