summaryrefslogtreecommitdiffstats
path: root/src/corelib/kernel/qjnitypes_impl.h
blob: d963509023820f1dfbef3e1eff2497c9ce41f8d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef QJNITYPES_IMPL_H
#define QJNITYPES_IMPL_H

#include <QtCore/qglobal.h>
#include <QtCore/q20type_traits.h>

#if defined(Q_QDOC) || defined(Q_OS_ANDROID)
#include <jni.h>

QT_BEGIN_NAMESPACE

namespace QtJniTypes
{

// a constexpr type for string literals of any character width, aware of the length
// of the string.
template<size_t N_WITH_NULL, typename BaseType = char>
struct CTString
{
    BaseType m_data[N_WITH_NULL] = {};

    constexpr CTString() noexcept {}
    // Can be instantiated (only) with a string literal
    constexpr explicit CTString(const BaseType (&data)[N_WITH_NULL]) noexcept
    {
        for (size_t i = 0; i < N_WITH_NULL - 1; ++i)
            m_data[i] = data[i];
    }

    constexpr BaseType at(size_t i) const { return m_data[i]; }
    constexpr BaseType operator[](size_t i) const { return at(i); }
    static constexpr size_t size() noexcept { return N_WITH_NULL; }
    constexpr operator const BaseType *() const noexcept { return m_data; }
    constexpr const BaseType *data() const noexcept { return m_data; }
    template<size_t N2_WITH_NULL>
    constexpr bool startsWith(const BaseType (&lit)[N2_WITH_NULL]) const noexcept
    {
        if constexpr (N2_WITH_NULL > N_WITH_NULL) {
            return false;
        } else {
            for (size_t i = 0; i < N2_WITH_NULL - 1; ++i) {
                if (m_data[i] != lit[i])
                    return false;
            }
        }
        return true;
    }
    constexpr bool startsWith(BaseType c) const noexcept
    {
        return N_WITH_NULL > 1 && m_data[0] == c;
    }
    template<size_t N2_WITH_NULL>
    constexpr bool endsWith(const BaseType (&lit)[N2_WITH_NULL]) const noexcept
    {
        if constexpr (N2_WITH_NULL > N_WITH_NULL) {
            return false;
        } else {
            for (size_t i = 0; i < N2_WITH_NULL; ++i) {
                if (m_data[N_WITH_NULL - i - 1] != lit[N2_WITH_NULL - i - 1])
                    return false;
            }
        }
        return true;
    }
    constexpr bool endsWith(BaseType c) const noexcept
    {
        return N_WITH_NULL > 1 && m_data[N_WITH_NULL - 2] == c;
    }

    template<size_t N2_WITH_NULL>
    friend inline constexpr bool operator==(const CTString<N_WITH_NULL> &lhs,
                                            const CTString<N2_WITH_NULL> &rhs) noexcept
    {
        if constexpr (N_WITH_NULL != N2_WITH_NULL) {
            return false;
        } else {
            for (size_t i = 0; i < N_WITH_NULL - 1; ++i) {
                if (lhs.at(i) != rhs.at(i))
                    return false;
            }
        }
        return true;
    }

    template<size_t N2_WITH_NULL>
    friend inline constexpr bool operator!=(const CTString<N_WITH_NULL> &lhs,
                                            const CTString<N2_WITH_NULL> &rhs) noexcept
    {
        return !operator==(lhs, rhs);
    }

    template<size_t N2_WITH_NULL>
    friend inline constexpr bool operator==(const CTString<N_WITH_NULL> &lhs,
                                            const BaseType (&rhs)[N2_WITH_NULL]) noexcept
    {
        return operator==(lhs, CTString<N2_WITH_NULL>(rhs));
    }
    template<size_t N2_WITH_NULL>
    friend inline constexpr bool operator==(const BaseType (&lhs)[N2_WITH_NULL],
                                            const CTString<N_WITH_NULL> &rhs) noexcept
    {
        return operator==(CTString<N2_WITH_NULL>(lhs), rhs);
    }

    template<size_t N2_WITH_NULL>
    friend inline constexpr bool operator!=(const CTString<N_WITH_NULL> &lhs,
                                            const BaseType (&rhs)[N2_WITH_NULL]) noexcept
    {
        return operator!=(lhs, CTString<N2_WITH_NULL>(rhs));
    }
    template<size_t N2_WITH_NULL>
    friend inline constexpr bool operator!=(const BaseType (&lhs)[N2_WITH_NULL],
                                            const CTString<N_WITH_NULL> &rhs) noexcept
    {
        return operator!=(CTString<N2_WITH_NULL>(lhs), rhs);
    }

