summaryrefslogtreecommitdiffstats
path: root/src/corelib/kernel/qiterable.cpp
blob: 5aed1832af54c995ee7966e9c98c43bf622dc44a (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/****************************************************************************
**
** 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$
**
****************************************************************************/

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

QT_BEGIN_NAMESPACE

/*!
    \class QSequentialIterable
    \since 5.2
    \inmodule QtCore
    \brief The QSequentialIterable class is an iterable interface for a container in a QVariant.

    This class allows several methods of accessing the values of a container held within
    a QVariant. An instance of QSequentialIterable can be extracted from a QVariant if it can
    be converted to a QVariantList.

    \snippet code/src_corelib_kernel_qvariant.cpp 9

    The container itself is not copied before iterating over it.

    \sa QVariant
*/

QSequentialIterable::const_iterator::const_iterator(const QSequentialIterable *iterable, void *iterator)
  : m_iterable(iterable), m_iterator(iterator), m_ref(new QAtomicInt(0))
{
    m_ref->ref();
}

/*! \fn QSequentialIterable::const_iterator QSequentialIterable::begin() const

    Returns a QSequentialIterable::const_iterator for the beginning of the container. This
    can be used in stl-style iteration.

    \sa end()
*/
QSequentialIterable::const_iterator QSequentialIterable::begin() const
{
    return const_iterator(this, m_metaSequence.constBegin(m_iterable.constPointer()));
}

/*!
    Returns a QSequentialIterable::const_iterator for the end of the container. This
    can be used in stl-style iteration.

    \sa begin()
*/
QSequentialIterable::const_iterator QSequentialIterable::end() const
{
    return const_iterator(this, m_metaSequence.constEnd(m_iterable.constPointer()));
}

/*!
    Returns the value at position \a idx in the container.
*/
QVariant QSequentialIterable::at(qsizetype idx) const
{
    QVariant v(m_metaSequence.valueMetaType());
    void *dataPtr;
    if (m_metaSequence.valueMetaType() == QMetaType::fromType<QVariant>())
        dataPtr = &v;
    else
        dataPtr = v.data();

    const QMetaSequence metaSequence = m_metaSequence;
    if (metaSequence.canGetValueAtIndex()) {
        metaSequence.valueAtIndex(m_iterable.constPointer(), idx, dataPtr);
    } else if (metaSequence.canGetValueAtConstIterator()) {
        void *iterator = metaSequence.constBegin(m_iterable.constPointer());
        metaSequence.advanceConstIterator(iterator, idx);
        metaSequence.valueAtConstIterator(iterator, dataPtr);
        metaSequence.destroyConstIterator(iterator);
    }

    return v;
}

/*!
    Returns the number of values in the container.
*/
qsizetype QSequentialIterable::size() const
{
    const QMetaSequence metaSequence = m_metaSequence;
    const void *container = m_iterable.constPointer();
    if (metaSequence.hasSize())
        return metaSequence.size(container);
    if (!metaSequence.hasConstIterator())
        return -1;

    const void *begin = metaSequence.constBegin(container);
    const void *end = metaSequence.constEnd(container);
    const qsizetype size = metaSequence.diffConstIterator(end, begin);
    metaSequence.destroyConstIterator(begin);
    metaSequence.destroyConstIterator(end);
    return size;
}

/*!
 * Adds \a value to the container, at \a position, if possible.
 */
void QSequentialIterable::addValue(const QVariant &value, Position position)
{
    QVariant converted;
    const void *valuePtr;
    if (valueMetaType() == QMetaType::fromType<QVariant>()) {
        valuePtr = &value;
    } else if (valueMetaType() == value.metaType()) {
        valuePtr = value.constData();
    } else if (value.canConvert(valueMetaType())) {
        converted = value;
        converted.convert(valueMetaType());
        valuePtr = converted.constData();
    } else {
        converted = QVariant(valueMetaType());
        valuePtr = converted.constData();
    }

    switch (position) {
    case AtBegin:
        if (metaSequence().canAddValueAtBegin())
            metaSequence().addValueAtBegin(mutableIterable(), valuePtr);
        break;
    case AtEnd:
        if (metaSequence().canAddValueAtEnd())
            metaSequence().addValueAtEnd(mutableIterable(), valuePtr);
        break;
    case Unspecified:
        if (metaSequence().canAddValue())
            metaSequence().addValue(mutableIterable(), valuePtr);
        break;
    }
}

/*!
 * Removes a value from the container, at \a position, if possible.
 */
void QSequentialIterable::removeValue(Position position)
{
    switch (position) {
    case AtBegin:
        if (metaSequence().canRemoveValueAtBegin())
            metaSequence().removeValueAtBegin(mutableIterable());
        break;
    case AtEnd:
        if (metaSequence().canRemoveValueAtEnd())
            metaSequence().removeValueAtEnd(mutableIterable());
        break;
    case Unspecified:
        if (metaSequence().canRemoveValue())
            metaSequence().removeValue(mutableIterable());
        break;
    }
}

/*!
    Returns whether it is possible to iterate over the container in forward
    direction. This corresponds to the std::forward_iterator_tag iterator trait
    of the iterator and const_iterator of the container.
*/
bool QSequentialIterable::canForwardIterate() const
{
    return m_metaSequence.hasForwardIterator();
}

/*!
    Returns whether it is possible to iterate over the container in reverse. This
    corresponds to the std::bidirectional_iterator_tag iterator trait of the
    const_iterator of the container.
*/
bool QSequentialIterable::canReverseIterate() const
{
    return m_metaSequence.hasBidirectionalIterator();
}

/*!
    \class QSequentialIterable::const_iterator
    \since 5.2
    \inmodule QtCore
    \brief The QSequentialIterable::const_iterator allows iteration over a container in a QVariant.

    A QSequentialIterable::const_iterator can only be created by a QSequentialIterable instance,
    and can be used in a way similar to other stl-style iterators.

    \snippet code/src_corelib_kernel_qvariant.cpp 9

    \sa QSequentialIterable
*/


/*!
    Destroys the QSequentialIterable::const_iterator.
*/
QSequentialIterable::const_iterator::~const_iterator() {
    if (!m_ref->deref()) {
        m_iterable->m_metaSequence.destroyConstIterator(m_iterator);
        delete m_ref;
    }
}

/*!
    Creates a copy of \a other.
*/
QSequentialIterable::const_iterator::const_iterator(const const_iterator &other)
  : m_iterable(other.m_iterable), m_iterator(other.m_iterator), m_ref(other.m_ref)
{
    m_ref->ref();
}

/*!
    Assigns \a other to this.
*/
QSequentialIterable::const_iterator&
QSequentialIterable::const_iterator::operator=(const const_iterator &other)
{
    if (this == &other)
        return *this;
    other.m_ref->ref();
    if (!m_ref->deref()) {
        m_iterable->m_metaSequence.destroyConstIterator(m_iterator);
        delete m_ref;
    }
    m_iterable = other.m_iterable;
    m_iterator = other.m_iterator;
    m_ref = other.m_ref;
    return *this;
}

/*!
    Returns the current item, converted to a QVariant.
*/
const QVariant QSequentialIterable::const_iterator::operator*() const
{
    QVariant v(m_iterable->m_metaSequence.valueMetaType());
    void *dataPtr;
    if (m_iterable->m_metaSequence.valueMetaType() == QMetaType::fromType<QVariant>())
        dataPtr = &v;
    else
        dataPtr = v.data();
    m_iterable->m_metaSequence.valueAtConstIterator(m_iterator, dataPtr);
    return v;
}

/*!
    Returns \c true if \a other points to the same item as this
    iterator; otherwise returns \c false.

    \sa operator!=()
*/
bool QSequentialIterable::const_iterator::operator==(const const_iterator &other) const
{
    return m_iterable->m_metaSequence.compareConstIterator(
                m_iterator, other.m_iterator);
}

/*!
    Returns \c true if \a other points to a different item than this
    iterator; otherwise returns \c false.

    \sa operator==()
*/
bool QSequentialIterable::const_iterator::operator!=(const const_iterator &other) const
{
    return !m_iterable->m_metaSequence.compareConstIterator(
                m_iterator, other.m_iterator);
}

/*!
    The prefix ++ operator (\c{++it}) advances the iterator to the
    next item in the container and returns an iterator to the new current
    item.

    Calling this function on QSequentialIterable::end() leads to undefined results.

    \sa operator--()
*/
QSequentialIterable::const_iterator &QSequentialIterable::const_iterator::operator++()
{
    m_iterable->m_metaSequence.advanceConstIterator(m_iterator, 1);
    return *this;
}

/*!
    \overload

    The postfix ++ operator (\c{it++}) advances the iterator to the
    next item in the container and returns an iterator to the previously
    current item.
*/
QSequentialIterable::const_iterator QSequentialIterable::const_iterator::operator++(int)
{
    const_iterator result(
                m_iterable,
                m_iterable->m_metaSequence.constBegin(m_iterable->m_iterable.constPointer()));
    m_iterable->m_metaSequence.copyConstIterator(result.m_iterator, m_iterator);
    m_iterable->m_metaSequence.advanceConstIterator(m_iterator, 1);
    return result;
}

/*!
    The prefix -- operator (\c{--it}) makes the preceding item
    current and returns an iterator to the new current item.

    Calling this function on QSequentialIterable::begin() leads to undefined results.

    If the container in the QVariant does not support bi-directional iteration, calling this function
    leads to undefined results.

    \sa operator++(), canReverseIterate()
*/
QSequentialIterable::const_iterator &QSequentialIterable::const_iterator::operator--()
{
    m_iterable->m_metaSequence.advanceConstIterator(m_iterator, -1);
    return *this;
}

/*!
    \overload

    The postfix -- operator (\c{it--}) makes the preceding item
    current and returns an iterator to the previously current item.

    If the container in the QVariant does not support bi-directional iteration, calling this function
    leads to undefined results.

    \sa canReverseIterate()
*/
QSequentialIterable::const_iterator QSequentialIterable::const_iterator::operator--(int)
{
    const_iterator result(
                m_iterable,
                m_iterable->m_metaSequence.constBegin(m_iterable->m_iterable.constPointer()));
    m_iterable->m_metaSequence.copyConstIterator(result.m_iterator, m_iterator);
    m_iterable->m_metaSequence.advanceConstIterator(m_iterator, -1);
    return result;
}

/*!
    Advances the iterator by \a j items.

    \sa operator-=(), operator+()
*/
QSequentialIterable::const_iterator &QSequentialIterable::const_iterator::operator+=(int j)
{
    m_iterable->m_metaSequence.advanceConstIterator(m_iterator, j);
    return *this;
}

/*!
    Makes the iterator go back by \a j items.

    If the container in the QVariant does not support bi-directional iteration, calling this function
    leads to undefined results.

    \sa operator+=(), operator-(), canReverseIterate()
*/
QSequentialIterable::const_iterator &QSequentialIterable::const_iterator::operator-=(int j)
{
    m_iterable->m_metaSequence.advanceConstIterator(m_iterator, -j);
    return *this;
}

/*!
    Returns an iterator to the item at \a j positions forward from
    this iterator.

    \sa operator-(), operator+=()
*/
QSequentialIterable::const_iterator QSequentialIterable::const_iterator::operator+(int j) const
{
    const_iterator result(
                m_iterable,
                m_iterable->m_metaSequence.constBegin(m_iterable->m_iterable.constPointer()));
    m_iterable->m_metaSequence.copyConstIterator(result.m_iterator, m_iterator);
    m_iterable->m_metaSequence.advanceConstIterator(result.m_iterator, j);
    return result;
}

/*!
    Returns an iterator to the item at \a j positions backward from
    this iterator.

    If the container in the QVariant does not support bi-directional iteration, calling this function
    leads to undefined results.

    \sa operator+(), operator-=(), canReverseIterate()
*/
QSequentialIterable::const_iterator QSequentialIterable::const_iterator::operator-(int j) const
{
    const_iterator result(
                m_iterable,
                m_iterable->m_metaSequence.constBegin(m_iterable->m_iterable.constPointer()));
    m_iterable->m_metaSequence.copyConstIterator(result.m_iterator, m_iterator);
    m_iterable->m_metaSequence.advanceConstIterator(result.m_iterator, -j);
    return result;
}

QT_END_NAMESPACE