Libosmium  2.9.0
Fast and flexible C++ library for working with OpenStreetMap data
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
tag.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_OSM_TAG_HPP
2 #define OSMIUM_OSM_TAG_HPP
3 
4 /*
5 
6 This file is part of Osmium (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2016 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 <algorithm>
37 #include <cassert>
38 #include <cstring>
39 #include <iosfwd>
40 #include <iterator>
41 
43 #include <osmium/memory/item.hpp>
44 #include <osmium/osm/item_type.hpp>
45 
46 namespace osmium {
47 
48  class Tag : public osmium::memory::detail::ItemHelper {
49 
50  Tag(const Tag&) = delete;
51  Tag(Tag&&) = delete;
52 
53  Tag& operator=(const Tag&) = delete;
54  Tag& operator=(Tag&&) = delete;
55 
56  template <typename TMember>
58 
59  static unsigned char* after_null(unsigned char* ptr) {
60  return reinterpret_cast<unsigned char*>(std::strchr(reinterpret_cast<char*>(ptr), 0) + 1);
61  }
62 
63  static const unsigned char* after_null(const unsigned char* ptr) {
64  return reinterpret_cast<const unsigned char*>(std::strchr(reinterpret_cast<const char*>(ptr), 0) + 1);
65  }
66 
67  unsigned char* next() {
68  return after_null(after_null(data()));
69  }
70 
71  const unsigned char* next() const {
72  return after_null(after_null(data()));
73  }
74 
75  public:
76 
78 
79  const char* key() const noexcept {
80  return reinterpret_cast<const char*>(data());
81  }
82 
83  const char* value() const {
84  return reinterpret_cast<const char*>(after_null(data()));
85  }
86 
87  }; // class Tag
88 
89  inline bool operator==(const Tag& a, const Tag& b) {
90  return !std::strcmp(a.key(), b.key()) && !std::strcmp(a.value(), b.value());
91  }
92 
93  inline bool operator<(const Tag& a, const Tag& b) {
94  return (!std::strcmp(a.key(), b.key()) && (std::strcmp(a.value(), b.value()) < 0)) || (std::strcmp(a.key(), b.key()) < 0);
95  }
96 
100  template <typename TChar, typename TTraits>
101  inline std::basic_ostream<TChar, TTraits>& operator<<(std::basic_ostream<TChar, TTraits>& out, const Tag& tag) {
102  return out << tag.key() << '=' << tag.value();
103  }
104 
105  class TagList : public osmium::memory::Collection<Tag, osmium::item_type::tag_list> {
106 
107  const_iterator find_key(const char* key) const noexcept {
108  return std::find_if(cbegin(), cend(), [key](const Tag& tag) {
109  return !std::strcmp(tag.key(), key);
110  });
111  }
112 
113  public:
114 
115  using size_type = size_t;
116 
118  osmium::memory::Collection<Tag, osmium::item_type::tag_list>() {
119  }
120 
124  size_type size() const noexcept {
125  return static_cast<size_type>(std::distance(begin(), end()));
126  }
127 
134  const char* get_value_by_key(const char* key, const char* default_value = nullptr) const noexcept {
135  assert(key);
136  const auto result = find_key(key);
137  return result == cend() ? default_value : result->value();
138  }
139 
146  const char* operator[](const char* key) const noexcept {
147  return get_value_by_key(key);
148  }
149 
155  bool has_key(const char* key) const noexcept {
156  assert(key);
157  return find_key(key) != cend();
158  }
159 
166  bool has_tag(const char* key, const char* value) const noexcept {
167  assert(key);
168  assert(value);
169  const auto result = find_key(key);
170  return result != cend() && !std::strcmp(result->value(), value);
171  }
172 
173  }; // class TagList
174 
175 
176 } // namespace osmium
177 
178 #endif // OSMIUM_OSM_TAG_HPP
Definition: tag.hpp:48
Definition: collection.hpp:47
Definition: tag.hpp:105
Tag & operator=(const Tag &)=delete
const char * value() const
Definition: tag.hpp:83
bool has_key(const char *key) const noexcept
Definition: tag.hpp:155
const_iterator cend() const
Definition: collection.hpp:147
static unsigned char * after_null(unsigned char *ptr)
Definition: tag.hpp:59
constexpr bool operator==(const Box &lhs, const Box &rhs) noexcept
Definition: box.hpp:221
item_type
Definition: item_type.hpp:43
iterator begin()
Definition: collection.hpp:135
bool has_tag(const char *key, const char *value) const noexcept
Definition: tag.hpp:166
double distance(const osmium::geom::Coordinates &c1, const osmium::geom::Coordinates &c2)
Definition: haversine.hpp:64
const unsigned char * next() const
Definition: tag.hpp:71
Tag(const Tag &)=delete
bool operator<(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:445
Namespace for everything in the Osmium library.
Definition: assembler.hpp:73
TagList()
Definition: tag.hpp:117
Definition: collection.hpp:117
unsigned char * data() const noexcept
Definition: collection.hpp:91
static constexpr item_type collection_type
Definition: tag.hpp:77
const char * key() const noexcept
Definition: tag.hpp:79
const_iterator cbegin() const
Definition: collection.hpp:143
const_iterator find_key(const char *key) const noexcept
Definition: tag.hpp:107
size_t size_type
Definition: tag.hpp:115
const char * get_value_by_key(const char *key, const char *default_value=nullptr) const noexcept
Definition: tag.hpp:134
unsigned char * next()
Definition: tag.hpp:67
size_type size() const noexcept
Definition: tag.hpp:124
const char * operator[](const char *key) const noexcept
Definition: tag.hpp:146
static const unsigned char * after_null(const unsigned char *ptr)
Definition: tag.hpp:63