19 bool m_autoDelete =
true;
21 Q_DISABLE_COPY(QRunnable)
23 virtual void run() = 0;
25 constexpr QRunnable()
noexcept =
default;
27#if QT_CORE_REMOVED_SINCE(6
, 6
)
28 static QRunnable *create(std::function<
void()> functionToRun);
30 template <
typename Callable>
31 using if_callable = std::enable_if_t<std::is_invocable_r_v<
void, Callable>,
bool>;
33 template <
typename Callable, if_callable<Callable> =
true>
34 static QRunnable *create(Callable &&functionToRun);
35 static QRunnable *create(std::nullptr_t) =
delete;
37 bool autoDelete()
const {
return m_autoDelete; }
38 void setAutoDelete(
bool autoDelete) { m_autoDelete = autoDelete; }
41 static Q_DECL_COLD_FUNCTION QRunnable *warnNullCallable();
42 class QGenericRunnable;
45class Q_CORE_EXPORT QRunnable::QGenericRunnable :
public QRunnable
55 using OpFn =
void* (*)(Op, HelperBase *,
void*);
58 constexpr explicit HelperBase(OpFn f)
noexcept : fn(f) {}
59 ~HelperBase() =
default;
61 void run() { fn(Op::Run,
this,
nullptr); }
62 void destroy() { fn(Op::Destroy,
this,
nullptr); }
65 template <
typename Callable>
66 class Helper :
public HelperBase,
private QtPrivate::CompactStorage<Callable>
68 using Storage = QtPrivate::CompactStorage<Callable>;
69 static void *impl(Op op, HelperBase *that, [[maybe_unused]]
void *arg)
71 const auto _this =
static_cast<Helper*>(that);
73 case Op::Run: _this->object()();
break;
74 case Op::Destroy:
delete _this;
break;
79 template <
typename UniCallable>
80 explicit Helper(UniCallable &&functionToRun)
noexcept
82 Storage{std::forward<UniCallable>(functionToRun)}
87 HelperBase *runHelper;
89 template <
typename Callable, if_callable<Callable> =
true>
90 explicit QGenericRunnable(Callable &&c)
91 : runHelper(
new Helper<std::decay_t<Callable>>(std::forward<Callable>(c)))
94 ~QGenericRunnable() override;
114QRunnable *QRunnable::create(Callable &&functionToRun)
116 using F = std::decay_t<Callable>;
117 constexpr bool is_std_function = QtPrivate::is_std_function_v<F>;
118 constexpr bool is_function_pointer = QtPrivate::is_function_pointer_v<F>;
119 if constexpr (is_std_function || is_function_pointer) {
121 if constexpr (is_std_function) {
122 is_null = !functionToRun;
123 }
else if constexpr (is_function_pointer) {
125 const void *functionPtr =
reinterpret_cast<
void *>(functionToRun);
126 is_null = !functionPtr;
129 return warnNullCallable();
132 return new QGenericRunnable(std::forward<Callable>(functionToRun));