Commit Graph

20 Commits (152153cb111db1bbd42e94f27712bbc39accf310)

Author SHA1 Message Date
Volker Hilsheimer 5797326b16 JNI: Constrain QJniArray::toContainer to compatible target containers
We can populate a container with the contents of the Java array as long
as the element types are convertible without narrowing (taking various
special cases into account, such as jstring to QString). The container
has to be either support emplace_back, or be a contiguous container
if a primitive element type.

The template helpers need to be in QJniArrayBase in order for qdoc to
accept the input.

Add test coverage, including static compile time tests.

Change-Id: Id9372deed5cf33446ee1969dc284a88991db2aee
Reviewed-by: Petri Virkkunen <petri.virkkunen@qt.io>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
(cherry picked from commit 8f04defa1e3973faec19a9cb1ab9bbf1ea7fb031)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
2024-07-20 01:13:40 +00:00
Volker Hilsheimer c21612d246 JNI: Improve the constraint on QJniArray::fromContainer
We can create a QJniArray from any container that has a forward
iterator. A contiguous container can be used to optimize the code path,
as long as the element type is primitive (i.e. not an object type).

Fixes: QTBUG-126151
Change-Id: I21915f944d226d6d4f1113a54e5602ddc9cd727e
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
(cherry picked from commit 71be7834e67216010dc74ed855dc7b513e302f1a)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
2024-07-20 01:13:35 +00:00
Volker Hilsheimer c4bd8a5492 JNI: clean up move semantics for QJniArray
Explicitly define (compiler-implemented) copy and move SMF's for
QJniArrayBase; we need to, as the destructor is declared, so without
them the compiler will implicitly convert an array to a QJniObject
and call that constructor.

Constrain the constructors and assignment operator from a QJniArray of
another type so that no narrowing conversion is allowed. Due to the
implicit conversion to QJniObject, we have to explicitly delete the
overload for narrowing conversions. Use the detector we have in
qobjectdefs_impl.h for that.

Make the detection helpers private, and add test coverage.

Change-Id: I1b2bb4435d52223567d20bb55ceb0d516e3b0b15
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
(cherry picked from commit ebbf7b0fdf866190cd20e62d6b13c7c6808101da)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
2024-07-20 01:13:28 +00:00
Volker Hilsheimer 6c3513f514 QJniArray: implement operator-> for the iterator
The QJniArray doesn't store values, so at() always returns a temporary.
As we cannot hand out a pointer to a temporary, use a wrapper reference
struct that stores the value and implements operator->. It's all inline,
so we can move it out in the future to return mutable references, if we
ever want to enable write access to QJniArray elements.

Change-Id: I3962df6160db8c5b573d47ebb7975864f8ea7a8b
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
(cherry picked from commit 3d5af4b912e60beb791f874c2dbfef5597b9aad7)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
2024-07-20 01:13:06 +00:00
Volker Hilsheimer d9c38b66aa QJniArray: implement offset dereference operator
The last requirement for making the iterator random-access, so
change category type.

Based on review comments.

Task-number: QTBUG-126150
Change-Id: I617f38f92d0f9279781e62ea3ab1929dbf6a07cd
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
(cherry picked from commit e4b2d7607c0243f2a7ca3f38f59e8532c543fcc7)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
2024-07-11 10:57:27 +02:00
Volker Hilsheimer 0db64ca53b QJniArray: add arithmetic operators
Required for making the iterator random access.

Based on review comments.

Task-number: QTBUG-126150
Change-Id: I80ee8ed584747759acb17ee956551caba4d5bdaa
Reviewed-by: Soheil Armin <soheil.armin@qt.io>
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
(cherry picked from commit c4e406e3792405cfc0b8cc97a29f136c0fd44a2e)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
2024-07-11 10:57:24 +02:00
Volker Hilsheimer 46ed4e47d2 QJniArray: make iterator strongly ordered
Required for making the iterator random access.

Implement compareThreeWay, add tests. Comparing iterators operating
on different containers is undefined behavior, so assert if they don't
match (even if they are different QJniArrays referencing the same
Java array).

Based on review comments.

Task-number: QTBUG-126150
Change-Id: Ib3b94558fc66fb9cff19139d2110c6bbd4ac14b5
Reviewed-by: Soheil Armin <soheil.armin@qt.io>
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
(cherry picked from commit d04f38f6f5e49a81211ab598ba23049dc4ef1507)
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
2024-07-11 10:57:22 +02:00
Volker Hilsheimer 708b84be11 JNI: establish API symmetry across QJniObject and QJniArray
We implicitly support QString, QStringList, and QByteArray, as well as
C++ types that are equivalent to JNI primitive types (such as bool for
jboolean, or int for jint) in QJniObject and some QJniArray APIs. Make
this symmetrical for QJniArray::to/fromContainer.

Add more compile- and run-time time test coverage.

Change-Id: I8cc84e6181a93f889282d2d3f0a05207416c4dbe
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
(cherry picked from commit d9dd8c4986373789b8bd250d0c2b36b660fe210f)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
2024-07-11 10:57:19 +02:00
Volker Hilsheimer eedbcad7a4 QJniArray: allow container type for toContainer to be set explicitly
Add a template parameter that is defaulted to what our logic would
select as the appropritate container type. If the type is contiguous
and the element is primitive, use the optimized implementation with
the JNI helpers for copying the entire array region.

Otherwise, use an emplace_back loop.

