diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2020-09-21 10:40:27 +0200 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2020-10-02 13:21:09 +0200 |
commit | 45594322fe91eadcd9b2d7b1d76c1a6662bc1472 (patch) | |
tree | d14e743f40351ca7a660984616b2500aa83032f5 /src/qml/qml/qqmlprivate.h | |
parent | d621027babff9a30d56ab6af871a465108c9eaba (diff) |
Use factory functions and ctors for creating value types
As you can extend value types with QML_EXTENDED we may as well allow a
factory function in the extended type. Furthermore, if the original type
allows construction from QJSValue, we may just use that. In turn, we can
get rid of the value type providers now.
Change-Id: I9124ea47537eab6c33d7451080ab2fff942eaa7b
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/qml/qml/qqmlprivate.h')
-rw-r--r-- | src/qml/qml/qqmlprivate.h | 53 |
1 files changed, 52 insertions, 1 deletions
diff --git a/src/qml/qml/qqmlprivate.h b/src/qml/qml/qqmlprivate.h index 00d277a9f7..6639ecbd26 100644 --- a/src/qml/qml/qqmlprivate.h +++ b/src/qml/qml/qqmlprivate.h @@ -58,6 +58,7 @@ #include <QtQml/qqmlparserstatus.h> #include <QtQml/qqmllist.h> #include <QtQml/qqmlpropertyvaluesource.h> +#include <QtQml/qjsvalue.h> #include <QtCore/qglobal.h> #include <QtCore/qvariant.h> @@ -109,7 +110,6 @@ public: }; -class QJSValue; class QJSEngine; class QQmlEngine; class QQmlCustomParser; @@ -163,6 +163,7 @@ namespace QQmlPrivate using CreateIntoFunction = void (*)(void *, void *); using CreateSingletonFunction = QObject *(*)(QQmlEngine *, QJSEngine *); using CreateParentFunction = QObject *(*)(QObject *); + using CreateValueTypeFunction = QVariant (*)(const QJSValue &); template<typename T, bool Constructible = isConstructible<T>()> struct Constructors; @@ -211,6 +212,51 @@ namespace QQmlPrivate static const QMetaObject *staticMetaObject() { return &T::staticMetaObject; } }; + template<typename F, typename Result = void> + struct ValueTypeFactory + { + static constexpr const Result (*create)(const QJSValue &) = nullptr; + }; + + template<typename F> + struct ValueTypeFactory<F, std::void_t<decltype(F::create(QJSValue()))>> + { + static decltype(F::create(QJSValue())) create(const QJSValue ¶ms) + { + return F::create(params); + } + }; + + template<typename T, typename F, + bool HasCtor = std::is_constructible_v<T, QJSValue>, + bool HasFactory = std::is_constructible_v< + QVariant, decltype(ValueTypeFactory<F>::create(QJSValue()))>> + struct ValueType; + + template<typename T, typename F> + struct ValueType<T, F, false, false> + { + static constexpr const CreateValueTypeFunction create = nullptr; + }; + + template<typename T, typename F, bool HasCtor> + struct ValueType<T, F, HasCtor, true> + { + static QVariant create(const QJSValue ¶ms) + { + return F::create(params); + } + }; + + template<typename T, typename F> + struct ValueType<T, F, true, false> + { + static QVariant create(const QJSValue ¶ms) + { + return QVariant::fromValue(T(params)); + } + }; + template<class From, class To, int N> struct StaticCastSelectorClass { @@ -348,6 +394,8 @@ namespace QQmlPrivate void *userdata; QString noCreationReason; + QVariant (*createValueType)(const QJSValue &); + const char *uri; QTypeRevision version; const char *elementName; @@ -378,6 +426,8 @@ namespace QQmlPrivate void (*create)(void *, void *); void *userdata; + QVariant (*createValueType)(const QJSValue &); + const char *uri; QTypeRevision version; @@ -676,6 +726,7 @@ namespace QQmlPrivate QmlMetaType<T>::list(), int(sizeof(T)), Constructors<T>::createInto, nullptr, + ValueType<T, E>::create, uri, QTypeRevision::fromMajorVersion(versionMajor), |