Android: JNI: Use void as default return value type on call[Static]Method
By using void as default template argument in callStaticMethod and callMethod, we can avoid having to type <void> after every void method call, while still allowing the caller to remain explicit about the type if they want to. Change-Id: Id7143d1116364be7699712d1fead257f26f20420 Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
parent
2defca4187
commit
f910954928
@ -132,7 +132,7 @@ public:
|
|||||||
jclass objectClass() const;
|
jclass objectClass() const;
|
||||||
QByteArray className() const;
|
QByteArray className() const;
|
||||||
|
|
||||||
template <typename Ret, typename ...Args
|
template <typename Ret = void, typename ...Args
|
||||||
#ifndef Q_QDOC
|
#ifndef Q_QDOC
|
||||||
, QtJniTypes::IfValidFieldType<Ret> = true
|
, QtJniTypes::IfValidFieldType<Ret> = true
|
||||||
#endif
|
#endif
|
||||||
@ -164,7 +164,7 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Ret, typename ...Args
|
template <typename Ret = void, typename ...Args
|
||||||
#ifndef Q_QDOC
|
#ifndef Q_QDOC
|
||||||
, QtJniTypes::IfValidSignatureTypes<Ret, Args...> = true
|
, QtJniTypes::IfValidSignatureTypes<Ret, Args...> = true
|
||||||
#endif
|
#endif
|
||||||
@ -191,7 +191,7 @@ public:
|
|||||||
|
|
||||||
QJniObject callObjectMethod(const char *methodName, const char *signature, ...) const;
|
QJniObject callObjectMethod(const char *methodName, const char *signature, ...) const;
|
||||||
|
|
||||||
template <typename Ret, typename ...Args>
|
template <typename Ret = void, typename ...Args>
|
||||||
static auto callStaticMethod(const char *className, const char *methodName, const char *signature, Args &&...args)
|
static auto callStaticMethod(const char *className, const char *methodName, const char *signature, Args &&...args)
|
||||||
{
|
{
|
||||||
JNIEnv *env = QJniEnvironment::getJniEnv();
|
JNIEnv *env = QJniEnvironment::getJniEnv();
|
||||||
@ -199,7 +199,7 @@ public:
|
|||||||
return callStaticMethod<Ret>(clazz, methodName, signature, std::forward<Args>(args)...);
|
return callStaticMethod<Ret>(clazz, methodName, signature, std::forward<Args>(args)...);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Ret, typename ...Args>
|
template <typename Ret = void, typename ...Args>
|
||||||
static auto callStaticMethod(jclass clazz, const char *methodName, const char *signature, Args &&...args)
|
static auto callStaticMethod(jclass clazz, const char *methodName, const char *signature, Args &&...args)
|
||||||
{
|
{
|
||||||
JNIEnv *env = QJniEnvironment::getJniEnv();
|
JNIEnv *env = QJniEnvironment::getJniEnv();
|
||||||
@ -208,7 +208,7 @@ public:
|
|||||||
return callStaticMethod<Ret>(clazz, id, std::forward<Args>(args)...);
|
return callStaticMethod<Ret>(clazz, id, std::forward<Args>(args)...);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Ret, typename ...Args
|
template <typename Ret = void, typename ...Args
|
||||||
#ifndef Q_QDOC
|
#ifndef Q_QDOC
|
||||||
, QtJniTypes::IfValidFieldType<Ret> = true
|
, QtJniTypes::IfValidFieldType<Ret> = true
|
||||||
#endif
|
#endif
|
||||||
@ -239,7 +239,7 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Ret, typename ...Args
|
template <typename Ret = void, typename ...Args
|
||||||
#ifndef Q_QDOC
|
#ifndef Q_QDOC
|
||||||
, QtJniTypes::IfValidSignatureTypes<Ret, Args...> = true
|
, QtJniTypes::IfValidSignatureTypes<Ret, Args...> = true
|
||||||
#endif
|
#endif
|
||||||
@ -254,7 +254,7 @@ public:
|
|||||||
return callStaticMethod<Ret>(clazz, id, std::forward<Args>(args)...);
|
return callStaticMethod<Ret>(clazz, id, std::forward<Args>(args)...);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Ret, typename ...Args
|
template <typename Ret = void, typename ...Args
|
||||||
#ifndef Q_QDOC
|
#ifndef Q_QDOC
|
||||||
, QtJniTypes::IfValidSignatureTypes<Ret, Args...> = true
|
, QtJniTypes::IfValidSignatureTypes<Ret, Args...> = true
|
||||||
#endif
|
#endif
|
||||||
@ -264,7 +264,7 @@ public:
|
|||||||
constexpr auto signature = QtJniTypes::methodSignature<Ret, Args...>();
|
constexpr auto signature = QtJniTypes::methodSignature<Ret, Args...>();
|
||||||
return callStaticMethod<Ret>(clazz, methodName, signature.data(), std::forward<Args>(args)...);
|
return callStaticMethod<Ret>(clazz, methodName, signature.data(), std::forward<Args>(args)...);
|
||||||
}
|
}
|
||||||
template <typename Klass, typename Ret, typename ...Args
|
template <typename Klass, typename Ret = void, typename ...Args
|
||||||
#ifndef Q_QDOC
|
#ifndef Q_QDOC
|
||||||
, QtJniTypes::IfValidSignatureTypes<Ret, Args...> = true
|
, QtJniTypes::IfValidSignatureTypes<Ret, Args...> = true
|
||||||
#endif
|
#endif
|
||||||
@ -761,7 +761,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// public API forwarding to QJniObject, with the implicit Class template parameter
|
// public API forwarding to QJniObject, with the implicit Class template parameter
|
||||||
template <typename Ret, typename ...Args
|
template <typename Ret = void, typename ...Args
|
||||||
#ifndef Q_QDOC
|
#ifndef Q_QDOC
|
||||||
, QtJniTypes::IfValidSignatureTypes<Ret, Args...> = true
|
, QtJniTypes::IfValidSignatureTypes<Ret, Args...> = true
|
||||||
#endif
|
#endif
|
||||||
@ -791,7 +791,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// keep only these overloads, the rest is made private
|
// keep only these overloads, the rest is made private
|
||||||
template <typename Ret, typename ...Args
|
template <typename Ret = void, typename ...Args
|
||||||
#ifndef Q_QDOC
|
#ifndef Q_QDOC
|
||||||
, QtJniTypes::IfValidSignatureTypes<Ret, Args...> = true
|
, QtJniTypes::IfValidSignatureTypes<Ret, Args...> = true
|
||||||
#endif
|
#endif
|
||||||
|
@ -113,6 +113,7 @@ private slots:
|
|||||||
void setStaticObjectField();
|
void setStaticObjectField();
|
||||||
|
|
||||||
void templateApiCheck();
|
void templateApiCheck();
|
||||||
|
void defaultTemplateApiCheck();
|
||||||
void isClassAvailable();
|
void isClassAvailable();
|
||||||
void fromLocalRef();
|
void fromLocalRef();
|
||||||
void largeObjectArray();
|
void largeObjectArray();
|
||||||
@ -1875,6 +1876,31 @@ void tst_QJniObject::templateApiCheck()
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void tst_QJniObject::defaultTemplateApiCheck()
|
||||||
|
{
|
||||||
|
// static QJniObject calls --------------------------------------------------------------------
|
||||||
|
QJniObject::callStaticMethod(testClassName, "staticVoidMethod");
|
||||||
|
QJniObject::callStaticMethod(testClassName, "staticVoidMethodWithArgs", "(IZC)V", 1, true, 'c');
|
||||||
|
QJniObject::callStaticMethod(testClassName, "staticVoidMethodWithArgs", 1, true, 'c');
|
||||||
|
|
||||||
|
// instance QJniObject calls ------------------------------------------------------------------
|
||||||
|
QJniObject testClass(testClassName);
|
||||||
|
QVERIFY(testClass.isValid());
|
||||||
|
|
||||||
|
testClass.callMethod("voidMethod");
|
||||||
|
testClass.callMethod("voidMethodWithArgs", "(IZC)V", 1, true, 'c');
|
||||||
|
testClass.callMethod("voidMethodWithArgs", 1, true, 'c');
|
||||||
|
|
||||||
|
// static QtJniType calls ---------------------------------------------------------------------
|
||||||
|
TestClass::callStaticMethod("staticVoidMethod");
|
||||||
|
TestClass::callStaticMethod("staticVoidMethodWithArgs", 1, true, 'c');
|
||||||
|
|
||||||
|
// instance QtJniType calls -------------------------------------------------------------------
|
||||||
|
TestClass instance;
|
||||||
|
instance.callMethod("voidMethod");
|
||||||
|
instance.callMethod("voidMethodWithArgs", 1, true, 'c');
|
||||||
|
}
|
||||||
|
|
||||||
void tst_QJniObject::isClassAvailable()
|
void tst_QJniObject::isClassAvailable()
|
||||||
{
|
{
|
||||||
QVERIFY(QJniObject::isClassAvailable("java/lang/String"));
|
QVERIFY(QJniObject::isClassAvailable("java/lang/String"));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user