From 941f49b0188c6f396a6dd6174c791029a0bed6ab Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Tue, 12 Sep 2023 11:37:37 +0200 Subject: JNI: treat enums as their underlying types Android APIs use integer constants like enum values, so they are mapped to one of the integeral types (jint, jshort, jlong etc) on the C++ side. Enable C++ code to declare an equivalent enum (scoped or unscoped), and to use that enum as a type in JNI calls by treating it as the underlying type in the signature() generator. Add a helper type trait that maps enums to their underlying type and other integral types to themselves (we can't use std::underlying_type_t on a non-enum type). Add tests. Note: Java Enums are special classes with fields; this change does not add any special support for those. Change-Id: Iec430a1553152dcf7a24209aaebbeceb1c6e38a8 Reviewed-by: Petri Virkkunen Reviewed-by: Zoltan Gera Reviewed-by: Juha Vuolle --- tests/auto/corelib/kernel/qjnitypes/tst_qjnitypes.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'tests/auto/corelib/kernel/qjnitypes/tst_qjnitypes.cpp') diff --git a/tests/auto/corelib/kernel/qjnitypes/tst_qjnitypes.cpp b/tests/auto/corelib/kernel/qjnitypes/tst_qjnitypes.cpp index b7685538f1..cbffb24ff8 100644 --- a/tests/auto/corelib/kernel/qjnitypes/tst_qjnitypes.cpp +++ b/tests/auto/corelib/kernel/qjnitypes/tst_qjnitypes.cpp @@ -115,6 +115,23 @@ static_assert(!QtJniTypes::CTString("ABCDE").endsWith("ABCDEF")); static_assert(QtJniTypes::CTString("ABCDE").endsWith('E')); static_assert(!QtJniTypes::CTString("ABCDE").endsWith('F')); +enum UnscopedEnum {}; +enum class ScopedEnum {}; +enum class IntEnum : int {}; +enum class UnsignedEnum : unsigned {}; +enum class Int8Enum : int8_t {}; +enum class ShortEnum : short {}; +enum class LongEnum : long {}; +enum class JIntEnum : jint {}; + +static_assert(QtJniTypes::Traits::signature() == "I"); +static_assert(QtJniTypes::Traits::signature() == "I"); +static_assert(QtJniTypes::Traits::signature() == "I"); +static_assert(QtJniTypes::Traits::signature() == "I"); +static_assert(QtJniTypes::Traits::signature() == "B"); +static_assert(QtJniTypes::Traits::signature() == "J"); +static_assert(QtJniTypes::Traits::signature() == "I"); + void tst_QJniTypes::initTestCase() { -- cgit v1.2.3