Loading...
Searching...
No Matches
metadata_options.hpp
Go to the documentation of this file.
#ifndef OSMIUM_OSM_METADATA_OPTIONS_HPP
#define OSMIUM_OSM_METADATA_OPTIONS_HPP
/*
This file is part of Osmium (https://osmcode.org/libosmium).
Copyright 2013-2026 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <osmium/osm/object.hpp>
#include <osmium/util/string.hpp>
#include <ostream>
#include <stdexcept>
#include <string>
namespace osmium {
// NOLINTBEGIN(clang-analyzer-optin.core.EnumCastOutOfRange)
class metadata_options {
enum options : unsigned int {
md_none = 0x00,
md_version = 0x01,
md_timestamp = 0x02,
md_changeset = 0x04,
md_uid = 0x08,
md_user = 0x10,
md_all = 0x1f
} m_options = md_all;
public:
metadata_options() noexcept = default;
explicit metadata_options(const std::string& attributes) {
if (attributes.empty() || attributes == "all" || attributes == "true" || attributes == "yes") {
return;
}
if (attributes == "none" || attributes == "false" || attributes == "no") {
m_options = options::md_none;
return;
}
const auto attrs = osmium::split_string(attributes, '+', true);
unsigned int opts = 0;
for (const auto& attr : attrs) {
if (attr == "version") {
opts |= options::md_version;
} else if (attr == "timestamp") {
opts |= options::md_timestamp;
} else if (attr == "changeset") {
opts |= options::md_changeset;
} else if (attr == "uid") {
opts |= options::md_uid;
} else if (attr == "user") {
opts |= options::md_user;
} else {
throw std::invalid_argument{std::string{"Unknown OSM object metadata attribute: '"} + attr + "'"};
}
}
m_options = static_cast<options>(opts);
}
bool any() const noexcept {
return m_options != 0;
}
bool all() const noexcept {
return m_options == options::md_all;
}
bool none() const noexcept {
return m_options == 0;
}
bool version() const noexcept {
return (m_options & options::md_version) != 0;
}
void set_version(bool flag) noexcept {
if (flag) {
m_options = static_cast<options>(m_options | options::md_version);
} else {
m_options = static_cast<options>(m_options & ~options::md_version);
}
}
bool timestamp() const noexcept {
return (m_options & options::md_timestamp) != 0;
}
void set_timestamp(bool flag) noexcept {
if (flag) {
m_options = static_cast<options>(m_options | options::md_timestamp);
} else {
m_options = static_cast<options>(m_options & ~options::md_timestamp);
}
}
bool changeset() const noexcept {
return (m_options & options::md_changeset) != 0;
}
void set_changeset(bool flag) noexcept {
if (flag) {
m_options = static_cast<options>(m_options | options::md_changeset);
} else {
m_options = static_cast<options>(m_options & ~options::md_changeset);
}
}
bool uid() const noexcept {
return (m_options & options::md_uid) != 0;
}
void set_uid(bool flag) noexcept {
if (flag) {
m_options = static_cast<options>(m_options | options::md_uid);
} else {
m_options = static_cast<options>(m_options & ~options::md_uid);
}
}
bool user() const noexcept {
return (m_options & options::md_user) != 0;
}
void set_user(bool flag) noexcept {
if (flag) {
m_options = static_cast<options>(m_options | options::md_user);
} else {
m_options = static_cast<options>(m_options & ~options::md_user);
}
}
metadata_options operator&=(const metadata_options& other) {
m_options = static_cast<options>(other.m_options & m_options);
return *this;
}
metadata_options operator|=(const metadata_options& other) {
m_options = static_cast<options>(other.m_options | m_options);
return *this;
}
std::string to_string() const {
std::string result;
if (none()) {
result = "none";
return result;
}
if (all()) {
result = "all";
return result;
}
if (version()) {
result += "version+";
}
if (timestamp()) {
result += "timestamp+";
}
if (changeset()) {
result += "changeset+";
}
if (uid()) {
result += "uid+";
}
if (user()) {
result += "user+";
}
// remove last '+' character
result.pop_back();
return result;
}
}; // class metadata_options
// NOLINTEND(clang-analyzer-optin.core.EnumCastOutOfRange)
template <typename TChar, typename TTraits>
inline std::basic_ostream<TChar, TTraits>& operator<<(std::basic_ostream<TChar, TTraits>& out, const metadata_options& options) {
return out << options.to_string();
}
inline osmium::metadata_options detect_available_metadata(const osmium::OSMObject& object) {
osmium::metadata_options opts;
opts.set_version(object.version() > 0);
opts.set_timestamp(object.timestamp().valid());
opts.set_changeset(object.changeset() > 0);
// Objects by anonymous users don't have these attributes set. There is no way
// to distinguish them from objects with a reduced number of metadata fields.
opts.set_uid(object.uid() > 0);
opts.set_user(object.user()[0] != '\0');
return opts;
}
} // namespace osmium
#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