From 6131030267f994a6167b508cd38a89f37b4c6c14 Mon Sep 17 00:00:00 2001 From: Mathew Sutcliffe Date: Thu, 10 Sep 2015 21:09:53 +0100 Subject: [PATCH] refs #457 Refactored SFINAE into a protected part of CWorker. --- src/blackmisc/worker.h | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/blackmisc/worker.h b/src/blackmisc/worker.h index 346b55a72..52a03432f 100644 --- a/src/blackmisc/worker.h +++ b/src/blackmisc/worker.h @@ -97,26 +97,15 @@ namespace BlackMisc Q_OBJECT public: - //! Connects to a slot which will be called when the task is finished. + //! Connects to a functor or method which will be called when the task is finished. //! \threadsafe template - auto then(T *receiver, F slot) -> typename std::enable_if::value>::type - { - Q_ASSERT(receiver->thread() == QThread::currentThread()); - QMutexLocker lock(&m_finishedMutex); - connect(this, &CWorkerBase::finished, receiver, slot); - if (m_finished) { (receiver->*slot)(); } - } - - //! Connects to a functor which will be called when the task is finished. - //! \threadsafe - template - auto then(T *context, F functor) -> typename std::enable_if::value>::type + void then(T *context, F functor) { Q_ASSERT(context->thread() == QThread::currentThread()); QMutexLocker lock(&m_finishedMutex); connect(this, &CWorkerBase::finished, context, functor); - if (m_finished) { functor(); } + if (m_finished) { invoke(context, functor); } } //! Connects to a functor which will be called when the task is finished. @@ -181,6 +170,20 @@ namespace BlackMisc emit finished(); } + //! Uniform way to invoke either a functor or a method. + template + static auto invoke(T *object, F func, Ts &&... args) -> typename std::enable_if::value>::type + { + return (object->*func)(std::forward(args)...); + } + + //! Uniform way to invoke either a functor or a method. + template + static auto invoke(T *, F func, Ts &&... args) -> typename std::enable_if::value>::type + { + return func(std::forward(args)...); + } + private: bool m_finished = false; mutable QMutex m_finishedMutex { QMutex::Recursive };