Libosmium  2.4.1
Fast and flexible C++ library for working with OpenStreetMap data
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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-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 <type_traits>
39 
41 
42 namespace osmium {
43 
44  class OSMObject;
45 
46  template <class TBasicIterator>
47  class DiffIterator : public std::iterator<std::input_iterator_tag, const osmium::DiffObject> {
48 
49 
50  TBasicIterator m_prev;
51  TBasicIterator m_curr;
52  TBasicIterator m_next;
53 
54  const TBasicIterator m_end;
55 
57 
58  void set_diff() const {
59  assert(m_curr != m_end);
60 
61  TBasicIterator prev = m_prev;
62  if (prev->type() != m_curr->type() || prev->id() != m_curr->id()) {
63  prev = m_curr;
64  }
65 
66  TBasicIterator next = m_next;
67  if (next == m_end || next->type() != m_curr->type() || next->id() != m_curr->id()) {
68  next = m_curr;
69  }
70 
71  m_diff = osmium::DiffObject(*prev, *m_curr, *next);
72  }
73 
74  public:
75 
76  explicit DiffIterator(TBasicIterator begin, TBasicIterator end) :
77  m_prev(begin),
78  m_curr(begin),
79  m_next(begin == end ? begin : ++begin),
80  m_end(end) {
81  }
82 
83  DiffIterator(const DiffIterator&) = default;
84  DiffIterator& operator=(const DiffIterator&) = default;
85 
86  DiffIterator(DiffIterator&&) = default;
87  DiffIterator& operator=(DiffIterator&&) = default;
88 
90  m_prev = std::move(m_curr);
91  m_curr = m_next;
92 
93  if (m_next != m_end) {
94  ++m_next;
95  }
96 
97  return *this;
98  }
99 
101  DiffIterator tmp(*this);
102  operator++();
103  return tmp;
104  }
105 
106  bool operator==(const DiffIterator& rhs) const {
107  return m_curr == rhs.m_curr && m_end == rhs.m_end;
108  }
109 
110  bool operator!=(const DiffIterator& rhs) const {
111  return !(*this == rhs);
112  }
113 
114  reference operator*() const {
115  set_diff();
116  return m_diff;
117  }
118 
119  pointer operator->() const {
120  set_diff();
121  return &m_diff;
122  }
123 
124  }; // class DiffIterator
125 
126 } // namespace osmium
127 
128 #endif // OSMIUM_DIFF_ITERATOR_HPP
void set_diff() const
Definition: diff_iterator.hpp:58
Definition: diff_object.hpp:47
const TBasicIterator m_end
Definition: diff_iterator.hpp:54
bool operator==(const DiffIterator &rhs) const
Definition: diff_iterator.hpp:106
Definition: diff_iterator.hpp:47
pointer operator->() const
Definition: diff_iterator.hpp:119
Namespace for everything in the Osmium library.
Definition: assembler.hpp:55
osmium::DiffObject m_diff
Definition: diff_iterator.hpp:56
osmium::io::InputIterator< osmium::io::Reader > end(osmium::io::Reader &)
Definition: reader_iterator.hpp:45
bool operator!=(const DiffIterator &rhs) const
Definition: diff_iterator.hpp:110
DiffIterator & operator++()
Definition: diff_iterator.hpp:89
TBasicIterator m_prev
Definition: diff_iterator.hpp:50
reference operator*() const
Definition: diff_iterator.hpp:114
DiffIterator operator++(int)
Definition: diff_iterator.hpp:100
DiffIterator & operator=(const DiffIterator &)=default
TBasicIterator m_curr
Definition: diff_iterator.hpp:51
TBasicIterator m_next
Definition: diff_iterator.hpp:52
osmium::io::InputIterator< osmium::io::Reader > begin(osmium::io::Reader &reader)
Definition: reader_iterator.hpp:41
DiffIterator(TBasicIterator begin, TBasicIterator end)
Definition: diff_iterator.hpp:76