diff --git a/src/libs/utils/functiontraits.h b/src/libs/utils/functiontraits.h new file mode 100644 index 00000000000..a0ffd291fe0 --- /dev/null +++ b/src/libs/utils/functiontraits.h @@ -0,0 +1,120 @@ +/**************************************************************************** +** +** 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 + +namespace Utils { + +// for callables. defined below. +template +struct functionTraits; + +// function +template +struct functionTraits +{ + using ResultType = Result; + static const unsigned arity = sizeof...(Args); // TODO const -> constexpr with MSVC2015 + + template + struct argument + { + using type = typename std::tuple_element>::type; + }; +}; + +// function pointer +template +struct functionTraits : public functionTraits +{ +}; + +// member function +template +struct functionTraits : public functionTraits +{ +}; + +// const member function +template +struct functionTraits : public functionTraits +{ +}; + +// TODO: enable lvalue and rvalue ref member function later (MSVC 2015?) +//// lvalue ref member function +//template +//struct functionTraits : public functionTraits +//{ +//}; + +//// const lvalue ref member function +//template +//struct functionTraits : public functionTraits +//{ +//}; + +//// rvalue ref member function +//template +//struct functionTraits : public functionTraits +//{ +//}; + +// callables. only works if operator() is not overloaded. +template +struct functionTraits +{ + using callableTraits = functionTraits; + using ResultType = typename callableTraits::ResultType; + static const unsigned arity = callableTraits::arity - 1; // ignore object pointer arg // TODO const -> constexpr with MSVC2015 + + template + struct argument + { + using type = typename callableTraits::template argument::type; // ignore object pointer arg + }; +}; + +// lvalue ref callables +template +struct functionTraits : public functionTraits +{ +}; + +// const lvalue ref callables +template +struct functionTraits : public functionTraits +{ +}; + +// rvalue ref callables +template +struct functionTraits : public functionTraits +{ +}; + +} // Utils diff --git a/src/libs/utils/runextensions.h b/src/libs/utils/runextensions.h index a3a2de3e859..837aba34bd2 100644 --- a/src/libs/utils/runextensions.h +++ b/src/libs/utils/runextensions.h @@ -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 futureInterface, const Con futureInterface.reportFinished(); } -template -typename std::enable_if::type>::value>::type -runAsyncImpl(QFutureInterface futureInterface, Function &&function, Obj &&obj, Args&&... args) +template +class MemberCallable; + +template +class MemberCallable { - std::mem_fn(std::forward(function))(std::forward(obj), - futureInterface, std::forward(args)...); +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)...); + } + +private: + Result(Obj::* m_function)(Args...) const; + const Obj *m_obj; +}; + +template +class MemberCallable +{ +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)...); + } + +private: + Result(Obj::* m_function)(Args...); + Obj *m_obj; +}; + +// void function that does not take QFutureInterface +template +void runAsyncReturnVoidDispatch(std::true_type, QFutureInterface futureInterface, Function &&function, Args&&... args) +{ + function(std::forward(args)...); if (futureInterface.isPaused()) futureInterface.waitForResume(); futureInterface.reportFinished(); } +// non-void function that does not take QFutureInterface template -typename std::enable_if::type>::value>::type -runAsyncImpl(QFutureInterface futureInterface, Function &&function, Args&&... args) +void runAsyncReturnVoidDispatch(std::false_type, QFutureInterface futureInterface, Function &&function, Args&&... args) +{ + futureInterface.reportResult(function(std::forward(args)...)); + if (futureInterface.isPaused()) + futureInterface.waitForResume(); + futureInterface.reportFinished(); +} + +// function that takes QFutureInterface +template +void runAsyncQFutureInterfaceDispatch(std::true_type, QFutureInterface futureInterface, Function &&function, Args&&... args) { function(futureInterface, std::forward(args)...); if (futureInterface.isPaused()) @@ -538,6 +591,55 @@ runAsyncImpl(QFutureInterface futureInterface, Function &&function, futureInterface.reportFinished(); } +// function that does not take QFutureInterface +template +void runAsyncQFutureInterfaceDispatch(std::false_type, QFutureInterface futureInterface, Function &&function, Args&&... args) +{ + runAsyncReturnVoidDispatch(std::is_void::type>(), + futureInterface, std::forward(function), std::forward(args)...); +} + +// function that takes at least one argument which could be QFutureInterface +template +void runAsyncArityDispatch(std::true_type, QFutureInterface futureInterface, Function &&function, Args&&... args) +{ + runAsyncQFutureInterfaceDispatch(std::is_same&, + typename functionTraits::template argument<0>::type>(), + futureInterface, std::forward(function), std::forward(args)...); +} + +// function that does not take an argument, so it does not take a QFutureInterface +template +void runAsyncArityDispatch(std::false_type, QFutureInterface futureInterface, Function &&function, Args&&... args) +{ + runAsyncQFutureInterfaceDispatch(std::false_type(), + futureInterface, std::forward(function), std::forward(args)...); +} + +// function, function pointer, or other callable object that is no member pointer +template ::type>::value + >::type> +void runAsyncImpl(QFutureInterface futureInterface, Function &&function, Args&&... args) +{ + runAsyncArityDispatch(std::integral_constant::arity > 0)>(), + futureInterface, std::forward(function), std::forward(args)...); +} + +// Function = member function +template ::type>::value + >::type> +void runAsyncImpl(QFutureInterface futureInterface, Function &&function, Obj &&obj, Args&&... args) +{ + // Wrap member function with object into callable + runAsyncImpl(futureInterface, + MemberCallable(std::forward(function), std::forward(obj)), + std::forward(args)...); +} + // can be replaced with std::(make_)index_sequence with C++14 template struct indexSequence { }; diff --git a/src/libs/utils/utils-lib.pri b/src/libs/utils/utils-lib.pri index 6057b116410..8bf92916ac5 100644 --- a/src/libs/utils/utils-lib.pri +++ b/src/libs/utils/utils-lib.pri @@ -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 \ diff --git a/src/libs/utils/utils.qbs b/src/libs/utils/utils.qbs index abf63ab3045..d4e272f7cb6 100644 --- a/src/libs/utils/utils.qbs +++ b/src/libs/utils/utils.qbs @@ -104,6 +104,7 @@ QtcLibrary { "filewizardpage.ui", "flowlayout.cpp", "flowlayout.h", + "functiontraits.h", "historycompleter.cpp", "historycompleter.h", "hostosinfo.h", diff --git a/tests/auto/runextensions/tst_runextensions.cpp b/tests/auto/runextensions/tst_runextensions.cpp index 5dadcc54ce9..13f8aabca1f 100644 --- a/tests/auto/runextensions/tst_runextensions.cpp +++ b/tests/auto/runextensions/tst_runextensions.cpp @@ -42,6 +42,7 @@ private slots: void moveOnlyType(); #endif void threadPriority(); + void runAsyncNoFutureInterface(); }; void report3(QFutureInterface &fi) @@ -103,6 +104,82 @@ public: { fi.reportResult(s); } + + void nonConstMember(QFutureInterface &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({cs})); QCOMPARE(Utils::runAsync(&MyObject::memberString2, &obj, QString(QLatin1String("rvalue"))).results(), QList({QString(QLatin1String("rvalue"))})); + MyObject nonConstObj{}; + QCOMPARE(Utils::runAsync(&MyObject::nonConstMember, &nonConstObj).results(), + QList({0, 2, 1})); } void tst_RunExtensions::runInThreadPool() @@ -317,6 +397,96 @@ void tst_RunExtensions::threadPriority() QList({0, 2, 1})); } +void tst_RunExtensions::runAsyncNoFutureInterface() +{ + // free function pointer + bool value = false; + Utils::runAsync(voidFunction, &value).waitForFinished(); + QCOMPARE(value, true); + + QCOMPARE(Utils::runAsync(one).results(), + QList({1})); + QCOMPARE(Utils::runAsync(identity, 5).results(), + QList({5})); + + QString s = QLatin1String("string"); + const QString &crs = QLatin1String("cr string"); + const QString cs = QLatin1String("c string"); + + QCOMPARE(Utils::runAsync(stringIdentity1, s).results(), + QList({s})); + QCOMPARE(Utils::runAsync(stringIdentity1, crs).results(), + QList({crs})); + QCOMPARE(Utils::runAsync(stringIdentity1, cs).results(), + QList({cs})); + QCOMPARE(Utils::runAsync(stringIdentity1, QString(QLatin1String("rvalue"))).results(), + QList({QString(QLatin1String("rvalue"))})); + QCOMPARE(Utils::runAsync(stringIdentity2, s).results(), + QList({s})); + QCOMPARE(Utils::runAsync(stringIdentity2, crs).results(), + QList({crs})); + QCOMPARE(Utils::runAsync(stringIdentity2, cs).results(), + QList({cs})); + QCOMPARE(Utils::runAsync(stringIdentity2, QString(QLatin1String("rvalue"))).results(), + QList({QString(QLatin1String("rvalue"))})); + + // lambda + QCOMPARE(Utils::runAsync([](int n) { + return n + 1; + }, 3).results(), + QList({4})); + + // std::function + const std::function fun = [](int n) { + return n + 1; + }; + QCOMPARE(Utils::runAsync(fun, 2).results(), + QList({3})); + + // operator() + value = false; + Utils::runAsync(CallableWithoutQFutureInterface(), &value).waitForFinished(); + QCOMPARE(value, true); + value = false; + const CallableWithoutQFutureInterface c{}; + Utils::runAsync(c, &value).waitForFinished(); + QCOMPARE(value, true); + + // static member functions + value = false; + Utils::runAsync(&MyObjectWithoutQFutureInterface::staticMember0, &value).waitForFinished(); + QCOMPARE(value, true); + QCOMPARE(Utils::runAsync(&MyObjectWithoutQFutureInterface::staticMember1, 2).results(), + QList({2})); + + // member functions + const MyObjectWithoutQFutureInterface obj{}; + value = false; + Utils::runAsync(&MyObjectWithoutQFutureInterface::member0, &obj, &value).waitForFinished(); + QCOMPARE(value, true); + QCOMPARE(Utils::runAsync(&MyObjectWithoutQFutureInterface::member1, &obj, 4).results(), + QList({4})); + QCOMPARE(Utils::runAsync(&MyObjectWithoutQFutureInterface::memberString1, &obj, s).results(), + QList({s})); + QCOMPARE(Utils::runAsync(&MyObjectWithoutQFutureInterface::memberString1, &obj, crs).results(), + QList({crs})); + QCOMPARE(Utils::runAsync(&MyObjectWithoutQFutureInterface::memberString1, &obj, cs).results(), + QList({cs})); + QCOMPARE(Utils::runAsync(&MyObjectWithoutQFutureInterface::memberString1, &obj, QString(QLatin1String("rvalue"))).results(), + QList({QString(QLatin1String("rvalue"))})); + QCOMPARE(Utils::runAsync(&MyObjectWithoutQFutureInterface::memberString2, &obj, s).results(), + QList({s})); + QCOMPARE(Utils::runAsync(&MyObjectWithoutQFutureInterface::memberString2, &obj, crs).results(), + QList({crs})); + QCOMPARE(Utils::runAsync(&MyObjectWithoutQFutureInterface::memberString2, &obj, cs).results(), + QList({cs})); + QCOMPARE(Utils::runAsync(&MyObjectWithoutQFutureInterface::memberString2, &obj, QString(QLatin1String("rvalue"))).results(), + QList({QString(QLatin1String("rvalue"))})); + MyObjectWithoutQFutureInterface nonConstObj{}; + QCOMPARE(Utils::runAsync(&MyObjectWithoutQFutureInterface::nonConstMember, &nonConstObj, 4).results(), + QList({4})); +} + QTEST_MAIN(tst_RunExtensions) #include "tst_runextensions.moc"