File indexing completed on 2024-06-23 05:08:44

0001 /*
0002     SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef KANDROIDEXTRAS_JNITYPETRAITS_H
0008 #define KANDROIDEXTRAS_JNITYPETRAITS_H
0009 
0010 #include "jniprimitivetypes.h"
0011 #include "jnitypes.h"
0012 
0013 #include <QJniObject>
0014 
0015 namespace KAndroidExtras {
0016 
0017 namespace Jni {
0018 
0019 template <typename T> class Array;
0020 
0021 /** Type conversion trait, see @c JNI_DECLARE_CONVERTER. */
0022 template <typename T> struct converter {
0023     typedef void type;
0024 };
0025 
0026 template <typename T> struct reverse_converter {
0027     typedef converter<typename converter<T>::type> type;
0028 };
0029 
0030 /** Type trait for checking whether @tparam T is a JNI array type. */
0031 template <typename T> struct is_array : std::false_type {};
0032 template <typename T> struct is_array<Array<T>> : std::true_type {};
0033 
0034 /** Type trais for checking whether @tparam T is a JNI object wrapper type. */
0035 template <typename T, typename = std::void_t<>> struct is_object_wrapper : std::false_type {};
0036 template <typename T> struct is_object_wrapper<T, std::void_t<typename T::_jni_ThisType>> : std::true_type {};
0037 
0038 /** Type trais for checking whether @tparam T is needs the generic JNI object wrapper (Jni::Object). */
0039 template <typename T> struct is_generic_wrapper : std::conditional_t<
0040     !is_primitive_type<T>::value && !is_array<T>::value && !is_object_wrapper<T>::value, std::true_type, std::false_type>
0041     {};
0042 
0043 }
0044 
0045 /**
0046  * Declare a JNI type to be convertible to a native type.
0047  * @param JniType A type declared with @p JNI_TYPE.
0048  * @param NativeType A C++ type @p JniType can be converted to/from.
0049  * @param FromJniFn Code converting a @c QJniObject @c value to @p NativeType.
0050  * @param ToJniCode converting a @p NativeType @c value to a QJniObject.
0051  */
0052 #define JNI_DECLARE_CONVERTER(JniType, NativeType, FromJniFn, ToJniFn) \
0053 namespace Jni { \
0054 template <> struct converter<NativeType> { \
0055     typedef JniType type; \
0056     static inline QJniObject convert(const NativeType &value) { return (ToJniFn); } \
0057 }; \
0058 template <> struct converter<JniType> { \
0059     typedef NativeType type; \
0060     static inline NativeType convert(const QJniObject &value) { return (FromJniFn); } \
0061 }; \
0062 }
0063 
0064 }
0065 
0066 #endif