diff options
Diffstat (limited to 'src/qml/qml/qqmllist.h')
-rw-r--r-- | src/qml/qml/qqmllist.h | 100 |
1 files changed, 48 insertions, 52 deletions
diff --git a/src/qml/qml/qqmllist.h b/src/qml/qml/qqmllist.h index abf4ec6ebf..6f2c077764 100644 --- a/src/qml/qml/qqmllist.h +++ b/src/qml/qml/qqmllist.h @@ -1,49 +1,15 @@ -/**************************************************************************** -** -** 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_H #define QQMLLIST_H #include <QtQml/qtqmlglobal.h> + +#include <QtCore/qcontainerinfo.h> #include <QtCore/qlist.h> +#include <QtCore/qmetatype.h> #include <QtCore/qvariant.h> -#include <QtCore/QMetaType> QT_BEGIN_NAMESPACE @@ -54,11 +20,11 @@ struct QMetaObject; #define QML_LIST_PROPERTY_ASSIGN_BEHAVIOR_REPLACE_IF_NOT_DEFAULT Q_CLASSINFO("QML.ListPropertyAssignBehavior", "ReplaceIfNotDefault") #define QML_LIST_PROPERTY_ASSIGN_BEHAVIOR_REPLACE Q_CLASSINFO("QML.ListPropertyAssignBehavior", "Replace") -#ifndef QQMLLISTPROPERTY -#define QQMLLISTPROPERTY template<typename T> class QQmlListProperty { public: + using value_type = T*; + using AppendFunction = void (*)(QQmlListProperty<T> *, T *); using CountFunction = qsizetype (*)(QQmlListProperty<T> *); using AtFunction = T *(*)(QQmlListProperty<T> *, qsizetype); @@ -122,24 +88,45 @@ public: ReplaceFunction replace = nullptr; RemoveLastFunction removeLast = nullptr; + template<typename List> + List toList() + { + if constexpr (std::is_same_v<List, QList<T *>>) { + if (append == qlist_append) + return *static_cast<QList<T *> *>(data); + } + + const qsizetype size = count(this); + + List result; + if constexpr (QContainerInfo::has_reserve_v<List>) + result.reserve(size); + + static_assert(QContainerInfo::has_push_back_v<List>); + for (qsizetype i = 0; i < size; ++i) + result.push_back(at(this, i)); + + return result; + } + private: static void qlist_append(QQmlListProperty *p, T *v) { - reinterpret_cast<QList<T *> *>(p->data)->append(v); + static_cast<QList<T *> *>(p->data)->append(v); } static qsizetype qlist_count(QQmlListProperty *p) { - return reinterpret_cast<QList<T *> *>(p->data)->count(); + return static_cast<QList<T *> *>(p->data)->size(); } static T *qlist_at(QQmlListProperty *p, qsizetype idx) { - return reinterpret_cast<QList<T *> *>(p->data)->at(idx); + return static_cast<QList<T *> *>(p->data)->at(idx); } static void qlist_clear(QQmlListProperty *p) { - return reinterpret_cast<QList<T *> *>(p->data)->clear(); + return static_cast<QList<T *> *>(p->data)->clear(); } static void qlist_replace(QQmlListProperty *p, qsizetype idx, T *v) { - return reinterpret_cast<QList<T *> *>(p->data)->replace(idx, v); + return static_cast<QList<T *> *>(p->data)->replace(idx, v); } static void qlist_removeLast(QQmlListProperty *p) { - return reinterpret_cast<QList<T *> *>(p->data)->removeLast(); + return static_cast<QList<T *> *>(p->data)->removeLast(); } static void qslow_replace(QQmlListProperty<T> *list, qsizetype idx, T *v) @@ -154,7 +141,7 @@ private: for (qsizetype i = 0; i < length; ++i) stash.append(i == idx ? v : list->at(list, i)); list->clear(list); - for (T *item : qAsConst(stash)) + for (T *item : std::as_const(stash)) list->append(list, item); } else { stash.reserve(length - idx - 1); @@ -185,11 +172,10 @@ private: for (qsizetype i = 0; i < length; ++i) stash.append(list->at(list, i)); list->clear(list); - for (T *item : qAsConst(stash)) + for (T *item : std::as_const(stash)) list->append(list, item); } }; -#endif class QQmlEngine; class QQmlListReferencePrivate; @@ -197,8 +183,17 @@ class Q_QML_EXPORT QQmlListReference { public: QQmlListReference(); - explicit QQmlListReference(const QVariant &variant, QQmlEngine *engine = nullptr); - QQmlListReference(QObject *, const char *property, QQmlEngine * = nullptr); + +#if QT_DEPRECATED_SINCE(6, 4) + QT_DEPRECATED_X("Drop the QQmlEngine* argument") + QQmlListReference(const QVariant &variant, [[maybe_unused]] QQmlEngine *engine); + + QT_DEPRECATED_X("Drop the QQmlEngine* argument") + QQmlListReference(QObject *o, const char *property, [[maybe_unused]] QQmlEngine *engine); +#endif + + explicit QQmlListReference(const QVariant &variant); + QQmlListReference(QObject *o, const char *property); QQmlListReference(const QQmlListReference &); QQmlListReference &operator=(const QQmlListReference &); ~QQmlListReference(); @@ -222,6 +217,7 @@ public: QObject *at(qsizetype) const; bool clear() const; qsizetype count() const; + qsizetype size() const { return count(); } bool replace(qsizetype, QObject *) const; bool removeLast() const; bool operator==(const QQmlListReference &other) const {return d == other.d;} |