From 786b48878f37edafd5eb928ed0f4d046ee1d6bec Mon Sep 17 00:00:00 2001 From: Karsten Heimrich Date: Thu, 26 Mar 2020 15:47:04 +0100 Subject: Improve Map|Map-Reduce and Filter|Filter-Reduce implementation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * support lambda expressions * remove the need to specify result_type * use std::invoke to apply map|filter function * remove usage of FunctionWrapper* and createFunctionWrapper Task-number: QTBUG-33735 Task-number: QTBUG-82646 Change-Id: Ibcbe4278f0742c29182bd506081db0abb516f85f Reviewed-by: MÃ¥rten Nordheim Reviewed-by: Sona Kurazyan --- src/concurrent/qtconcurrentfunctionwrappers.h | 272 +++++--------------------- 1 file changed, 51 insertions(+), 221 deletions(-) (limited to 'src/concurrent/qtconcurrentfunctionwrappers.h') diff --git a/src/concurrent/qtconcurrentfunctionwrappers.h b/src/concurrent/qtconcurrentfunctionwrappers.h index 4731de77cc2..34bbe89c860 100644 --- a/src/concurrent/qtconcurrentfunctionwrappers.h +++ b/src/concurrent/qtconcurrentfunctionwrappers.h @@ -43,165 +43,64 @@ #include #include +#include + #if !defined(QT_NO_CONCURRENT) || defined(Q_CLANG_QDOC) QT_BEGIN_NAMESPACE -namespace QtConcurrent { +namespace QtPrivate { -template -class FunctionWrapper1 +struct PushBackWrapper { -public: - typedef T (*FunctionPointerType)(U u); - typedef T result_type; - inline FunctionWrapper1(FunctionPointerType _functionPointer) - :functionPointer(_functionPointer) { } - - inline T operator()(U u) + template + inline void operator()(C &c, const U &u) const { - return functionPointer(u); + return c.push_back(u); } -private: - FunctionPointerType functionPointer; -}; - -template -class MemberFunctionWrapper -{ -public: - typedef T (C::*FunctionPointerType)(); - typedef T result_type; - inline MemberFunctionWrapper(FunctionPointerType _functionPointer) - :functionPointer(_functionPointer) { } - - inline T operator()(C &c) + template + inline void operator()(C &c, U &&u) const { - return (c.*functionPointer)(); + return c.push_back(u); } -private: - FunctionPointerType functionPointer; }; -template -class MemberFunctionWrapper1 -{ -public: - typedef T (C::*FunctionPointerType)(U); - typedef T result_type; - - inline MemberFunctionWrapper1(FunctionPointerType _functionPointer) - : functionPointer(_functionPointer) - { } - - inline T operator()(C &c, U u) - { - return (c.*functionPointer)(u); - } - -private: - FunctionPointerType functionPointer; -}; +// -- MapResultType -template -class ConstMemberFunctionWrapper +template +struct Argument { -public: - typedef T (C::*FunctionPointerType)() const; - typedef T result_type; - inline ConstMemberFunctionWrapper(FunctionPointerType _functionPointer) - :functionPointer(_functionPointer) { } - - inline T operator()(const C &c) const - { - return (c.*functionPointer)(); - } -private: - FunctionPointerType functionPointer; + using Type = void; }; -} // namespace QtConcurrent. - -namespace QtPrivate { - -template -const T& createFunctionWrapper(const T& t) +template +struct Argument>::type> { - return t; -} - -template -QtConcurrent::FunctionWrapper1 createFunctionWrapper(T (*func)(U)) -{ - return QtConcurrent::FunctionWrapper1(func); -} - -template -QtConcurrent::MemberFunctionWrapper createFunctionWrapper(T (C::*func)()) -{ - return QtConcurrent::MemberFunctionWrapper(func); -} - -template -QtConcurrent::MemberFunctionWrapper1 createFunctionWrapper(T (C::*func)(U)) -{ - return QtConcurrent::MemberFunctionWrapper1(func); -} - -template -QtConcurrent::ConstMemberFunctionWrapper createFunctionWrapper(T (C::*func)() const) -{ - return QtConcurrent::ConstMemberFunctionWrapper(func); -} - -#if defined(__cpp_noexcept_function_type) && __cpp_noexcept_function_type >= 201510 -template -QtConcurrent::FunctionWrapper1 createFunctionWrapper(T (*func)(U) noexcept) -{ - return QtConcurrent::FunctionWrapper1(func); -} + using Type = std::decay_t()))>; +}; -template -QtConcurrent::MemberFunctionWrapper createFunctionWrapper(T (C::*func)() noexcept) +template +struct Argument>::type> { - return QtConcurrent::MemberFunctionWrapper(func); -} + using Type = std::decay_t())>; +}; -template -QtConcurrent::MemberFunctionWrapper1 createFunctionWrapper(T (C::*func)(U) noexcept) -{ - return QtConcurrent::MemberFunctionWrapper1(func); -} +template +using ArgumentType = typename Argument::Type; -template -QtConcurrent::ConstMemberFunctionWrapper createFunctionWrapper(T (C::*func)() const noexcept) +template +struct MapResult { - return QtConcurrent::ConstMemberFunctionWrapper(func); -} -#endif - -struct PushBackWrapper -{ - typedef void result_type; - - template - inline void operator()(C &c, const U &u) const - { - return c.push_back(u); - } - - template - inline void operator()(C &c, U &&u) const - { - return c.push_back(u); - } + static_assert(std::is_invocable_v, ArgumentType>, + "It's not possible to invoke the function with passed argument."); + using Type = std::invoke_result_t, ArgumentType>; }; -template ::Value> -struct LazyResultType { typedef typename Functor::result_type Type; }; -template -struct LazyResultType { typedef void Type; }; +template +using MapResultType = typename MapResult::Type; + +// -- ReduceResultType template struct ReduceResultType; @@ -218,121 +117,52 @@ struct ReduceResultType typedef C ResultType; }; -#if defined(__cpp_noexcept_function_type) && __cpp_noexcept_function_type >= 201510 -template -struct ReduceResultType -{ - typedef U ResultType; -}; - -template -struct ReduceResultType -{ - typedef C ResultType; -}; -#endif - -template -struct MapResultType -{ - typedef typename LazyResultType::Type ResultType; -}; - template -struct MapResultType +struct ReduceResultType> { typedef U ResultType; }; -template -struct MapResultType +template +struct ReduceResultType { - typedef T ResultType; + using ResultType = typename std::tuple_element<0, std::tuple>::type; }; #if defined(__cpp_noexcept_function_type) && __cpp_noexcept_function_type >= 201510 template -struct MapResultType +struct ReduceResultType { typedef U ResultType; }; -template -struct MapResultType +template +struct ReduceResultType { - typedef T ResultType; + typedef C ResultType; }; #endif -#ifndef QT_NO_TEMPLATE_TEMPLATE_PARAMETERS - -template