Libosmium: include/osmium/osm/tag.hpp Source File

Libosmium Fast and flexible C++ library for working with OpenStreetMap data

Loading...
Searching...
No Matches
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-2026 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
36#include <osmium/memory/collection.hpp>
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
46namespace osmium {
47
48 class Tag : public osmium::memory::detail::ItemHelper {
49
50 template <typename TMember>
51 friend class osmium::memory::CollectionIterator;
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
osmium::TagList::get_value_by_key
const char * get_value_by_key(const char *key, const char *default_value=nullptr) const noexcept
Definition tag.hpp:137
osmium::TagList::has_tag
bool has_tag(const char *key, const char *value) const noexcept
Definition tag.hpp:169
osmium::TagList::operator[]
const char * operator[](const char *key) const noexcept
Definition tag.hpp:149
osmium::TagList::find_key
const_iterator find_key(const char *key) const noexcept
Definition tag.hpp:121
osmium::TagList::TagList
TagList() noexcept=default
osmium::TagList::has_key
bool has_key(const char *key) const noexcept
Definition tag.hpp:158
osmium::Tag
Definition tag.hpp:48
osmium::Tag::Tag
Tag(const Tag &)=delete
osmium::Tag::operator=
Tag & operator=(Tag &&)=delete
osmium::Tag::key
const char * key() const noexcept
Definition tag.hpp:86
osmium::Tag::Tag
Tag(Tag &&)=delete
osmium::Tag::value
const char * value() const noexcept
Definition tag.hpp:95
osmium::Tag::next
unsigned char * next() noexcept
Definition tag.hpp:61
osmium::Tag::next
const unsigned char * next() const noexcept
Definition tag.hpp:65
osmium::Tag::~Tag
~Tag() noexcept=default
osmium::Tag::operator=
Tag & operator=(const Tag &)=delete
osmium::Tag::after_null
static const unsigned char * after_null(const unsigned char *ptr) noexcept
Definition tag.hpp:57
osmium::Tag::after_null
static unsigned char * after_null(unsigned char *ptr) noexcept
Definition tag.hpp:53
osmium::Tag::collection_type
static constexpr item_type collection_type
Definition tag.hpp:79
osmium::memory::CollectionIterator
Definition collection.hpp:48
osmium::memory::Collection
Definition collection.hpp:182
osmium::memory::Collection< Tag, osmium::item_type::tag_list >::const_iterator
CollectionIterator< const Tag > const_iterator
Definition collection.hpp:190
osmium::memory::Collection< Tag, osmium::item_type::tag_list >::cend
const_iterator cend() const noexcept
Definition collection.hpp:233
osmium::memory::Collection< Tag, osmium::item_type::tag_list >::cbegin
const_iterator cbegin() const noexcept
Definition collection.hpp:229
collection.hpp
item.hpp
item_type.hpp
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
osmium::operator==
bool operator==(const Changeset &lhs, const Changeset &rhs)
Definition changeset.hpp:440
osmium::item_type
item_type
Definition item_type.hpp:45
osmium::item_type::tag_list
@ tag_list
Definition item_type.hpp:53
osmium::operator<<
std::basic_ostream< TChar, TTraits > & operator<<(std::basic_ostream< TChar, TTraits > &out, const item_type item_type)
Definition item_type.hpp:187
osmium::operator<
bool operator<(const Changeset &lhs, const Changeset &rhs)
Definition changeset.hpp:451