aboutsummaryrefslogtreecommitdiff
path: root/src/base
diff options
context:
space:
mode:
authorLinus Jahn <lnj@kaidan.im>2021-07-03 18:28:27 +0200
committerLinus Jahn <lnj@kaidan.im>2021-07-03 20:09:03 +0200
commit3d493f9c61f27b7c35304efbc563325c69d49147 (patch)
tree9a116c2dffea18c2d491efa6c1fd2271c50c342c /src/base
parent4ae1923665d4d4be602e45febb06fdecb590394f (diff)
downloadqxmpp-3d493f9c61f27b7c35304efbc563325c69d49147.tar.gz
Move QFuture utilities into new QXmppFutureUtils_p.h
Also ran clang-format on the file.
Diffstat (limited to 'src/base')
-rw-r--r--src/base/QXmppFutureUtils_p.h (renamed from src/base/QXmppGlobal_p.h)63
1 files changed, 41 insertions, 22 deletions
diff --git a/src/base/QXmppGlobal_p.h b/src/base/QXmppFutureUtils_p.h
index 0df63fed..d109f74b 100644
--- a/src/base/QXmppGlobal_p.h
+++ b/src/base/QXmppFutureUtils_p.h
@@ -21,8 +21,20 @@
*
*/
-#ifndef QXMPPGLOBAL_P_H
-#define QXMPPGLOBAL_P_H
+#ifndef QXMPPFUTUREUTILS_P_H
+#define QXMPPFUTUREUTILS_P_H
+
+//
+// W A R N I N G
+// -------------
+//
+// This file is not part of the QXmpp API. This header file may change from
+// version to version without notice, or even be removed.
+//
+// We mean it.
+//
+
+#include <QXmppIq.h>
#include <memory>
#include <variant>
@@ -30,12 +42,16 @@
#include <QFutureWatcher>
#include <QObject>
-#include <QXmppIq.h>
+namespace QXmpp::Private {
// helper for std::visit
-template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
+template<class... Ts>
+struct overloaded : Ts... {
+ using Ts::operator()...;
+};
// explicit deduction guide (not needed as of C++20)
-template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
+template<class... Ts>
+overloaded(Ts...) -> overloaded<Ts...>;
template<typename T>
QFuture<T> makeReadyFuture(T &&value)
@@ -52,7 +68,7 @@ QFuture<Result> chain(QFuture<Input> &&source, QObject *context, Converter task)
auto resultInterface = std::make_shared<QFutureInterface<Result>>(QFutureInterfaceBase::Started);
auto *watcher = new QFutureWatcher<Input>(context);
- QObject::connect(watcher, &QFutureWatcherBase::finished, context, [=](){
+ QObject::connect(watcher, &QFutureWatcherBase::finished, context, [=]() {
resultInterface->reportResult(task(watcher->result()));
resultInterface->reportFinished();
watcher->deleteLater();
@@ -65,20 +81,21 @@ template<typename Result, typename IqType, typename Input, typename Converter>
Result parseIq(Input &&sendResult, Converter convert)
{
return std::visit(overloaded {
- [convert { std::move(convert) }](const QDomElement &element) -> Result {
- IqType iq;
- iq.parse(element);
- if (iq.type() == QXmppIq::Error) {
- return iq.error();
- }
- return convert(std::move(iq));
- },
- [](QXmpp::PacketState) -> Result {
- using Error = QXmppStanza::Error;
- return Error(Error::Wait, Error::UndefinedCondition,
- QStringLiteral("Couldn't send request: lost connection."));
- },
- }, sendResult);
+ [convert { std::move(convert) }](const QDomElement &element) -> Result {
+ IqType iq;
+ iq.parse(element);
+ if (iq.type() == QXmppIq::Error) {
+ return iq.error();
+ }
+ return convert(std::move(iq));
+ },
+ [](QXmpp::PacketState) -> Result {
+ using Error = QXmppStanza::Error;
+ return Error(Error::Wait, Error::UndefinedCondition,
+ QStringLiteral("Couldn't send request: lost connection."));
+ },
+ },
+ sendResult);
}
template<typename Result, typename IqType, typename Input>
@@ -93,7 +110,7 @@ Result parseIq(Input &&sendResult)
template<typename Result, typename IqType, typename Input, typename Converter>
QFuture<Result> chainIq(QFuture<Input> &&input, QObject *context, Converter convert)
{
- return chain<Result>(std::move(input), context, [convert {std::move(convert)}](Input &&input) -> Result {
+ return chain<Result>(std::move(input), context, [convert { std::move(convert) }](Input &&input) -> Result {
return parseIq<Result, IqType>(std::move(input), convert);
});
}
@@ -106,4 +123,6 @@ QFuture<Result> chainIq(QFuture<Input> &&input, QObject *context)
});
}
-#endif // QXMPPGLOBAL_P_H
+}
+
+#endif // QXMPPFUTUREUTILS_P_H