diff options
Diffstat (limited to 'src/qml/qml/qqmllist_p.h')
-rw-r--r-- | src/qml/qml/qqmllist_p.h | 230 |
1 files changed, 189 insertions, 41 deletions
diff --git a/src/qml/qml/qqmllist_p.h b/src/qml/qml/qqmllist_p.h index e182ced51d..642b3c3db1 100644 --- a/src/qml/qml/qqmllist_p.h +++ b/src/qml/qml/qqmllist_p.h @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtQml 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$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #ifndef QQMLLIST_P_H #define QQMLLIST_P_H @@ -53,6 +17,10 @@ #include "qqmllist.h" #include "qqmlmetaobject_p.h" +#include "qqmlmetatype_p.h" +#include <QtQml/private/qbipointer_p.h> + +#include <QtCore/qpointer.h> QT_BEGIN_NAMESPACE @@ -61,12 +29,11 @@ class QQmlListReferencePrivate public: QQmlListReferencePrivate(); - static QQmlListReference init(const QQmlListProperty<QObject> &, int, QQmlEngine *); + static QQmlListReference init(const QQmlListProperty<QObject> &, QMetaType); QPointer<QObject> object; - QQmlMetaObject elementType; QQmlListProperty<QObject> property; - int propertyType; + QMetaType propertyType; void addref(); void release(); @@ -75,8 +42,189 @@ public: static inline QQmlListReferencePrivate *get(QQmlListReference *ref) { return ref->d; } + + const QMetaObject *elementType() + { + if (!m_elementType) { + m_elementType = QQmlMetaType::rawMetaObjectForType( + QQmlMetaType::listValueType(propertyType)).metaObject(); + } + + return m_elementType; + } + +private: + const QMetaObject *m_elementType = nullptr; +}; + +template<typename T> +class QQmlListIterator { +public: + using difference_type = qsizetype; + using iterator_category = std::random_access_iterator_tag; + using value_type = T*; + + class reference + { + public: + explicit reference(const QQmlListIterator *iter) : m_iter(iter) {} + reference(const reference &) = default; + reference(reference &&) = default; + ~reference() = default; + + operator T *() const + { + if (m_iter == nullptr) + return nullptr; + return m_iter->m_list->at(m_iter->m_list, m_iter->m_i); + } + + reference &operator=(T *value) { + m_iter->m_list->replace(m_iter->m_list, m_iter->m_i, value); + return *this; + } + + reference &operator=(const reference &value) { return operator=((T *)(value)); } + reference &operator=(reference &&value) { return operator=((T *)(value)); } + + friend void swap(reference a, reference b) + { + T *tmp = a; + a = b; + b = std::move(tmp); + } + private: + const QQmlListIterator *m_iter; + }; + + class pointer + { + public: + explicit pointer(const QQmlListIterator *iter) : m_iter(iter) {} + reference operator*() const { return reference(m_iter); } + QQmlListIterator operator->() const { return *m_iter; } + + private: + const QQmlListIterator *m_iter; + }; + + QQmlListIterator() = default; + QQmlListIterator(QQmlListProperty<T> *list, qsizetype i) : m_list(list), m_i(i) {} + + QQmlListIterator &operator++() + { + ++m_i; + return *this; + } + + QQmlListIterator operator++(int) + { + QQmlListIterator result = *this; + ++m_i; + return result; + } + + QQmlListIterator &operator--() + { + --m_i; + return *this; + } + + QQmlListIterator operator--(int) + { + QQmlListIterator result = *this; + --m_i; + return result; + } + + QQmlListIterator &operator+=(qsizetype j) + { + m_i += j; + return *this; + } + + QQmlListIterator &operator-=(qsizetype j) + { + m_i -= j; + return *this; + } + + QQmlListIterator operator+(qsizetype j) + { + return QQmlListIterator(m_list, m_i + j); + } + + QQmlListIterator operator-(qsizetype j) + { + return QQmlListIterator(m_list, m_i - j); + } + + reference operator*() const + { + return reference(this); + } + + pointer operator->() const + { + return pointer(this); + } + +private: + friend inline bool operator==(const QQmlListIterator &a, const QQmlListIterator &b) + { + return a.m_list == b.m_list && a.m_i == b.m_i; + } + + friend inline bool operator!=(const QQmlListIterator &a, const QQmlListIterator &b) + { + return a.m_list != b.m_list || a.m_i != b.m_i; + } + + friend inline bool operator<(const QQmlListIterator &i, const QQmlListIterator &j) + { + return i - j < 0; + } + + friend inline bool operator>=(const QQmlListIterator &i, const QQmlListIterator &j) + { + return !(i < j); + } + + friend inline bool operator>(const QQmlListIterator &i, const QQmlListIterator &j) + { + return i - j > 0; + } + + friend inline bool operator<=(const QQmlListIterator &i, const QQmlListIterator &j) + { + return !(i > j); + } + + friend inline QQmlListIterator operator+(qsizetype i, const QQmlListIterator &j) + { + return j + i; + } + + friend inline qsizetype operator-(const QQmlListIterator &i, const QQmlListIterator &j) + { + return i.m_i - j.m_i; + } + + QQmlListProperty<T> *m_list = nullptr; + qsizetype m_i = 0; }; +template<typename T> +QQmlListIterator<T> begin(QQmlListProperty<T> &list) +{ + return QQmlListIterator<T>(&list, 0); +} + +template<typename T> +QQmlListIterator<T> end(QQmlListProperty<T> &list) +{ + return QQmlListIterator<T>(&list, list.count(&list)); +} QT_END_NAMESPACE |