Coverage Report

Created: 2026-03-12 07:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qtbase/src/dbus/qdbuspendingreply.h
Line
Count
Source
1
// Copyright (C) 2016 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
// Qt-Security score:significant reason:default
4
5
#ifndef QDBUSPENDINGREPLY_H
6
#define QDBUSPENDINGREPLY_H
7
8
#include <QtDBus/qtdbusglobal.h>
9
#include <QtDBus/qdbusargument.h>
10
#include <QtDBus/qdbuspendingcall.h>
11
12
#ifndef QT_NO_DBUS
13
14
class tst_QDBusPendingReply;
15
16
QT_BEGIN_NAMESPACE
17
18
19
class Q_DBUS_EXPORT QDBusPendingReplyBase : public QDBusPendingCall
20
{
21
protected:
22
    QDBusPendingReplyBase();
23
#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
24
    ~QDBusPendingReplyBase();
25
    QDBusPendingReplyBase(const QDBusPendingReplyBase &) = default;
26
    QDBusPendingReplyBase &operator=(const QDBusPendingReplyBase &) = default;
27
    QDBusPendingReplyBase(QDBusPendingReplyBase &&) noexcept = default;
28
    QDBusPendingReplyBase &operator=(QDBusPendingReplyBase &&) noexcept = default;
29
#endif
30
31
    void assign(const QDBusPendingCall &call);
32
    void assign(const QDBusMessage &message);
33
34
    QVariant argumentAt(int index) const;
35
    void setMetaTypes(int count, const QMetaType *metaTypes);
36
};
37
38
namespace QDBusPendingReplyTypes {
39
    template<int Index, typename T, typename... Types>
40
    struct Select
41
    {
42
        typedef Select<Index - 1, Types...> Next;
43
        typedef typename Next::Type Type;
44
    };
45
    template<typename T, typename... Types>
46
    struct Select<0, T, Types...>
47
    {
48
        typedef T Type;
49
    };
50
51
    template<typename T> inline QMetaType metaTypeFor()
52
    { return QMetaType::fromType<T>(); }
53
    // specialize for QVariant, allowing it to be used in place of QDBusVariant
54
    template<> inline QMetaType metaTypeFor<QVariant>()
55
0
    { return QMetaType::fromType<QDBusVariant>(); }
56
}
57
58
59
template<typename... Types>
60
class QDBusPendingReply : public QDBusPendingReplyBase
61
{
62
    friend class ::tst_QDBusPendingReply;
63
    template<int Index> using Select = QDBusPendingReplyTypes::Select<Index, Types...>;
64
public:
65
    enum { Count = std::is_same_v<typename Select<0>::Type, void> ? 0 : sizeof...(Types) };
66
67
    inline constexpr int count() const { return Count; }
68
69
    inline QDBusPendingReply() = default;
70
    // Rule Of Zero applies!
71
72
    inline Q_IMPLICIT QDBusPendingReply(const QDBusPendingCall &call) // required by qdbusxml2cpp-generated code
73
    { *this = call; }
74
    inline Q_IMPLICIT QDBusPendingReply(const QDBusMessage &message)
75
    { *this = message; }
76
77
    inline QDBusPendingReply &operator=(const QDBusPendingCall &call)
78
    { assign(call); return *this; }
79
    inline QDBusPendingReply &operator=(const QDBusMessage &message)
80
    { assign(message); return *this; }
81
82
    using QDBusPendingReplyBase::argumentAt;
83
    template<int Index> inline
84
    typename Select<Index>::Type argumentAt() const
85
    {
86
        static_assert(Index >= 0 && Index < Count, "Index out of bounds");
87
        typedef typename Select<Index>::Type ResultType;
88
        return qdbus_cast<ResultType>(argumentAt(Index));
89
    }
90
91
#if defined(Q_QDOC)
92
    bool isFinished() const;
93
    void waitForFinished();
94
    QVariant argumentAt(int index) const;
95
96
    bool isValid() const;
97
    bool isError() const;
98
    QDBusError error() const;
99
    QDBusMessage reply() const;
100
#endif
101
102
    inline typename Select<0>::Type value() const
103
    {
104
        return argumentAt<0>();
105
    }
106
107
    inline operator typename Select<0>::Type() const
108
    {
109
        return argumentAt<0>();
110
    }
111
112
private:
113
    inline void calculateMetaTypes()
114
    {
115
        if (!d) return;
116
        if constexpr (Count == 0) {
117
                setMetaTypes(0, nullptr);
118
        } else {
119
            std::array<QMetaType, Count> typeIds = { QDBusPendingReplyTypes::metaTypeFor<Types>()... };
120
            setMetaTypes(Count, typeIds.data());
121
        }
122
    }
123
124
    inline void assign(const QDBusPendingCall &call)
125
    {
126
        QDBusPendingReplyBase::assign(call);
127
        calculateMetaTypes();
128
    }
129
130
    inline void assign(const QDBusMessage &message)
131
    {
132
        QDBusPendingReplyBase::assign(message);
133
        calculateMetaTypes();
134
    }
135
};
136
137
template<>
138
class QDBusPendingReply<> : public QDBusPendingReplyBase
139
{
140
    friend class ::tst_QDBusPendingReply;
141
public:
142
    enum { Count = 0 };
143
0
    inline int count() const { return Count; }
144
145
    inline QDBusPendingReply() = default;
146
    // Rule Of Zero applies!
147
148
    inline Q_IMPLICIT QDBusPendingReply(const QDBusPendingCall &call) // required by qdbusxml2cpp-generated code
149
0
    { *this = call; }
150
    inline Q_IMPLICIT QDBusPendingReply(const QDBusMessage &message)
151
0
    { *this = message; }
152
153
    inline QDBusPendingReply &operator=(const QDBusPendingCall &call)
154
0
    { assign(call); return *this; }
155
    inline QDBusPendingReply &operator=(const QDBusMessage &message)
156
0
    { assign(message); return *this; }
157
158
private:
159
    inline void assign(const QDBusPendingCall &call)
160
0
    {
161
0
        QDBusPendingReplyBase::assign(call);
162
0
        if (d)
163
0
            setMetaTypes(0, nullptr);
164
0
    }
165
166
    inline void assign(const QDBusMessage &message)
167
0
    {
168
0
        QDBusPendingReplyBase::assign(message);
169
0
        if (d)
170
0
            setMetaTypes(0, nullptr);
171
0
    }
172
173
};
174
175
QT_END_NAMESPACE
176
177
#endif // QT_NO_DBUS
178
#endif