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
|
// Copyright (C) 2024 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 ANDROIDBACKENDREGISTER_H
#define ANDROIDBACKENDREGISTER_H
#include <type_traits>
#include <QtCore/qjnienvironment.h>
#include <QtCore/qjnitypes.h>
#include <QtCore/qjniobject.h>
#include <QtCore/qstring.h>
#include <QtCore/qmap.h>
#include <QtCore/qmutex.h>
#include <QtCore/qloggingcategory.h>
QT_BEGIN_NAMESPACE
Q_DECLARE_LOGGING_CATEGORY(lcAndroidBackendRegister)
template <typename T>
using ValidInterfaceType = std::enable_if_t<std::is_base_of_v<QtJniTypes::JObjectBase, T>, bool>;
class AndroidBackendRegister
{
public:
static bool registerNatives();
template <typename T, ValidInterfaceType<T> = true>
[[nodiscard]] T getInterface()
{
QMutexLocker lock(&m_registerMutex);
return m_register.value(QString(QtJniTypes::Traits<T>::className().data()));
}
template <typename Object>
using IsObjectType =
typename std::disjunction<std::is_base_of<QJniObject, Object>,
std::is_base_of<QtJniTypes::JObjectBase, Object>>;
template <typename Interface, typename Ret, typename... Args,
ValidInterfaceType<Interface> = true>
auto callInterface(const char *func, Args... args)
{
if (const auto obj = getInterface<Interface>(); obj.isValid())
return obj.template callMethod<Ret, Args...>(func, std::forward<Args>(args)...);
if constexpr (IsObjectType<Ret>::value)
return Ret(QJniObject());
if constexpr (!std::is_same_v<Ret, void>)
return Ret{};
}
private:
QMutex m_registerMutex;
QMap<QString, QJniObject> m_register;
static void registerBackend(JNIEnv *, jclass, jclass interfaceClass, jobject interface);
Q_DECLARE_JNI_NATIVE_METHOD_IN_CURRENT_SCOPE(registerBackend)
static void unregisterBackend(JNIEnv *, jclass, jclass interfaceClass);
Q_DECLARE_JNI_NATIVE_METHOD_IN_CURRENT_SCOPE(unregisterBackend)
};
QT_END_NAMESPACE
#endif // ANDROIDBACKENDREGISTER_H
|