Libosmium  2.13.0
Fast and flexible C++ library for working with OpenStreetMap data
relations_database.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_RELATIONS_RELATIONS_DATABASE_HPP
2 #define OSMIUM_RELATIONS_RELATIONS_DATABASE_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 <cstddef>
39 #include <utility>
40 #include <vector>
41 
42 #include <osmium/osm/relation.hpp>
44 
45 namespace osmium {
46 
47  namespace relations {
48 
49  class RelationHandle;
50 
83 
84  friend class RelationHandle;
85 
86  struct element {
87 
90 
98  std::size_t members;
99 
100  }; // struct element
101 
103  std::vector<element> m_elements;
104 
105  osmium::Relation& get_relation(std::size_t pos) {
106  assert(pos < m_elements.size());
107  return m_stash.get<osmium::Relation>(m_elements[pos].handle);
108  }
109 
114  std::size_t& members(std::size_t pos) noexcept {
115  return m_elements[pos].members;
116  }
117 
118  void remove(std::size_t pos) {
119  auto& elem = m_elements[pos];
120  m_stash.remove_item(elem.handle);
122  }
123 
124  public:
125 
134  m_stash(stash) {
135  }
136 
144  std::size_t used_memory() const noexcept {
145  return sizeof(element) * m_elements.capacity() +
146  sizeof(RelationsDatabase);
147  }
148 
155  std::size_t size() const noexcept {
156  return m_elements.size();
157  }
158 
168  RelationHandle add(const osmium::Relation& relation);
169 
176  RelationHandle operator[](std::size_t pos) noexcept;
177 
184  std::size_t count_relations() const noexcept {
185  return std::count_if(m_elements.cbegin(), m_elements.cend(), [&](const element& elem) {
186  return elem.handle.valid();
187  });
188  }
189 
197  template <typename TFunc>
198  void for_each_relation(TFunc&& func);
199 
200  }; // RelationsDatabase
201 
209 
210  friend class RelationsDatabase;
211 
213  std::size_t m_pos;
214 
215  RelationHandle(RelationsDatabase* relation_database, std::size_t pos) :
216  m_relation_database(relation_database),
217  m_pos(pos) {
218  }
219 
220  public:
221 
226  return m_relation_database;
227  }
228 
238  std::size_t pos() const noexcept {
239  return m_pos;
240  }
241 
246  return m_relation_database->get_relation(m_pos);
247  }
248 
252  const Relation& operator*() const {
253  return m_relation_database->get_relation(m_pos);
254  }
255 
260  return &m_relation_database->get_relation(m_pos);
261  }
262 
266  const Relation* operator->() const {
267  return &m_relation_database->get_relation(m_pos);
268  }
269 
274  void remove() {
275  m_relation_database->remove(pos());
276  }
277 
281  void set_members(std::size_t value) noexcept {
282  m_relation_database->members(m_pos) = value;
283  }
284 
288  void increment_members() noexcept {
289  ++(m_relation_database->members(m_pos));
290  }
291 
297  void decrement_members() noexcept {
298  assert(m_relation_database->members(m_pos) > 0);
299  --(m_relation_database->members(m_pos));
300  }
301 
306  bool has_all_members() const noexcept {
307  return m_relation_database->members(m_pos) == 0;
308  }
309 
310  }; // class RelationHandle
311 
312  inline RelationHandle RelationsDatabase::operator[](std::size_t pos) noexcept {
313  assert(pos < m_elements.size());
314  return {this, pos};
315  }
316 
318  m_elements.push_back(element{m_stash.add_item(relation), 0});
319  return {this, m_elements.size() - 1};
320  }
321 
322  template <typename TFunc>
324  for (std::size_t pos = 0; pos < m_elements.size(); ++pos) {
325  if (m_elements[pos].handle.valid()) {
326  std::forward<TFunc>(func)(RelationHandle{this, pos});
327  }
328  }
329  }
330 
331  } // namespace relations
332 
333 } // namespace osmium
334 
335 #endif // OSMIUM_RELATIONS_RELATIONS_DATABASE_HPP
std::size_t & members(std::size_t pos) noexcept
Definition: relations_database.hpp:114
std::size_t members
Definition: relations_database.hpp:98
void for_each_relation(TFunc &&func)
Definition: relations_database.hpp:323
handle_type add_item(const osmium::memory::Item &item)
Definition: item_stash.hpp:241
Definition: relation.hpp:168
Definition: item_stash.hpp:71
osmium::ItemStash & m_stash
Definition: relations_database.hpp:102
Definition: relations_database.hpp:86
void decrement_members() noexcept
Definition: relations_database.hpp:297
std::size_t used_memory() const noexcept
Definition: relations_database.hpp:144
void remove(std::size_t pos)
Definition: relations_database.hpp:118
std::size_t pos() const noexcept
Definition: relations_database.hpp:238
Relation * operator->()
Definition: relations_database.hpp:259
Definition: item_stash.hpp:57
Relation & operator*()
Definition: relations_database.hpp:245
T & get(handle_type handle) const
Definition: item_stash.hpp:284
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
void increment_members() noexcept
Definition: relations_database.hpp:288
void set_members(std::size_t value) noexcept
Definition: relations_database.hpp:281
const Relation * operator->() const
Definition: relations_database.hpp:266
RelationHandle operator[](std::size_t pos) noexcept
Definition: relations_database.hpp:312
RelationsDatabase * m_relation_database
Definition: relations_database.hpp:212
osmium::ItemStash::handle_type handle
A handle to the relation in the ItemStash.
Definition: relations_database.hpp:89
bool has_all_members() const noexcept
Definition: relations_database.hpp:306
std::vector< element > m_elements
Definition: relations_database.hpp:103
Definition: relations_database.hpp:208
RelationsDatabase(osmium::ItemStash &stash)
Definition: relations_database.hpp:133
std::size_t count_relations() const noexcept
Definition: relations_database.hpp:184
Definition: relations_database.hpp:82
std::size_t size() const noexcept
Definition: relations_database.hpp:155
const Relation & operator*() const
Definition: relations_database.hpp:252
void remove_item(handle_type handle)
Definition: item_stash.hpp:328
RelationHandle add(const osmium::Relation &relation)
Definition: relations_database.hpp:317
bool valid() const noexcept
Is this a valid handle?
Definition: item_stash.hpp:90
osmium::Relation & get_relation(std::size_t pos)
Definition: relations_database.hpp:105
RelationsDatabase * relation_database() const noexcept
Definition: relations_database.hpp:225
Definition: entity_bits.hpp:70
std::size_t m_pos
Definition: relations_database.hpp:213
RelationHandle(RelationsDatabase *relation_database, std::size_t pos)
Definition: relations_database.hpp:215