#ifndef ASYNC_CALL_H #define ASYNC_CALL_H #include #include #include #include "DoricAsyncResult.h" class DoricAsyncCall { public: template static std::shared_ptr ensureRunInThreadPool(QThreadPool *threadPool, Function &&function) { std::shared_ptr asyncResult = std::make_shared(); Function lambda = std::forward(function); QtConcurrent::run(threadPool, [lambda, asyncResult]() { lambda(); // asyncResult->setResult(result); }); return asyncResult; } template static std::shared_ptr ensureRunInMain(Function &&function, QThread *thread = qApp->thread()) { std::shared_ptr asyncResult = std::make_shared(); auto *obj = QAbstractEventDispatcher::instance(thread); Q_ASSERT(obj); Function lambda = std::forward(function); QMetaObject::invokeMethod(obj, [lambda, asyncResult]() { lambda(); // asyncResult->setResult(result); }); return asyncResult; } }; #endif // ASYNC_CALL_H