Commit fc3f5f16 authored by Volker Krause's avatar Volker Krause
Browse files

Make Jni::Object construction from a JNI handle also explicit

Same reasoning as for the custom wrapper types, although we don't have
the problem of ambiguous constructors here.
parent fc7fbbd9
......@@ -57,7 +57,8 @@ private Q_SLOTS:
QString s = obj.getName();
Q_UNUSED(s);
obj.setName(QStringLiteral("bla"));
obj.setName(QAndroidJniObject::fromString(QStringLiteral("bla")));
// explicit cast needed when coming from untyped JNI handler
obj.setName(Jni::Object<java::lang::String>(QAndroidJniObject::fromString(QStringLiteral("bla"))));
int i = obj.getFlags();
Q_UNUSED(i);
obj.setFlags(42);
......@@ -74,8 +75,8 @@ private Q_SLOTS:
obj.startIntent(intent);
// returning a non-wrapped type
QAndroidJniObject j = obj.getIntent();
// lvalue QAndroidJniObject argument
obj.setName(j);
// lvalue QAndroidJniObject argument needs explicit cast
obj.setName(Jni::Object<java::lang::String>(j));
// implicit conversion from a static property wrapper
obj.setFlags(Intent::FLAG_GRANT_READ_URI_PERMISSION);
......
......@@ -17,6 +17,8 @@ namespace Internal { struct FromHandleTag{}; }
namespace Jni {
template <typename T> class Object;
template <typename T>
inline QAndroidJniObject handle(const T &wrapper)
{
......@@ -29,13 +31,17 @@ inline QAndroidJniObject handle(const T &wrapper)
* things will horribly go wrong.
*/
template <typename T>
inline T fromHandle(const QAndroidJniObject &handle)
inline auto fromHandle(const QAndroidJniObject &handle)
{
return T(handle, Internal::FromHandleTag());
if constexpr (Jni::is_object_wrapper<T>::value) {
return T(handle, Internal::FromHandleTag());
} else {
return Jni::Object<T>(handle);
}
}
template <typename T>
inline T fromHandle(jobject handle)
inline auto fromHandle(jobject handle)
{
return fromHandle<T>(QAndroidJniObject(handle));
}
......@@ -49,7 +55,7 @@ class Object {
private:
template <typename DummyT> class _dummy_t {};
public:
inline Object(const QAndroidJniObject &v) : m_handle(v) {}
inline explicit Object(const QAndroidJniObject &v) : m_handle(v) {}
// implicit conversion from a compatible C++ type
inline Object(const std::conditional_t<std::is_same_v<typename Jni::converter<T>::type, void>,
......@@ -130,7 +136,7 @@ private: \
inline QAndroidJniObject jniHandle() const { return _m_jni_handle; } \
inline void setJniHandle(const QAndroidJniObject &h) { _m_jni_handle = h; } \
friend QAndroidJniObject KAndroidExtras::Jni::handle<Class>(const Class&); \
friend Class KAndroidExtras::Jni::fromHandle<Class>(const QAndroidJniObject&); \
friend auto KAndroidExtras::Jni::fromHandle<Class>(const QAndroidJniObject&); \
explicit inline Class(const QAndroidJniObject &handle, KAndroidExtras::Internal::FromHandleTag) : _m_jni_handle(handle) {}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment