diff --git a/autotests/jnisignaturetest.cpp b/autotests/jnisignaturetest.cpp index f432201..6c72602 100644 --- a/autotests/jnisignaturetest.cpp +++ b/autotests/jnisignaturetest.cpp @@ -1,48 +1,55 @@ /* Copyright (C) 2019 Volker Krause This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include <../src/kandroidextras/jnisignature.h> #include <../src/kandroidextras/jnitypes.h> #include using namespace KAndroidExtras; class JniSignatureTest : public QObject { Q_OBJECT private Q_SLOTS: void testSignature() { QCOMPARE((const char*)Jni::signature(), "Z"); + QCOMPARE((const char*)Jni::signature(), "()Z"); QCOMPARE((const char*)Jni::signature(), "([F)V"); QCOMPARE((const char*)Jni::signature(), "(Ljava/lang/String;)V"); QCOMPARE((const char*)Jni::signature(), "()Ljava/lang/String;"); QCOMPARE((const char*)Jni::signature(), "(Ljava/lang/String;[Z)Landroid/content/Intent;"); + QCOMPARE((const char*)Jni::signature(), "(Landroid/net/Uri;[Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;)Landroid/database/Cursor;"); + } + + void testTypeName() + { + QCOMPARE((const char*)Jni::typeName(), "android/provider/OpenableColumns"); } void testImplementationDetails() { static_assert(Internal::static_strlen("Hello!") == 6, ""); QCOMPARE(java::lang::String::jniName(), "java/lang/String"); QCOMPARE(((const char*)Internal::staticStringFromJniType>::value()), "java/lang/String"); } }; QTEST_GUILESS_MAIN(JniSignatureTest) #include "jnisignaturetest.moc" diff --git a/src/kandroidextras/jnitypes.h b/src/kandroidextras/jnitypes.h index 7bfd8e9..b17a55f 100644 --- a/src/kandroidextras/jnitypes.h +++ b/src/kandroidextras/jnitypes.h @@ -1,58 +1,71 @@ /* Copyright (C) 2019 Volker Krause This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #ifndef KANDROIDEXTRAS_JNITYPES_H #define KANDROIDEXTRAS_JNITYPES_H +namespace KAndroidExtras { + // determine how many elements are in __VA_ARGS__ #define PP_NARG(...) PP_NARG_(__VA_ARGS__, PP_RSEQ_N()) #define PP_NARG_(...) PP_ARG_N(__VA_ARGS__) #define PP_ARG_N(_1, _2, _3, _4, _5, _6, _7, N, ...) N #define PP_RSEQ_N() 7, 6, 5, 4, 3, 2, 1, 0 // preprocessor-level token concat #define PP_CONCAT(arg1, arg2) PP_CONCAT1(arg1, arg2) #define PP_CONCAT1(arg1, arg2) PP_CONCAT2(arg1, arg2) #define PP_CONCAT2(arg1, arg2) arg1##arg2 // preprocessor "iteration" #define JNI_TYPE_1(name, type, ...) \ struct type { static constexpr const char* jniName() { return name #type; } }; #define JNI_TYPE_2(name, type, ...) \ namespace type { JNI_TYPE_1(name #type "/", __VA_ARGS__) } #define JNI_TYPE_3(name, type, ...) \ namespace type { JNI_TYPE_2(name #type "/", __VA_ARGS__) } #define JNI_TYPE_4(name, type, ...) \ namespace type { JNI_TYPE_3(name #type "/", __VA_ARGS__) } #define JNI_TYPE_5(name, type, ...) \ namespace type { JNI_TYPE_4(name #type "/", __VA_ARGS__) } #define JNI_TYPE_6(name, type, ...) \ namespace type { JNI_TYPE_5(name #type "/", __VA_ARGS__) } #define JNI_TYPE_7(name, type, ...) \ namespace type { JNI_TYPE_6(#type "/", __VA_ARGS__) } #define JNI_TYPE_(N, name, ...) PP_CONCAT(JNI_TYPE_, N)(name, __VA_ARGS__) /** Macro to define Java types with their corresponding JNI signature strings. */ #define JNI_TYPE(...) JNI_TYPE_(PP_NARG(__VA_ARGS__), "", __VA_ARGS__) // type declarations +JNI_TYPE(android, content, ContentResolver) JNI_TYPE(android, content, Intent) +JNI_TYPE(android, database, Cursor) JNI_TYPE(android, net, Uri) +JNI_TYPE(android, provider, OpenableColumns) JNI_TYPE(java, lang, String) +namespace Jni +{ + /** Returns the JNI type name of the given template arugment. */ + template inline const char* typeName() { return T::jniName(); } +} + +} + #endif // KANDROIDEXTRAS_JNITYPES_H