Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qjnitypes.h
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#ifndef QJNITYPES_H
6#define QJNITYPES_H
7
8#include <QtCore/qglobal.h>
9
10#if defined(Q_QDOC) || defined(Q_OS_ANDROID)
11
12#include <QtCore/qjnitypes_impl.h>
13#include <QtCore/qjniobject.h>
14#include <QtCore/qjniarray.h>
15
16#if 0
17// This is needed for generating the QtJniTypes forward header
18#pragma qt_class(QtJniTypes)
19#endif
20
21QT_BEGIN_NAMESPACE
22
23#define Q_DECLARE_JNI_TYPE_HELPER(Type) struct
24 Type##Tag { explicit Type##Tag() = default; }; using
25 Type = JObject<Type##Tag>;
26
27// internal - Q_DECLARE_JNI_CLASS is the public macro
28#define Q_DECLARE_JNI_TYPE(Type, Signature) namespace
29 QtJniTypes { Q_DECLARE_JNI_TYPE_HELPER
30 (Type) \
31} template
32 <> struct
33 ::QtJniTypes::Traits<QtJniTypes::Type##Tag> {
34 static constexpr auto signature()
35 {
36 constexpr QtJniTypes::CTString sig(Signature);
37 static_assert((sig.startsWith('L') || sig.startsWith("[L"))
38 && sig.endsWith(';'),
39 "Type signature needs to start with 'L' or"
40 " '[L', and end with ';'");
41 return sig;
42 } \
43};
44
45#define Q_DECLARE_JNI_CLASS_2(Type, _) Q_DECLARE_JNI_TYPE_HELPER
46 (Type)
47
48#define Q_DECLARE_JNI_CLASS_SPECIALIZATION_2(Type, Signature) template
49 <> struct
50 QtJniTypes::Traits<QtJniTypes::Type##Tag> {
51 static constexpr auto className()
52 {
53 return QtJniTypes::CTString(Signature);
54 }
55 static constexpr auto signature()
56 {
57 return QtJniTypes::CTString("L")
58 + className()
59 + QtJniTypes::CTString(";");
60 } \
61};
62
63#define Q_DECLARE_JNI_CLASS_3(NS0, NS1, Type) namespace
64 NS0 { namespace
65 NS1 { Q_DECLARE_JNI_CLASS_2
66 (Type, Q_UNUSED(0)) \
67} \
68}
69
70#define Q_DECLARE_JNI_CLASS_SPECIALIZATION_3(NS0, NS1, Type)
71 Q_DECLARE_JNI_CLASS_SPECIALIZATION_2(NS0::NS1::Type,
72 #NS0 "/" #NS1 "/" #Type)
73
74#define Q_DECLARE_JNI_CLASS_4(NS0, NS1, NS2, Type) namespace
75 NS0 { Q_DECLARE_JNI_CLASS_3
76 (NS1, NS2, Type) \
77}
78
79#define Q_DECLARE_JNI_CLASS_SPECIALIZATION_4(NS0, NS1, NS2, Type)
80 Q_DECLARE_JNI_CLASS_SPECIALIZATION_2(NS0::NS1::NS2::Type,
81 #NS0 "/" #NS1 "/" #NS2 "/" #Type)
82
83#define Q_DECLARE_JNI_CLASS_5(NS0, NS1, NS2, NS3, Type) namespace
84 NS0 { Q_DECLARE_JNI_CLASS_4
85 (NS1, NS2, NS3, Type) \
86}
87
88#define Q_DECLARE_JNI_CLASS_SPECIALIZATION_5(NS0, NS1, NS2, NS3, Type)
89 Q_DECLARE_JNI_CLASS_SPECIALIZATION_2(NS0::NS1::NS2::NS3::Type,
90 #NS0 "/" #NS1 "/" #NS2 "/" #NS3 "/" #Type)
91
92#define Q_DECLARE_JNI_CLASS_6(NS0, NS1, NS2, NS3, NS4, Type) namespace
93 NS0 { Q_DECLARE_JNI_CLASS_5
94 (NS1, NS2, NS3, NS4, Type) \
95}
96
97#define Q_DECLARE_JNI_CLASS_SPECIALIZATION_6(NS0, NS1, NS2, NS3, NS4, Type)
98 Q_DECLARE_JNI_CLASS_SPECIALIZATION_2(NS0::NS1::NS2::NS3::NS4::Type,
99 #NS0 "/" #NS1 "/" #NS2 "/" #NS3 "/" #NS4 "/" #Type)
100
101#define Q_DECLARE_JNI_CLASS_7(NS0, NS1, NS2, NS3, NS4, NS5, Type) namespace
102 NS0 { Q_DECLARE_JNI_CLASS_6
103 (NS1, NS2, NS3, NS4, NS5, Type) \
104}
105
106#define Q_DECLARE_JNI_CLASS_SPECIALIZATION_7(NS0, NS1, NS2, NS3, NS4, NS5, Type)
107 Q_DECLARE_JNI_CLASS_SPECIALIZATION_2(NS0::NS1::NS2::NS3::NS4::NS5::Type,
108 #NS0 "/" #NS1 "/" #NS2 "/" #NS3 "/" #NS4 "/" #NS5 "/" #Type)
109
110#define Q_DECLARE_JNI_CLASS_8(NS0, NS1, NS2, NS3, NS4, NS5, NS6, Type) namespace
111 NS0 { Q_DECLARE_JNI_CLASS_7
112 (NS1, NS2, NS3, NS4, NS5, NS6, Type) \
113}
114
115#define Q_DECLARE_JNI_CLASS_SPECIALIZATION_8(NS0, NS1, NS2, NS3, NS4, NS5, NS6, Type)
116 Q_DECLARE_JNI_CLASS_SPECIALIZATION_2(NS0::NS1::NS2::NS3::NS4::NS5::NS6::Type,
117 #NS0 "/" #NS1 "/" #NS2 "/" #NS3 "/" #NS4 "/" #NS5 "/" #NS6 "/" #Type)
118
119#define Q_DECLARE_JNI_CLASS_9(NS0, NS1, NS2, NS3, NS4, NS5, NS6, NS7, Type) namespace
120 NS0 { Q_DECLARE_JNI_CLASS_8
121 (NS1, NS2, NS3, NS4, NS5, NS6, NS7, Type) \
122}
123
124#define Q_DECLARE_JNI_CLASS_SPECIALIZATION_9(NS0, NS1, NS2, NS3, NS4, NS5, NS6, NS7, Type)
125 Q_DECLARE_JNI_CLASS_SPECIALIZATION_2(NS0::NS1::NS2::NS3::NS4::NS5::NS6::NS7::Type,
126 #NS0 "/" #NS1 "/" #NS2 "/" #NS3 "/" #NS4 "/" #NS5 "/" #NS6 "/" #NS7 "/" #Type)
127
128#define Q_DECLARE_JNI_CLASS(...) namespace
129 QtJniTypes { QT_OVERLOADED_MACRO
130 (Q_DECLARE_JNI_CLASS, __VA_ARGS__) \
131} QT_OVERLOADED_MACRO
132 (Q_DECLARE_JNI_CLASS_SPECIALIZATION, __VA_ARGS__)
133
134// Macros for native methods
135
136namespace QtJniMethods {
137namespace Detail {
138// A helper to forward the call to the registered function (with JNI types
139// as arguments) to the real function, using the type conversion implemented in
140// QJniObject::LocalFrame::convertTo/FromJni. This is needed because we want to
141// write functions that take Qt-style arguments (QJniObject, declared types,
142// QList etc), while Java can only call functions that take jobjects.
143template <typename Arg>
144struct JNITypeForArgImpl
145{
146 using JNIType = decltype(QtJniTypes::Traits<Arg>::convertToJni(nullptr, {}));
147 static Arg fromVarArg(JNIType t) // JNIType is always POD
148 {
149 // Special case: if convertToJni doesn't do anything, don't do anything
150 // here either. convertFromJni would always give us a QJniObject (so
151 // that QJniObject::callMethod etc returns an owning QJniObject).
152 if constexpr (std::is_same_v<JNIType, Arg>) {
153 return t;
154 } else {
155 return QtJniTypes::Traits<Arg>::convertFromJni(t);
156 }
157 }
158};
159
160template <typename Arg>
161using JNITypeForArg = typename JNITypeForArgImpl<std::decay_t<Arg>>::JNIType;
162
163// Returns go the other way round, from the declared C++ type to the JNI type.
164template <typename Ret>
165struct JNITypeForReturnImpl
166{
167 using JNIType = JNITypeForArg<Ret>;
168};
169template <>
170struct JNITypeForReturnImpl<void>
171{
172 using JNIType = void;
173};
174
175template <typename Ret>
176using JNITypeForReturn = typename JNITypeForReturnImpl<std::decay_t<Ret>>::JNIType;
177
178template <typename Ret, typename Fn, typename JType, typename ...Args>
179JNITypeForReturn<Ret> callAndConvert(JNIEnv *env, Fn function, JType thiz, Args &&...args)
180{
181 using Type = std::decay_t<Ret>;
182 if constexpr (std::is_same_v<Ret, void>) {
183 function(env, thiz, std::forward<Args>(args)...);
184 } else if constexpr (std::is_same_v<JNITypeForReturn<Ret>, Type>) {
185 return function(env, thiz, std::forward<Args>(args)...);
186 } else {
187 const auto value = function(env, thiz, std::forward<Args>(args)...);
188 const auto ref = QtJniTypes::Traits<Type>::convertToJni(env, value);
189 // value is about to go out of scope and may delete the reference it
190 // handed out, so give Java a local one that Java owns.
191 return static_cast<JNITypeForReturn<Ret>>(env->NewLocalRef(ref));
192 }
193}
194} // namespace Detail
195} // namespace QtJniMethods
196
197// Declaring a JNI method results in a struct with a template function call() that
198// gets instantiated with the return type and arguments of the declared method,
199// and registered with JNI. That template is implemented to call the declared
200// method, with arguments explicitly converted to the types the declared method
201// expects (e.g. jobject becomes QJniObject, a QString, a QList etc).
202#define Q_DECLARE_JNI_NATIVE_METHOD_HELPER(Method, Postfix, Name) struct
203 Method##_##Postfix { template
204 <typename Ret, typename JType, typename... Args> JNICALL
205 static QtJniMethods
206 ::Detail::JNITypeForReturn<Ret> call(JNIEnv *env, JType thiz,
207 QtJniMethods::Detail::JNITypeForArg<Args> ...args) \
208{
209 return QtJniMethods::Detail::callAndConvert<Ret>(env, Method, thiz,
210 QtJniMethods::Detail::JNITypeForArgImpl<
211 std::decay_t<Args>>::fromVarArg(args)...
212 ); \
213} static
214 constexpr auto signature = QtJniTypes::nativeMethodSignature(Method); template
215 <typename Ret, typename JType, typename ...Args> static
216 constexpr JNINativeMethod makeJNIMethod(Ret(*)(JNIEnv *, JType, Args...)) \
217{
218 return JNINativeMethod {
219 #Name, signature.data(),
220 reinterpret_cast<void *>(&call<Ret, JType, Args...>)
221 }; \
222} \
223};
224
225#define Q_DECLARE_JNI_NATIVE_METHOD(...)
226 QT_OVERLOADED_MACRO(QT_DECLARE_JNI_NATIVE_METHOD, __VA_ARGS__)
227
228#define QT_DECLARE_JNI_NATIVE_METHOD_2(Method, Name) namespace
229 QtJniMethods { Q_DECLARE_JNI_NATIVE_METHOD_HELPER
230 (Method, Helper, Name) \
231}
232
233#define QT_DECLARE_JNI_NATIVE_METHOD_1(Method)
234 QT_DECLARE_JNI_NATIVE_METHOD_2(Method, Method)
235
236#define Q_JNI_NATIVE_METHOD(Method)
237 QtJniMethods::Method##_Helper::makeJNIMethod(QT_PREPEND_NAMESPACE(Method))
238
239#define Q_DECLARE_JNI_NATIVE_METHOD_IN_CURRENT_SCOPE(...)
240 QT_OVERLOADED_MACRO(QT_DECLARE_JNI_NATIVE_METHOD_IN_CURRENT_SCOPE, __VA_ARGS__)
241
242#define QT_DECLARE_JNI_NATIVE_METHOD_IN_CURRENT_SCOPE_2(Method, Name)
243 Q_DECLARE_JNI_NATIVE_METHOD_HELPER(Method, QtJniMethod, Name)
244
245#define QT_DECLARE_JNI_NATIVE_METHOD_IN_CURRENT_SCOPE_1(Method)
246 QT_DECLARE_JNI_NATIVE_METHOD_IN_CURRENT_SCOPE_2(Method, Method)
247
248#define Q_JNI_NATIVE_SCOPED_METHOD(Method, Scope)
249 Scope::Method##_QtJniMethod::makeJNIMethod(Scope::Method)
250
251// Classes for value types
252Q_DECLARE_JNI_CLASS(String, "java/lang/String")
253Q_DECLARE_JNI_CLASS(Integer, "java/lang/Integer");
254Q_DECLARE_JNI_CLASS(Long, "java/lang/Long");
255Q_DECLARE_JNI_CLASS(Double, "java/lang/Double");
256Q_DECLARE_JNI_CLASS(Float, "java/lang/Float");
257Q_DECLARE_JNI_CLASS(Boolean, "java/lang/Boolean");
258Q_DECLARE_JNI_CLASS(Void, "java/lang/Void");
259
260// Utility and I/O
261Q_DECLARE_JNI_CLASS(UUID, "java/util/UUID")
262Q_DECLARE_JNI_CLASS(ArrayList, "java/util/ArrayList")
263Q_DECLARE_JNI_CLASS(HashMap, "java/util/HashMap")
264Q_DECLARE_JNI_CLASS(Set, "java/util/Set")
265Q_DECLARE_JNI_CLASS(File, "java/io/File");
266
267// Android specific types
268Q_DECLARE_JNI_CLASS(Uri, "android/net/Uri");
269Q_DECLARE_JNI_CLASS(Parcelable, "android/os/Parcelable");
270Q_DECLARE_JNI_CLASS(Context, "android/content/Context");
271Q_DECLARE_JNI_CLASS(Intent, "android/content/Intent");
272Q_DECLARE_JNI_CLASS(ContentResolver, "android/content/ContentResolver");
273Q_DECLARE_JNI_CLASS(Activity, "android/app/Activity");
274Q_DECLARE_JNI_CLASS(Service, "android/app/Service");
275
276#define QT_DECLARE_JNI_CLASS_STANDARD_TYPES
277
278QT_END_NAMESPACE
279
280#endif // defined(Q_QDOC) || defined(Q_OS_ANDROID)
281
282#endif // QJNITYPES_H