swift
invoke.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (C) 2015 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #ifndef SWIFT_MISC_INVOKE_H
7 #define SWIFT_MISC_INVOKE_H
8 
9 #include <tuple>
10 
11 #include <QMetaObject>
12 #include <QtGlobal>
13 
14 #include "misc/integersequence.h"
15 #include "misc/promise.h"
16 #include "misc/typetraits.h"
17 
18 namespace swift::misc::private_ns
19 {
21 
22  // Like invoke() but ignores the first argument if callable is not a member function. For uniform calling of
23  // callables with slot semantics.
24  template <typename F, typename T, typename U, size_t... Is>
25  decltype(auto) invokeSlotImpl(F ptr, T *object, U tuple, std::index_sequence<Is...>, std::true_type)
26  {
27  Q_UNUSED(tuple); // in case the pack expansion is empty
28  return (object->*ptr)(std::forward<std::tuple_element_t<Is, U>>(std::get<Is>(tuple))...);
29  }
30  template <typename F, typename T, typename U, size_t... Is>
31  decltype(auto) invokeSlotImpl(F &&func, T *, U tuple, std::index_sequence<Is...>, std::false_type)
32  {
33  Q_UNUSED(tuple); // in case the pack expansion is empty
34  return std::forward<F>(func)(std::forward<std::tuple_element_t<Is, U>>(std::get<Is>(tuple))...);
35  }
36  template <typename F, typename T, typename... Ts>
37  decltype(auto) invokeSlot(F &&func, T *object, Ts &&...args)
38  {
39  using seq =
40  MaskSequence<std::make_index_sequence<sizeof...(Ts)>, !TIsQPrivateSignal<std::decay_t<Ts>>::value...>;
41  return invokeSlotImpl(std::forward<F>(func), object, std::forward_as_tuple(std::forward<Ts>(args)...), seq(),
42  std::is_member_pointer<std::decay_t<F>>());
43  }
44 
45  // Like QMetaObject::invokeMethod but the return value is accessed through a QFuture, and extra arguments can be
46  // provided.
47  template <typename T, typename F, typename... Ts>
48  auto invokeMethod(T *object, F &&func, Ts &&...args)
49  {
50  const auto invoker = [](auto &&...x) { return private_ns::invokeSlot(std::forward<decltype(x)>(x)...); };
51  auto method = std::bind(invoker, std::forward<F>(func), object, std::forward<Ts>(args)...);
52  CPromise<decltype(std::move(method)())> promise;
53  QMetaObject::invokeMethod(
54  object, [promise, method = std::move(method)]() mutable { promise.setResultFrom(std::move(method)); });
55  return promise.future();
56  }
57 
59 } // namespace swift::misc::private_ns
60 
61 #endif // SWIFT_MISC_INVOKE_H