summaryrefslogtreecommitdiffstats
path: root/src/corelib/kernel/qassociativeiterable.h
blob: 6fe0ff670ca052851fe6d892a73978b4d5ac88c5 (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
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
/****************************************************************************
**
** Copyright (C) 2020 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef QASSOCIATIVEITERABLE_H
#define QASSOCIATIVEITERABLE_H

#include <QtCore/qiterable.h>
#include <QtCore/qvariant.h>

QT_BEGIN_NAMESPACE

class Q_CORE_EXPORT QAssociativeIterator : public QIterator<QMetaAssociation>
{
public:
    using key_type = QVariant;
    using mapped_type = QVariant;
    using reference = QVariantRef<QAssociativeIterator>;
    using pointer = QVariantPointer<QAssociativeIterator>;

    QAssociativeIterator(QIterator &&it)
        : QIterator(std::move(it))
    {}

    QVariant key() const;
    QVariantRef<QAssociativeIterator> value() const;

    QVariantRef<QAssociativeIterator> operator*() const;
    QVariantPointer<QAssociativeIterator> operator->() const;
};

class Q_CORE_EXPORT QAssociativeConstIterator : public QConstIterator<QMetaAssociation>
{
public:
    using key_type = QVariant;
    using mapped_type = QVariant;
    using reference = const QVariant &;
    using pointer = QVariantConstPointer;

    QAssociativeConstIterator(QConstIterator &&it)
        : QConstIterator(std::move(it))
    {}

    QVariant key() const;
    QVariant value() const;

    QVariant operator*() const;
    QVariantConstPointer operator->() const;
};

class Q_CORE_EXPORT QAssociativeIterable : public QIterable<QMetaAssociation>
{
public:
    using iterator = QTaggedIterator<QAssociativeIterator, void>;
    using const_iterator = QTaggedIterator<QAssociativeConstIterator, void>;

    using RandomAccessIterator = QTaggedIterator<iterator, std::random_access_iterator_tag>;
    using BidirectionalIterator = QTaggedIterator<iterator, std::bidirectional_iterator_tag>;
    using ForwardIterator = QTaggedIterator<iterator, std::forward_iterator_tag>;
    using InputIterator = QTaggedIterator<iterator, std::input_iterator_tag>;

    using RandomAccessConstIterator = QTaggedIterator<const_iterator, std::random_access_iterator_tag>;
    using BidirectionalConstIterator = QTaggedIterator<const_iterator, std::bidirectional_iterator_tag>;
    using ForwardConstIterator = QTaggedIterator<const_iterator, std::forward_iterator_tag>;
    using InputConstIterator = QTaggedIterator<const_iterator, std::input_iterator_tag>;

    template<class T>
    QAssociativeIterable(const T *p)
        : QIterable(QMetaAssociation::fromContainer<T>(), p)
    {
    }

    template<class T>
    QAssociativeIterable(T *p)
        : QIterable(QMetaAssociation::fromContainer<T>(), p)
    {
    }

    QAssociativeIterable()
        : QIterable(QMetaAssociation(), nullptr)
    {
    }

    template<typename Pointer>
    QAssociativeIterable(const QMetaAssociation &metaAssociation, Pointer iterable)
        : QIterable(metaAssociation, iterable)
    {
    }

    // ### Qt7: Pass QMetaType as value rather than const ref.
    QAssociativeIterable(const QMetaAssociation &metaAssociation, const QMetaType &metaType,
                         void *iterable)
        : QIterable(metaAssociation, metaType.alignOf(), iterable)
    {
    }

    // ### Qt7: Pass QMetaType as value rather than const ref.
    QAssociativeIterable(const QMetaAssociation &metaAssociation, const QMetaType &metaType,
                         const void *iterable)
        : QIterable(metaAssociation, metaType.alignOf(), iterable)
    {
    }

    QAssociativeIterable(QIterable<QMetaAssociation> &&other) : QIterable(std::move(other)) {}

    QAssociativeIterable &operator=(QIterable<QMetaAssociation> &&other)
    {
        QIterable::operator=(std::move(other));
        return *this;
    }

    const_iterator begin() const { return constBegin(); }
    const_iterator end() const { return constEnd(); }

    const_iterator constBegin() const { return const_iterator(QIterable::constBegin()); }
    const_iterator constEnd() const { return const_iterator(QIterable::constEnd()); }

    iterator mutableBegin() { return iterator(QIterable::mutableBegin()); }
    iterator mutableEnd() { return iterator(QIterable::mutableEnd()); }

    const_iterator find(const QVariant &key) const;
    const_iterator constFind(const QVariant &key) const { return find(key); }
    iterator mutableFind(const QVariant &key);

    bool containsKey(const QVariant &key);
    void insertKey(const QVariant &key);
    void removeKey(const QVariant &key);

    QVariant value(const QVariant &key) const;
    void setValue(const QVariant &key, const QVariant &mapped);
};

template<>
inline QVariantRef<QAssociativeIterator>::operator QVariant() const
{
    if (m_pointer == nullptr)
        return QVariant();

    const auto metaAssociation = m_pointer->metaContainer();
    const QMetaType metaType(metaAssociation.mappedMetaType());
    if (!metaType.isValid())
        return m_pointer->key();

    QVariant v(metaType);
    metaAssociation.mappedAtIterator(m_pointer->constIterator(),
                                     metaType == QMetaType::fromType<QVariant>() ? &v : v.data());
    return v;
}

template<>
inline QVariantRef<QAssociativeIterator> &QVariantRef<QAssociativeIterator>::operator=(
        const QVariant &value)
{
    if (m_pointer == nullptr)
        return *this;

    const auto metaAssociation = m_pointer->metaContainer();
    const QMetaType metaType(metaAssociation.mappedMetaType());
    if (metaType.isValid()) {
        QtPrivate::QVariantTypeCoercer coercer;
        metaAssociation.setMappedAtIterator(m_pointer->constIterator(),
                                            coercer.coerce(value, metaType));
    }

    return *this;
}

Q_DECLARE_TYPEINFO(QAssociativeIterable, Q_RELOCATABLE_TYPE);
Q_DECLARE_TYPEINFO(QAssociativeIterable::iterator, Q_RELOCATABLE_TYPE);
Q_DECLARE_TYPEINFO(QAssociativeIterable::const_iterator, Q_RELOCATABLE_TYPE);

QT_END_NAMESPACE

#endif // QASSOCIATIVEITERABLE_H