summaryrefslogtreecommitdiffstats
path: root/src/interfaceframework/qifpendingreply.h
blob: fed190316eedd07f04aa76265ac1442c979bafd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Copyright (C) 2021 The Qt Company Ltd.
// Copyright (C) 2019 Luxoft Sweden AB
// Copyright (C) 2018 Pelagicore AG
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef QIFPENDINGREPLY_H
#define QIFPENDINGREPLY_H

#include <QtQml/QJSValue>
#include <QtCore/QObject>
#include <QtCore/QSharedPointer>
#include <QtCore/QVariant>
#include <QtCore/QDebug>
#include <QtCore/QMetaEnum>

#include <QtInterfaceFramework/qtifglobal.h>

QT_BEGIN_NAMESPACE

class QIfPendingReplyWatcherPrivate;

Q_QTINTERFACEFRAMEWORK_EXPORT void qifRegisterPendingReplyBasicTypes();

// AXIVION Next Line Qt-CtorMissingParentArgument: private ctor
class Q_QTINTERFACEFRAMEWORK_EXPORT QIfPendingReplyWatcher : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QVariant value READ value NOTIFY valueChanged FINAL)
    Q_PROPERTY(bool valid READ isValid CONSTANT FINAL)
    Q_PROPERTY(bool resultAvailable READ isResultAvailable NOTIFY valueChanged FINAL)
    Q_PROPERTY(bool success READ isSuccessful NOTIFY valueChanged FINAL)

public:
    QVariant value() const;
    bool isValid() const;
    bool isResultAvailable() const;
    bool isSuccessful() const;

    Q_INVOKABLE void setSuccess(const QVariant &value);
    Q_INVOKABLE void setFailed();
    Q_INVOKABLE void then(const QJSValue &success, const QJSValue &failed = QJSValue());

Q_SIGNALS:
    void replyFailed();
    void replySuccess();
    void valueChanged(const QVariant &value);

private:
    explicit QIfPendingReplyWatcher(int userType);
    Q_DECLARE_PRIVATE(QIfPendingReplyWatcher)
    friend class QIfPendingReplyBase;
};

class Q_QTINTERFACEFRAMEWORK_EXPORT QIfPendingReplyBase
{
    Q_GADGET
    Q_PROPERTY(QIfPendingReplyWatcher* watcher READ watcher FINAL)
    Q_PROPERTY(QVariant value READ value FINAL)
    Q_PROPERTY(bool valid READ isValid FINAL)
    Q_PROPERTY(bool resultAvailable READ isResultAvailable FINAL)
    Q_PROPERTY(bool success READ isSuccessful FINAL)

public:
    explicit QIfPendingReplyBase(int userType);
    QIfPendingReplyBase() = default;
    QIfPendingReplyBase(const QIfPendingReplyBase & other);
    ~QIfPendingReplyBase() = default;
    QIfPendingReplyBase& operator=(const QIfPendingReplyBase&) = default;
    QIfPendingReplyBase& operator=(QIfPendingReplyBase&&) = default;

    QIfPendingReplyWatcher* watcher() const;
    QVariant value() const;
    bool isValid() const;
    bool isResultAvailable() const;
    bool isSuccessful() const;

    Q_INVOKABLE void then(const QJSValue &success, const QJSValue &failed = QJSValue());
    Q_INVOKABLE void setSuccess(const QVariant & value);
    Q_INVOKABLE void setFailed();

protected:
    void setSuccessNoCheck(const QVariant & value);

    QSharedPointer<QIfPendingReplyWatcher> m_watcher;
};

template <typename T> class QIfPendingReply : public QIfPendingReplyBase
{
public:
    QIfPendingReply(const T &successValue)
        : QIfPendingReply()
    {
        setSuccess(successValue);
    }

    QIfPendingReply()
        : QIfPendingReplyBase(qMetaTypeId<T>())
    {}

    using QIfPendingReplyBase::setSuccess;

    void setSuccess(const T &val)
    {
        setSuccessNoCheck(QVariant::fromValue(val));
    }

    T reply() const { return m_watcher->value().template value<T>(); }

    using QIfPendingReplyBase::then;

    void then(const std::function<void(const T &)> &success, const std::function<void()> &failed = std::function<void()>()) {
        if (isResultAvailable()) {
            if (isSuccessful() && success)
                success(reply());
            else if (failed)
                failed();
        } else {
            QSharedPointer<QIfPendingReplyWatcher> w = m_watcher;
            if (success) {
                QObject::connect(watcher(), &QIfPendingReplyWatcher::replySuccess, watcher(), [success, w]() {
                    success(w->value().value<T>());
                });
            }
            if (failed) {
                QObject::connect(watcher(), &QIfPendingReplyWatcher::replyFailed, watcher(), [failed]() {
                    failed();
                });
            }
        }
    }

    static QIfPendingReply createFailedReply()
    {
        QIfPendingReply<T> reply;
        reply.setFailed();
        return reply;
    }
};

