Libosmium  2.1.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-2015 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 <cstddef>
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 <class 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& a, const Tag& b) {
90  return !std::strcmp(a.key(), b.key()) && !strcmp(a.value(), b.value());
91  }
92 
93  inline bool operator<(const Tag& a, const Tag& b) {
94  return (!std::strcmp(a.key(), b.key()) && (std::strcmp(a.value(), b.value()) < 0)) || (std::strcmp(a.key(), b.key()) < 0);
95  }
96 
100  template <typename TChar, typename TTraits>
101  inline std::basic_ostream<TChar, TTraits>& operator<<(std::basic_ostream<TChar, TTraits>& out, const Tag& tag) {
102  return out << tag.key() << '=' << tag.value();
103  }
104 
105  class TagList : public osmium::memory::Collection<Tag, osmium::item_type::tag_list> {
106 
107  public:
108 
109  typedef size_t size_type;
110 
112  osmium::memory::Collection<Tag, osmium::item_type::tag_list>() {
113  }
114 
115  size_type size() const noexcept {
116  return static_cast<size_type>(std::distance(begin(), end()));
117  }
118 
119  const char* get_value_by_key(const char* key, const char* default_value = nullptr) const noexcept {
120  auto result = std::find_if(cbegin(), cend(), [key](const Tag& tag) {
121  return !strcmp(tag.key(), key);
122  });
123  if (result == cend()) {
124  return default_value;
125  } else {
126  return result->value();
127  }
128  }
129 
130  const char* operator[](const char* key) const noexcept {
131  return get_value_by_key(key);
132  }
133 
134  }; // class TagList
135 
136 
137 } // namespace osmium
138 
139 #endif // OSMIUM_OSM_TAG_HPP
Definition: tag.hpp:48
Definition: collection.hpp:48
Definition: tag.hpp:105
Tag & operator=(const Tag &)=delete
const char * value() const
Definition: tag.hpp:83
const_iterator cend() const
Definition: collection.hpp:136
static unsigned char * after_null(unsigned char *ptr)
Definition: tag.hpp:59
item_type
Definition: item_type.hpp:42
size_t size_type
Definition: tag.hpp:109
iterator begin()
Definition: collection.hpp:124
double distance(const osmium::geom::Coordinates &c1, const osmium::geom::Coordinates &c2)
Definition: haversine.hpp:64
const unsigned char * next() const
Definition: tag.hpp:71
OSMIUM_CONSTEXPR bool operator==(const Box &lhs, const Box &rhs) noexcept
Definition: box.hpp:219
Tag(const Tag &)=delete
bool operator<(const Changeset &lhs, const Changeset &rhs)
Definition: changeset.hpp:316
Namespace for everything in the Osmium library.
Definition: assembler.hpp:55
TagList()
Definition: tag.hpp:111
Definition: collection.hpp:106
unsigned char * data() const noexcept
Definition: collection.hpp:86
static constexpr item_type collection_type
Definition: tag.hpp:77
const char * key() const noexcept
Definition: tag.hpp:79
const_iterator cbegin() const
Definition: collection.hpp:132
const char * get_value_by_key(const char *key, const char *default_value=nullptr) const noexcept
Definition: tag.hpp:119
unsigned char * next()
Definition: tag.hpp:67
size_type size() const noexcept
Definition: tag.hpp:115
const char * operator[](const char *key) const noexcept
Definition: tag.hpp:130
static const unsigned char * after_null(const unsigned char *ptr)
Definition: tag.hpp:63