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
qohoscommon_p.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 QOHOSCOMMON_H
5#define QOHOSCOMMON_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/qglobal.h>
19#include <QtCore/qlogging.h>
20#include <QtCore/qdebug.h>
21#include <cstdlib>
22#include <functional>
23#include <memory>
24#include <mutex>
25#ifndef QT_NO_EXCEPTIONS
26#include <stdexcept>
27#endif
28#include <type_traits>
29#include <utility>
30
31#define Q_OHOS_NAMED_FUNC(func) (QT_PREPEND_NAMESPACE(makeQOhosNamedFunc)<decltype(func)*, func>)(QT_STRINGIFY(func))
32
33#define qOhosReportFatalErrorAndAbort(...)
34 do {
35 QT_PREPEND_NAMESPACE(QMessageLogger)(QT_MESSAGELOG_FILE, QT_MESSAGELOG_LINE, QT_MESSAGELOG_FUNC).fatal(__VA_ARGS__);
36 std::abort();
37 } while (false)
38
39QT_BEGIN_NAMESPACE
40
41template<typename Func, typename... FuncArgs>
42using QOhosInvokeResult = decltype(std::declval<Func>()(std::declval<FuncArgs>()...));
43
44template<typename ...Ts>
45using QOhosConsumer = std::function<void(Ts...)>;
46
47template<typename T>
48using QOhosSupplier = std::function<T()>;
49
50template<typename Func, Func func>
52{
53public:
54 static constexpr Func funcPtr = func;
55
56 QOhosNamedFunc(const char *funcName);
57
58 const char *name() const;
59 Func ptr() const;
60
61 operator Func () const;
62
63private:
64 static_assert(std::is_function_v<std::remove_pointer_t<Func>>);
65
66 const char *m_funcName;
67};
68
69template<typename Func, Func func>
71{
72 return {funcName};
73}
74
76{
77public:
78 template<typename ...Ts>
79 void operator()(const Ts &...) const;
80};
81
82template<typename ...Ts>
84
86
87template<typename T>
89{
90public:
92
95
98
99 template<typename ProcessFunc>
100 void processValue(ProcessFunc &&processFunc);
101
102 template<typename EvalFunc>
103 auto evalWithValue(EvalFunc &&evalFunc) const -> decltype(evalFunc(std::declval<const T &>()));
104
105private:
106 mutable std::mutex m_valueMutex;
107 T m_value{};
108};
109
110namespace QtOhos {
111
112template<typename T>
113std::shared_ptr<T> moveToSharedPtr(T &&obj);
114
115template<typename T>
117 std::shared_ptr<T> baseSharedPtr, std::shared_ptr<void> extraData);
118
119template<typename T>
120std::weak_ptr<T> makeWeakPtr(const std::shared_ptr<T> &obj);
121
122std::shared_ptr<void> makeDestroyNotifier(std::function<void()> callOnDestroy);
123
124}
125
126template<typename Func, Func func>
127QOhosNamedFunc<Func, func>::QOhosNamedFunc(const char *funcName)
128 : m_funcName(funcName)
129{
130}
131
132template<typename Func, Func func>
133const char *QOhosNamedFunc<Func, func>::name() const
134{
135 return m_funcName;
136}
137
138template<typename Func, Func func>
139Func QOhosNamedFunc<Func, func>::ptr() const
140{
141 return func;
142}
143
144template<typename Func, Func func>
145QOhosNamedFunc<Func, func>::operator Func () const
146{
147 return func;
148}
149
150template<typename ...Ts>
151void QOhosNoOpConsumer::operator()(const Ts &...) const
152{
153}
154
155template<typename ...Ts>
157{
158 return [](const Ts &...) {
159 };
160}
161
166
167template<typename T>
169
170template<typename T>
171template<typename ProcessFunc>
172void QOhosMutexProtectedValue<T>::processValue(ProcessFunc &&processFunc)
173{
174 std::lock_guard<std::mutex> valueLock(m_valueMutex);
175 std::forward<ProcessFunc>(processFunc)(m_value);
176}
177
178template<typename T>
179template<typename EvalFunc>
180auto QOhosMutexProtectedValue<T>::evalWithValue(EvalFunc &&evalFunc) const -> decltype(evalFunc(std::declval<const T &>()))
181{
182 std::lock_guard<std::mutex> valueLock(m_valueMutex);
183 return std::forward<EvalFunc>(evalFunc)(m_value);
184}
185
186namespace QtOhos {
187
188template<typename T>
189std::shared_ptr<T> moveToSharedPtr(T &&obj)
190{
191 return std::make_shared<T>(std::forward<T>(obj));
192}
193
194template<typename T>
196 std::shared_ptr<T> baseSharedPtr, std::shared_ptr<void> extraData)
197{
198 auto *baseRawPtr = baseSharedPtr.get();
199 return std::shared_ptr<T>(
200 moveToSharedPtr(
201 std::make_pair(
202 std::move(baseSharedPtr),
203 std::move(extraData))),
204 baseRawPtr);
205}
206
207template<typename T>
208std::weak_ptr<T> makeWeakPtr(const std::shared_ptr<T> &obj)
209{
210 return obj;
211}
212
213inline std::shared_ptr<void> makeDestroyNotifier(std::function<void()> callOnDestroy)
214{
215 class DestroyNotifier
216 {
217 public:
218 explicit DestroyNotifier(std::function<void()> callOnDestroy)
219 : callOnDestroy(std::move(callOnDestroy))
220 {
221 }
222
223 DestroyNotifier(const DestroyNotifier &) = delete;
224 DestroyNotifier(DestroyNotifier &&) = delete;
225 DestroyNotifier &operator=(const DestroyNotifier &) = delete;
226 DestroyNotifier &operator=(DestroyNotifier &&) = delete;
227
228 ~DestroyNotifier()
229 {
230 callOnDestroy();
231 };
232
233 private:
234 std::function<void()> callOnDestroy;
235 };
236
237 return std::make_shared<DestroyNotifier>(std::move(callOnDestroy));
238}
239
240}
241
242QT_END_NAMESPACE
243
244#endif
auto evalWithValue(EvalFunc &&evalFunc) const -> decltype(evalFunc(std::declval< const T & >()))
QOhosMutexProtectedValue & operator=(const QOhosMutexProtectedValue< T > &other)=delete
void processValue(ProcessFunc &&processFunc)
QOhosMutexProtectedValue & operator=(QOhosMutexProtectedValue< T > &&other)=delete
QOhosMutexProtectedValue(const QOhosMutexProtectedValue< T > &other)=delete
QOhosMutexProtectedValue(QOhosMutexProtectedValue< T > &&other)=delete
operator Func() const
Func ptr() const
QOhosNamedFunc(const char *funcName)
const char * name() const
static constexpr Func funcPtr
void operator()(const Ts &...) const
std::shared_ptr< T > makeSharedPtrWithAttachedExtraData(std::shared_ptr< T > baseSharedPtr, std::shared_ptr< void > extraData)
std::shared_ptr< T > moveToSharedPtr(T &&obj)
std::weak_ptr< T > makeWeakPtr(const std::shared_ptr< T > &obj)
std::shared_ptr< void > makeDestroyNotifier(std::function< void()> callOnDestroy)
QOhosNoOpConsumer makeQOhosNoOpConsumer()
std::function< T()> QOhosSupplier
std::function< void(Ts...)> QOhosConsumer
QOhosConsumer< Ts... > makeQOhosNoOpConsumer()
QOhosNamedFunc< Func, func > makeQOhosNamedFunc(const char *funcName)