Commit d362abf1 authored by Eike Ziller's avatar Eike Ziller

runAsync: Support functions without QFutureInterface parameter.

Change-Id: I8e387d683eceab642ddd3fac90a2a5f407a868dc
Reviewed-by: default avatarTobias Hunger <tobias.hunger@theqtcompany.com>
parent 51d4e56e
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include <tuple>
namespace Utils {
// for callables. defined below.
template <typename Callable>
struct functionTraits;
// function
template <typename Result, typename... Args>
struct functionTraits<Result(Args...)>
{
using ResultType = Result;
static const unsigned arity = sizeof...(Args); // TODO const -> constexpr with MSVC2015
template <unsigned i>
struct argument
{
using type = typename std::tuple_element<i, std::tuple<Args...>>::type;
};
};
// function pointer
template <typename Result, typename... Args>
struct functionTraits<Result(*)(Args...)> : public functionTraits<Result(Args...)>
{
};
// member function
template <typename Type, typename Result, typename... Args>
struct functionTraits<Result(Type::*)(Args...)> : public functionTraits<Result(Type&,Args...)>
{
};
// const member function
template <typename Type, typename Result, typename... Args>
struct functionTraits<Result(Type::*)(Args...) const> : public functionTraits<Result(Type&,Args...)>
{
};
// TODO: enable lvalue and rvalue ref member function later (MSVC 2015?)
//// lvalue ref member function
//template <typename Type, typename Result, typename... Args>
//struct functionTraits<Result(Type::*)(Args...) &> : public functionTraits<Result(Type&,Args...)>
//{
//};
//// const lvalue ref member function
//template <typename Type, typename Result, typename... Args>
//struct functionTraits<Result(Type::*)(Args...) const &> : public functionTraits<Result(Type&,Args...)>
//{
//};
//// rvalue ref member function
//template <typename Type, typename Result, typename... Args>
//struct functionTraits<Result(Type::*)(Args...) &&> : public functionTraits<Result(Type&,Args...)>
//{
//};
// callables. only works if operator() is not overloaded.
template <typename Callable>
struct functionTraits
{
using callableTraits = functionTraits<decltype(&Callable::operator())>;
using ResultType = typename callableTraits::ResultType;
static const unsigned arity = callableTraits::arity - 1; // ignore object pointer arg // TODO const -> constexpr with MSVC2015
template <unsigned i>
struct argument
{
using type = typename callableTraits::template argument<i+1>::type; // ignore object pointer arg
};
};
// lvalue ref callables
template <typename Callable>
struct functionTraits<Callable&> : public functionTraits<Callable>
{
};
// const lvalue ref callables
template <typename Callable>
struct functionTraits<const Callable&> : public functionTraits<Callable>
{
};
// rvalue ref callables
template <typename Callable>
struct functionTraits<Callable&&> : public functionTraits<Callable>
{
};
} // Utils
......@@ -26,6 +26,7 @@
#ifndef RUNEXTENSIONS_H
#define RUNEXTENSIONS_H
#include "functiontraits.h"
#include "qtcassert.h"
#include "utils_global.h"
......@@ -517,20 +518,72 @@ void blockingMapReduce(QFutureInterface<ReduceResult> futureInterface, const Con
futureInterface.reportFinished();
}
template <typename ResultType, typename Function, typename Obj, typename... Args>
typename std::enable_if<std::is_member_pointer<typename std::decay<Function>::type>::value>::type
runAsyncImpl(QFutureInterface<ResultType> futureInterface, Function &&function, Obj &&obj, Args&&... args)
template <typename Function>
class MemberCallable;
template <typename Result, typename Obj, typename... Args>
class MemberCallable<Result(Obj::*)(Args...) const>
{
public:
MemberCallable(Result(Obj::* function)(Args...) const, const Obj *obj)
: m_function(function),
m_obj(obj)
{
}
Result operator()(Args&&... args) const
{
return ((*m_obj).*m_function)(std::forward<Args>(args)...);
}
private:
Result(Obj::* m_function)(Args...) const;
const Obj *m_obj;
};
template <typename Result, typename Obj, typename... Args>
class MemberCallable<Result(Obj::*)(Args...)>
{
public:
MemberCallable(Result(Obj::* function)(Args...), Obj *obj)
: m_function(function),
m_obj(obj)
{
}
Result operator()(Args&&... args) const
{
return ((*m_obj).*m_function)(std::forward<Args>(args)...);
}
private:
Result(Obj::* m_function)(Args...);
Obj *m_obj;
};
// void function that does not take QFutureInterface
template <typename ResultType, typename Function, typename... Args>
void runAsyncReturnVoidDispatch(std::true_type, QFutureInterface<ResultType> futureInterface, Function &&function, Args&&... args)
{
function(std::forward<Args>(args)...);
if (futureInterface.isPaused())
futureInterface.waitForResume();
futureInterface.reportFinished();
}
// non-void function that does not take QFutureInterface
template <typename ResultType, typename Function, typename... Args>
void runAsyncReturnVoidDispatch(std::false_type, QFutureInterface<ResultType> futureInterface, Function &&function, Args&&... args)
{
std::mem_fn(std::forward<Function>(function))(std::forward<Obj>(obj),
futureInterface, std::forward<Args>(args)...);
futureInterface.reportResult(function(std::forward<Args>(args)...));
if (futureInterface.isPaused())
futureInterface.waitForResume();
futureInterface.reportFinished();
}
// function that takes QFutureInterface
template <typename ResultType, typename Function, typename... Args>
typename std::enable_if<!std::is_member_pointer<typename std::decay<Function>::type>::value>::type
runAsyncImpl(QFutureInterface<ResultType> futureInterface, Function &&function, Args&&... args)
void runAsyncQFutureInterfaceDispatch(std::true_type, QFutureInterface<ResultType> futureInterface, Function &&function, Args&&... args)
{
function(futureInterface, std::forward<Args>(args)...);
if (futureInterface.isPaused())
......@@ -538,6 +591,55 @@ runAsyncImpl(QFutureInterface<ResultType> futureInterface, Function &&function,
futureInterface.reportFinished();
}
// function that does not take QFutureInterface
template <typename ResultType, typename Function, typename... Args>
void runAsyncQFutureInterfaceDispatch(std::false_type, QFutureInterface<ResultType> futureInterface, Function &&function, Args&&... args)
{
runAsyncReturnVoidDispatch(std::is_void<typename std::result_of<Function(Args...)>::type>(),
futureInterface, std::forward<Function>(function), std::forward<Args>(args)...);
}
// function that takes at least one argument which could be QFutureInterface
template <typename ResultType, typename Function, typename... Args>
void runAsyncArityDispatch(std::true_type, QFutureInterface<ResultType> futureInterface, Function &&function, Args&&... args)
{
runAsyncQFutureInterfaceDispatch(std::is_same<QFutureInterface<ResultType>&,
typename functionTraits<Function>::template argument<0>::type>(),
futureInterface, std::forward<Function>(function), std::forward<Args>(args)...);
}
// function that does not take an argument, so it does not take a QFutureInterface
template <typename ResultType, typename Function, typename... Args>
void runAsyncArityDispatch(std::false_type, QFutureInterface<ResultType> futureInterface, Function &&function, Args&&... args)
{
runAsyncQFutureInterfaceDispatch(std::false_type(),
futureInterface, std::forward<Function>(function), std::forward<Args>(args)...);
}
// function, function pointer, or other callable object that is no member pointer
template <typename ResultType, typename Function, typename... Args,
typename = typename std::enable_if<
!std::is_member_pointer<typename std::decay<Function>::type>::value
>::type>
void runAsyncImpl(QFutureInterface<ResultType> futureInterface, Function &&function, Args&&... args)
{
runAsyncArityDispatch(std::integral_constant<bool, (functionTraits<Function>::arity > 0)>(),
futureInterface, std::forward<Function>(function), std::forward<Args>(args)...);
}
// Function = member function
template <typename ResultType, typename Function, typename Obj, typename... Args,
typename = typename std::enable_if<
std::is_member_pointer<typename std::decay<Function>::type>::value
>::type>
void runAsyncImpl(QFutureInterface<ResultType> futureInterface, Function &&function, Obj &&obj, Args&&... args)
{
// Wrap member function with object into callable
runAsyncImpl(futureInterface,
MemberCallable<Function>(std::forward<Function>(function), std::forward<Obj>(obj)),
std::forward<Args>(args)...);
}
// can be replaced with std::(make_)index_sequence with C++14
template <std::size_t...>
struct indexSequence { };
......
......@@ -208,7 +208,8 @@ HEADERS += \
$$PWD/dropsupport.h \
$$PWD/utilsicons.h \
$$PWD/icon.h \
$$PWD/port.h
$$PWD/port.h \
$$PWD/functiontraits.h
FORMS += $$PWD/filewizardpage.ui \
$$PWD/projectintropage.ui \
......
......@@ -104,6 +104,7 @@ QtcLibrary {
"filewizardpage.ui",
"flowlayout.cpp",
"flowlayout.h",
"functiontraits.h",
"historycompleter.cpp",
"historycompleter.h",
"hostosinfo.h",
......
......@@ -42,6 +42,7 @@ private slots:
void moveOnlyType();
#endif
void threadPriority();
void runAsyncNoFutureInterface();
};
void report3(QFutureInterface<int> &fi)
......@@ -103,6 +104,82 @@ public:
{
fi.reportResult(s);
}
void nonConstMember(QFutureInterface<double> &fi)
{
fi.reportResults({0, 2, 1});
}
};
void voidFunction(bool *value) // can be useful to get QFuture for watching when it is finished
{
*value = true;
}
int one()
{
return 1;
}
int identity(int input)
{
return input;
}
QString stringIdentity1(const QString &s)
{
return s;
}
QString stringIdentity2(QString s)
{
return s;
}
class CallableWithoutQFutureInterface {
public:
void operator()(bool *value) const
{
*value = true;
}
};
class MyObjectWithoutQFutureInterface {
public:
static void staticMember0(bool *value)
{
*value = true;
}
static double staticMember1(int n)
{
return n;
}
void member0(bool *value) const
{
*value = true;
}
double member1(int n) const
{
return n;
}
QString memberString1(const QString &s) const
{
return s;
}
QString memberString2(QString s) const
{
return s;
}
double nonConstMember(int n)
{
return n;
}
};
void tst_RunExtensions::runAsync()
......@@ -188,6 +265,9 @@ void tst_RunExtensions::runAsync()
QList<QString>({cs}));
QCOMPARE(Utils::runAsync<QString>(&MyObject::memberString2, &obj, QString(QLatin1String("rvalue"))).results(),
QList<QString>({QString(QLatin1String("rvalue"))}));
MyObject nonConstObj{};
QCOMPARE(Utils::runAsync<double>(&MyObject::nonConstMember, &nonConstObj).results(),
QList<double>({0, 2, 1}));
}
void tst_RunExtensions::runInThreadPool()
......@@ -317,6 +397,96 @@ void tst_RunExtensions::threadPriority()
QList<int>({0, 2, 1}));
}
void tst_RunExtensions::runAsyncNoFutureInterface()
{
// free function pointer
bool value = false;
Utils::runAsync<void>(voidFunction, &value).waitForFinished();
QCOMPARE(value, true);
QCOMPARE(Utils::runAsync<int>(one).results(),
QList<int>({1}));
QCOMPARE(Utils::runAsync<int>(identity, 5).results(),
QList<int>({5}));
QString s = QLatin1String("string");
const QString &crs = QLatin1String("cr string");
const QString cs = QLatin1String("c string");
QCOMPARE(Utils::runAsync<QString>(stringIdentity1, s).results(),
QList<QString>({s}));
QCOMPARE(Utils::runAsync<QString>(stringIdentity1, crs).results(),
QList<QString>({crs}));
QCOMPARE(Utils::runAsync<QString>(stringIdentity1, cs).results(),
QList<QString>({cs}));
QCOMPARE(Utils::runAsync<QString>(stringIdentity1, QString(QLatin1String("rvalue"))).results(),
QList<QString>({QString(QLatin1String("rvalue"))}));
QCOMPARE(Utils::runAsync<QString>(stringIdentity2, s).results(),
QList<QString>({s}));
QCOMPARE(Utils::runAsync<QString>(stringIdentity2, crs).results(),
QList<QString>({crs}));
QCOMPARE(Utils::runAsync<QString>(stringIdentity2, cs).results(),
QList<QString>({cs}));
QCOMPARE(Utils::runAsync<QString>(stringIdentity2, QString(QLatin1String("rvalue"))).results(),
QList<QString>({QString(QLatin1String("rvalue"))}));
// lambda
QCOMPARE(Utils::runAsync<double>([](int n) {
return n + 1;
}, 3).results(),
QList<double>({4}));
// std::function
const std::function<double(int)> fun = [](int n) {
return n + 1;
};
QCOMPARE(Utils::runAsync<double>(fun, 2).results(),
QList<double>({3}));
// operator()
value = false;
Utils::runAsync<void>(CallableWithoutQFutureInterface(), &value).waitForFinished();
QCOMPARE(value, true);
value = false;
const CallableWithoutQFutureInterface c{};
Utils::runAsync<void>(c, &value).waitForFinished();
QCOMPARE(value, true);
// static member functions
value = false;
Utils::runAsync<void>(&MyObjectWithoutQFutureInterface::staticMember0, &value).waitForFinished();
QCOMPARE(value, true);
QCOMPARE(Utils::runAsync<double>(&MyObjectWithoutQFutureInterface::staticMember1, 2).results(),
QList<double>({2}));
// member functions
const MyObjectWithoutQFutureInterface obj{};
value = false;
Utils::runAsync<void>(&MyObjectWithoutQFutureInterface::member0, &obj, &value).waitForFinished();
QCOMPARE(value, true);
QCOMPARE(Utils::runAsync<double>(&MyObjectWithoutQFutureInterface::member1, &obj, 4).results(),
QList<double>({4}));
QCOMPARE(Utils::runAsync<QString>(&MyObjectWithoutQFutureInterface::memberString1, &obj, s).results(),
QList<QString>({s}));
QCOMPARE(Utils::runAsync<QString>(&MyObjectWithoutQFutureInterface::memberString1, &obj, crs).results(),
QList<QString>({crs}));
QCOMPARE(Utils::runAsync<QString>(&MyObjectWithoutQFutureInterface::memberString1, &obj, cs).results(),
QList<QString>({cs}));
QCOMPARE(Utils::runAsync<QString>(&MyObjectWithoutQFutureInterface::memberString1, &obj, QString(QLatin1String("rvalue"))).results(),
QList<QString>({QString(QLatin1String("rvalue"))}));
QCOMPARE(Utils::runAsync<QString>(&MyObjectWithoutQFutureInterface::memberString2, &obj, s).results(),
QList<QString>({s}));
QCOMPARE(Utils::runAsync<QString>(&MyObjectWithoutQFutureInterface::memberString2, &obj, crs).results(),
QList<QString>({crs}));
QCOMPARE(Utils::runAsync<QString>(&MyObjectWithoutQFutureInterface::memberString2, &obj, cs).results(),
QList<QString>({cs}));
QCOMPARE(Utils::runAsync<QString>(&MyObjectWithoutQFutureInterface::memberString2, &obj, QString(QLatin1String("rvalue"))).results(),
QList<QString>({QString(QLatin1String("rvalue"))}));
MyObjectWithoutQFutureInterface nonConstObj{};
QCOMPARE(Utils::runAsync<double>(&MyObjectWithoutQFutureInterface::nonConstMember, &nonConstObj, 4).results(),
QList<double>({4}));
}
QTEST_MAIN(tst_RunExtensions)
#include "tst_runextensions.moc"
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment