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
6This file is part of Osmium (https://osmcode.org/libosmium).
7
8Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
9
10Boost Software License - Version 1.0 - August 17th, 2003
11
12Permission is hereby granted, free of charge, to any person or organization
13obtaining a copy of the software and accompanying documentation covered by
14this license (the "Software") to use, reproduce, display, distribute,
15execute, and transmit the Software, and to prepare derivative works of the
16Software, and to permit third-parties to whom the Software is furnished to
17do so, all subject to the following:
18
19The copyright notices in the Software and this entire statement, including
20the above license grant, this restriction and the following disclaimer,
21must be included in all copies of the Software, in whole or in part, and
22all derivative works of the Software, unless such copies or derivative
23works are solely in the form of machine-executable object code generated by
24a source language processor.
25
26THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32DEALINGS IN THE SOFTWARE.
33
34*/
35
39
40#include <algorithm>
41#include <cassert>
42#include <cstring>
43#include <iosfwd>
44#include <iterator>
45
46namespace 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
const char * get_value_by_key(const char *key, const char *default_value=nullptr) const noexcept
Definition: tag.hpp:137
bool has_tag(const char *key, const char *value) const noexcept
Definition: tag.hpp:169
const char * operator[](const char *key) const noexcept
Definition: tag.hpp:149
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
Definition: tag.hpp:48
Tag(const Tag &)=delete
Tag & operator=(Tag &&)=delete
const char * key() const noexcept
Definition: tag.hpp:86
Tag(Tag &&)=delete
const char * value() const noexcept
Definition: tag.hpp:95
unsigned char * next() noexcept
Definition: tag.hpp:61
const unsigned char * next() const noexcept
Definition: tag.hpp:65
~Tag() noexcept=default
Tag & operator=(const Tag &)=delete
static const unsigned char * after_null(const unsigned char *ptr) noexcept
Definition: tag.hpp:57
static unsigned char * after_null(unsigned char *ptr) noexcept
Definition: tag.hpp:53
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