6#ifndef QXMPPACCOUNTMIGRATIONMANAGER_H
7#define QXMPPACCOUNTMIGRATIONMANAGER_H
9#include "QXmppClientExtension.h"
10#include "QXmppFutureUtils_p.h"
15#include <unordered_map>
17struct QXmppExportDataPrivate;
19class QXMPP_EXPORT QXmppExportData
22 template<
typename T = QXmpp::Success>
23 using Result = std::variant<T, QXmppError>;
26 QXMPP_PRIVATE_DECLARE_RULE_OF_SIX(QXmppExportData)
28 static std::variant<QXmppExportData, QXmppError> fromDom(
const QDomElement &);
29 void toXml(QXmlStreamWriter *)
const;
31 const QString &accountJid()
const;
32 void setAccountJid(
const QString &jid);
35 std::optional<T> extension()
const
37 const auto it = extensions().find(std::type_index(
typeid(T)));
38 return it != extensions().cend() ? std::any_cast<T>(it->second) : std::optional<T>();
42 void setExtension(T &&value)
44 Q_ASSERT(isExtensionRegistered<T>());
45 setExtension(std::any(std::move(value)));
49 using ExtensionParser = Result<T> (*)(
const QDomElement &);
51 using ExtensionSerializer = void (*)(
const T &, QXmlStreamWriter &);
53 template<
typename T, ExtensionParser<T> parse, ExtensionSerializer<T> serialize>
54 static void registerExtension(QStringView tagName, QStringView xmlns)
56 using namespace QXmpp::Private;
57 using AnyParser = ExtensionParser<std::any>;
58 using AnySerializer = ExtensionSerializer<std::any>;
60 AnyParser parseAny = [](
const QDomElement &el) {
61 return std::visit(overloaded {
62 [](T data) -> Result<std::any> {
return std::move(data); },
63 [](
QXmppError e) -> Result<std::any> {
return std::move(e); },
68 AnySerializer serializeAny = [](
const std::any &data, QXmlStreamWriter &w) {
69 return std::invoke(serialize, std::any_cast<const T &>(data), w);
72 registerExtensionInternal(std::type_index(
typeid(T)), parseAny, serializeAny, tagName, xmlns);
76 static bool isExtensionRegistered() {
return isExtensionRegistered(std::type_index(
typeid(T))); }
80 friend class tst_QXmppAccountMigrationManager;
82 const std::unordered_map<std::type_index, std::any> &extensions()
const;
83 void setExtension(std::any value);
85 static void registerExtensionInternal(std::type_index, ExtensionParser<std::any>, ExtensionSerializer<std::any>, QStringView tagName, QStringView xmlns);
86 static bool isExtensionRegistered(std::type_index);
88 QSharedDataPointer<QXmppExportDataPrivate> d;
91struct QXmppAccountMigrationManagerPrivate;
97 friend struct QXmppAccountMigrationManagerPrivate;
100 template<
typename T = QXmpp::Success>
101 using Result = std::variant<T, QXmppError>;
109 template<
typename DataType,
typename ImportFunc,
typename ExportFunc>
110 void registerExportData(ImportFunc importFunc, ExportFunc exportFunc);
112 template<
typename DataType>
113 void unregisterExportData();
119 void unregisterMigrationDataInternal(std::type_index dataType);
121 std::unique_ptr<QXmppAccountMigrationManagerPrivate> d;
124template<
typename DataType,
typename ImportFunc,
typename ExportFunc>
127 using namespace QXmpp::Private;
129 static_assert(std::is_constructible_v<std::function<QXmppTask<Result<>>(
const DataType &)>, ImportFunc>);
130 static_assert(std::is_constructible_v<std::function<QXmppTask<Result<DataType>>()>, ExportFunc>);
131 static_assert(std::is_invocable_v<ImportFunc, const DataType &>);
132 static_assert(std::is_invocable_v<ExportFunc>);
133 static_assert(std::is_same_v<first_argument_t<ImportFunc>,
const DataType &>);
135 auto importInternal = [importFunc = std::move(importFunc)](std::any data) ->
QXmppTask<
Result<>> {
136 Q_ASSERT(std::type_index(data.type()) == std::type_index(
typeid(DataType)));
137 return importFunc(std::any_cast<DataType>(std::move(data)));
140 using AnyResult = std::variant<std::any, QXmppError>;
143 return std::visit(overloaded {
144 [](DataType data) -> AnyResult {
return std::any(std::move(data)); },
145 [](
QXmppError err) -> AnyResult {
return err; } },
150 registerMigrationDataInternal(std::type_index(
typeid(DataType)), std::move(importInternal), std::move(exportInternal));
153template<
typename DataType>
156 unregisterMigrationDataInternal(std::type_index(
typeid(DataType)));
Allows to export and import account data.
Definition: QXmppAccountMigrationManager.h:94
std::variant< T, QXmppError > Result
Definition: QXmppAccountMigrationManager.h:101
void unregisterExportData()
Definition: QXmppAccountMigrationManager.h:154
void registerExportData(ImportFunc importFunc, ExportFunc exportFunc)
Definition: QXmppAccountMigrationManager.h:125
Q_SIGNAL void errorOccurred(const QXmppError &error)
The QXmppClientExtension class is the base class for QXmppClient extensions.
Definition: QXmppClientExtension.h:32
Definition: QXmppTask.h:62
Definition: QXmppError.h:17