    template<size_t N2_WITH_NULL>
    friend inline constexpr auto operator+(const CTString<N_WITH_NULL> &lhs,
                                           const CTString<N2_WITH_NULL> &rhs) noexcept
    {
        char data[N_WITH_NULL + N2_WITH_NULL - 1] = {};
        for (size_t i = 0; i < N_WITH_NULL - 1; ++i)
            data[i] = lhs[i];
        for (size_t i = 0; i < N2_WITH_NULL - 1; ++i)
            data[N_WITH_NULL - 1 + i] = rhs[i];
        return CTString<N_WITH_NULL + N2_WITH_NULL - 1>(data);
    }
};

// Helper types that allow us to disable variadic overloads that would conflict
// with overloads that take a const char*.
template<typename T, size_t N = 0> struct IsStringType : std::false_type {};
template<> struct IsStringType<const char *, 0> : std::true_type {};
template<> struct IsStringType<const char *&, 0> : std::true_type {};
template<size_t N> struct IsStringType<CTString<N>> : std::true_type {};
template<size_t N> struct IsStringType<const char[N]> : std::true_type {};
template<size_t N> struct IsStringType<const char(&)[N]> : std::true_type {};
template<size_t N> struct IsStringType<char[N]> : std::true_type {};

template <typename T>
struct Traits {
    // The return type of className/signature becomes void for any type
    // not handled here. This indicates that the Traits type is not specialized
    // for the respective type, which we use to detect invalid types in the
    // IfValidSignatureTypes and IfValidFieldType predicates below.

    static constexpr auto className()
    {
        if constexpr (std::is_same_v<T, jstring>)
            return CTString("java/lang/String");
        else if constexpr (std::is_same_v<T, jobject>)
            return CTString("java/lang/Object");
        else if constexpr (std::is_same_v<T, jclass>)
            return CTString("java/lang/Class");
        else if constexpr (std::is_same_v<T, jthrowable>)
            return CTString("java/lang/Throwable");
        // else: return void -> not implemented
    }

