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

Make JNI converter traits easier to create

Not entirely happy with the syntax yet, but it reduces declaring converters
to a single line. Added one for QUrl to demonstrate this.
parent 2ebca550
......@@ -7,6 +7,8 @@
#include <kandroidextras/jniproperty.h>
#include <kandroidextras/jnitypes.h>
#include <kandroidextras/manifestpermission.h>
#include <kandroidextras/intent.h>
#include <kandroidextras/uri.h>
#include <QtTest/qtest.h>
......@@ -23,6 +25,8 @@ class TestClass : android::content::Intent
JNI_PROPERTY(java::lang::String, myStringField)
JNI_PROPERTY(int, myIntField)
JNI_PROPERTY(android::net::Uri, myUriField)
JNI_PROPERTY(android::content::Intent, myIntentField)
inline QAndroidJniObject handle() const { return m_handle; }
private:
......@@ -58,17 +62,22 @@ private Q_SLOTS:
const QString foo = obj.myStringField;
obj.myStringField = foo;
const int bar = obj.myIntField;
Q_UNUSED(bar);
obj.myIntField = 42;
const QAndroidJniObject bla = obj.myUriField;
obj.myUriField = bla;
const QUrl url = obj.myUriField;
obj.myUriField = url;
const QAndroidJniObject bla = obj.myIntentField;
obj.myIntentField = bla;
QCOMPARE(obj.handle().protocol().size(), 6);
QCOMPARE(obj.handle().protocol().size(), 8);
QCOMPARE(obj.handle().protocol().at(0), QLatin1String("getObjectField: myStringField Ljava/lang/String;"));
QCOMPARE(obj.handle().protocol().at(1), QLatin1String("setField: myStringField Ljava/lang/String;"));
QCOMPARE(obj.handle().protocol().at(2), QLatin1String("getField: myIntField I"));
QCOMPARE(obj.handle().protocol().at(3), QLatin1String("setField: myIntField I"));
QCOMPARE(obj.handle().protocol().at(4), QLatin1String("getObjectField: myUriField Landroid/net/Uri;"));
QCOMPARE(obj.handle().protocol().at(5), QLatin1String("setField: myUriField Landroid/net/Uri;"));
QCOMPARE(obj.handle().protocol().at(6), QLatin1String("getObjectField: myIntentField Landroid/content/Intent;"));
QCOMPARE(obj.handle().protocol().at(7), QLatin1String("setField: myIntentField Landroid/content/Intent;"));
#endif
}
};
......
......@@ -6,12 +6,10 @@
#include "uri.h"
#include <KAndroidExtras/AndroidTypes>
#include <KAndroidExtras/JniSignature>
#include <KAndroidExtras/JavaTypes>
#include <QAndroidJniObject>
#include <QUrl>
using namespace KAndroidExtras;
......
......@@ -9,8 +9,12 @@
#include "kandroidextras_export.h"
#include <KAndroidExtras/AndroidTypes>
#include <KAndroidExtras/JniTypeTraits>
#include <QUrl>
class QAndroidJniObject;
class QUrl;
namespace KAndroidExtras {
......@@ -24,6 +28,8 @@ namespace Uri
KANDROIDEXTRAS_EXPORT QUrl toUrl(const QAndroidJniObject &uri);
}
JNI_DECLARE_CONVERTER(android::net::Uri, QUrl, (Uri::toUrl(value)), (Uri::fromUrl(value)))
}
#endif // KANDROIDEXTRAS_URI_H
......@@ -16,16 +16,7 @@ JNI_TYPE(java, io, File)
JNI_TYPE(java, lang, String)
JNI_TYPE(java, util, Locale)
namespace Jni {
template <> struct converter<QString> {
typedef java::lang::String type;
static inline QAndroidJniObject convert(const QString &value) { return QAndroidJniObject::fromString(value); }
};
template <> struct converter<java::lang::String> {
typedef QString type;
static inline QString convert(const QAndroidJniObject &value) { return value.toString(); }
};
}
JNI_DECLARE_CONVERTER(java::lang::String, QString, (value.toString()), (QAndroidJniObject::fromString(value)))
}
......
......@@ -30,7 +30,7 @@ template <> struct is_basic_type<jlong> : std::true_type {};
template <> struct is_basic_type<jfloat> : std::true_type {};
template <> struct is_basic_type<jdouble> : std::true_type {};
/** Type conversion trait, see @c JNI_DECLARE_CONVERTER. */
template <typename T> struct converter {
typedef void type;
};
......@@ -41,6 +41,25 @@ template <typename T> struct reverse_converter {
}
/**
* Declare a JNI type to be convertible to a native type.
* @param JniType A type declared with @p JNI_TYPE.
* @param NativeType A C++ type @p JniType can be converted to/from.
* @param FromJniFn Code converting a @c QAndroidJniObject @c value to @p NativeType.
* @param ToJniCode converting a @p NativeType @c value to a QAndroidJniObject.
*/
#define JNI_DECLARE_CONVERTER(JniType, NativeType, FromJniFn, ToJniFn) \
namespace Jni { \
template <> struct converter<NativeType> { \
typedef JniType type; \
static inline QAndroidJniObject convert(const NativeType &value) { return (ToJniFn); } \
}; \
template <> struct converter<JniType> { \
typedef NativeType type; \
static inline NativeType convert(const QAndroidJniObject &value) { return (FromJniFn); } \
}; \
}
}
#endif
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