JNI: simplify code for declared types a bit

Template type is implied, and forwarding to QJniObject implementations
avoids code duplication.

Change-Id: I7c25c93a7fdf20de7a4c61129d9383f0f6f508c7
Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
This commit is contained in:
Volker Hilsheimer 2023-10-20 22:07:19 +02:00
parent 72ad419116
commit b7e0f45a85

View File

@ -28,10 +28,10 @@ struct Object : QJniObject
Q_IMPLICIT Object(QJniObject &&object) : QJniObject(std::move(object)) {}
// Compiler-generated copy/move semantics based on QJniObject's shared d-pointer are fine!
Object(const Object<Type> &other) = default;
Object(Object<Type> &&other) = default;
Object<Type> &operator=(const Object<Type> &other) = default;
Object<Type> &operator=(Object<Type> &&other) = default;
Object(const Object &other) = default;
Object(Object &&other) = default;
Object &operator=(const Object &other) = default;
Object &operator=(Object &&other) = default;
// avoid ambiguities with deleted const char * constructor
Q_IMPLICIT Object(std::nullptr_t) : QJniObject() {}
@ -41,18 +41,13 @@ struct Object : QJniObject
, ValidSignatureTypes<Args...> = true
>
explicit Object(Args &&...args)
: QJniObject(Qt::Initialization::Uninitialized)
{
*this = Object<Type>{QJniObject::construct<Class>(std::forward<Args>(args)...)};
}
: QJniObject(QtJniTypes::Traits<Class>::className(), std::forward<Args>(args)...)
{}
// named constructors avoid ambiguities
static Object<Type> fromJObject(jobject object) { return Object<Type>(object); }
static Object fromJObject(jobject object) { return Object(object); }
template <typename ...Args>
static Object<Type> construct(Args &&...args)
{
return Object<Type>{QJniObject::construct<Class, Args...>(std::forward<Args>(args)...)};
}
static Object construct(Args &&...args) { return Object{std::forward<Args>(args)...}; }
// public API forwarding to QJniObject, with the implicit Class template parameter
template <typename Ret, typename ...Args