    static constexpr auto signature()
    {
        if constexpr (!std::is_same_v<decltype(className()), void>) {
            // the type signature of any object class is L<className>;
            return CTString("L") + className() + CTString(";");
        } else if constexpr (std::is_array_v<T>) {
            using UnderlyingType = typename std::remove_extent_t<T>;
            static_assert(!std::is_array_v<UnderlyingType>,
                        "Traits::signature() does not handle multi-dimensional arrays");
            return CTString("[") + Traits<UnderlyingType>::signature();
        } else if constexpr (std::is_same_v<T, jobjectArray>) {
            return CTString("[Ljava/lang/Object;");
        } else if constexpr (std::is_same_v<T, jbooleanArray>) {
            return CTString("[Z");
        } else if constexpr (std::is_same_v<T, jbyteArray>) {
            return CTString("[B");
        } else if constexpr (std::is_same_v<T, jshortArray>) {
            return CTString("[S");
        } else if constexpr (std::is_same_v<T, jintArray>) {
            return CTString("[I");
        } else if constexpr (std::is_same_v<T, jlongArray>) {
            return CTString("[J");
        } else if constexpr (std::is_same_v<T, jfloatArray>) {
            return CTString("[F");
        } else if constexpr (std::is_same_v<T, jdoubleArray>) {
            return CTString("[D");
        } else if constexpr (std::is_same_v<T, jcharArray>) {
            return CTString("[C");
        } else if constexpr (std::is_same_v<T, jboolean>) {
            return CTString("Z");
        } else if constexpr (std::is_same_v<T, bool>) {
            return CTString("Z");
        } else if constexpr (std::is_same_v<T, jbyte>) {
            return CTString("B");
        } else if constexpr (std::is_same_v<T, jchar>) {
            return CTString("C");
        } else if constexpr (std::is_same_v<T, char>) {
            return CTString("C");
        } else if constexpr (std::is_same_v<T, jshort>) {
            return CTString("S");
        } else if constexpr (std::is_same_v<T, short>) {
            return CTString("S");
        } else if constexpr (std::is_same_v<T, jint>) {
            return CTString("I");
        } else if constexpr (std::is_same_v<T, int>) {
            return CTString("I");
        } else if constexpr (std::is_same_v<T, uint>) {
            return CTString("I");
        } else if constexpr (std::is_same_v<T, jlong>) {
            return CTString("J");
        } else if constexpr (std::is_same_v<T, quint64>) {
            return CTString("J");
        } else if constexpr (std::is_same_v<T, jfloat>) {
            return CTString("F");
        } else if constexpr (std::is_same_v<T, float>) {
            return CTString("F");
        } else if constexpr (std::is_same_v<T, jdouble>) {
            return CTString("D");
        } else if constexpr (std::is_same_v<T, double>) {
            return CTString("D");
        } else if constexpr (std::is_same_v<T, void>) {
            return CTString("V");
        } else if constexpr (std::is_enum_v<T>) {
            return Traits<std::underlying_type_t<T>>::signature();
        } else if constexpr (std::is_same_v<T, QString>) {
            return CTString("Ljava/lang/String;");
        }
        // else: return void -> not implemented
    }
};

template <typename Have, typename Want>
static constexpr bool sameTypeForJni = (QtJniTypes::Traits<Have>::signature()
                                        == QtJniTypes::Traits<Want>::signature())
                                    && (sizeof(Have) == sizeof(Want));

template <typename, typename = void>
struct Caller
{};

#define MAKE_CALLER(Type, Method) \
template <typename T> \
struct Caller<T, std::enable_if_t<sameTypeForJni<T, Type>>> \
{ \
    static constexpr void callMethodForType(JNIEnv *env, T &res, jobject obj, jmethodID id, va_list args) \
    { \
        res = T(env->Call##Method##MethodV(obj, id, args)); \
    } \
    static constexpr void callStaticMethodForType(JNIEnv *env, T &res, jclass clazz, jmethodID id, va_list args) \
    { \
        res = T(env->CallStatic##Method##MethodV(clazz, id, args)); \
    } \
    static constexpr void getFieldForType(JNIEnv *env, T &res, jobject obj, jfieldID id) \
    { \
        res = T(env->Get##Method##Field(obj, id)); \
    } \
    static constexpr void getStaticFieldForType(JNIEnv *env, T &res, jclass clazz, jfieldID id) \
    { \
        res = T(env->GetStatic##Method##Field(clazz, id)); \
    } \
    static constexpr void setFieldForType(JNIEnv *env, jobject obj, jfieldID id, T value) \
    { \
        env->Set##Method##Field(obj, id, static_cast<Type>(value)); \
    } \
    static constexpr void setStaticFieldForType(JNIEnv *env, jclass clazz, jfieldID id, T value) \
    { \
        env->SetStatic##Method##Field(clazz, id, static_cast<Type>(value)); \
    } \
}

MAKE_CALLER(jboolean, Boolean);
MAKE_CALLER(jbyte, Byte);
MAKE_CALLER(jchar, Char);
MAKE_CALLER(jshort, Short);
MAKE_CALLER(jint, Int);
MAKE_CALLER(jlong, Long);
MAKE_CALLER(jfloat, Float);
MAKE_CALLER(jdouble, Double);

#undef MAKE_CALLER

template<typename T>
static constexpr bool isPrimitiveType()
{
    return Traits<T>::signature().size() == 2;
}

template<typename T>
static constexpr bool isArrayType()
{
    constexpr auto signature = Traits<T>::signature();
    return signature.startsWith('[') && signature.size() > 2;
}

template<typename T>
static constexpr bool isObjectType()
{
    if constexpr (std::is_convertible_v<T, jobject>) {
        return true;
    } else {
        constexpr auto signature = Traits<T>::signature();
        return (signature.startsWith('L') && signature.endsWith(';')) || isArrayType<T>();
    }
}

template<typename T>
static constexpr void assertObjectType()
{
    static_assert(isObjectType<T>(),
                  "Type needs to be a JNI object type (convertible to jobject, or with "
                  "an object type signature registered)!");
}

// A set of types is valid if Traits::signature is implemented for all of them
template<typename ...Types>
constexpr bool ValidSignatureTypesDetail = !std::disjunction<std::is_same<
                                                    decltype(Traits<Types>::signature()),
                                                    void>...,
                                                    IsStringType<Types>...>::value;
template<typename ...Types>
using IfValidSignatureTypes = std::enable_if_t<
    ValidSignatureTypesDetail<q20::remove_cvref_t<Types>...>, bool>;

template<typename Type>
constexpr bool ValidFieldTypeDetail = isObjectType<Type>() || isPrimitiveType<Type>();
template<typename Type>
using IfValidFieldType = std::enable_if_t<
    ValidFieldTypeDetail<q20::remove_cvref_t<Type>>, bool>;


template<typename R, typename ...Args, IfValidSignatureTypes<R, Args...> = true>
static constexpr auto methodSignature()
{
    return (CTString("(") +
                ... + Traits<q20::remove_cvref_t<Args>>::signature())
            + CTString(")")
            + Traits<R>::signature();
}

template<typename T, IfValidSignatureTypes<T> = true>
static constexpr auto fieldSignature()
{
    return QtJniTypes::Traits<T>::signature();
}

template<typename ...Args, IfValidSignatureTypes<Args...> = true>
static constexpr auto constructorSignature()
{
    return methodSignature<void, Args...>();
}

template<typename Ret, typename ...Args, IfValidSignatureTypes<Ret, Args...> = true>
static constexpr auto nativeMethodSignature(Ret (*)(JNIEnv *, jobject, Args...))
{
    return methodSignature<Ret, Args...>();
}

template<typename Ret, typename ...Args, IfValidSignatureTypes<Ret, Args...> = true>
static constexpr auto nativeMethodSignature(Ret (*)(JNIEnv *, jclass, Args...))
{
    return methodSignature<Ret, Args...>();
}

} // namespace QtJniTypes

QT_END_NAMESPACE

#endif

#endif // QJNITYPES_IMPL_H