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