File indexing completed on 2024-06-23 05:08:44
0001 /* 0002 SPDX-FileCopyrightText: 2020-2022 Volker Krause <vkrause@kde.org> 0003 SPDX-License-Identifier: LGPL-2.0-or-later 0004 */ 0005 0006 #ifndef KANDROIDEXTRAS_JNIPRIMITIVETYPES_H 0007 #define KANDROIDEXTRAS_JNIPRIMITIVETYPES_H 0008 0009 #include "jni.h" 0010 0011 #include <type_traits> 0012 0013 namespace KAndroidExtras { 0014 0015 namespace Jni { 0016 0017 /** Type trait to check whether @tparam T is a primitive JNI type or an object type. */ 0018 template <typename T> struct is_primitive_type : std::false_type {}; 0019 template <> struct is_primitive_type<jboolean> : std::true_type {}; 0020 template <> struct is_primitive_type<jbyte> : std::true_type {}; 0021 template <> struct is_primitive_type<jchar> : std::true_type {}; 0022 template <> struct is_primitive_type<jshort> : std::true_type {}; 0023 template <> struct is_primitive_type<jint> : std::true_type {}; 0024 template <> struct is_primitive_type<jlong> : std::true_type {}; 0025 template <> struct is_primitive_type<jfloat> : std::true_type {}; 0026 template <> struct is_primitive_type<jdouble> : std::true_type {}; 0027 template <> struct is_primitive_type<void> : std::true_type {}; 0028 // special case for bool <-> jboolean conversion 0029 template <> struct is_primitive_type<bool> : std::true_type {}; 0030 0031 } 0032 0033 namespace Internal { 0034 0035 /** Utility trait to check for basic C++ types that are not JNI primitive types. */ 0036 template <typename T> struct is_invalid_primitive_type : std::false_type {}; 0037 template <> struct is_invalid_primitive_type<uint8_t> : std::true_type {}; 0038 template <> struct is_invalid_primitive_type<char> : std::true_type {}; 0039 template <> struct is_invalid_primitive_type<uint32_t> : std::true_type {}; 0040 template <> struct is_invalid_primitive_type<uint64_t> : std::true_type {}; 0041 0042 // C++ primitive to JNI primitive conversion 0043 template <typename T> 0044 struct primitive_value { 0045 static_assert(!is_invalid_primitive_type<T>::value, "Using an incompatible primitive type!"); 0046 typedef T JniType; 0047 typedef T NativeType; 0048 static constexpr inline T toJni(T value) { return value; } 0049 static constexpr inline T fromJni(T value) { return value; } 0050 }; 0051 template <> struct primitive_value<void> {}; 0052 template <> 0053 struct primitive_value<jboolean> { 0054 typedef jboolean JniType; 0055 typedef bool NativeType; 0056 static constexpr inline jboolean toJni(bool value) { return value ? 1 : 0; } 0057 static constexpr inline bool fromJni(jboolean value) { return value != 0; } 0058 }; 0059 template <> 0060 struct primitive_value<bool> { 0061 typedef jboolean JniType; 0062 typedef bool NativeType; 0063 static constexpr inline jboolean toJni(bool value) { return value ? 1 : 0; } 0064 static constexpr inline bool fromJni(jboolean value) { return value != 0; } 0065 }; 0066 0067 } 0068 } 0069 0070 #endif