Loading...
Searching...
No Matches
tags_filter.hpp
Go to the documentation of this file.
#ifndef OSMIUM_TAGS_TAGS_FILTER_HPP
#define OSMIUM_TAGS_TAGS_FILTER_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/tag.hpp>
#include <osmium/tags/matcher.hpp>
#include <utility>
#include <vector>
namespace osmium {
template <typename TResult>
class TagsFilterBase {
std::vector<std::pair<TResult, TagMatcher>> m_rules;
TResult m_default_result;
public:
using iterator = osmium::memory::CollectionFilterIterator<TagsFilterBase, const osmium::Tag>;
explicit TagsFilterBase(const TResult default_result = TResult{}) :
m_default_result(default_result) {
}
void set_default_result(const TResult default_result) noexcept {
m_default_result = default_result;
}
TagsFilterBase& add_rule(const TResult result, const TagMatcher& matcher) {
m_rules.emplace_back(result, matcher);
return *this;
}
template <typename... TArgs>
TagsFilterBase& add_rule(const TResult result, TArgs&&... args) {
m_rules.emplace_back(result, osmium::TagMatcher{std::forward<TArgs>(args)...});
return *this;
}
TResult operator()(const osmium::Tag& tag) const noexcept {
for (const auto& rule : m_rules) {
if (rule.second(tag)) {
return rule.first;
}
}
return m_default_result;
}
std::size_t count() const noexcept {
return m_rules.size();
}
bool empty() const noexcept {
return m_rules.empty();
}
}; // class TagsFilterBase
using TagsFilter = TagsFilterBase<bool>;
inline bool compare_tags(osmium::TagList const &tags1,
osmium::TagList const &tags2,
TagsFilter const &filter)
{
auto const end1 = tags1.cend();
auto const end2 = tags2.cend();
auto it1 = tags1.cbegin();
auto it2 = tags2.cbegin();
while (it1 != end1 && it2 != end2) {
if (!filter(*it1)) {
++it1;
continue;
}
if (!filter(*it2)) {
++it2;
continue;
}
if (*it1 == *it2) {
++it1;
++it2;
continue;
}
return false;
}
while (it1 != end1 && !filter(*it1)) {
++it1;
}
while (it2 != end2 && !filter(*it2)) {
++it2;
}
return it1 == end1 && it2 == end2;
}
} // namespace osmium
#endif // OSMIUM_TAGS_TAGS_FILTER_HPP
osmium::TagList
Definition tag.hpp:119
osmium::TagMatcher
Definition matcher.hpp:48
osmium::Tag
Definition tag.hpp:48
osmium::TagsFilterBase
Definition tags_filter.hpp:66
osmium::TagsFilterBase::add_rule
TagsFilterBase & add_rule(const TResult result, TArgs &&... args)
Definition tags_filter.hpp:114
osmium::TagsFilterBase< bool >::m_default_result
bool m_default_result
Definition tags_filter.hpp:69
osmium::TagsFilterBase::iterator
osmium::memory::CollectionFilterIterator< TagsFilterBase, const osmium::Tag > iterator
Definition tags_filter.hpp:73
osmium::TagsFilterBase::count
std::size_t count() const noexcept
Definition tags_filter.hpp:140
osmium::TagsFilterBase::empty
bool empty() const noexcept
Definition tags_filter.hpp:149
osmium::TagsFilterBase::set_default_result
void set_default_result(const TResult default_result) noexcept
Definition tags_filter.hpp:89
osmium::TagsFilterBase::operator()
TResult operator()(const osmium::Tag &tag) const noexcept
Definition tags_filter.hpp:126
osmium::TagsFilterBase::add_rule
TagsFilterBase & add_rule(const TResult result, const TagMatcher &matcher)
Definition tags_filter.hpp:100
osmium::TagsFilterBase< bool >::m_rules
std::vector< std::pair< bool, TagMatcher > > m_rules
Definition tags_filter.hpp:68
osmium::TagsFilterBase::TagsFilterBase
TagsFilterBase(const TResult default_result=TResult{})
Definition tags_filter.hpp:81
osmium::memory::CollectionFilterIterator
Definition collection.hpp:118
osmium::memory::Collection::cend
const_iterator cend() const noexcept
Definition collection.hpp:233
osmium::memory::Collection::cbegin
const_iterator cbegin() const noexcept
Definition collection.hpp:229
matcher.hpp
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
osmium::compare_tags
bool compare_tags(osmium::TagList const &tags1, osmium::TagList const &tags2, TagsFilter const &filter)
Definition tags_filter.hpp:162
osmium::TagsFilter
TagsFilterBase< bool > TagsFilter
Definition tags_filter.hpp:155
tag.hpp