7#include <QtCore/qcompilerdetection.h>
8#include <QtCore/qfunctionaltools_impl.h>
9#include <QtCore/qtclasshelpermacros.h>
10#include <QtCore/qtcoreexports.h>
19 bool m_autoDelete =
true;
23 virtual void run() = 0;
27#if QT_CORE_REMOVED_SINCE(6, 6)
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>
42 class QGenericRunnable;
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>
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>
91 : runHelper(new Helper<
std::decay_t<Callable>>(
std::forward<Callable>(
c)))
104 std::is_function<std::remove_pointer_t<T>>
109constexpr inline bool is_std_function_v<std::function<T>> =
true;
113template <
typename Callable, QRunnable::if_callable<Callable>>
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();
bool autoDelete() const
Returns true is auto-deletion is enabled; false otherwise.
static QRunnable * create(Callable &&functionToRun)
std::enable_if_t< std::is_invocable_r_v< void, Callable >, bool > if_callable
~QGenericRunnable() override
static QRunnable * create(std::nullptr_t)=delete
void setAutoDelete(bool autoDelete)
Enables auto-deletion if autoDelete is true; otherwise auto-deletion is disabled.
virtual void run()=0
Implement this pure virtual function in your subclass.
QGenericRunnable(Callable &&c)
constexpr QRunnable() noexcept=default
Constructs a QRunnable.
Combined button and popup list for selecting options.
auto run(QThreadPool *pool, Function &&f, Args &&...args)
typename std::conditional_t< std::conjunction_v< std::is_empty< Object >, std::negation< std::is_final< Object > > >, detail::StorageEmptyBaseClassOptimization< Object, Tag >, detail::StorageByValue< Object, Tag > > CompactStorage
constexpr bool is_std_function_v
constexpr bool is_function_pointer_v
#define Q_DECL_COLD_FUNCTION