BluezQt

tpendingcall.h
1 /*
2  * BluezQt - Asynchronous BlueZ wrapper library
3  *
4  * SPDX-FileCopyrightText: 2019 Manuel Weichselbaumer <[email protected]>
5  *
6  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7  */
8 
9 #pragma once
10 
11 #include <QDBusPendingReply>
12 
13 #include "pendingcall.h"
14 
15 namespace BluezQt
16 {
17 using namespace std::placeholders;
18 
19 /**
20  * @class BluezQt::TPendingCall tpendingcall.h <BluezQt/TPendingCall>
21  *
22  * Pending method call (template version).
23  *
24  * This class represents a pending method call. It is a convenient wrapper
25  * around QDBusPendingReply and QDBusPendingCallWatcher.
26  * The TPendingCall is a template class whose parameters are the types that will
27  * be used to extract the contents of the reply's data.
28  */
29 
30 // KF6 TODO: convert all PendingCalls to TPendingCall (or convert existing PendingCall class to templated version).
31 template<class... T>
32 class TPendingCall : public PendingCall
33 {
34 private:
35  template<int Index, typename Ty, typename... Ts>
36  struct Select {
37  using Type = typename Select<Index - 1, Ts...>::Type;
38  };
39  template<typename Ty, typename... Ts>
40  struct Select<0, Ty, Ts...> {
41  using Type = Ty;
42  };
43 
44 public:
45  /**
46  * Returns a return value at given index of the call.
47  *
48  * Returns the return value at position Index (which is a template parameter) cast to type Type.
49  * This function uses template code to determine the proper Type type, according to the type
50  * list used in the construction of this object.
51  *
52  * @return return value at index
53  */
54  template<int Index>
55  inline const typename Select<Index, T...>::Type valueAt() const
56  {
57  using ResultType = typename Select<Index, T...>::Type;
58 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
59  return qdbus_cast<ResultType>(m_reply.argumentAt(Index));
60 #else
61  return qdbus_cast<ResultType>(m_reply.argumentAt(Index), nullptr);
62 #endif
63  }
64 
65 private:
66  TPendingCall(const QDBusPendingCall &call, QObject *parent = nullptr)
67  : PendingCall(call, std::bind(&TPendingCall::process, this, _1, _2, _3), parent)
68  {
69  }
70 
71  void process(QDBusPendingCallWatcher *watcher, ErrorProcessor errorProcessor, QVariantList *values)
72  {
73  m_reply = *watcher;
74  errorProcessor(m_reply.error());
75  if (m_reply.isError()) {
76  return;
77  }
78 
79  for (int i = 0; i < m_reply.count(); ++i) {
80  values->append(m_reply.argumentAt(i));
81  }
82  }
83 
84  QDBusPendingReply<T...> m_reply;
85 
86  friend class MediaTransport;
87 };
88 
89 } // namespace BluezQt
const Select< Index, T... >::Type valueAt() const
Returns a return value at given index of the call.
Definition: tpendingcall.h:55
QVector< V > values(const QMultiHash< K, V > &c)
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sun Sep 25 2022 04:19:10 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.