diff options
author | Marc Mutz <marc.mutz@qt.io> | 2024-02-29 14:48:01 +0100 |
---|---|---|
committer | Marc Mutz <marc.mutz@qt.io> | 2024-03-01 01:31:07 +0000 |
commit | ddaf7642828c970a69c995a606c5cb16e003c26c (patch) | |
tree | fa7bd76306985ce9a59509565eb074ed9e7fe977 /src/corelib/kernel | |
parent | 41854cfaac0fcd2c8f4386a46955716e652c4edc (diff) |
QJniObject: use ctor delegation instead of construct+assign
The old code used unintialized construction followed by (move)
assignment to construct a QJniObject that requires a LocalFrame
object. That is two constructors and therefore one more than we need
(and need to destroy in inline code again).
Everything can be solved with another level of indirection.™
Since the LocalFrame needs to remain alive for the duration of
argument evaluation, the usual comma operator trick won't work. But we
can add a private helper ctor that takes the LocalFrame as an
additional argument to inject the object with the correct lifetime
into the ctor delegation chain.
Put the new argument in the front, to avoid clashes with the primary
contructor's trailing universal references, which might be a better
match than the new overload had we added the argument at the end. The
hope is that the compiler will avoid the ensuing register shuffling by
inlining the outer two constructor calls.
This brings the number of QJniObjects constructed down to one, as it
should be. We might also be able to remove the Uninitialized ctor
again.
Found in API-review.
Amends 62cb5589b3723fe8162e190cd54d9c78929b98d2.
Pick-to: 6.7
Change-Id: I326187e54fd0705a1bbedb2d51d94a46b108a3c8
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'src/corelib/kernel')
-rw-r--r-- | src/corelib/kernel/qjniobject.h | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/src/corelib/kernel/qjniobject.h b/src/corelib/kernel/qjniobject.h index 3a02c0e31e..589f6489f7 100644 --- a/src/corelib/kernel/qjniobject.h +++ b/src/corelib/kernel/qjniobject.h @@ -71,12 +71,17 @@ public: #endif > explicit QJniObject(const char *className, Args &&...args) - : QJniObject(Qt::Uninitialized) + : QJniObject(LocalFrame<Args...>{}, className, std::forward<Args>(args)...) { - LocalFrame<Args...> localFrame; - *this = QJniObject(className, QtJniTypes::constructorSignature<Args...>().data(), - localFrame.convertToJni(std::forward<Args>(args))...); } +private: + template<typename ...Args> + explicit QJniObject(LocalFrame<Args...> localFrame, const char *className, Args &&...args) + : QJniObject(className, QtJniTypes::constructorSignature<Args...>().data(), + localFrame.convertToJni(std::forward<Args>(args))...) + { + } +public: explicit QJniObject(jclass clazz); explicit QJniObject(jclass clazz, const char *signature, ...); template<typename ...Args |