From 0022b05a9a222912ed91f658c058dff6955d2de4 Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Sat, 16 Sep 2023 11:28:49 +0200 Subject: [PATCH] JNI: rename our compile-time String type to CTString MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The type lives in the QtJniTypes namespace, which is where types end up that are declared through the Q_DECLARE_JNI_CLASS/TYPE macros. Having a String type in that namespace prevents us from declaring the Java String class as a QtJniTypes type, which is silly. Perhaps this type becomes obsolete at some point with std::string being a constexpr type in C++23, but until then we need it. It has no ABI, so renaming it us safe. Until submodules are ported, leave a compatibility alias String type, which also prevents us from declaring a String JNI class in tests until the alias is removed in a later commit. Change-Id: I489a40a9b9e94e6495cf54548238438e9220d5c1 Reviewed-by: Zoltan Gera Reviewed-by: Tinja Paavoseppä Reviewed-by: Petri Virkkunen --- src/corelib/kernel/qjnitypes.h | 10 +- src/corelib/kernel/qjnitypes_impl.h | 109 +++++++++--------- .../kernel/qjnitypes/tst_qjnitypes.cpp | 36 +++--- 3 files changed, 78 insertions(+), 77 deletions(-) diff --git a/src/corelib/kernel/qjnitypes.h b/src/corelib/kernel/qjnitypes.h index 72d6f2783c..d3634d6bc5 100644 --- a/src/corelib/kernel/qjnitypes.h +++ b/src/corelib/kernel/qjnitypes.h @@ -45,7 +45,7 @@ constexpr auto QtJniTypes::typeSignature() \ && Signature[sizeof(Signature) - 2] == ';', \ "Type signature needs to start with 'L' or '['" \ " and end with ';'"); \ - return QtJniTypes::String(Signature); \ + return QtJniTypes::CTString(Signature); \ } \ #define Q_DECLARE_JNI_CLASS(Type, Signature) \ @@ -53,14 +53,14 @@ Q_DECLARE_JNI_TYPE_HELPER(Type) \ template<> \ constexpr auto QtJniTypes::className() \ { \ - return QtJniTypes::String(Signature); \ + return QtJniTypes::CTString(Signature); \ } \ template<> \ constexpr auto QtJniTypes::typeSignature() \ { \ - return QtJniTypes::String("L") \ - + QtJniTypes::String(Signature) \ - + QtJniTypes::String(";"); \ + return QtJniTypes::CTString("L") \ + + QtJniTypes::CTString(Signature) \ + + QtJniTypes::CTString(";"); \ } \ #define Q_DECLARE_JNI_NATIVE_METHOD(...) \ diff --git a/src/corelib/kernel/qjnitypes_impl.h b/src/corelib/kernel/qjnitypes_impl.h index 97be4f4394..a671eeae35 100644 --- a/src/corelib/kernel/qjnitypes_impl.h +++ b/src/corelib/kernel/qjnitypes_impl.h @@ -18,13 +18,13 @@ namespace QtJniTypes // a constexpr type for string literals of any character width, aware of the length // of the string. template -struct String +struct CTString { BaseType m_data[N_WITH_NULL] = {}; - constexpr String() noexcept {} + constexpr CTString() noexcept {} // Can be instantiated (only) with a string literal - constexpr explicit String(const BaseType (&data)[N_WITH_NULL]) noexcept + constexpr explicit CTString(const BaseType (&data)[N_WITH_NULL]) noexcept { for (size_t i = 0; i < N_WITH_NULL - 1; ++i) m_data[i] = data[i]; @@ -71,8 +71,8 @@ struct String } template - friend inline constexpr bool operator==(const String &lhs, - const String &rhs) noexcept + friend inline constexpr bool operator==(const CTString &lhs, + const CTString &rhs) noexcept { if constexpr (N_WITH_NULL != N2_WITH_NULL) { return false; @@ -86,57 +86,60 @@ struct String } template - friend inline constexpr bool operator!=(const String &lhs, - const String &rhs) noexcept + friend inline constexpr bool operator!=(const CTString &lhs, + const CTString &rhs) noexcept { return !operator==(lhs, rhs); } template - friend inline constexpr bool operator==(const String &lhs, + friend inline constexpr bool operator==(const CTString &lhs, const BaseType (&rhs)[N2_WITH_NULL]) noexcept { - return operator==(lhs, String(rhs)); + return operator==(lhs, CTString(rhs)); } template friend inline constexpr bool operator==(const BaseType (&lhs)[N2_WITH_NULL], - const String &rhs) noexcept + const CTString &rhs) noexcept { - return operator==(String(lhs), rhs); + return operator==(CTString(lhs), rhs); } template - friend inline constexpr bool operator!=(const String &lhs, + friend inline constexpr bool operator!=(const CTString &lhs, const BaseType (&rhs)[N2_WITH_NULL]) noexcept { - return operator!=(lhs, String(rhs)); + return operator!=(lhs, CTString(rhs)); } template friend inline constexpr bool operator!=(const BaseType (&lhs)[N2_WITH_NULL], - const String &rhs) noexcept + const CTString &rhs) noexcept { - return operator!=(String(lhs), rhs); + return operator!=(CTString(lhs), rhs); } template - friend inline constexpr auto operator+(const String &lhs, - const String &rhs) noexcept + friend inline constexpr auto operator+(const CTString &lhs, + const CTString &rhs) noexcept { char data[N_WITH_NULL + N2_WITH_NULL - 1] = {}; for (size_t i = 0; i < N_WITH_NULL - 1; ++i) data[i] = lhs[i]; for (size_t i = 0; i < N2_WITH_NULL - 1; ++i) data[N_WITH_NULL - 1 + i] = rhs[i]; - return String(data); + return CTString(data); } }; +// compatibility alias until submodules are ported +template +using String = CTString; // Helper types that allow us to disable variadic overloads that would conflict // with overloads that take a const char*. template struct IsStringType : std::false_type {}; template<> struct IsStringType : std::true_type {}; -template struct IsStringType> : std::true_type {}; +template struct IsStringType> : std::true_type {}; template struct IsStringType : std::true_type {}; template @@ -153,67 +156,67 @@ constexpr auto typeSignature() using UnderlyingType = typename std::remove_extent_t; static_assert(!std::is_array_v, "typeSignature() does not handle multi-dimensional arrays"); - return String("[") + typeSignature(); + return CTString("[") + typeSignature(); } else if constexpr (std::is_same_v) { - return String("Ljava/lang/Object;"); + return CTString("Ljava/lang/Object;"); } else if constexpr (std::is_same_v) { - return String("Ljava/lang/Class;"); + return CTString("Ljava/lang/Class;"); } else if constexpr (std::is_same_v) { - return String("Ljava/lang/String;"); + return CTString("Ljava/lang/String;"); } else if constexpr (std::is_same_v) { - return String("[Ljava/lang/Object;"); + return CTString("[Ljava/lang/Object;"); } else if constexpr (std::is_same_v) { - return String("Ljava/lang/Throwable;"); + return CTString("Ljava/lang/Throwable;"); } else if constexpr (std::is_same_v) { - return String("[Z"); + return CTString("[Z"); } else if constexpr (std::is_same_v) { - return String("[B"); + return CTString("[B"); } else if constexpr (std::is_same_v) { - return String("[S"); + return CTString("[S"); } else if constexpr (std::is_same_v) { - return String("[I"); + return CTString("[I"); } else if constexpr (std::is_same_v) { - return String("[J"); + return CTString("[J"); } else if constexpr (std::is_same_v) { - return String("[F"); + return CTString("[F"); } else if constexpr (std::is_same_v) { - return String("[D"); + return CTString("[D"); } else if constexpr (std::is_same_v) { - return String("[C"); + return CTString("[C"); } else if constexpr (std::is_same_v) { - return String("Z"); + return CTString("Z"); } else if constexpr (std::is_same_v) { - return String("Z"); + return CTString("Z"); } else if constexpr (std::is_same_v) { - return String("B"); + return CTString("B"); } else if constexpr (std::is_same_v) { - return String("C"); + return CTString("C"); } else if constexpr (std::is_same_v) { - return String("C"); + return CTString("C"); } else if constexpr (std::is_same_v) { - return String("S"); + return CTString("S"); } else if constexpr (std::is_same_v) { - return String("S"); + return CTString("S"); } else if constexpr (std::is_same_v) { - return String("I"); + return CTString("I"); } else if constexpr (std::is_same_v) { - return String("I"); + return CTString("I"); } else if constexpr (std::is_same_v) { - return String("I"); + return CTString("I"); } else if constexpr (std::is_same_v) { - return String("J"); + return CTString("J"); } else if constexpr (std::is_same_v) { - return String("J"); + return CTString("J"); } else if constexpr (std::is_same_v) { - return String("F"); + return CTString("F"); } else if constexpr (std::is_same_v) { - return String("F"); + return CTString("F"); } else if constexpr (std::is_same_v) { - return String("D"); + return CTString("D"); } else if constexpr (std::is_same_v) { - return String("D"); + return CTString("D"); } else if constexpr (std::is_same_v) { - return String("V"); + return CTString("V"); } // else: The return type becomes void, indicating that the typeSignature @@ -232,7 +235,7 @@ template constexpr auto className() { if constexpr (std::is_same_v) - return String("java/lang/String"); + return CTString("java/lang/String"); else staticAssertClassNotRegistered(); } @@ -289,9 +292,9 @@ using ValidFieldType = std::enable_if_t< template = true> static constexpr auto methodSignature() { - return (String("(") + + return (CTString("(") + ... + typeSignature>()) - + String(")") + + CTString(")") + typeSignature(); } diff --git a/tests/auto/corelib/kernel/qjnitypes/tst_qjnitypes.cpp b/tests/auto/corelib/kernel/qjnitypes/tst_qjnitypes.cpp index 3ad4827f93..d7b7059bd3 100644 --- a/tests/auto/corelib/kernel/qjnitypes/tst_qjnitypes.cpp +++ b/tests/auto/corelib/kernel/qjnitypes/tst_qjnitypes.cpp @@ -21,20 +21,20 @@ struct QtJavaWrapper {}; template<> constexpr auto QtJniTypes::typeSignature() { - return QtJniTypes::String("Lorg/qtproject/qt/android/QtJavaWrapper;"); + return QtJniTypes::CTString("Lorg/qtproject/qt/android/QtJavaWrapper;"); } template<> constexpr auto QtJniTypes::typeSignature() { - return QtJniTypes::String("Ljava/lang/Object;"); + return QtJniTypes::CTString("Ljava/lang/Object;"); } struct QtCustomJniObject : QJniObject {}; template<> constexpr auto QtJniTypes::typeSignature() { - return QtJniTypes::String("Lorg/qtproject/qt/android/QtCustomJniObject;"); + return QtJniTypes::CTString("Lorg/qtproject/qt/android/QtCustomJniObject;"); } static_assert(QtJniTypes::typeSignature() == "Lorg/qtproject/qt/android/QtJavaWrapper;"); @@ -46,8 +46,6 @@ static_assert(QtJniTypes::typeSignature() == "Lorg/qtproje Q_DECLARE_JNI_TYPE(ArrayType, "[Lorg/qtproject/qt/ArrayType;") static_assert(QtJniTypes::typeSignature() == "[Lorg/qtproject/qt/ArrayType;"); -static_assert(QtJniTypes::className() == "java/lang/String"); - Q_DECLARE_JNI_CLASS(QtTextToSpeech, "org/qtproject/qt/android/speech/QtTextToSpeech") static_assert(QtJniTypes::className() == "org/qtproject/qt/android/speech/QtTextToSpeech"); @@ -91,21 +89,21 @@ static_assert(QtJniTypes::isArrayType()); static_assert(QtJniTypes::isArrayType()); static_assert(QtJniTypes::isArrayType()); -static_assert(QtJniTypes::String("ABCDE").startsWith("ABC")); -static_assert(QtJniTypes::String("ABCDE").startsWith("A")); -static_assert(QtJniTypes::String("ABCDE").startsWith("ABCDE")); -static_assert(!QtJniTypes::String("ABCDE").startsWith("ABCDEF")); -static_assert(!QtJniTypes::String("ABCDE").startsWith("9AB")); -static_assert(QtJniTypes::String("ABCDE").startsWith('A')); -static_assert(!QtJniTypes::String("ABCDE").startsWith('B')); +static_assert(QtJniTypes::CTString("ABCDE").startsWith("ABC")); +static_assert(QtJniTypes::CTString("ABCDE").startsWith("A")); +static_assert(QtJniTypes::CTString("ABCDE").startsWith("ABCDE")); +static_assert(!QtJniTypes::CTString("ABCDE").startsWith("ABCDEF")); +static_assert(!QtJniTypes::CTString("ABCDE").startsWith("9AB")); +static_assert(QtJniTypes::CTString("ABCDE").startsWith('A')); +static_assert(!QtJniTypes::CTString("ABCDE").startsWith('B')); -static_assert(QtJniTypes::String("ABCDE").endsWith("CDE")); -static_assert(QtJniTypes::String("ABCDE").endsWith("E")); -static_assert(QtJniTypes::String("ABCDE").endsWith("ABCDE")); -static_assert(!QtJniTypes::String("ABCDE").endsWith("DEF")); -static_assert(!QtJniTypes::String("ABCDE").endsWith("ABCDEF")); -static_assert(QtJniTypes::String("ABCDE").endsWith('E')); -static_assert(!QtJniTypes::String("ABCDE").endsWith('F')); +static_assert(QtJniTypes::CTString("ABCDE").endsWith("CDE")); +static_assert(QtJniTypes::CTString("ABCDE").endsWith("E")); +static_assert(QtJniTypes::CTString("ABCDE").endsWith("ABCDE")); +static_assert(!QtJniTypes::CTString("ABCDE").endsWith("DEF")); +static_assert(!QtJniTypes::CTString("ABCDE").endsWith("ABCDEF")); +static_assert(QtJniTypes::CTString("ABCDE").endsWith('E')); +static_assert(!QtJniTypes::CTString("ABCDE").endsWith('F')); void tst_QJniTypes::initTestCase() {