Libosmium  2.11.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 (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2017 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 
36 #include <cstddef>
37 #include <string>
38 #include <type_traits>
39 #include <vector>
40 
41 #include <boost/iterator/filter_iterator.hpp>
42 
44 #include <osmium/osm/tag.hpp>
45 
46 namespace osmium {
47 
48  namespace tags {
49 
50  template <typename TKey>
51  struct match_key {
52  bool operator()(const TKey& rule_key, const char* tag_key) {
53  return rule_key == tag_key;
54  }
55  }; // struct match_key
56 
58  bool operator()(const std::string& rule_key, const char* tag_key) {
59  return rule_key.compare(0, std::string::npos, tag_key, 0, rule_key.size()) == 0;
60  }
61  }; // struct match_key_prefix
62 
63  template <typename TValue>
64  struct match_value {
65  bool operator()(const TValue& rule_value, const char* tag_value) {
66  return rule_value == tag_value;
67  }
68  }; // struct match_value
69 
70  template <>
71  struct match_value<void> {
72  bool operator()(const bool, const char*) {
73  return true;
74  }
75  }; // struct match_value<void>
76 
77  template <typename TKey, typename TValue=void, typename TKeyComp=match_key<TKey>, typename TValueComp=match_value<TValue>>
78  class Filter {
79 
80  using key_type = TKey;
81  using value_type = typename std::conditional<std::is_void<TValue>::value, bool, TValue>::type;
82 
83  struct Rule {
87  bool result;
88 
89  explicit Rule(bool r, bool ignore, const key_type& k, const value_type& v) :
90  key(k),
91  value(v),
92  ignore_value(ignore),
93  result(r) {
94  }
95 
96  explicit Rule(bool r, bool ignore, const key_type& k) :
97  key(k),
98  value(),
99  ignore_value(ignore),
100  result(r) {
101  }
102 
103  }; // struct Rule
104 
105  std::vector<Rule> m_rules;
107 
108  public:
109 
111  using argument_type = const osmium::Tag&;
112  using result_type = bool;
113  using iterator = boost::filter_iterator<filter_type, osmium::TagList::const_iterator>;
114 
115  explicit Filter(bool default_result = false) :
116  m_default_result(default_result) {
117  }
118 
119  template <typename V=TValue, typename std::enable_if<!std::is_void<V>::value, int>::type = 0>
120  Filter& add(bool result, const key_type& key, const value_type& value) {
121  m_rules.emplace_back(result, false, key, value);
122  return *this;
123  }
124 
125  Filter& add(bool result, const key_type& key) {
126  m_rules.emplace_back(result, true, key);
127  return *this;
128  }
129 
130  bool operator()(const osmium::Tag& tag) const {
131  for (const Rule& rule : m_rules) {
132  if (TKeyComp()(rule.key, tag.key()) && (rule.ignore_value || TValueComp()(rule.value, tag.value()))) {
133  return rule.result;
134  }
135  }
136  return m_default_result;
137  }
138 
142  size_t count() const {
143  return m_rules.count();
144  }
145 
149  bool empty() const {
150  return m_rules.empty();
151  }
152 
153  }; // class Filter
154 
158 
159  } // namespace tags
160 
161 } // namespace osmium
162 
163 #endif // OSMIUM_TAGS_FILTER_HPP
Definition: tag.hpp:48
Definition: filter.hpp:78
Filter & add(bool result, const key_type &key)
Definition: filter.hpp:125
key_type key
Definition: filter.hpp:84
type
Definition: entity_bits.hpp:63
Definition: filter.hpp:64
bool empty() const
Definition: filter.hpp:149
Filter & add(bool result, const key_type &key, const value_type &value)
Definition: filter.hpp:120
bool m_default_result
Definition: filter.hpp:106
Filter(bool default_result=false)
Definition: filter.hpp:115
Rule(bool r, bool ignore, const key_type &k)
Definition: filter.hpp:96
bool ignore_value
Definition: filter.hpp:86
TKey key_type
Definition: filter.hpp:80
Definition: filter.hpp:83
Definition: filter.hpp:57
Namespace for everything in the Osmium library.
Definition: assembler.hpp:73
bool operator()(const TKey &rule_key, const char *tag_key)
Definition: filter.hpp:52
bool operator()(const std::string &rule_key, const char *tag_key)
Definition: filter.hpp:58
Rule(bool r, bool ignore, const key_type &k, const value_type &v)
Definition: filter.hpp:89
bool operator()(const osmium::Tag &tag) const
Definition: filter.hpp:130
bool operator()(const TValue &rule_value, const char *tag_value)
Definition: filter.hpp:65
size_t count() const
Definition: filter.hpp:142
const char * key() const noexcept
Definition: tag.hpp:79
value_type value
Definition: filter.hpp:85
Definition: filter.hpp:51
bool result_type
Definition: filter.hpp:112
bool operator()(const bool, const char *)
Definition: filter.hpp:72
bool result
Definition: filter.hpp:87
typename std::conditional< std::is_void< TValue >::value, bool, TValue >::type value_type
Definition: filter.hpp:81
boost::filter_iterator< filter_type, osmium::TagList::const_iterator > iterator
Definition: filter.hpp:113
const char * value() const
Definition: tag.hpp:83
std::vector< Rule > m_rules
Definition: filter.hpp:105