Libosmium  2.16.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-2021 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 <boost/iterator/filter_iterator.hpp>
40 
41 #include <cstddef>
42 #include <cstring>
43 #include <string>
44 #include <type_traits>
45 #include <vector>
46 
47 namespace 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 
127  using argument_type = const osmium::Tag&;
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
osmium::tags::match_value< std::string >::operator()
bool operator()(const std::string &rule_value, const char *tag_value) const
Definition: filter.hpp:80
osmium::tags::Filter::Rule::value
value_type value
Definition: filter.hpp:101
osmium::tags::Filter::Rule::result
bool result
Definition: filter.hpp:103
osmium::tags::match_value
Definition: filter.hpp:72
osmium::tags::Filter::Rule::ignore_value
bool ignore_value
Definition: filter.hpp:102
osmium::tags::Filter::result_type
bool result_type
Definition: filter.hpp:128
osmium::tags::Filter::m_rules
std::vector< Rule > m_rules
Definition: filter.hpp:121
osmium::tags::Filter::iterator
boost::filter_iterator< filter_type, osmium::TagList::const_iterator > iterator
Definition: filter.hpp:129
osmium::tags::Filter
Definition: filter.hpp:94
osmium::tags::Filter::Rule::Rule
Rule(bool r, bool ignore, key_type k, value_type v)
Definition: filter.hpp:105
tag.hpp
osmium::Tag
Definition: tag.hpp:48
osmium::tags::Filter::empty
bool empty() const noexcept
Definition: filter.hpp:169
osmium::tags::Filter::m_default_result
bool m_default_result
Definition: filter.hpp:122
osmium::tags::Filter::key_type
TKey key_type
Definition: filter.hpp:96
osmium::tags::match_value::operator()
bool operator()(const TValue &rule_value, const char *tag_value) const
Definition: filter.hpp:73
osmium::tags::match_key_prefix::operator()
bool operator()(const std::string &rule_key, const char *tag_key) const
Definition: filter.hpp:66
osmium::Tag::key
const char * key() const noexcept
Definition: tag.hpp:86
osmium::tags::Filter::add
Filter & add(bool result, const key_type &key, const value_type &value)
Definition: filter.hpp:136
osmium::Tag::value
const char * value() const noexcept
Definition: tag.hpp:95
osmium
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
osmium::tags::match_key< std::string >::operator()
bool operator()(const std::string &rule_key, const char *tag_key) const
Definition: filter.hpp:60
osmium::tags::Filter::Filter
Filter(bool default_result=false)
Definition: filter.hpp:131
osmium::tags::Filter::Rule::Rule
Rule(bool r, bool ignore, key_type k)
Definition: filter.hpp:112
osmium::tags::Filter::operator()
bool operator()(const osmium::Tag &tag) const
Definition: filter.hpp:146
collection.hpp
osmium::tags::Filter::Rule
Definition: filter.hpp:99
osmium::tags::match_key::operator()
bool operator()(const TKey &rule_key, const char *tag_key) const
Definition: filter.hpp:53
osmium::tags::match_value< void >::operator()
bool operator()(const bool, const char *) const noexcept
Definition: filter.hpp:87
osmium::tags::match_key_prefix
Definition: filter.hpp:65
std
Definition: location.hpp:551
osmium::tags::Filter::count
size_t count() const noexcept
Definition: filter.hpp:160
osmium::tags::Filter::value_type
typename std::conditional< std::is_void< TValue >::value, bool, TValue >::type value_type
Definition: filter.hpp:97
osmium::tags::Filter::add
Filter & add(bool result, const key_type &key)
Definition: filter.hpp:141
osmium::tags::Filter::Rule::key
key_type key
Definition: filter.hpp:100
osmium::osm_entity_bits::type
type
Definition: entity_bits.hpp:63
osmium::tags::match_key
Definition: filter.hpp:52