Libosmium  2.6.1
Fast and flexible C++ library for working with OpenStreetMap data
diff_iterator.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_DIFF_ITERATOR_HPP
2 #define OSMIUM_DIFF_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 <type_traits>
39 
41 
42 namespace osmium {
43 
44  class OSMObject;
45 
51  template <typename TBasicIterator>
52  class DiffIterator : public std::iterator<std::input_iterator_tag, const osmium::DiffObject> {
53 
54 
55  TBasicIterator m_prev;
56  TBasicIterator m_curr;
57  TBasicIterator m_next;
58 
59  const TBasicIterator m_end;
60 
62 
63  void set_diff() const noexcept {
64  assert(m_curr != m_end);
65 
66  bool use_curr_for_prev = m_prev->type() != m_curr->type() || m_prev->id() != m_curr->id();
67  bool use_curr_for_next = m_next == m_end || m_next->type() != m_curr->type() || m_next->id() != m_curr->id();
68 
69  m_diff = std::move(osmium::DiffObject{
70  *(use_curr_for_prev ? m_curr : m_prev),
71  *m_curr,
72  *(use_curr_for_next ? m_curr : m_next)
73  });
74  }
75 
76  public:
77 
78  DiffIterator(TBasicIterator begin, TBasicIterator end) :
79  m_prev(begin),
80  m_curr(begin),
81  m_next(begin == end ? begin : ++begin),
82  m_end(std::move(end)),
83  m_diff() {
84  }
85 
87  m_prev = std::move(m_curr);
88  m_curr = m_next;
89 
90  if (m_next != m_end) {
91  ++m_next;
92  }
93 
94  return *this;
95  }
96 
98  DiffIterator tmp(*this);
99  operator++();
100  return tmp;
101  }
102 
103  bool operator==(const DiffIterator& rhs) const noexcept {
104  return m_curr == rhs.m_curr && m_end == rhs.m_end;
105  }
106 
107  bool operator!=(const DiffIterator& rhs) const noexcept {
108  return !(*this == rhs);
109  }
110 
111  reference operator*() const noexcept {
112  set_diff();
113  return m_diff;
114  }
115 
116  pointer operator->() const noexcept {
117  set_diff();
118  return &m_diff;
119  }
120 
121  }; // class DiffIterator
122 
126  template <typename TBasicIterator>
128  TBasicIterator end) {
129  return DiffIterator<TBasicIterator>{begin, end};
130  }
131 
132 } // namespace osmium
133 
134 #endif // OSMIUM_DIFF_ITERATOR_HPP
Definition: diff_object.hpp:63
DiffIterator< TBasicIterator > make_diff_iterator(TBasicIterator begin, TBasicIterator end)
Definition: diff_iterator.hpp:127
const TBasicIterator m_end
Definition: diff_iterator.hpp:59
Definition: reader_iterator.hpp:39
Definition: diff_iterator.hpp:52
Namespace for everything in the Osmium library.
Definition: assembler.hpp:59
osmium::DiffObject m_diff
Definition: diff_iterator.hpp:61
osmium::io::InputIterator< osmium::io::Reader > end(osmium::io::Reader &)
Definition: reader_iterator.hpp:45
bool operator==(const DiffIterator &rhs) const noexcept
Definition: diff_iterator.hpp:103
void set_diff() const noexcept
Definition: diff_iterator.hpp:63
DiffIterator & operator++()
Definition: diff_iterator.hpp:86
TBasicIterator m_prev
Definition: diff_iterator.hpp:55
reference operator*() const noexcept
Definition: diff_iterator.hpp:111
DiffIterator operator++(int)
Definition: diff_iterator.hpp:97
TBasicIterator m_curr
Definition: diff_iterator.hpp:56
pointer operator->() const noexcept
Definition: diff_iterator.hpp:116
TBasicIterator m_next
Definition: diff_iterator.hpp:57
osmium::io::InputIterator< osmium::io::Reader > begin(osmium::io::Reader &reader)
Definition: reader_iterator.hpp:41
bool operator!=(const DiffIterator &rhs) const noexcept
Definition: diff_iterator.hpp:107
DiffIterator(TBasicIterator begin, TBasicIterator end)
Definition: diff_iterator.hpp:78