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