refs #457 Refactored SFINAE into a protected part of CWorker.

This commit is contained in:
Mathew Sutcliffe
2015-09-10 21:09:53 +01:00
parent 08716705ca
commit 6131030267

View File

@@ -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 <typename T, typename F>
auto then(T *receiver, F slot) -> typename std::enable_if<std::is_member_function_pointer<F>::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 <typename T, typename F>
auto then(T *context, F functor) -> typename std::enable_if<! std::is_member_function_pointer<F>::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 <typename T, typename F, typename... Ts>
static auto invoke(T *object, F func, Ts &&... args) -> typename std::enable_if<std::is_member_function_pointer<F>::value>::type
{
return (object->*func)(std::forward<Ts>(args)...);
}
//! Uniform way to invoke either a functor or a method.
template <typename T, typename F, typename... Ts>
static auto invoke(T *, F func, Ts &&... args) -> typename std::enable_if<! std::is_member_function_pointer<F>::value>::type
{
return func(std::forward<Ts>(args)...);
}
private:
bool m_finished = false;
mutable QMutex m_finishedMutex { QMutex::Recursive };