From a9e8034e8c9207876d97ee98f33f0e8006d12d8d Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 1 Mar 2023 22:21:45 +0100 Subject: [PATCH] Implement [variant.get] for QVariant [2/2]: get() This second patch of the series implements get(). Unlike other get() implementations in Qt, don't use my trick with the constrained friend free function here. Instead, provide the four overloads manually, like mandated by the standard library for std::variant (and, indeed, tuple), such that these functions can also be used on subclasses of QVariant. [ChangeLog][QtCore][QVariant] Implemented the type-based std::variant access protocol (get()/get_if()) to allow easier access to the contained element than the previous solution of casting data(), as well as to allow generic code to treat QVariant and std::variant the same. The holds_alternative() function is not provided, since it's the same as get_if != nullptr. The index-based variant access functions (get()/get_if()) are also not provided, because, unlike std::variant, QVariant does not have a bounded number of alternative types, and QMetaType IDs are not (all) compile-time constants. Fixes: QTBUG-111598 Change-Id: Id7bc41f7d91761b3483ec5604f1a4685c8079844 Reviewed-by: Fabian Kosmale --- src/corelib/kernel/qvariant.cpp | 19 ++++ src/corelib/kernel/qvariant.h | 22 ++++- .../corelib/kernel/qvariant/tst_qvariant.cpp | 89 ++++++++++++++++++- 3 files changed, 128 insertions(+), 2 deletions(-) diff --git a/src/corelib/kernel/qvariant.cpp b/src/corelib/kernel/qvariant.cpp index 704b46e1c1..d799487d3c 100644 --- a/src/corelib/kernel/qvariant.cpp +++ b/src/corelib/kernel/qvariant.cpp @@ -2370,6 +2370,25 @@ void *QVariant::data() \sa data() */ +/*! + \since 6.6 + \fn template T &QVariant::get(QVariant &v) + \fn template const T &QVariant::get(const QVariant &v) + \fn template T &&QVariant::get(QVariant &&v) + \fn template const T &&QVariant::get(const QVariant &&v) + + If \a v contains an object of type \c T, returns a reference to the contained + object, otherwise the call has undefined behavior. + + The overloads taking a mutable \a v detach \a v: When called on a + \l{isNull()}{null} \a v with matching type \c T, \a v will not be null + after the call. + + These functions are provided for compatibility with \c{std::variant}. + + \sa get_if(), data() +*/ + /*! Returns \c true if this is a null variant, false otherwise. diff --git a/src/corelib/kernel/qvariant.h b/src/corelib/kernel/qvariant.h index 50b7e8c2bd..50ede63987 100644 --- a/src/corelib/kernel/qvariant.h +++ b/src/corelib/kernel/qvariant.h @@ -10,9 +10,12 @@ #ifndef QT_NO_DEBUG_STREAM #include #endif + #include -#include +#include +#include #include + #if !defined(QT_LEAN_HEADERS) || QT_LEAN_HEADERS < 1 # include # include @@ -25,6 +28,7 @@ QT_BEGIN_NAMESPACE QT_ENABLE_P0846_SEMANTICS_FOR(get_if) +QT_ENABLE_P0846_SEMANTICS_FOR(get) class QBitArray; class QDataStream; @@ -494,6 +498,22 @@ private: return static_cast(v->data()); } +#define Q_MK_GET(cvref) \ + template \ + friend T cvref get(QVariant cvref v) \ + { \ + if constexpr (std::is_const_v) \ + Q_ASSERT(!v.d.is_null); \ + Q_ASSERT(v.d.type() == QMetaType::fromType>()); \ + return static_cast(*get_if(&v)); \ + } \ + /* end */ + Q_MK_GET(&) + Q_MK_GET(const &) + Q_MK_GET(&&) + Q_MK_GET(const &&) +#undef Q_MK_GET + template friend inline T qvariant_cast(const QVariant &); protected: diff --git a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp index e5ffe14c97..cbe3f90b94 100644 --- a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp +++ b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp @@ -17,9 +17,17 @@ constexpr inline bool my_is_same_v = true; #define CHECK_GET_IF(Variant, cvref) \ CHECK_IMPL(get_if, int, Variant, cvref *, int) +#define CHECK_GET(Variant, cvref) \ + CHECK_IMPL(get, int, Variant, cvref, int) + CHECK_GET_IF(QVariant, /* unadorned */); CHECK_GET_IF(QVariant, const); +CHECK_GET(QVariant, &); +CHECK_GET(QVariant, const &); +CHECK_GET(QVariant, &&); +CHECK_GET(QVariant, const &&); + // check for a type derived from QVariant: struct MyVariant : QVariant @@ -30,7 +38,13 @@ struct MyVariant : QVariant CHECK_GET_IF(MyVariant, /* unadorned */); CHECK_GET_IF(MyVariant, const); +CHECK_GET(MyVariant, &); +CHECK_GET(MyVariant, const &); +CHECK_GET(MyVariant, &&); +CHECK_GET(MyVariant, const &&); + #undef CHECK_GET_IF +#undef CHECK_GET #undef CHECK_IMPL #include @@ -368,6 +382,10 @@ private slots: void getIf_QString() { getIf_impl(u"string"_s); }; void getIf_NonDefaultConstructible(); + void get_int() { get_impl(42); } + void get_QString() { get_impl(u"string"_s); } + void get_NonDefaultConstructible(); + private: using StdVariant = std::variant; template void getIf_impl(T t) const; + template + void get_impl(T t) const; void dataStream_data(QDataStream::Version version); void loadQVariantFromDataStream(QDataStream::Version version); void saveQVariantFromDataStream(QDataStream::Version version); @@ -5688,12 +5708,14 @@ void tst_QVariant::copyNonDefaultConstructible() QVERIFY(var.constData() != &ndc); // qvariant_cast and QVariant::value don't compile - QCOMPARE(*static_cast(var.constData()), ndc); + QCOMPARE(get(std::as_const(var)), ndc); QVariant var2 = var; var2.detach(); // force another copy QVERIFY(var2.isDetached()); QVERIFY(var2.constData() != var.constData()); + QCOMPARE(get(std::as_const(var2)), + get(std::as_const(var))); QCOMPARE(var2, var); } @@ -5702,6 +5724,11 @@ void tst_QVariant::getIf_NonDefaultConstructible() getIf_impl(NonDefaultConstructible{42}); } +void tst_QVariant::get_NonDefaultConstructible() +{ + get_impl(NonDefaultConstructible{42}); +} + template T mutate(const T &t) { return t + t; } template <> @@ -5813,5 +5840,65 @@ void tst_QVariant::getIf_impl(T t) const } } +template +void tst_QVariant::get_impl(T t) const +{ + QVariant v = QVariant::fromValue(t); + + // for behavioral comparison: + StdVariant stdv = t; + + #define FOR_EACH_CVREF(op) \ + op(/*unadorned*/, &&) \ + op(&, &) \ + op(&&, &&) \ + op(const, const &&) \ + op(const &, const &) \ + op(const &&, const &&) \ + /* end */ + + + #define CHECK_RETURN_TYPE_OF(Variant, cvref_in, cvref_out) \ + static_assert(std::is_same_v< \ + decltype(get(std::declval())), \ + T cvref_out \ + >); \ + /* end */ + #define CHECK_RETURN_TYPE(cvref_in, cvref_out) \ + CHECK_RETURN_TYPE_OF(StdVariant, cvref_in, cvref_out) \ + CHECK_RETURN_TYPE_OF(QVariant, cvref_in, cvref_out) \ + /* end */ + FOR_EACH_CVREF(CHECK_RETURN_TYPE) + #undef CHECK_RETURN_TYPE + + #undef FOR_EACH_CVREF + + // const access: + { + auto &&rs = get(std::as_const(stdv)); + QCOMPARE_EQ(rs, t); + + auto &&rv = get(std::as_const(v)); + QCOMPARE_EQ(rv, t); + } + + // mutable access: + { + T t2 = mutate(t); + + auto &&rs = get(stdv); + QCOMPARE_EQ(rs, t); + rs = t2; + auto &&rs2 = get(stdv); + QCOMPARE_EQ(rs2, t2); + + auto &&rv = get(v); + QCOMPARE_EQ(rv, t); + rv = t2; + auto &&rv2 = get(v); + QCOMPARE_EQ(rv2, t2); + } +} + QTEST_MAIN(tst_QVariant) #include "tst_qvariant.moc"