Change-Id: I669bc3261f111bc0c7e3dd2af8fd33293c083801
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
(cherry picked from commit a6b1f80cd6c833f6eb2fbfb778254cd7fcbc000f)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
2024-07-11 10:57:16 +02:00
Volker Hilsheimer b2f8a7b6a8 JNI: make it safe to iterate over or convert an invalid QJniArray
This is expected behavior for default-constructed containers.

Also add the missing isEmpty() member function to QJniArrayBase.

Change-Id: I23111f6906ef5476567272cb23746fec962afa35
Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
(cherry picked from commit e5516191b25d70ee9d6e87246d932fa559ef0b59)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
2024-06-18 21:12:12 +00:00
Volker Hilsheimer e4c386b0b7 QJniArray: add deduction guide for direct construction from container
That way,

QList list{1, 2, 3};
QJniArray array(list);

works.

Change-Id: If04f8115c51fce533cb4287bf36841ff0daeb11b
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
(cherry picked from commit f199d4f78d528f7a70170d2469ae4807e4a5e765)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
2024-06-08 08:52:49 +00:00
Volker Hilsheimer 8d613f8a94 JNI: support construction of QJniArray from std::initializer_list
Add implict constructor, treat the list like any other container.

Simplify the test code, and explicitly constructor-initialize when
we want an array and might have an array, so that we don't end up
with constructing arrays of arrays.

Change-Id: I14615f897cf8a2188510cfe1085ffc70a2396d5d
Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
2024-02-26 20:17:27 +01:00
Volker Hilsheimer a730c42608 JNI: Support QStringList as a parameter of native functions
Since we support QString and QList specializations, we should also
support QStringList directly. That's a bit more involved as we need
to specialize the code path for QString, which is not convertible to
or from jobject. But once we have mapped the type to jstring it
follows the implementation for lists of objects.

We now need to generate temporary local references when
converting a QString to a jstring, so manage a local frame. We do
so explicitly in chunks of 100 local references.

Change-Id: I7ae5cf7d0ba0099992c36f3677980c346526804b
Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
2024-02-23 19:11:04 +01:00
Lucie Gérard ff1039c217 Change license for tests files
According to QUIP-18 [1], all tests file should be
LicenseRef-Qt-Commercial OR GPL-3.0-only

[1]: https://contribute.qt-project.org/quips/18

Pick-to: 6.7
Task-number: QTBUG-121787
Change-Id: I9657df5d660820e56c96d511ea49d321c54682e8
Reviewed-by: Christian Ehrlicher <ch.ehrlicher@gmx.de>
2024-02-04 09:56:42 +01:00
Volker Hilsheimer cc5251ed59 QJniArray: make reverse-iterable
Add rbeing/rend overload, relevant typedefs, and decrement operators.
As a drive-by, add noexcept to begin/end functions.

Found during header review.

Pick-to: 6.7
Task-number: QTBUG-119952
Change-Id: I32d9a7d50a1f03550944c2247516c455d4822fe7
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
2024-01-29 16:06:47 +01:00
Volker Hilsheimer e5074cd354 QJniArray: add missing post-increment operator
Augment test case. Found during header review.

Pick-to: 6.7
Task-number: QTBUG-119952
Change-Id: I326395397167edb05ff1f45f7151614c02b7e7eb
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
2024-01-29 16:06:47 +01:00
Volker Hilsheimer 8f04615bcd QJniArray: add missing typedefs
Add missing nested typedefs for both the QJniArray container and
the QJniArrayIterator. Expand test case to make sure that some standard
algorithms (such as std::distance and ranged for) work with those
types.

Found during header review.

Pick-to: 6.7
Task-number: QTBUG-119952
Change-Id: I96f348215c6f1e0e1ce777d9bdd2f172d7e52974
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
2024-01-29 16:06:47 +01:00
Volker Hilsheimer 100071af82 QJniArray: fix const_iterator declaration, make it default-constructible
Iterators are required to be default-constructible and
value-initialized iterators must compare equal.

In a const_iterator, the pointee should be const, not
the iterator itself.

Found during header review.

Pick-to: 6.7
Task-number: QTBUG-119952
Change-Id: I036c0a62ade8c59dc5d62c0823b375223719af3f
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
2024-01-24 00:26:37 +00:00
Volker Hilsheimer 2c9525a501 QJniArray: add test case for QJniArray::size
Fix build of the test by removing the reference from the
container type before accessing the nested typedef.

Pick-to: 6.7
Change-Id: Ic35f312bac70b8f8f80149a3432329070c8c8c7d
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
2024-01-23 13:56:54 +01:00
Volker Hilsheimer 80d4d55e25 JNI: add QJniArray class for easier working with arrays
Implements an iterator API and other standard member access functions
for sequential containers so that we can use ranged-for over an object
that is a jarray. Provides read-only access to individual elements
(which is mostly relevant for arrays of objects), or the entire data()
as a contiguous memory block (which is useful for arrays of primitive
types).

QJniObject call functions can return QJniArray<T> when the return type
is either explicitly QJniArray<T> or T[], or their Qt equivalent (e.g.
a jbyteArray can be taken or returned as a QByteArray). If the return
type is a jarray type, then a QJniObject is returned as before.

Arrays can be created from a Qt container through a constructor or the
generic fromData named constructor in the QJniArrayBase class, which
implements the generic logic.

Not documented as public API yet.

Added a compile-time test to verify that types are mapped correctly.
The function test coverage is added to the QJniObject auto-test, as
that already provides the Java test class with functions taking and
returning arrays of different types.

Change-Id: I0750fc4f4cce7314df3b10e122eafbcfd68297b6
Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
2023-10-16 18:43:16 +02:00