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
qohosutils.h
Go to the documentation of this file.
1// Copyright (C) 2025 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 QOHOSUTILS_H
5#define QOHOSUTILS_H
6
7#include <QtCore/private/qohoscommon_p.h>
8#include <QtCore/qglobal.h>
9#include <QtCore/qlogging.h>
10#include <cstdint>
11#include <cstdlib>
12#include <functional>
13#include <limits>
14#include <memory>
15#include <mutex>
16#include <qohosplugincore.h>
17#include <string>
18#include <tuple>
19#include <type_traits>
20#include <utility>
21
22QT_BEGIN_NAMESPACE
23
24namespace QtOhos
25{
26
28
29QOhosOptional<std::uintmax_t> tryParseStringAsUIntMax(const std::string &inputString);
30
31}
32
33template<typename IdValueType, typename TypeTag>
35{
36public:
37 using ValueType = IdValueType;
38
39 explicit TypedId(IdValueType value);
40 TypedId() = default;
41
42 TypedId(const TypedId &other);
43 TypedId &operator=(const TypedId &other);
44
45 TypedId(TypedId &&other);
47
48 ~TypedId() = default;
49
50 bool operator==(const TypedId &other) const;
51 bool operator!=(const TypedId &other) const;
52 bool operator<(const TypedId &other) const;
53
54 IdValueType value() const;
55
56private:
57 IdValueType m_value;
58};
59
60template<typename ...Ts>
62 bool, std::make_tuple(bool(Ts::value)...) == std::make_tuple(((void) Ts::value, true)...)>;
63
64template<typename ForwardIt, typename Predicate>
65ForwardIt removeMatchingWithLookahead(ForwardIt firstIt, ForwardIt lastIt, Predicate &&predicate);
66
68{
69 template<typename... Args>
70 EvaluateSequentially(Args && ...) {}
71};
72
73template<typename UnaryFunc, typename... TupleElements, std::size_t... TupleIndices>
74void tupleForEach(const std::tuple<TupleElements...> &tuple, UnaryFunc func, std::index_sequence<TupleIndices...>);
75
76template<typename UnaryFunc, typename... TupleElements>
77void tupleForEach(const std::tuple<TupleElements...> &tuple, UnaryFunc func);
78
79template<typename T>
81 QOhosConsumer<T> baseConsumer, QOhosConsumer<std::function<void()>> asyncExecutor);
82
83template<typename ...Ts, typename BaseConsumer>
86
87QOhosOptional<double> tryParseStringAsFiniteDouble(const std::string &inputString);
88
89template<typename T>
91tryParseStringAsUnsignedInteger(const std::string &inputString);
92
94
95const char *mapBoolToTrueFalseStr(bool value);
96
98 QtOhos::JsState &jsState, QOhosOptional<QtOhos::QObjectThreadSafeRef> optInstanceMainWindowRef);
99
100template<typename ForwardIt, typename Predicate>
101ForwardIt removeMatchingWithLookahead(ForwardIt firstIt, ForwardIt lastIt, Predicate &&predicate)
102{
103 if (firstIt == lastIt)
104 return firstIt;
105
106 auto outputIt = firstIt;
107 auto currentIt = firstIt;
108 auto nextIt = currentIt;
109 ++nextIt;
110
111 while (nextIt != lastIt) {
112 if (!predicate(*currentIt, *nextIt)) {
113 if (outputIt != currentIt) {
114 *outputIt = std::move(*currentIt);
115 }
116 ++outputIt;
117 }
118 ++currentIt;
119 ++nextIt;
120 }
121
122 if (outputIt != currentIt) {
123 *outputIt = std::move(*currentIt);
124 }
125 ++outputIt;
126
127 return outputIt;
128}
129
130template<typename UnaryFunc, typename... TupleElements, std::size_t... TupleIndices>
131void tupleForEach(const std::tuple<TupleElements...> &tuple, UnaryFunc func, std::index_sequence<TupleIndices...>)
132{
133 EvaluateSequentially { (func(std::get<TupleIndices>(tuple)), 0)... };
134}
135
136template<typename UnaryFunc, typename... TupleElements>
137void tupleForEach(const std::tuple<TupleElements...> &tuple, UnaryFunc func)
138{
139 tupleForEach(tuple, func, std::make_index_sequence<sizeof...(TupleElements)>());
140}
141
142template<typename T>
143constexpr T makeCopyByValue(T value)
144{
145 return value;
146}
147
148template<typename IdValueType, typename Tag>
149TypedId<IdValueType, Tag>::TypedId(IdValueType value)
150 : m_value(value)
151{
152}
153
154template<typename IdValueType, typename Tag>
155bool TypedId<IdValueType, Tag>::operator==(const TypedId &other) const
156{
157 return m_value == other.m_value;
158}
159
160template<typename IdValueType, typename Tag>
161bool TypedId<IdValueType, Tag>::operator!=(const TypedId &other) const
162{
163 return m_value != other.m_value;
164}
165
166template<typename IdValueType, typename Tag>
167bool TypedId<IdValueType, Tag>::operator<(const TypedId &other) const
168{
169 return m_value < other.m_value;
170}
171
172template<typename IdValueType, typename Tag>
173IdValueType TypedId<IdValueType, Tag>::value() const
174{
175 return m_value;
176}
177
178template<typename IdValueType, typename Tag>
179TypedId<IdValueType, Tag>::TypedId(const TypedId &other) = default;
180
181template<typename IdValueType, typename Tag>
182TypedId<IdValueType, Tag> &TypedId<IdValueType, Tag>::operator=(const TypedId &other) = default;
183
184template<typename IdValueType, typename Tag>
185TypedId<IdValueType, Tag>::TypedId(TypedId &&other) = default;
186
187template<typename IdValueType, typename Tag>
188TypedId<IdValueType, Tag> &TypedId<IdValueType, Tag>::operator=(TypedId &&other) = default;
189
190template<typename T>
192 QOhosConsumer<T> baseConsumer, QOhosConsumer<std::function<void()>> asyncExecutor)
193{
194 struct Context
195 {
196 QOhosConsumer<T> baseConsumer;
197 QOhosConsumer<std::function<void()>> asyncExecutor;
198 std::mutex pendingValueMutex;
199 QOhosOptional<T> pendingValue;
200 };
201
202 auto context = std::make_shared<Context>();
203 context->baseConsumer = std::move(baseConsumer);
204 context->asyncExecutor = std::move(asyncExecutor);
205
206 return [context](T value) {
207 std::lock_guard<std::mutex> pendingValueLock(context->pendingValueMutex);
208 if (!context->pendingValue.hasValue()) {
209 context->asyncExecutor(
210 [context]() {
211 QOhosOptional<T> pendingValue;
212 {
213 std::lock_guard<std::mutex> pendingValueLock(context->pendingValueMutex);
214 std::swap(pendingValue, context->pendingValue);
215 }
216 context->baseConsumer(pendingValue.value());
217 });
218 }
219 context->pendingValue.emplace(std::move(value));
220 };
221}
222
223template<typename ...Ts, typename BaseConsumer>
226{
227 return [baseConsumer = QOhosConsumer<Ts...>(std::move(baseConsumer))](Ts &&...args) mutable {
228 if (baseConsumer) {
229 std::exchange(baseConsumer, nullptr)(std::forward<Ts>(args)...);
230 return true;
231 } else {
232 return false;
233 }
234 };
235}
236
237template<typename T>
239tryParseStringAsUnsignedInteger(const std::string &inputString)
240{
241 auto parsedValue = qohosutils_details::tryParseStringAsUIntMax(inputString);
242 bool valueValidForType = parsedValue.hasValue()
243 && parsedValue.value() <= std::numeric_limits<T>::max();
244 return valueValidForType
245 ? makeQOhosOptional(static_cast<T>(parsedValue.value()))
247}
248
249}
250
251QT_END_NAMESPACE
252
253#endif // QOHOSUTILS_H
QNapi::Object jsObject()
Definition window.cpp:77
JsWindowRef(JsWindowRef &&)=delete
bool isFocused() const
Definition window.cpp:65
JsWindowRef(const JsWindowRef &)=delete
JsWindowRef & operator=(JsWindowRef &&)=delete
JsWindowRef(JsWindowId windowId, QNapi::Object jsWindow)
Definition window.cpp:46
Result call(const std::string &methodName, const std::vector< QNapi::ValueWrapper > &args={}) const
Definition window.h:59
bool isWindowShown() const
Definition window.cpp:52
JsWindowId id() const
Definition window.cpp:72
JsWindowRef & operator=(const JsWindowRef &)=delete
QOhosOptional()=default
QOhosOptional(const std::optional< U > &other)
T valueOr(const T &fallback) const
QOhosOptional< T > & operator=(const std::optional< U > &other)
bool hasValue() const
QOhosOptional(const QOhosOptional< void > &empty)
QOhosOptional< T > & operator=(const T &value)
std::enable_if_t< qohosplugincore_h_detail::isQOhosOptional< QOhosInvokeResult< Func, T > >, QOhosInvokeResult< Func, T > > andThen(Func &&func) const
QOhosOptional< T > & operator=(const QOhosOptional< void > &empty)
QOhosOptional(const T &value)
const T & value() const
T & emplace(Args &&...args)
QOhosOptional< T > & operator=(const QOhosOptional< T > &other)
QOhosOptional< QOhosInvokeResult< Func, T > > transform(Func &&func) const
QOhosOptional(const QOhosOptional< T > &other)
virtual void startQAbilityInstance(QNapi::Object baseQAbility, QObjectThreadSafeRef qwindow, QNapi::Object optStartOptions, std::function< void(JsState &, std::shared_ptr< QAbilityPeer >)> startupNotifyFunc)=0
virtual void tagWidgetOrWindowAsFloatWindow(QObject *widgetOrWindow, bool floatWindowEnabled)=0
virtual void startAppProcess(QNapi::Object baseQAbility, const std::string &processId, QNapi::Object want, QNapi::Object optStartOptions)=0
virtual void startAppProcess(QNapi::Object baseQAbility, const std::string &processId, QNapi::Object want, QNapi::Object optStartOptions, std::function< void(JsState &)> continueFunc)=0
virtual void startNoUiChildProcess(JsState &jsState, const std::string &libraryName, const std::vector< std::string > &args)=0
JsState & jsState() const
JsState & operator=(const JsState &)=delete
virtual void addNewWantConsumer(QOhosConsumer< JsState &, QNapi::Object, QNapi::Object > wantConsumer)=0
virtual QtRunMode qtRunMode()=0
JsState(const JsState &)=delete
virtual std::shared_ptr< QAbilityPeer > tryGetQAbilityPeerByInstanceId(const std::string &instanceId)=0
virtual void * getAttachedObjectWithLazyCreate(const std::type_info &objectTypeInfo, QOhosSupplier< std::shared_ptr< void > > objectFactory)=0
virtual void startAppProcess(const std::string &processId, QNapi::Object requestWant, QNapi::Object optStartOptions, std::function< void(JsState &)> continueFunc)=0
virtual void startNoUiChildProcess(const std::string &libraryName, const std::vector< std::string > &args)=0
~JsState() override
virtual std::shared_ptr< QAbilityPeer > tryGetQAbilityPeerByQWindow(QObjectThreadSafeRef qwindow)=0
virtual void visitEachQAbilityPeer(const std::function< void(std::shared_ptr< QAbilityPeer >)> &visitor)=0
QNapi::Symbol getJsSymbolForType()
virtual std::shared_ptr< QAbilityPeer > defaultQAbilityPeer()=0
virtual std::shared_ptr< QAbilityPeer > tryGetQAbilityPeerByInstance(QNapi::Object qAbility)=0
virtual QOhosOptional< QNapi::Object > optAppLaunchParam()=0
virtual QNapi::Object appLaunchWant()=0
std::enable_if_t< std::is_default_constructible< T >::value, T > & getAttachedObjectWithLazyCreate()
virtual QNapi::Symbol getJsSymbolForType(const std::type_info &typeInfo)=0
virtual void startNewQAbilityInstance(std::shared_ptr< QAbilityPeer > baseQAbilityPeer, QObjectThreadSafeRef qwindow, QNapi::Object optStartOptions, std::function< void(JsState &, std::shared_ptr< QAbilityPeer >)> startupNotifyFunc)=0
virtual void startAppProcess(const std::string &processId, QNapi::Object requestWant, QNapi::Object optStartOptions={})=0
static void tagWindowAsClosing(QNapi::Object jsWindow, const char *logContext)
static bool isWindowClosing(QNapi::Object jsWindow)
virtual QAbilityInfo readAbilityInfo(const QNapi::Object &ability) const =0
virtual QOhosOptional< QNapi::Promise > qWindowDestroyPromise()=0
virtual std::string instanceId()=0
virtual QObjectThreadSafeRef qWindowRef()=0
virtual QNapi::Object launchWant()=0
virtual bool isTerminating()=0
virtual void setQWindow(Napi::Env env, QObjectThreadSafeRef qwindow)=0
virtual void * tryCastWithTypeIdObject(const void *matchTypeIdObject)=0
virtual std::shared_ptr< std::atomic_bool > destroyAllowedFlag()=0
virtual QNapi::Object qAbility()=0
virtual QNapi::Object uiContext()=0
virtual void setOnContinueRequestsHandler(std::function< void(JsState &, QNapi::Object, QOhosConsumer< JsState &, QOhosAbilityOnContinueResult >)> requestsHandler)=0
virtual QNapi::Object launchParam()=0
virtual QNapi::Object windowStage()=0
void * tryCastWithTypeIdObject(const void *matchTypeIdObject) final
static std::shared_ptr< QUiAbilityPeer > tryCastFromQAbilityPeerOrNull(std::shared_ptr< QAbilityPeer > qAbilityPeer)
~QUiAbilityPeer() override
virtual QNapi::Object window()=0
bool operator!=(const TypedId &other) const
Definition qohosutils.h:161
IdValueType value() const
Definition qohosutils.h:173
IdValueType ValueType
Definition qohosutils.h:37
bool operator==(const TypedId &other) const
Definition qohosutils.h:155
TypedId(IdValueType value)
Definition qohosutils.h:149
TypedId(const TypedId &other)
TypedId & operator=(const TypedId &other)
~TypedId()=default
TypedId & operator=(TypedId &&other)
bool operator<(const TypedId &other) const
Definition qohosutils.h:167
TypedId()=default
TypedId(TypedId &&other)
QOhosOptional< WindowProperties > tryGetWindowProperties(JsWindowId jsWindowId)
Definition window.cpp:27
Combined button and popup list for selecting options.
QOhosOptional< std::uintmax_t > tryParseStringAsUIntMax(const std::string &inputString)
std::string const char * mapBoolToTrueFalseStr(bool value)
void invokeInJsThread(std::function< void(JsState &)> task)
void dispatchNewWant(QNapi::Object want, QNapi::Object launchParam)
QOhosConsumer< T > makeCompressingAsyncConsumer(QOhosConsumer< T > baseConsumer, QOhosConsumer< std::function< void()> > asyncExecutor)
Definition qohosutils.h:191
void removeMatchingJsQAbilityPeer(QNapi::Object qAbility)
std::enable_if_t< std::is_integral< T >::value &&std::is_unsigned< T >::value, QOhosOptional< T > > tryParseStringAsUnsignedInteger(const std::string &inputString)
Definition qohosutils.h:239
T evalInJsThreadWithConsumer(std::function< void(QtOhos::JsState &, std::function< void(T)>)> evalFunc)
ForwardIt removeMatchingWithLookahead(ForwardIt firstIt, ForwardIt lastIt, Predicate &&predicate)
Definition qohosutils.h:101
void initJsThreadState(napi_env env, std::map< std::string, QNapi::Reference< QNapi::Function > > &&jsModulesFactories, std::shared_ptr< AppFunctions > appFunctions, QtRunMode qtRunMode)
Q_REQUIRED_RESULT bool tryInvokeInQtThreadAndTryWaitForContinue(std::function< void(std::function< void()>)> &&task, std::chrono::nanoseconds timeout)
auto evalInJsThread(Func &&func) -> decltype(func(std::declval< JsState & >()))
QOhosAbilityOnContinueResult
void tupleForEach(const std::tuple< TupleElements... > &tuple, UnaryFunc func, std::index_sequence< TupleIndices... >)
Definition qohosutils.h:131
void runInJsThreadAndWait(const std::function< void(JsState &)> &task)
std::shared_ptr< QtOhos::QAbilityPeer > tryMapOptMainWindowToAbilityPeer(QtOhos::JsState &jsState, QOhosOptional< QtOhos::QObjectThreadSafeRef > optInstanceMainWindowRef)
void tupleForEach(const std::tuple< TupleElements... > &tuple, UnaryFunc func)
Definition qohosutils.h:137
void invokeInJsThreadAndWaitForContinue(std::function< void(JsState &, std::function< void()>)> &&task)
QOhosOptional< double > tryParseStringAsFiniteDouble(const std::string &inputString)
void addJsQAbilityPeer(std::shared_ptr< QAbilityPeer > qAbilityPeer)
constexpr T makeCopyByValue(T value)
Definition qohosutils.h:143
constexpr bool isQOhosOptional
constexpr bool hasEqualityComparator
Q_DECLARE_METATYPE(QT_PREPEND_NAMESPACE(QOhosDisplayInfo::DisplaySourceMode))
Q_DECLARE_METATYPE(QT_PREPEND_NAMESPACE(QOhosDisplayInfo::JsDisplayOrientation))
QT_END_NAMESPACE Q_DECLARE_METATYPE(QT_PREPEND_NAMESPACE(QOhosPlatformWindow::NativeNodeRenderFitPolicy))
std::enable_if_t< qohosplugincore_h_detail::hasEqualityComparator< T, U >, bool > operator!=(const QOhosOptional< T > &lhs, const U &rhs)
std::enable_if_t< qohosplugincore_h_detail::hasEqualityComparator< T, U >, bool > operator==(const QOhosOptional< T > &lhs, const QOhosOptional< U > &rhs)
std::enable_if_t< qohosplugincore_h_detail::hasEqualityComparator< T, U >, bool > operator!=(const T &lhs, const QOhosOptional< U > &rhs)
std::enable_if_t< qohosplugincore_h_detail::hasEqualityComparator< T, U >, bool > operator==(const T &lhs, const QOhosOptional< U > &rhs)
QOhosOptional< void > makeEmptyQOhosOptional()
QOhosOptional< T > makeQOhosOptional(const T &value)
std::enable_if_t< qohosplugincore_h_detail::hasEqualityComparator< T, U >, bool > operator!=(const QOhosOptional< T > &lhs, const QOhosOptional< U > &rhs)
std::enable_if_t< qohosplugincore_h_detail::hasEqualityComparator< T, U >, bool > operator==(const QOhosOptional< T > &lhs, const U &rhs)
QOhosOptional< QOhosDisplayInfo::JsDisplayId > displayId
Definition window.h:28
QSizeF physicalSize() const
QOhosOptional< QPoint > topLeftOffsetPixels
QRect displayGeometryPixels() const
static QOhosDisplayInfo makeFromOhosDisplayObject(QtOhos::JsState &jsState, QNapi::Object displayObject)
QOhosOptional< JsDisplayOrientation > orientation
QOhosOptional< DisplaySourceMode > sourceMode
static constexpr std::array< std::pair< QOhosAbilityOnContinueResult, const char * >, 3 > enumeratorsNames