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