1 #ifndef OSMIUM_DIFF_ITERATOR_HPP
2 #define OSMIUM_DIFF_ITERATOR_HPP
3 
4 /*
5 
6 This file is part of Osmium (https://osmcode.org/libosmium).
7 
8 Copyright 2013-2021 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 <osmium/osm/diff_object.hpp>
37 
38 #include <cassert>
39 #include <cstddef>
40 #include <iterator>
41 #include <type_traits>
42 #include <utility>
43 
44 namespace osmium {
45 
46     class OSMObject;
47 
48     /**
49      * An input iterator wrapping any iterator over OSMObjects. When
50      * dereferenced it will yield DiffObject objects pointing to the
51      * underlying OSMObjects.
52      *
53      * Note that this class uses a mutable member variable internally.
54      * It can not be used safely in multiple threads!
55      */
56     template <typename TBasicIterator>
57     class DiffIterator {
58 
59         static_assert(std::is_base_of<osmium::OSMObject, typename TBasicIterator::value_type>::value, "TBasicIterator::value_type must derive from osmium::OSMObject");
60 
61         TBasicIterator m_prev;
62         TBasicIterator m_curr;
63         TBasicIterator m_next;
64 
65         const TBasicIterator m_end;
66 
67         mutable osmium::DiffObject m_diff;
68 
set_diff() const69         void set_diff() const noexcept {
70             assert(m_curr != m_end);
71 
72             const bool use_curr_for_prev =                    m_prev->type() != m_curr->type() || m_prev->id() != m_curr->id();
73             const bool use_curr_for_next = m_next == m_end || m_next->type() != m_curr->type() || m_next->id() != m_curr->id();
74 
75             m_diff = osmium::DiffObject{
76                 *(use_curr_for_prev ? m_curr : m_prev),
77                 *m_curr,
78                 *(use_curr_for_next ? m_curr : m_next)
79             };
80         }
81 
82     public:
83 
84         using iterator_category = std::input_iterator_tag;
85         using value_type        = const osmium::DiffObject;
86         using difference_type   = std::ptrdiff_t;
87         using pointer           = value_type*;
88         using reference         = value_type&;
89 
DiffIterator(TBasicIterator begin,TBasicIterator end)90         DiffIterator(TBasicIterator begin, TBasicIterator end) :
91             m_prev(begin),
92             m_curr(begin),
93             m_next(begin == end ? begin : ++begin),
94             m_end(std::move(end)),
95             m_diff() {
96         }
97 
operator ++()98         DiffIterator& operator++() {
99             m_prev = std::move(m_curr);
100             m_curr = m_next;
101 
102             if (m_next != m_end) {
103                 ++m_next;
104             }
105 
106             return *this;
107         }
108 
operator ++(int)109         DiffIterator operator++(int) {
110             DiffIterator tmp{*this};
111             operator++();
112             return tmp;
113         }
114 
operator ==(const DiffIterator & rhs) const115         bool operator==(const DiffIterator& rhs) const noexcept {
116             return m_curr == rhs.m_curr && m_end == rhs.m_end;
117         }
118 
operator !=(const DiffIterator & rhs) const119         bool operator!=(const DiffIterator& rhs) const noexcept {
120             return !(*this == rhs);
121         }
122 
operator *() const123         reference operator*() const noexcept {
124             set_diff();
125             return m_diff;
126         }
127 
operator ->() const128         pointer operator->() const noexcept {
129             set_diff();
130             return &m_diff;
131         }
132 
133     }; // class DiffIterator
134 
135     /**
136      * Create a DiffIterator based on the given iterators.
137      */
138     template <typename TBasicIterator>
make_diff_iterator(TBasicIterator begin,TBasicIterator end)139     inline DiffIterator<TBasicIterator> make_diff_iterator(TBasicIterator begin,
140                                                            TBasicIterator end) {
141         return DiffIterator<TBasicIterator>{begin, end};
142     }
143 
144 } // namespace osmium
145 
146 #endif // OSMIUM_DIFF_ITERATOR_HPP
147