Libosmium  2.17.0
Fast and flexible C++ library for working with OpenStreetMap data
Loading...
Searching...
No Matches
filter.hpp
Go to the documentation of this file.
1#ifndef OSMIUM_TAGS_FILTER_HPP
2#define OSMIUM_TAGS_FILTER_HPP
3
4/*
5
6This file is part of Osmium (https://osmcode.org/libosmium).
7
8Copyright 2013-2021 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
37#include <osmium/osm/tag.hpp>
38
39#include <boost/iterator/filter_iterator.hpp>
40
41#include <cstddef>
42#include <cstring>
43#include <string>
44#include <type_traits>
45#include <vector>
46
47namespace osmium {
48
49 namespace tags {
50
51 template <typename TKey>
52 struct match_key {
53 bool operator()(const TKey& rule_key, const char* tag_key) const {
54 return rule_key == tag_key;
55 }
56 }; // struct match_key
57
58 template <>
59 struct match_key<std::string> {
60 bool operator()(const std::string& rule_key, const char* tag_key) const {
61 return !std::strcmp(rule_key.c_str(), tag_key);
62 }
63 }; // struct match_key
64
66 bool operator()(const std::string& rule_key, const char* tag_key) const {
67 return rule_key.compare(0, std::string::npos, tag_key, 0, rule_key.size()) == 0;
68 }
69 }; // struct match_key_prefix
70
71 template <typename TValue>
72 struct match_value {
73 bool operator()(const TValue& rule_value, const char* tag_value) const {
74 return rule_value == tag_value;
75 }
76 }; // struct match_value
77
78 template <>
79 struct match_value<std::string> {
80 bool operator()(const std::string& rule_value, const char* tag_value) const {
81 return !std::strcmp(rule_value.c_str(), tag_value);
82 }
83 }; // struct match_value
84
85 template <>
86 struct match_value<void> {
87 bool operator()(const bool /*rule_value*/, const char* /*tag_value*/) const noexcept {
88 return true;
89 }
90 }; // struct match_value<void>
91
93 template <typename TKey, typename TValue = void, typename TKeyComp = match_key<TKey>, typename TValueComp = match_value<TValue>>
94 class Filter {
95
96 using key_type = TKey;
97 using value_type = typename std::conditional<std::is_void<TValue>::value, bool, TValue>::type;
98
99 struct Rule {
103 bool result;
104
105 explicit Rule(bool r, bool ignore, key_type k, value_type v) :
106 key(std::move(k)),
107 value(std::move(v)),
108 ignore_value(ignore),
109 result(r) {
110 }
111
112 explicit Rule(bool r, bool ignore, key_type k) :
113 key(std::move(k)),
114 value(),
115 ignore_value(ignore),
116 result(r) {
117 }
118
119 }; // struct Rule
120
121 std::vector<Rule> m_rules;
123
124 public:
125
128 using result_type = bool;
129 using iterator = boost::filter_iterator<filter_type, osmium::TagList::const_iterator>;
130
131 explicit Filter(bool default_result = false) :
132 m_default_result(default_result) {
133 }
134
135 template <typename V = TValue, typename std::enable_if<!std::is_void<V>::value, int>::type = 0>
136 Filter& add(bool result, const key_type& key, const value_type& value) {
137 m_rules.emplace_back(result, false, key, value);
138 return *this;
139 }
140
141 Filter& add(bool result, const key_type& key) {
142 m_rules.emplace_back(result, true, key);
143 return *this;
144 }
145
146 bool operator()(const osmium::Tag& tag) const {
147 for (const Rule& rule : m_rules) {
148 if (TKeyComp()(rule.key, tag.key()) && (rule.ignore_value || TValueComp()(rule.value, tag.value()))) {
149 return rule.result;
150 }
151 }
152 return m_default_result;
153 }
154
160 size_t count() const noexcept {
161 return m_rules.size();
162 }
163
169 bool empty() const noexcept {
170 return m_rules.empty();
171 }
172
173 }; // class Filter
174
177
180
183
184 } // namespace tags
185
186} // namespace osmium
187
188#endif // OSMIUM_TAGS_FILTER_HPP
Definition: tag.hpp:48
const char * key() const noexcept
Definition: tag.hpp:86
const char * value() const noexcept
Definition: tag.hpp:95
Definition: filter.hpp:94
Filter(bool default_result=false)
Definition: filter.hpp:131
typename std::conditional< std::is_void< TValue >::value, bool, TValue >::type value_type
Definition: filter.hpp:97
bool m_default_result
Definition: filter.hpp:122
bool empty() const noexcept
Definition: filter.hpp:169
std::vector< Rule > m_rules
Definition: filter.hpp:121
bool operator()(const osmium::Tag &tag) const
Definition: filter.hpp:146
bool result_type
Definition: filter.hpp:128
size_t count() const noexcept
Definition: filter.hpp:160
Filter & add(bool result, const key_type &key)
Definition: filter.hpp:141
Filter & add(bool result, const key_type &key, const value_type &value)
Definition: filter.hpp:136
TKey key_type
Definition: filter.hpp:96
boost::filter_iterator< filter_type, osmium::TagList::const_iterator > iterator
Definition: filter.hpp:129
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
Definition: location.hpp:551
Definition: filter.hpp:99
key_type key
Definition: filter.hpp:100
value_type value
Definition: filter.hpp:101
Rule(bool r, bool ignore, key_type k, value_type v)
Definition: filter.hpp:105
bool ignore_value
Definition: filter.hpp:102
bool result
Definition: filter.hpp:103
Rule(bool r, bool ignore, key_type k)
Definition: filter.hpp:112
bool operator()(const std::string &rule_key, const char *tag_key) const
Definition: filter.hpp:60
Definition: filter.hpp:65
bool operator()(const std::string &rule_key, const char *tag_key) const
Definition: filter.hpp:66
Definition: filter.hpp:52
bool operator()(const TKey &rule_key, const char *tag_key) const
Definition: filter.hpp:53
bool operator()(const std::string &rule_value, const char *tag_value) const
Definition: filter.hpp:80
bool operator()(const bool, const char *) const noexcept
Definition: filter.hpp:87
Definition: filter.hpp:72
bool operator()(const TValue &rule_value, const char *tag_value) const
Definition: filter.hpp:73