| Directory: | ./ |
|---|---|
| File: | QtPdCom1/FutureWatchers.h |
| Date: | 2025-11-14 14:56:08 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 9 | 9 | 100.0% |
| Branches: | 2 | 4 | 50.0% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /***************************************************************************** | ||
| 2 | * | ||
| 3 | * Copyright (C) 2009 - 2022 Bjarne von Horn <vh@igh.de> | ||
| 4 | * | ||
| 5 | * This file is part of the QtPdCom library. | ||
| 6 | * | ||
| 7 | * The QtPdCom library is free software: you can redistribute it and/or modify | ||
| 8 | * it under the terms of the GNU Lesser General Public License as published by | ||
| 9 | * the Free Software Foundation, either version 3 of the License, or (at your | ||
| 10 | * option) any later version. | ||
| 11 | * | ||
| 12 | * The QtPdCom library is distributed in the hope that it will be useful, but | ||
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
| 14 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public | ||
| 15 | * License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU Lesser General Public License | ||
| 18 | * along with the QtPdCom Library. If not, see <http://www.gnu.org/licenses/>. | ||
| 19 | * | ||
| 20 | ****************************************************************************/ | ||
| 21 | |||
| 22 | |||
| 23 | #ifndef QTPDCOM_FUTUREWATCHERS_H | ||
| 24 | #define QTPDCOM_FUTUREWATCHERS_H | ||
| 25 | |||
| 26 | #include <QFutureWatcher> | ||
| 27 | #include <pdcom5/Variable.h> | ||
| 28 | #include "VariableList.h" | ||
| 29 | #include "Export.h" | ||
| 30 | #include "FutureWatchersDetails.h" | ||
| 31 | |||
| 32 | |||
| 33 | namespace QtPdCom { | ||
| 34 | |||
| 35 | /** | ||
| 36 | * Convenience class for processing the result of Process::findQt() | ||
| 37 | * | ||
| 38 | * Let's say you have a Widget which needs to process a PdCom::Variable | ||
| 39 | * instance. | ||
| 40 | * | ||
| 41 | * | ||
| 42 | * @code | ||
| 43 | * | ||
| 44 | * class MyWidget : public QObject | ||
| 45 | * { | ||
| 46 | * Q_OBJECT | ||
| 47 | * | ||
| 48 | * public: | ||
| 49 | * MyWidget(QObject *parent) : QObject(parent) {} | ||
| 50 | * | ||
| 51 | * void setVariable(QtPdCom::Process& proc, QString path) | ||
| 52 | * { | ||
| 53 | * const auto watcher = new QtPdCom::VariableWatcher(this); | ||
| 54 | * // this will be fired when the result has arrived | ||
| 55 | * connect(watcher, &QtPdCom::VariableWatcher::finished, | ||
| 56 | * this, &MyWidget::onVariableFound); | ||
| 57 | * // delete watcher if Process is resetted in the meantime | ||
| 58 | * connect(watcher, &QtPdCom::VarableWatcher::cancelled, | ||
| 59 | * watcher, &QObject::deleteLater); | ||
| 60 | * // start watching | ||
| 61 | * watcher->setFuture(proc.find(path)); | ||
| 62 | * } | ||
| 63 | * | ||
| 64 | * private slots: | ||
| 65 | * void onVariableFound() | ||
| 66 | * { | ||
| 67 | * const auto watcher = | ||
| 68 | * qobject_cast<QtPdCom::FindVariableWatcher*>(QObject::sender()); if | ||
| 69 | * (!watcher) return; | ||
| 70 | * // fetch result from watcher | ||
| 71 | * const PdCom::Variable result = watcher->result(); | ||
| 72 | * watcher->deleteLater(); | ||
| 73 | * // handle result | ||
| 74 | * } | ||
| 75 | * }; | ||
| 76 | * | ||
| 77 | * | ||
| 78 | * @endcode | ||
| 79 | * | ||
| 80 | * | ||
| 81 | */ | ||
| 82 | class QTPDCOM_PUBLIC VariableWatcher: public QFutureWatcher<PdCom::Variable> | ||
| 83 | { | ||
| 84 | Q_OBJECT | ||
| 85 | |||
| 86 | public: | ||
| 87 | using QFutureWatcher<PdCom::Variable>::QFutureWatcher; | ||
| 88 | }; | ||
| 89 | |||
| 90 | class QTPDCOM_PUBLIC ListWatcher: public QFutureWatcher<VariableList> | ||
| 91 | { | ||
| 92 | Q_OBJECT | ||
| 93 | |||
| 94 | public: | ||
| 95 | using QFutureWatcher<VariableList>::QFutureWatcher; | ||
| 96 | }; | ||
| 97 | |||
| 98 | |||
| 99 | template< class Result,class Object, class Callback> | ||
| 100 | 18 | inline QFutureWatcher<Result>& createWatcher(Object *obj, Callback &&callback) | |
| 101 | { | ||
| 102 |
2/4✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 7 taken 13 times.
✗ Branch 8 not taken.
|
18 | const auto watcher = new QFutureWatcher<Result>(obj); |
| 103 | 18 | QObject::connect( | |
| 104 | watcher, | ||
| 105 | &QFutureWatcherBase::finished, | ||
| 106 | obj, | ||
| 107 | 30 | [callback, watcher, obj]() { | |
| 108 | 15 | details::invoke<Result, Object>::call(callback, *obj, watcher); | |
| 109 | 15 | watcher->deleteLater(); | |
| 110 | 15 | }); | |
| 111 | 18 | QObject::connect( | |
| 112 | watcher, | ||
| 113 | &QFutureWatcherBase::canceled, | ||
| 114 | watcher, | ||
| 115 | &QObject::deleteLater); | ||
| 116 | 18 | return *watcher; | |
| 117 | } | ||
| 118 | |||
| 119 | |||
| 120 | } // namespace QtPdCom | ||
| 121 | |||
| 122 | #endif // QTPDCOM_FUTUREWATCHERS_H | ||
| 123 |