Libosmium  2.13.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-2017 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& lhs, const Tag& rhs) {
90  return !std::strcmp(lhs.key(), rhs.key()) &&
91  !std::strcmp(lhs.value(), rhs.value());
92  }
93 
94  inline bool operator<(const Tag& lhs, const Tag& rhs) {
95  const auto c = std::strcmp(lhs.key(), rhs.key());
96  return (c == 0 ? std::strcmp(lhs.value(), rhs.value()) : c) < 0;
97  }
98 
102  template <typename TChar, typename TTraits>
103  inline std::basic_ostream<TChar, TTraits>& operator<<(std::basic_ostream<TChar, TTraits>& out, const Tag& tag) {
104  return out << tag.key() << '=' << tag.value();
105  }
106 
107  class TagList : public osmium::memory::Collection<Tag, osmium::item_type::tag_list> {
108 
109  const_iterator find_key(const char* key) const noexcept {
110  return std::find_if(cbegin(), cend(), [key](const Tag& tag) {
111  return !std::strcmp(tag.key(), key);
112  });
113  }
114 
115  public:
116 
118  osmium::memory::Collection<Tag, osmium::item_type::tag_list>() {
119  }
120 
127  const char* get_value_by_key(const char* key, const char* default_value = nullptr) const noexcept {
128  assert(key);
129  const auto result = find_key(key);
130  return result == cend() ? default_value : result->value();
131  }
132 
139  const char* operator[](const char* key) const noexcept {
140  return get_value_by_key(key);
141  }
142 
148  bool has_key(const char* key) const noexcept {
149  assert(key);
150  return find_key(key) != cend();
151  }
152 
159  bool has_tag(const char* key, const char* value) const noexcept {
160  assert(key);
161  assert(value);
162  const auto result = find_key(key);
163  return result != cend() && !std::strcmp(result->value(), value);
164  }
165 
166  }; // class TagList
167 
168 
169 } // namespace osmium
170 
171 #endif // OSMIUM_OSM_TAG_HPP
Definition: tag.hpp:48
Definition: collection.hpp:47
Definition: tag.hpp:107
Tag & operator=(const Tag &)=delete
bool has_key(const char *key) const noexcept
Definition: tag.hpp:148
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
bool has_tag(const char *key, const char *value) const noexcept
Definition: tag.hpp:159
const unsigned char * next() const
Definition: tag.hpp:71
Tag(const Tag &)=delete
bool operator<(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:447
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
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 find_key(const char *key) const noexcept
Definition: tag.hpp:109
const char * get_value_by_key(const char *key, const char *default_value=nullptr) const noexcept
Definition: tag.hpp:127
unsigned char * next()
Definition: tag.hpp:67
const char * operator[](const char *key) const noexcept
Definition: tag.hpp:139
static const unsigned char * after_null(const unsigned char *ptr)
Definition: tag.hpp:63
const char * value() const
Definition: tag.hpp:83