/* 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. 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 "blackmisc/typetraits.h" #include "blackmisc/integersequence.h" #include "blackmisc/promise.h" #include #include #include #include #include namespace BlackMisc { //! \cond PRIVATE namespace Private { // Our own version of C++17 std::invoke(). template ::value>> decltype(auto) invoke(F ptr, T && object) { return std::forward(object).*ptr; } template ::value>> decltype(auto) invoke(F ptr, T && object, Ts && ... args) { return (std::forward(object).*ptr)(std::forward(args)...); } template < typename F, typename... Ts, typename = std::enable_if_t < ! std::is_member_pointer>::value >> decltype(auto) invoke(F && func, Ts && ... args) { return std::forward(func)(std::forward(args)...); } // Like invoke() but ignores the first argument if callable is not a member function. For uniform calling of callables with slot semantics. template decltype(auto) invokeSlotImpl(F ptr, T *object, U tuple, std::index_sequence, std::true_type) { Q_UNUSED(tuple); // in case the pack expansion is empty return (object->*ptr)(std::forward>(std::get(tuple))...); } template decltype(auto) invokeSlotImpl(F &&func, T *, U tuple, std::index_sequence, std::false_type) { Q_UNUSED(tuple); // in case the pack expansion is empty return std::forward(func)(std::forward>(std::get(tuple))...); } template decltype(auto) invokeSlot(F &&func, T *object, Ts &&... args) { using seq = MaskSequence < std::make_index_sequence, ! TIsQPrivateSignal>::value... >; return invokeSlotImpl(std::forward(func), object, std::forward_as_tuple(std::forward(args)...), seq(), std::is_member_pointer>()); } // Like QMetaObject::invokeMethod but the return value is accessed through a QFuture, and extra arguments can be provided. template auto invokeMethod(T *object, F &&func, Ts &&... args) { const auto invoker = [](auto &&... x) { return Private::invokeSlot(std::forward(x)...); }; auto method = std::bind(invoker, std::forward(func), object, std::forward(args)...); CPromise promise; QMetaObject::invokeMethod(object, [promise, method = std::move(method)]() mutable { promise.setResultFrom(std::move(method)); }); return promise.future(); } } //! \endcond } #endif