about summary refs log tree commit diff
path: root/immer/detail/iterator_facade.hpp
blob: ffc237943e098d5575991f378ff547c01da1e2f5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//
// immer: immutable data structures for C++
// Copyright (C) 2016, 2017, 2018 Juan Pedro Bolivar Puente
//
// This software is distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE or copy at http://boost.org/LICENSE_1_0.txt
//

#pragma once

#include <cstddef>
#include <iterator>
#include <type_traits>

namespace immer {
namespace detail {

struct iterator_core_access
{
    template <typename T>
    static decltype(auto) dereference(T&& x)
    {
        return x.dereference();
    }

    template <typename T>
    static decltype(auto) increment(T&& x)
    {
        return x.increment();
    }

    template <typename T>
    static decltype(auto) decrement(T&& x)
    {
        return x.decrement();
    }

    template <typename T1, typename T2>
    static decltype(auto) equal(T1&& x1, T2&& x2)
    {
        return x1.equal(x2);
    }

    template <typename T, typename D>
    static decltype(auto) advance(T&& x, D d)
    {
        return x.advance(d);
    }

    template <typename T1, typename T2>
    static decltype(auto) distance_to(T1&& x1, T2&& x2)
    {
        return x1.distance_to(x2);
    }
};

/*!
 * Minimalistic reimplementation of boost::iterator_facade
 */
template <typename DerivedT,
          typename IteratorCategoryT,
          typename T,
          typename ReferenceT      = T&,
          typename DifferenceTypeT = std::ptrdiff_t,
          typename PointerT        = T*>
class iterator_facade
{
public:
    using iterator_category = IteratorCategoryT;
    using value_type        = T;
    using difference_type   = DifferenceTypeT;
    using pointer           = PointerT;
    using reference         = ReferenceT;

protected:
    using access_t = iterator_core_access;

    constexpr static auto is_random_access =
        std::is_base_of<std::random_access_iterator_tag,
                        IteratorCategoryT>::value;
    constexpr static auto is_bidirectional =
        std::is_base_of<std::bidirectional_iterator_tag,
                        IteratorCategoryT>::value;

    class reference_proxy
    {
        friend iterator_facade;
        DerivedT iter_;

        reference_proxy(DerivedT iter)
            : iter_{std::move(iter)}
        {}

    public:
        operator ReferenceT() const { return *iter_; }
    };

    const DerivedT& derived() const
    {
        static_assert(std::is_base_of<iterator_facade, DerivedT>::value,
                      "must pass a derived thing");
        return *static_cast<const DerivedT*>(this);
    }
    DerivedT& derived()
    {
        static_assert(std::is_base_of<iterator_facade, DerivedT>::value,
                      "must pass a derived thing");
        return *static_cast<DerivedT*>(this);
    }

public:
    ReferenceT operator*() const { return access_t::dereference(derived()); }
    PointerT operator->() const { return &access_t::dereference(derived()); }
    reference_proxy operator[](DifferenceTypeT n) const
    {
        static_assert(is_random_access, "");
        return derived() + n;
    }

    bool operator==(const DerivedT& rhs) const
    {
        return access_t::equal(derived(), rhs);
    }
    bool operator!=(const DerivedT& rhs) const
    {
        return !access_t::equal(derived(), rhs);
    }

    DerivedT& operator++()
    {
        access_t::increment(derived());
        return derived();
    }
    DerivedT operator++(int)
    {
        auto tmp = derived();
        access_t::increment(derived());
        return tmp;
    }

    DerivedT& operator--()
    {
        static_assert(is_bidirectional || is_random_access, "");
        access_t::decrement(derived());
        return derived();
    }
    DerivedT operator--(int)
    {
        static_assert(is_bidirectional || is_random_access, "");
        auto tmp = derived();
        access_t::decrement(derived());
        return tmp;
    }

    DerivedT& operator+=(DifferenceTypeT n)
    {
        access_t::advance(derived(), n);
        return derived();
    }
    DerivedT& operator-=(DifferenceTypeT n)
    {
        access_t::advance(derived(), -n);
        return derived();
    }

    DerivedT operator+(DifferenceTypeT n) const
    {
        static_assert(is_random_access, "");
        auto tmp = derived();
        return tmp += n;
    }
    friend DerivedT operator+(DifferenceTypeT n, const DerivedT& i)
    {
        static_assert(is_random_access, "");
        return i + n;
    }
    DerivedT operator-(DifferenceTypeT n) const
    {
        static_assert(is_random_access, "");
        auto tmp = derived();
        return tmp -= n;
    }
    DifferenceTypeT operator-(const DerivedT& rhs) const
    {
        static_assert(is_random_access, "");
        return access_t::distance_to(rhs, derived());
    }

    bool operator<(const DerivedT& rhs) const
    {
        static_assert(is_random_access, "");
        return access_t::distance_to(derived(), rhs) > 0;
    }
    bool operator<=(const DerivedT& rhs) const
    {
        static_assert(is_random_access, "");
        return access_t::distance_to(derived(), rhs) >= 0;
    }
    bool operator>(const DerivedT& rhs) const
    {
        static_assert(is_random_access, "");
        return access_t::distance_to(derived(), rhs) < 0;
    }
    bool operator>=(const DerivedT& rhs) const
    {
        static_assert(is_random_access, "");
        return access_t::distance_to(derived(), rhs) <= 0;
    }
};

} // namespace detail
} // namespace immer