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
qohosmemoizingjsthreadfetcher_p.h
Go to the documentation of this file.
1// Copyright (C) 2026 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
4#ifndef QOHOSMEMOIZINGJSTHREADFETCHER_P_H
5#define QOHOSMEMOIZINGJSTHREADFETCHER_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtCore/private/qcore_ohos_p.h>
19#include <QtCore/qmutex.h>
20#include <atomic>
21#include <optional>
22#include <type_traits>
23
24QT_BEGIN_NAMESPACE
25
26// must be called from the JS thread, once the JS thread gateway is registered
27Q_CORE_EXPORT void qOhosEnableMemoizingJsThreadFetchers(QOhosJsState &jsState);
28
30
31class Q_CORE_EXPORT FetcherBase
32{
33public:
34 FetcherBase(const FetcherBase &) = delete;
35 FetcherBase &operator=(const FetcherBase &) = delete;
36
38
39protected:
40 explicit FetcherBase(const char *fetcherName);
41 virtual ~FetcherBase();
42
43 // must be called at the end of the most derived constructor
44 void registerSelf();
45 // must be called at the beginning of the most derived destructor
46 void unregisterSelf();
47
48 // may be called from any thread
49 void ensureFetched();
50
51private:
53
55
58 static std::atomic<bool> s_fetchingEnabled;
59
60 const char *m_fetcherName;
61 FetcherBase *m_nextFetcher = nullptr;
62 std::atomic<bool> m_fetched{false};
63};
64
65}
66
67template<typename T>
68class QOhosMemoizingJsThreadFetcher final : public details_qohosmemoizingjsthreadfetcher_p_h::FetcherBase
69{
70public:
71 static_assert(
72 !std::is_convertible<T, napi_value>::value && !std::is_convertible<T, napi_ref>::value,
73 "NAPI values/references must not be accessed outside the JS thread");
74
75 // Contract for fetchFunc:
76 // - it must return a value that never changes for the lifetime of the process, because it is read once and never refreshed
77 // - it runs in the JS thread, and never runs at all in a process that has no JS thread
78 // - it may run at any time after construction, not only at this fetcher's first optValue(), so it must not depend on state initialized later in the application startup
79 // - it must not read another fetcher
80 // - it must be cheap: its cost is paid by whoever triggers the batch it lands in
81 // - it may return nullopt, and any outcome, including an escaping exception, is final and never retried
82 // fetcherName is stored by pointer and must outlive the fetcher, so pass a string literal
84 std::optional<T> (*fetchFunc)(QOhosJsState &), const char *fetcherName);
86
87 // the first call may block until the JS thread runs the fetch function
88 std::optional<T> optValue();
89
90private:
91 void fetchInJsThread(QOhosJsState &jsState) override;
92
93 std::optional<T> (*m_fetchFunc)(QOhosJsState &);
94 std::optional<T> m_optValue;
95};
96
97template<typename T>
98QOhosMemoizingJsThreadFetcher<T>::QOhosMemoizingJsThreadFetcher(
99 std::optional<T> (*fetchFunc)(QOhosJsState &), const char *fetcherName)
100 : FetcherBase(fetcherName), m_fetchFunc(fetchFunc)
101{
102 registerSelf();
103}
104
105template<typename T>
106QOhosMemoizingJsThreadFetcher<T>::~QOhosMemoizingJsThreadFetcher()
107{
108 unregisterSelf();
109}
110
111template<typename T>
112std::optional<T> QOhosMemoizingJsThreadFetcher<T>::optValue()
113{
114 ensureFetched();
115 return m_optValue;
116}
117
118template<typename T>
119void QOhosMemoizingJsThreadFetcher<T>::fetchInJsThread(QOhosJsState &jsState)
120{
121 m_optValue = m_fetchFunc(jsState);
122}
123
124QT_END_NAMESPACE
125
126#endif // QOHOSMEMOIZINGJSTHREADFETCHER_P_H
QOhosMemoizingJsThreadFetcher(std::optional< T >(*fetchFunc)(QOhosJsState &), const char *fetcherName)
void fetchInJsThread(QOhosJsState &jsState) override
Combined button and popup list for selecting options.
void qOhosEnableMemoizingJsThreadFetchers(QOhosJsState &jsState)