aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/utils/indexedcontainerproxyconstiterator.h
blob: 53008a3d453a29aa3d241ccd22273301a78668a1 (plain)
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
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#pragma once

#include "utils_global.h"

#include <QtGlobal>

#include <type_traits>

namespace Utils {

/// A class useful for the implementation of the -> operator of proxy iterators.
template <typename Reference>
struct ArrowProxy {
    Reference r;
    Reference *operator->() {
        return &r;
    }
};

/// Random-access const iterator over elements of a container providing an overloaded operator[]
/// (which may return a proxy object, like std::vector<bool>, rather than a reference).
template <typename Container>
class IndexedContainerProxyConstIterator
{
public:
    typedef std::random_access_iterator_tag iterator_category;
    typedef typename std::make_signed<typename Container::size_type>::type difference_type;
    typedef typename Container::value_type value_type;
    typedef value_type reference;
    typedef ArrowProxy<reference> pointer;
    typedef typename Container::size_type size_type;

    IndexedContainerProxyConstIterator()
      : m_container(nullptr), m_index(0)
    {}

    IndexedContainerProxyConstIterator(const Container &container, size_type index)
      : m_container(&container), m_index(index)
    {}

    reference operator*() const
    {
        Q_ASSERT(m_container);
        return (*m_container)[m_index];
    }

    pointer operator->() const
    {
        Q_ASSERT(m_container);
        return pointer{(*m_container)[m_index]};
    }

    reference operator[](difference_type j) const
    {
        Q_ASSERT(m_container);
        return (*m_container)[m_index + j];
    }

    bool operator==(const IndexedContainerProxyConstIterator &other) const
    {
        Q_ASSERT(m_container == other.m_container);
        return m_index == other.m_index;
    }

    bool operator!=(const IndexedContainerProxyConstIterator &other) const
    {
        Q_ASSERT(m_container == other.m_container);
        return m_index != other.m_index;
    }

    bool operator<(const IndexedContainerProxyConstIterator &other) const
    {
        Q_ASSERT(m_container == other.m_container);
        return m_index < other.m_index;
    }

    bool operator<=(const IndexedContainerProxyConstIterator &other) const
    {
        Q_ASSERT(m_container == other.m_container);
        return m_index <= other.m_index;
    }

    bool operator>(const IndexedContainerProxyConstIterator &other) const
    {
        Q_ASSERT(m_container == other.m_container);
        return m_index > other.m_index;
    }

    bool operator>=(const IndexedContainerProxyConstIterator &other) const
    {
        Q_ASSERT(m_container == other.m_container);
        return m_index >= other.m_index;
    }

    IndexedContainerProxyConstIterator &operator++()
    {
        ++m_index;
        return *this;
    }

    IndexedContainerProxyConstIterator operator++(int)
    {
        IndexedContainerProxyConstIterator copy(*this);
        ++m_index;
        return copy;
    }

    IndexedContainerProxyConstIterator &operator--()
    {
        --m_index;
        return *this;
    }

    IndexedContainerProxyConstIterator operator--(int)
    {
        IndexedContainerProxyConstIterator copy(*this);
        --m_index;
        return copy;
    }

    IndexedContainerProxyConstIterator &operator+=(difference_type j)
    {
        m_index += j;
        return *this;
    }

    IndexedContainerProxyConstIterator &operator-=(difference_type j)
    {
        m_index -= j;
        return *this;
    }

    IndexedContainerProxyConstIterator operator+(difference_type j) const
    {
        IndexedContainerProxyConstIterator result(*this);
        result += j;
        return result;
    }

    IndexedContainerProxyConstIterator operator-(difference_type j) const
    {
        IndexedContainerProxyConstIterator result(*this);
        result -= j;
        return result;
    }

    friend IndexedContainerProxyConstIterator operator+(
            difference_type j, const IndexedContainerProxyConstIterator &other)
    {
        return other + j;
    }

    difference_type operator-(const IndexedContainerProxyConstIterator &other) const
    {
        return static_cast<difference_type>(m_index) - static_cast<difference_type>(other.m_index);
    }

private:
    const Container *m_container;
    size_type m_index;
};

} // namespace Utils