Libosmium  2.1.0
Fast and flexible C++ library for working with OpenStreetMap data
item_iterator.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_ITEM_ITERATOR_HPP
2 #define OSMIUM_ITEM_ITERATOR_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 <cassert>
37 #include <iterator>
38 #include <iosfwd>
39 #include <type_traits>
40 
41 #include <osmium/memory/item.hpp>
42 #include <osmium/osm/item_type.hpp>
43 
44 namespace osmium {
45 
46  class Node;
47  class Way;
48  class Relation;
49  class Area;
50  class Changeset;
51  class OSMObject;
52  class OSMEntity;
53  class TagList;
54  class WayNodeList;
55  class RelationMemberList;
56  class InnerRing;
57  class OuterRing;
58 
59  namespace memory {
60 
61  namespace detail {
62 
63  template <class T>
64  inline bool type_is_compatible(osmium::item_type) noexcept {
65  return true;
66  }
67 
68  template <>
69  inline bool type_is_compatible<osmium::Node>(osmium::item_type t) noexcept {
70  return t == osmium::item_type::node;
71  }
72 
73  template <>
74  inline bool type_is_compatible<osmium::Way>(osmium::item_type t) noexcept {
75  return t == osmium::item_type::way;
76  }
77 
78  template <>
79  inline bool type_is_compatible<osmium::Relation>(osmium::item_type t) noexcept {
80  return t == osmium::item_type::relation;
81  }
82 
83  template <>
84  inline bool type_is_compatible<osmium::Area>(osmium::item_type t) noexcept {
85  return t == osmium::item_type::area;
86  }
87 
88  template <>
89  inline bool type_is_compatible<osmium::Changeset>(osmium::item_type t) noexcept {
90  return t == osmium::item_type::changeset;
91  }
92 
93  template <>
94  inline bool type_is_compatible<osmium::OSMObject>(osmium::item_type t) noexcept {
96  }
97 
98  template <>
99  inline bool type_is_compatible<osmium::OSMEntity>(osmium::item_type t) noexcept {
101  }
102 
103  template <>
104  inline bool type_is_compatible<osmium::TagList>(osmium::item_type t) noexcept {
105  return t == osmium::item_type::tag_list;
106  }
107 
108  template <>
109  inline bool type_is_compatible<osmium::WayNodeList>(osmium::item_type t) noexcept {
111  }
112 
113  template <>
114  inline bool type_is_compatible<osmium::RelationMemberList>(osmium::item_type t) noexcept {
116  }
117 
118  template <>
119  inline bool type_is_compatible<osmium::OuterRing>(osmium::item_type t) noexcept {
120  return t == osmium::item_type::outer_ring;
121  }
122 
123  template <>
124  inline bool type_is_compatible<osmium::InnerRing>(osmium::item_type t) noexcept {
125  return t == osmium::item_type::inner_ring;
126  }
127 
128  } // namespace detail
129 
130  template <class TMember>
131  class ItemIterator : public std::iterator<std::forward_iterator_tag, TMember> {
132 
133 
134  // This data_type is either 'unsigned char*' or 'const unsigned char*' depending
135  // on whether TMember is const. This allows this class to be used as an iterator and
136  // as a const_iterator.
137  typedef typename std::conditional<std::is_const<TMember>::value, const unsigned char*, unsigned char*>::type data_type;
138 
139  data_type m_data;
140  data_type m_end;
141 
143  while (m_data != m_end &&
144  !detail::type_is_compatible<typename std::remove_const<TMember>::type>(reinterpret_cast<const osmium::memory::Item*>(m_data)->type())) {
145  m_data = reinterpret_cast<TMember*>(m_data)->next();
146  }
147  }
148 
149  public:
150 
151  ItemIterator() noexcept :
152  m_data(nullptr),
153  m_end(nullptr) {
154  }
155 
156  ItemIterator(data_type data, data_type end) :
157  m_data(data),
158  m_end(end) {
160  }
161 
162  template <class T>
164  return ItemIterator<T>(m_data, m_end);
165  }
166 
168  assert(m_data);
169  assert(m_data != m_end);
170  m_data = reinterpret_cast<TMember*>(m_data)->next();
172  return *static_cast<ItemIterator<TMember>*>(this);
173  }
174 
181  assert(m_data);
182  assert(m_data != m_end);
183  m_data = reinterpret_cast<TMember*>(m_data)->next();
184  return *static_cast<ItemIterator<TMember>*>(this);
185  }
186 
188  ItemIterator<TMember> tmp(*this);
189  operator++();
190  return tmp;
191  }
192 
193  bool operator==(const ItemIterator<TMember>& rhs) const {
194  return m_data == rhs.m_data && m_end == rhs.m_end;
195  }
196 
197  bool operator!=(const ItemIterator<TMember>& rhs) const {
198  return !(*this == rhs);
199  }
200 
201  unsigned char* data() const {
202  assert(m_data);
203  return m_data;
204  }
205 
206  TMember& operator*() const {
207  assert(m_data);
208  assert(m_data != m_end);
209  return *reinterpret_cast<TMember*>(m_data);
210  }
211 
212  TMember* operator->() const {
213  assert(m_data);
214  assert(m_data != m_end);
215  return reinterpret_cast<TMember*>(m_data);
216  }
217 
218  explicit operator bool() const {
219  return m_data != nullptr;
220  }
221 
222  template <typename TChar, typename TTraits>
223  friend std::basic_ostream<TChar, TTraits>& operator<<(std::basic_ostream<TChar, TTraits>& out, const ItemIterator<TMember>& iter) {
224  return out << static_cast<void*>(iter.m_data);
225  }
226 
227  }; // class ItemIterator
228 
229  } // namespace memory
230 
231 } // namespace osmium
232 
233 #endif // OSMIUM_ITEM_ITERATOR_HPP
std::conditional< std::is_const< TMember >::value, const unsigned char *, unsigned char * >::type data_type
Definition: item_iterator.hpp:137
TMember * operator->() const
Definition: item_iterator.hpp:212
type
Definition: entity_bits.hpp:60
TMember & operator*() const
Definition: item_iterator.hpp:206
Definition: item_iterator.hpp:131
item_type
Definition: item_type.hpp:42
ItemIterator< TMember > & advance_once()
Definition: item_iterator.hpp:180
bool operator==(const ItemIterator< TMember > &rhs) const
Definition: item_iterator.hpp:193
unsigned char * data() const
Definition: item_iterator.hpp:201
Namespace for everything in the Osmium library.
Definition: assembler.hpp:55
osmium::io::InputIterator< osmium::io::Reader > end(osmium::io::Reader &)
Definition: reader_iterator.hpp:45
void advance_to_next_item_of_right_type()
Definition: item_iterator.hpp:142
ItemIterator() noexcept
Definition: item_iterator.hpp:151
data_type m_end
Definition: item_iterator.hpp:140
data_type m_data
Definition: item_iterator.hpp:139
bool operator!=(const ItemIterator< TMember > &rhs) const
Definition: item_iterator.hpp:197
ItemIterator< TMember > operator++(int)
Definition: item_iterator.hpp:187
ItemIterator< T > cast() const
Definition: item_iterator.hpp:163
ItemIterator(data_type data, data_type end)
Definition: item_iterator.hpp:156
ItemIterator< TMember > & operator++()
Definition: item_iterator.hpp:167