20 bool m_autoDelete =
true;
22 Q_DISABLE_COPY(QRunnable)
24 virtual void run() = 0;
26 constexpr QRunnable()
noexcept =
default;
28#if QT_CORE_REMOVED_SINCE(6
, 6
)
29 static QRunnable *create(std::function<
void()> functionToRun);
31 template <
typename Callable>
32 using if_callable = std::enable_if_t<std::is_invocable_r_v<
void, Callable>,
bool>;
34 template <
typename Callable, if_callable<Callable> =
true>
35 static QRunnable *create(Callable &&functionToRun);
36 static QRunnable *create(std::nullptr_t) =
delete;
38 bool autoDelete()
const {
return m_autoDelete; }
39 void setAutoDelete(
bool autoDelete) { m_autoDelete = autoDelete; }
42 static Q_DECL_COLD_FUNCTION QRunnable *warnNullCallable();
43 class QGenericRunnable;
46class Q_CORE_EXPORT QRunnable::QGenericRunnable :
public QRunnable
56 using OpFn =
void* (*)(Op, HelperBase *,
void*);
59 constexpr explicit HelperBase(OpFn f)
noexcept : fn(f) {}
60 ~HelperBase() =
default;
62 void run() { fn(Op::Run,
this,
nullptr); }
63 void destroy() { fn(Op::Destroy,
this,
nullptr); }
66 template <
typename Callable>
67 class Helper :
public HelperBase,
private QtPrivate::CompactStorage<Callable>
69 using Storage = QtPrivate::CompactStorage<Callable>;
70 static void *impl(Op op, HelperBase *that, [[maybe_unused]]
void *arg)
72 const auto _this =
static_cast<Helper*>(that);
74 case Op::Run: _this->object()();
break;
75 case Op::Destroy:
delete _this;
break;
80 template <
typename UniCallable>
81 explicit Helper(UniCallable &&functionToRun)
noexcept
83 Storage{std::forward<UniCallable>(functionToRun)}
88 HelperBase *runHelper;
90 template <
typename Callable, if_callable<Callable> =
true>
91 explicit QGenericRunnable(Callable &&c)
92 : runHelper(
new Helper<std::decay_t<Callable>>(std::forward<Callable>(c)))
95 ~QGenericRunnable() override;
115QRunnable *QRunnable::create(Callable &&functionToRun)
117 using F = std::decay_t<Callable>;
118 constexpr bool is_std_function = QtPrivate::is_std_function_v<F>;
119 constexpr bool is_function_pointer = QtPrivate::is_function_pointer_v<F>;
120 if constexpr (is_std_function || is_function_pointer) {
122 if constexpr (is_std_function) {
123 is_null = !functionToRun;
124 }
else if constexpr (is_function_pointer) {
126 const void *functionPtr =
reinterpret_cast<
void *>(functionToRun);
127 is_null = !functionPtr;
130 return warnNullCallable();
133 return new QGenericRunnable(std::forward<Callable>(functionToRun));