Libosmium  2.20.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 (https://osmcode.org/libosmium).
7 
8 Copyright 2013-2023 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/memory/item.hpp>
38 #include <osmium/osm/item_type.hpp>
39 
40 #include <algorithm>
41 #include <cassert>
42 #include <cstring>
43 #include <iosfwd>
44 #include <iterator>
45 
46 namespace osmium {
47 
48  class Tag : public osmium::memory::detail::ItemHelper {
49 
50  template <typename TMember>
52 
53  static unsigned char* after_null(unsigned char* ptr) noexcept {
54  return reinterpret_cast<unsigned char*>(std::strchr(reinterpret_cast<char*>(ptr), 0) + 1);
55  }
56 
57  static const unsigned char* after_null(const unsigned char* ptr) noexcept {
58  return reinterpret_cast<const unsigned char*>(std::strchr(reinterpret_cast<const char*>(ptr), 0) + 1);
59  }
60 
61  unsigned char* next() noexcept {
62  return after_null(after_null(data()));
63  }
64 
65  const unsigned char* next() const noexcept {
66  return after_null(after_null(data()));
67  }
68 
69  public:
70 
71  Tag(const Tag&) = delete;
72  Tag& operator=(const Tag&) = delete;
73 
74  Tag(Tag&&) = delete;
75  Tag& operator=(Tag&&) = delete;
76 
77  ~Tag() noexcept = default;
78 
79  static constexpr item_type collection_type = item_type::tag_list;
80 
86  const char* key() const noexcept {
87  return reinterpret_cast<const char*>(data());
88  }
89 
95  const char* value() const noexcept {
96  return reinterpret_cast<const char*>(after_null(data()));
97  }
98 
99  }; // class Tag
100 
101  inline bool operator==(const Tag& lhs, const Tag& rhs) noexcept {
102  return !std::strcmp(lhs.key(), rhs.key()) &&
103  !std::strcmp(lhs.value(), rhs.value());
104  }
105 
106  inline bool operator<(const Tag& lhs, const Tag& rhs) noexcept {
107  const auto c = std::strcmp(lhs.key(), rhs.key());
108  return (c == 0 ? std::strcmp(lhs.value(), rhs.value()) : c) < 0;
109  }
110 
114  template <typename TChar, typename TTraits>
115  inline std::basic_ostream<TChar, TTraits>& operator<<(std::basic_ostream<TChar, TTraits>& out, const Tag& tag) {
116  return out << tag.key() << '=' << tag.value();
117  }
118 
119  class TagList : public osmium::memory::Collection<Tag, osmium::item_type::tag_list> {
120 
121  const_iterator find_key(const char* key) const noexcept {
122  return std::find_if(cbegin(), cend(), [key](const Tag& tag) {
123  return !std::strcmp(tag.key(), key);
124  });
125  }
126 
127  public:
128 
129  TagList() noexcept = default;
130 
137  const char* get_value_by_key(const char* key, const char* default_value = nullptr) const noexcept {
138  assert(key);
139  const auto result = find_key(key);
140  return result == cend() ? default_value : result->value();
141  }
142 
149  const char* operator[](const char* key) const noexcept {
150  return get_value_by_key(key);
151  }
152 
158  bool has_key(const char* key) const noexcept {
159  assert(key);
160  return find_key(key) != cend();
161  }
162 
169  bool has_tag(const char* key, const char* value) const noexcept {
170  assert(key);
171  assert(value);
172  const auto result = find_key(key);
173  return result != cend() && !std::strcmp(result->value(), value);
174  }
175 
176  }; // class TagList
177 
178 
179 } // namespace osmium
180 
181 #endif // OSMIUM_OSM_TAG_HPP
Definition: tag.hpp:119
bool has_tag(const char *key, const char *value) const noexcept
Definition: tag.hpp:169
const char * get_value_by_key(const char *key, const char *default_value=nullptr) const noexcept
Definition: tag.hpp:137
const_iterator find_key(const char *key) const noexcept
Definition: tag.hpp:121
TagList() noexcept=default
bool has_key(const char *key) const noexcept
Definition: tag.hpp:158
const char * operator[](const char *key) const noexcept
Definition: tag.hpp:149
Definition: tag.hpp:48
unsigned char * next() noexcept
Definition: tag.hpp:61
Tag(const Tag &)=delete
Tag & operator=(Tag &&)=delete
Tag(Tag &&)=delete
static unsigned char * after_null(unsigned char *ptr) noexcept
Definition: tag.hpp:53
const unsigned char * next() const noexcept
Definition: tag.hpp:65
static const unsigned char * after_null(const unsigned char *ptr) noexcept
Definition: tag.hpp:57
~Tag() noexcept=default
const char * value() const noexcept
Definition: tag.hpp:95
const char * key() const noexcept
Definition: tag.hpp:86
Tag & operator=(const Tag &)=delete
static constexpr item_type collection_type
Definition: tag.hpp:79
Definition: collection.hpp:47
unsigned char * data() const noexcept
Definition: collection.hpp:91
Definition: collection.hpp:181
const_iterator cend() const noexcept
Definition: collection.hpp:232
const_iterator cbegin() const noexcept
Definition: collection.hpp:228
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
bool operator==(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:440
item_type
Definition: item_type.hpp:45
std::basic_ostream< TChar, TTraits > & operator<<(std::basic_ostream< TChar, TTraits > &out, const item_type item_type)
Definition: item_type.hpp:187
bool operator<(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:451