From 4ef415825c75abf68a4c092f464f9b87d4930544 Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Wed, 5 Jun 2024 14:46:31 +0200 Subject: [PATCH] JNI: clean up SFINAE constraints in QJniArray Rename the predictate for checking whether a container type has the required member functions and types to IfContiguousContainer (we need size(), data(), and value_type members). And since std::initializer_list always meets that criteria, remove the constraint from the respective constructor. Add an IfConvertible predicate to test whether the element type of an existing array can be converted to the element type of the new array. Change-Id: I7e5ba31de9664088b027c277c068c948f2189238 Reviewed-by: Marc Mutz (cherry picked from commit 468126d34a8c3a2fdd486768f935d13ebe565dd0) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/kernel/qjniarray.h | 39 +++++++++++++++------------------ src/corelib/kernel/qjniobject.h | 4 ++-- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/src/corelib/kernel/qjniarray.h b/src/corelib/kernel/qjniarray.h index 2ea82e39db7..c97f2234580 100644 --- a/src/corelib/kernel/qjniarray.h +++ b/src/corelib/kernel/qjniarray.h @@ -89,13 +89,13 @@ private: class QT_TECH_PREVIEW_API QJniArrayBase { // for SFINAE'ing out the fromContainer named constructor - template struct CanConvertHelper : std::false_type {}; - template - struct CanConvertHelper())), - decltype(std::size(std::declval())), - typename Container::value_type - > - > : std::true_type {}; + template struct IsContiguousContainerHelper : std::false_type {}; + template + struct IsContiguousContainerHelper())), + decltype(std::size(std::declval())), + typename C::value_type + > + > : std::true_type {}; public: using size_type = jsize; @@ -114,13 +114,12 @@ public: return 0; } - template - static constexpr bool canConvert = CanConvertHelper>::value; - template - using IfCanConvert = std::enable_if_t, bool>; - template = true - > + template + static constexpr bool isContiguousContainer = IsContiguousContainerHelper>::value; + template + using if_contiguous_container = std::enable_if_t, bool>; + + template = true> static auto fromContainer(Container &&container) { Q_ASSERT_X(size_t(std::size(container)) <= size_t((std::numeric_limits::max)()), @@ -218,23 +217,21 @@ public: QJniArray &operator=(const QJniArray &other) = default; QJniArray &operator=(QJniArray &&other) noexcept = default; - template = true - > + template = true> explicit QJniArray(Container &&container) : QJniArrayBase(QJniArrayBase::fromContainer(std::forward(container))) { } - template > = true - > Q_IMPLICIT inline QJniArray(std::initializer_list list) : QJniArrayBase(QJniArrayBase::fromContainer(list)) { } - template , bool> = true> + template + using if_convertible = std::enable_if_t, bool>; + + template = true> QJniArray(QJniArray &&other) : QJniArrayBase(std::forward>(other)) { diff --git a/src/corelib/kernel/qjniobject.h b/src/corelib/kernel/qjniobject.h index 707d1ae28a1..f9fc5cb03ab 100644 --- a/src/corelib/kernel/qjniobject.h +++ b/src/corelib/kernel/qjniobject.h @@ -822,7 +822,7 @@ auto QJniObject::LocalFrame::convertToJni(T &&value) return newLocalRef(QJniObject::fromString(value)); } else if constexpr (QtJniTypes::IsJniArray::value) { return value.arrayObject(); - } else if constexpr (QJniArrayBase::canConvert) { + } else if constexpr (QJniArrayBase::isContiguousContainer) { using QJniArrayType = decltype(QJniArrayBase::fromContainer(std::forward(value))); using ArrayType = decltype(std::declval().arrayObject()); return newLocalRef(QJniArrayBase::fromContainer(std::forward(value)).template object()); @@ -843,7 +843,7 @@ auto QJniObject::LocalFrame::convertFromJni(QJniObject &&object) return object.toString(); } else if constexpr (QtJniTypes::IsJniArray::value) { return T(std::move(object)); - } else if constexpr (QJniArrayBase::canConvert) { + } else if constexpr (QJniArrayBase::isContiguousContainer) { // if we were to create a QJniArray from Type... using QJniArrayType = decltype(QJniArrayBase::fromContainer(std::declval())); // then that QJniArray would have elements of type