template <> class QIfPendingReply <QVariant> : public QIfPendingReplyBase
{
public:
    QIfPendingReply(const QVariant &successValue)
        : QIfPendingReply()
    {
        setSuccess(successValue);
    }

    QIfPendingReply()
        : QIfPendingReplyBase(qMetaTypeId<QVariant>())
    {}

    void setSuccess(const QVariant &val)
    {
        setSuccessNoCheck(val);
    }

    QVariant reply() const { return m_watcher->value(); }

    void then(const std::function<void(const QVariant &)> &success, const std::function<void()> &failed = std::function<void()>()) {
        if (isResultAvailable()) {
            if (isSuccessful() && success)
                success(reply());
            else if (failed)
                failed();
        } else {
            QSharedPointer<QIfPendingReplyWatcher> w = m_watcher;
            if (success) {
                QObject::connect(watcher(), &QIfPendingReplyWatcher::replySuccess, watcher(), [success, w]() {
                    success(w->value());
                });
            }
            if (failed) {
                QObject::connect(watcher(), &QIfPendingReplyWatcher::replyFailed, watcher(), [failed]() {
                    failed();
                });
            }
        }
    }

    static QIfPendingReply createFailedReply()
    {
        QIfPendingReply<QVariant> reply;
        reply.setFailed();
        return reply;
    }
};

template <> class QIfPendingReply <void> : public QIfPendingReplyBase
{
public:
    QIfPendingReply()
        : QIfPendingReplyBase(qMetaTypeId<void>())
    {}

    using QIfPendingReplyBase::setSuccess;

    void setSuccess()
    {
        setSuccessNoCheck(QVariant());
    }

    void reply() const { return; }

    void then(const std::function<void()> &success, const std::function<void()> &failed = std::function<void()>()) {
        if (isResultAvailable()) {
            if (isSuccessful() && success)
                success();
            else if (failed)
                failed();
        } else {
            QSharedPointer<QIfPendingReplyWatcher> w = m_watcher;
            if (success) {
                QObject::connect(watcher(), &QIfPendingReplyWatcher::replySuccess, watcher(), [success, w]() {
                    success();
                });
            }
            if (failed) {
                QObject::connect(watcher(), &QIfPendingReplyWatcher::replyFailed, watcher(), [failed]() {
                    failed();
                });
            }
        }
    }

    static QIfPendingReply createFailedReply()
    {
        QIfPendingReply<void> reply;
        reply.setFailed();
        return reply;
    }
};

//Workaround for QTBUG-83664
//If T is a enum
template <typename T> Q_INLINE_TEMPLATE typename std::enable_if<QtPrivate::IsQEnumHelper<T>::Value, void>::type qIfRegisterPendingReplyType(const char *name = nullptr)
{
    qRegisterMetaType<T>();
    QString n;
    if (name) {
        n = QLatin1String(name);
    } else {
        QMetaEnum me = QMetaEnum::fromType<T>();
        if (me.isValid() && me.isFlag())
            n = QLatin1String(me.scope()) + QLatin1String("::") + QLatin1String(me.name());
        else
            n = QLatin1String(QMetaType(qMetaTypeId<T>()).name());
    }

    const QString t_name = QLatin1String("QIfPendingReply<") + n + QLatin1String(">");
    qRegisterMetaType<QIfPendingReplyBase>(qPrintable(t_name));
}

//If T is NOT a enum
template <typename T> Q_INLINE_TEMPLATE typename std::enable_if<!QtPrivate::IsQEnumHelper<T>::Value, void>::type qIfRegisterPendingReplyType(const char *name = nullptr)
{
    qRegisterMetaType<T>();
    const char* n = name ? name : QMetaType(qMetaTypeId<T>()).name();
    const QString t_name = QLatin1String("QIfPendingReply<") + QLatin1String(n) + QLatin1String(">");
    qRegisterMetaType<QIfPendingReplyBase>(qPrintable(t_name));
}

QT_END_NAMESPACE

#endif // QIFPENDINGREPLY_H