#ifndef ASYNC_CALL_H #define ASYNC_CALL_H #include #include #include class DoricAsyncCall { public: static void ensureRunInThreadPool(QThreadPool *threadPool, std::function lambda) { QFuture::result_type> future = QtConcurrent::run(threadPool, lambda); } template static void ensureRunInMain(Function &&function) { struct Event : public QEvent { using DecayedFunction = typename std::decay::type; DecayedFunction decayedFunction; Event(DecayedFunction &&decayedFunction) : QEvent(QEvent::None), decayedFunction(std::move(decayedFunction)) {} Event(const DecayedFunction &decayedFunction) : QEvent(QEvent::None), decayedFunction(decayedFunction) {} ~Event() { decayedFunction(); } }; QCoreApplication::postEvent(qApp, new Event(std::forward(function))); } }; #endif // ASYNC_CALL_H