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
qcoreapplication_platform.h
Go to the documentation of this file.
1// Copyright (C) 2021 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 QCOREAPPLICATION_PLATFORM_H
6#define QCOREAPPLICATION_PLATFORM_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is part of the native interface APIs. Usage of
13// this API may make your code source and binary incompatible
14// with future versions of Qt.
15//
16
17#include <QtCore/qglobal.h>
18#include <QtCore/qnativeinterface.h>
19#include <QtCore/qcoreapplication.h>
20
21#if defined(Q_OS_ANDROID) || defined(Q_QDOC)
22#include <QtCore/qjnitypes.h>
23#if QT_CONFIG(future) && !defined(QT_NO_QOBJECT)
24#include <QtCore/qfuture.h>
25#include <QtCore/qvariant.h>
26#endif
27#endif // #if defined(Q_OS_ANDROID) || defined(Q_QDOC)
28
29#if defined(Q_OS_ANDROID)
30class _jobject;
31typedef _jobject* jobject;
32#endif
33
34#if defined(Q_OS_HARMONY) || defined(Q_QDOC)
35#include <QtCore/qharmonyjscontext.h>
36#include <functional>
37#include <optional>
38#include <type_traits>
39#endif // #if defined(Q_OS_HARMONY) || defined(Q_QDOC)
40
41QT_BEGIN_NAMESPACE
42
43namespace QNativeInterface
44{
45#if defined(Q_OS_ANDROID) || defined(Q_QDOC)
46struct Q_CORE_EXPORT QAndroidApplication
47{
48 QT_DECLARE_NATIVE_INTERFACE(QAndroidApplication, 1, QCoreApplication)
49#ifdef Q_QDOC
50 static QJniObject context();
51#else
52 static QtJniTypes::Context context();
53#endif
54 static bool isActivityContext();
55 static int sdkVersion();
56 static void hideSplashScreen(int duration = 0);
57
58#if QT_CONFIG(future) && !defined(QT_NO_QOBJECT)
59 static QFuture<QVariant> runOnAndroidMainThread(const std::function<QVariant()> &runnable,
60 const QDeadlineTimer timeout = QDeadlineTimer::Forever);
61
62 template <class T>
63 std::enable_if_t<std::is_invocable_v<T> && std::is_same_v<std::invoke_result_t<T>, void>,
64 QFuture<void>> static runOnAndroidMainThread(const T &runnable,
65 const QDeadlineTimer timeout = QDeadlineTimer::Forever)
66 {
67 std::function<QVariant()> func = [runnable](){ runnable(); return QVariant(); };
68 return static_cast<QFuture<void>>(runOnAndroidMainThread(func, timeout));
69 }
70#endif
71};
72#endif
73
74#if defined(Q_OS_HARMONY) || defined(Q_QDOC)
75struct Q_CORE_EXPORT QHarmonyApplication
76{
77 QT_DECLARE_NATIVE_INTERFACE(QHarmonyApplication, 1, QCoreApplication)
78
79 static void runOnJsThread(std::function<void(QHarmonyJsContext &)> task);
80 static void runOnJsThreadAndWait(const std::function<void(QHarmonyJsContext &)> &task);
81
82#ifdef Q_QDOC
83 template <typename Func, typename Result>
84 static Result runOnJsThreadAndWait(Func &&task);
85#else
86 template <typename Func, typename Result = std::invoke_result_t<Func, QHarmonyJsContext &>>
87 static std::enable_if_t<!std::is_void_v<Result>, Result> runOnJsThreadAndWait(Func &&task)
88 {
89 static_assert(!std::is_reference_v<Result>, "The task must return a value, not a reference");
90 static_assert(std::is_default_constructible_v<Result>, "The task's result type must be default-constructible");
91 static_assert(
92 !std::is_convertible_v<Result, napi_value> && !std::is_convertible_v<Result, napi_ref>,
93 "A napi_value or napi_ref result must not leave the JS thread");
94 std::optional<Result> optResult;
95 runOnJsThreadAndWait(
96 [&](QHarmonyJsContext &jsContext) {
97 auto result = std::invoke(std::forward<Func>(task), jsContext);
98 bool exceptionPending = false;
99 napi_is_exception_pending(jsContext.env(), &exceptionPending);
100 if (!exceptionPending)
101 optResult.emplace(std::move(result));
102 });
103 return optResult ? std::move(*optResult) : Result();
104 }
105#endif
106
107 static void runOnQtThread(std::function<void()> task);
108};
109#endif
110}
111
112QT_END_NAMESPACE
113
114#endif // QCOREAPPLICATION_PLATFORM_H