Libosmium  2.8.0
Fast and flexible C++ library for working with OpenStreetMap data
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 <cstddef>
39 #include <cstring>
40 #include <iosfwd>
41 #include <iterator>
42 
44 #include <osmium/memory/item.hpp>
45 #include <osmium/osm/item_type.hpp>
46 
47 namespace osmium {
48 
49  class Tag : public osmium::memory::detail::ItemHelper {
50 
51  Tag(const Tag&) = delete;
52  Tag(Tag&&) = delete;
53 
54  Tag& operator=(const Tag&) = delete;
55  Tag& operator=(Tag&&) = delete;
56 
57  template <typename TMember>
59 
60  static unsigned char* after_null(unsigned char* ptr) {
61  return reinterpret_cast<unsigned char*>(std::strchr(reinterpret_cast<char*>(ptr), 0) + 1);
62  }
63 
64  static const unsigned char* after_null(const unsigned char* ptr) {
65  return reinterpret_cast<const unsigned char*>(std::strchr(reinterpret_cast<const char*>(ptr), 0) + 1);
66  }
67 
68  unsigned char* next() {
69  return after_null(after_null(data()));
70  }
71 
72  const unsigned char* next() const {
73  return after_null(after_null(data()));
74  }
75 
76  public:
77 
79 
80  const char* key() const noexcept {
81  return reinterpret_cast<const char*>(data());
82  }
83 
84  const char* value() const {
85  return reinterpret_cast<const char*>(after_null(data()));
86  }
87 
88  }; // class Tag
89 
90  inline bool operator==(const Tag& a, const Tag& b) {
91  return !std::strcmp(a.key(), b.key()) && !std::strcmp(a.value(), b.value());
92  }
93 
94  inline bool operator<(const Tag& a, const Tag& b) {
95  return (!std::strcmp(a.key(), b.key()) && (std::strcmp(a.value(), b.value()) < 0)) || (std::strcmp(a.key(), b.key()) < 0);
96  }
97 
101  template <typename TChar, typename TTraits>
102  inline std::basic_ostream<TChar, TTraits>& operator<<(std::basic_ostream<TChar, TTraits>& out, const Tag& tag) {
103  return out << tag.key() << '=' << tag.value();
104  }
105 
106  class TagList : public osmium::memory::Collection<Tag, osmium::item_type::tag_list> {
107 
108  const_iterator find_key(const char* key) const noexcept {
109  return std::find_if(cbegin(), cend(), [key](const Tag& tag) {
110  return !std::strcmp(tag.key(), key);
111  });
112  }
113 
114  public:
115 
116  using size_type = size_t;
117 
119  osmium::memory::Collection<Tag, osmium::item_type::tag_list>() {
120  }
121 
125  size_type size() const noexcept {
126  return static_cast<size_type>(std::distance(begin(), end()));
127  }
128 
135  const char* get_value_by_key(const char* key, const char* default_value = nullptr) const noexcept {
136  assert(key);
137  const auto result = find_key(key);
138  return result == cend() ? default_value : result->value();
139  }
140 
147  const char* operator[](const char* key) const noexcept {
148  return get_value_by_key(key);
149  }
150 
156  bool has_key(const char* key) const noexcept {
157  assert(key);
158  return find_key(key) != cend();
159  }
160 
167  bool has_tag(const char* key, const char* value) const noexcept {
168  assert(key);
169  assert(value);
170  const auto result = find_key(key);
171  return result != cend() && !std::strcmp(result->value(), value);
172  }
173 
174  }; // class TagList
175 
176 
177 } // namespace osmium
178 
179 #endif // OSMIUM_OSM_TAG_HPP
Definition: tag.hpp:49
Definition: collection.hpp:47
Definition: tag.hpp:106
Tag & operator=(const Tag &)=delete
const char * value() const
Definition: tag.hpp:84
bool has_key(const char *key) const noexcept
Definition: tag.hpp:156
static unsigned char * after_null(unsigned char *ptr)
Definition: tag.hpp:60
constexpr bool operator==(const Box &lhs, const Box &rhs) noexcept
Definition: box.hpp:222
item_type
Definition: item_type.hpp:43
bool has_tag(const char *key, const char *value) const noexcept
Definition: tag.hpp:167
double distance(const osmium::geom::Coordinates &c1, const osmium::geom::Coordinates &c2)
Definition: haversine.hpp:64
const unsigned char * next() const
Definition: tag.hpp:72
Tag(const Tag &)=delete
bool operator<(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:438
Namespace for everything in the Osmium library.
Definition: assembler.hpp:66
TagList()
Definition: tag.hpp:118
Definition: collection.hpp:117
osmium::io::InputIterator< osmium::io::Reader > end(osmium::io::Reader &)
Definition: reader_iterator.hpp:45
unsigned char * data() const noexcept
Definition: collection.hpp:91
static constexpr item_type collection_type
Definition: tag.hpp:78
const char * key() const noexcept
Definition: tag.hpp:80
const_iterator find_key(const char *key) const noexcept
Definition: tag.hpp:108
osmium::io::InputIterator< osmium::io::Reader > begin(osmium::io::Reader &reader)
Definition: reader_iterator.hpp:41
size_t size_type
Definition: tag.hpp:116
const char * get_value_by_key(const char *key, const char *default_value=nullptr) const noexcept
Definition: tag.hpp:135
unsigned char * next()
Definition: tag.hpp:68
size_type size() const noexcept
Definition: tag.hpp:125
const char * operator[](const char *key) const noexcept
Definition: tag.hpp:147
static const unsigned char * after_null(const unsigned char *ptr)
Definition: tag.hpp:64