mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-27 11:05:44 +08:00
refs #628 Refactor CWorker::invoke into a free function and expand its API.
This commit is contained in:
56
src/blackmisc/invoke.h
Normal file
56
src/blackmisc/invoke.h
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
/* Copyright (C) 2015
|
||||||
|
* swift Project Community / Contributors
|
||||||
|
*
|
||||||
|
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
|
||||||
|
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
|
||||||
|
* including this file, may be copied, modified, propagated, or distributed except according to the terms
|
||||||
|
* contained in the LICENSE file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
//! \file
|
||||||
|
|
||||||
|
#ifndef BLACKMISC_INVOKE_H
|
||||||
|
#define BLACKMISC_INVOKE_H
|
||||||
|
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
|
namespace BlackMisc
|
||||||
|
{
|
||||||
|
//! \cond PRIVATE
|
||||||
|
namespace Private
|
||||||
|
{
|
||||||
|
|
||||||
|
// Our own version of C++17 std::invoke().
|
||||||
|
template <typename F, typename T, typename = std::enable_if_t<std::is_member_object_pointer<F>::value>>
|
||||||
|
decltype(auto) invoke(F ptr, T &&object)
|
||||||
|
{
|
||||||
|
return std::forward<T>(object).*ptr;
|
||||||
|
}
|
||||||
|
template <typename F, typename T, typename... Ts, typename = std::enable_if_t<std::is_member_function_pointer<F>::value>>
|
||||||
|
decltype(auto) invoke(F ptr, T &&object, Ts &&... args)
|
||||||
|
{
|
||||||
|
return (std::forward<T>(object).*ptr)(std::forward<Ts>(args)...);
|
||||||
|
}
|
||||||
|
template <typename F, typename... Ts, typename = std::enable_if_t<! std::is_member_pointer<std::decay_t<F>>::value>>
|
||||||
|
decltype(auto) invoke(F &&func, Ts &&... args)
|
||||||
|
{
|
||||||
|
return std::forward<F>(func)(std::forward<Ts>(args)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Like invoke() but ignores the first argument if callable is not a member function. For uniform calling of callables with slot semantics.
|
||||||
|
template <typename F, typename T, typename... Ts, typename = std::enable_if_t<std::is_member_function_pointer<F>::value>>
|
||||||
|
decltype(auto) invokeSlot(F ptr, T *object, Ts &&... args)
|
||||||
|
{
|
||||||
|
return (object->*ptr)(std::forward<Ts>(args)...);
|
||||||
|
}
|
||||||
|
template <typename F, typename T, typename... Ts, typename = std::enable_if_t<! std::is_member_pointer<std::decay_t<F>>::value>>
|
||||||
|
decltype(auto) invokeSlot(F &&func, T *, Ts &&... args)
|
||||||
|
{
|
||||||
|
return std::forward<F>(func)(std::forward<Ts>(args)...);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
//! \endcond
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -15,6 +15,7 @@
|
|||||||
#include "blackmiscexport.h"
|
#include "blackmiscexport.h"
|
||||||
#include "variant.h"
|
#include "variant.h"
|
||||||
#include "stacktrace.h"
|
#include "stacktrace.h"
|
||||||
|
#include "invoke.h"
|
||||||
#include <QThread>
|
#include <QThread>
|
||||||
#include <QMutex>
|
#include <QMutex>
|
||||||
#include <QTimer>
|
#include <QTimer>
|
||||||
@@ -109,7 +110,7 @@ namespace BlackMisc
|
|||||||
Q_ASSERT(context->thread() == QThread::currentThread());
|
Q_ASSERT(context->thread() == QThread::currentThread());
|
||||||
QMutexLocker lock(&m_finishedMutex);
|
QMutexLocker lock(&m_finishedMutex);
|
||||||
connect(this, &CWorkerBase::finished, context, functor);
|
connect(this, &CWorkerBase::finished, context, functor);
|
||||||
if (m_finished) { invoke(context, functor); }
|
if (m_finished) { Private::invokeSlot(functor, context); }
|
||||||
}
|
}
|
||||||
|
|
||||||
//! Connects to a functor which will be called when the task is finished.
|
//! Connects to a functor which will be called when the task is finished.
|
||||||
@@ -185,20 +186,6 @@ namespace BlackMisc
|
|||||||
emit finished();
|
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) -> std::enable_if_t<std::is_member_function_pointer<F>::value>
|
|
||||||
{
|
|
||||||
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) -> std::enable_if_t<! std::is_member_function_pointer<F>::value>
|
|
||||||
{
|
|
||||||
return func(std::forward<Ts>(args)...);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
virtual void quit() noexcept {}
|
virtual void quit() noexcept {}
|
||||||
virtual void quitAndWait() noexcept { waitForFinished(); }
|
virtual void quitAndWait() noexcept { waitForFinished(); }
|
||||||
@@ -250,7 +237,7 @@ namespace BlackMisc
|
|||||||
void thenWithResult(T *context, F functor)
|
void thenWithResult(T *context, F functor)
|
||||||
{
|
{
|
||||||
Q_ASSERT_X(m_result.canConvert<R>(), Q_FUNC_INFO, "Type in thenWithResult must match return type of task");
|
Q_ASSERT_X(m_result.canConvert<R>(), Q_FUNC_INFO, "Type in thenWithResult must match return type of task");
|
||||||
then(context, [this, context, functor]() { invoke(context, functor, this->result<R>()); });
|
then(context, [this, context, functor]() { Private::invokeSlot(functor, context, this->result<R>()); });
|
||||||
}
|
}
|
||||||
|
|
||||||
//! Returns the result of the task, waiting for it to finish if necessary.
|
//! Returns the result of the task, waiting for it to finish if necessary.
|
||||||
|
|||||||
Reference in New Issue
Block a user