QXmpp: QXmppDataFormBase.h Source File

QXmpp Version: 1.14.4

Loading...
Searching...
No Matches
QXmppDataFormBase.h
1// SPDX-FileCopyrightText: 2021 Linus Jahn <lnj@kaidan.im>
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4
5#ifndef QXMPPDATAFORMBASED_H
6#define QXMPPDATAFORMBASED_H
7
8#include "QXmppDataForm.h"
9
10#include <optional>
11
12class QXmppDataForm;
13
14 class QXMPP_EXPORT QXmppDataFormBase
15{
16public:
17 virtual QXmppDataForm toDataForm() const;
18
19protected:
20 static bool fromDataForm(const QXmppDataForm &form, QXmppDataFormBase &parent);
21
22 virtual QString formType() const = 0;
23 virtual void parseForm(const QXmppDataForm &) = 0;
24 virtual void serializeForm(QXmppDataForm &) const = 0;
25
26 std::optional<quint32> parseUInt(const QVariant &variant)
27 {
28 bool ok;
29 if (const auto result = variant.toString().toUInt(&ok); ok) {
30 return result;
31 }
32 return std::nullopt;
33 }
34
35 std::optional<quint64> parseULongLong(const QVariant &variant)
36 {
37 bool ok;
38 if (const auto result = variant.toString().toULongLong(&ok); ok) {
39 return result;
40 }
41 return std::nullopt;
42 }
43
44 std::optional<bool> parseBool(const QVariant &variant)
45 {
46#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
47 if (variant.typeId() == QMetaType::Type::Bool) {
48#else
49 if (variant.type() == QVariant::Bool) {
50#endif
51 return variant.toBool();
52#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
53 } else if (variant.typeId() == QMetaType::Type::QString) {
54#else
55 } else if (variant.type() == QVariant::String) {
56#endif
57 auto string = variant.toString();
58 if (string == u"1" || string == u"true") {
59 return true;
60 } else if (string == u"0" || string == u"false") {
61 return false;
62 }
63 }
64 return std::nullopt;
65 }
66
67 template<typename T>
68 static void serializeValue(QXmppDataForm &form, QXmppDataForm::Field::Type type, const QString &name, const T &value)
69 {
70 form.appendField(QXmppDataForm::Field(type, name, value));
71 }
72
73 template<typename T>
74 static void serializeNullable(QXmppDataForm &form, QXmppDataForm::Field::Type type, QStringView name, const T &value)
75 {
76 if (!value.isNull()) {
77 serializeValue(form, type, name.toString(), value);
78 }
79 }
80
81 template<typename T>
82 static void serializeEmptyable(QXmppDataForm &form, QXmppDataForm::Field::Type type, QStringView name, const T &value)
83 {
84 if (!value.isEmpty()) {
85 serializeValue(form, type, name.toString(), value);
86 }
87 }
88
89 template<typename T, typename ValueConverter = T (*)(T)>
90 static void serializeOptional(
91 QXmppDataForm &form, QXmppDataForm::Field::Type type, QStringView name, const std::optional<T> &optional, ValueConverter convert = [](T a) { return a; })
92 {
93 if (optional.has_value()) {
94 serializeValue(form, type, name.toString(), convert(*optional));
95 }
96 }
97
98 template<typename T>
99 static void serializeOptionalNumber(QXmppDataForm &form, QXmppDataForm::Field::Type type, QStringView name, std::optional<T> optional)
100 {
101 if (optional.has_value()) {
102 serializeValue(form, type, name.toString(), QString::number(*optional));
103 }
104 }
105
106 static void serializeDatetime(QXmppDataForm &form, const QString &name, const QDateTime &datetime, QXmppDataForm::Field::Type type = QXmppDataForm::Field::TextSingleField);
107};
108
109class QXmppExtensibleDataFormBasePrivate;
110
111 class QXMPP_EXPORT QXmppExtensibleDataFormBase : public QXmppDataFormBase
112{
113public:
114 QXmppExtensibleDataFormBase();
116 QXmppExtensibleDataFormBase(const QXmppExtensibleDataFormBase &);
117 QXmppExtensibleDataFormBase(QXmppExtensibleDataFormBase &&);
118 virtual ~QXmppExtensibleDataFormBase();
119
120 QXmppExtensibleDataFormBase &operator=(const QXmppExtensibleDataFormBase &);
121 QXmppExtensibleDataFormBase &operator=(QXmppExtensibleDataFormBase &&);
123
124 QList<QXmppDataForm::Field> unknownFields() const;
125 void setUnknownFields(const QList<QXmppDataForm::Field> &unknownFields);
126
127protected:
128 void parseForm(const QXmppDataForm &) override;
129 void serializeForm(QXmppDataForm &) const override;
130
131 virtual bool parseField(const QXmppDataForm::Field &);
132
133private:
134 QSharedDataPointer<QXmppExtensibleDataFormBasePrivate> d;
135};
136
137namespace QXmpp::Private {
138
139template<typename T>
140concept DataFormConvertible = requires(const QXmppDataForm &form) {
141 { T::DataFormType };
142 { T::fromDataForm(form) } -> std::same_as<std::optional<T>>;
143};
144
145template<DataFormConvertible T>
146inline constexpr auto DataFormType = T::DataFormType;
147
148} // namespace QXmpp::Private
149
150#endif // QXMPPDATAFORMBASED_H
QXmppDataForm::Field
Definition QXmppDataForm.h:93
QXmppDataForm::Field::Type
Type
This enum is used to describe a field's type.
Definition QXmppDataForm.h:96
QXmppDataFormBase
Definition QXmppDataFormBase.h:15
QXmppDataFormBase::fromDataForm
static bool fromDataForm(const QXmppDataForm &form, QXmppDataFormBase &parent)
Definition QXmppDataFormBase.cpp:55
QXmppDataFormBase::parseUInt
std::optional< quint32 > parseUInt(const QVariant &variant)
Definition QXmppDataFormBase.h:26
QXmppDataFormBase::serializeOptionalNumber
static void serializeOptionalNumber(QXmppDataForm &form, QXmppDataForm::Field::Type type, QStringView name, std::optional< T > optional)
Definition QXmppDataFormBase.h:99
QXmppDataFormBase::serializeForm
virtual void serializeForm(QXmppDataForm &) const =0
QXmppDataFormBase::parseBool
std::optional< bool > parseBool(const QVariant &variant)
Definition QXmppDataFormBase.h:44
QXmppDataFormBase::serializeNullable
static void serializeNullable(QXmppDataForm &form, QXmppDataForm::Field::Type type, QStringView name, const T &value)
Definition QXmppDataFormBase.h:74
QXmppDataFormBase::parseForm
virtual void parseForm(const QXmppDataForm &)=0
QXmppDataFormBase::parseULongLong
std::optional< quint64 > parseULongLong(const QVariant &variant)
Definition QXmppDataFormBase.h:35
QXmppDataFormBase::serializeValue
static void serializeValue(QXmppDataForm &form, QXmppDataForm::Field::Type type, const QString &name, const T &value)
Definition QXmppDataFormBase.h:68
QXmppDataFormBase::serializeOptional
static void serializeOptional(QXmppDataForm &form, QXmppDataForm::Field::Type type, QStringView name, const std::optional< T > &optional, ValueConverter convert=[](T a) { return a;})
Definition QXmppDataFormBase.h:90
QXmppDataFormBase::serializeEmptyable
static void serializeEmptyable(QXmppDataForm &form, QXmppDataForm::Field::Type type, QStringView name, const T &value)
Definition QXmppDataFormBase.h:82
QXmppDataFormBase::formType
virtual QString formType() const =0
QXmppDataFormBase::toDataForm
virtual QXmppDataForm toDataForm() const
Definition QXmppDataFormBase.cpp:39
QXmppDataForm
Definition QXmppDataForm.h:28
QXmppDataForm::appendField
void appendField(QXmppDataForm::Field &&field)
Definition QXmppDataForm.cpp:779
QXmppExtensibleDataFormBase::parseForm
void parseForm(const QXmppDataForm &) override
Definition QXmppDataFormBase.cpp:191
QXmppExtensibleDataFormBase::serializeForm
void serializeForm(QXmppDataForm &) const override
Definition QXmppDataFormBase.cpp:204
QXmppExtensibleDataFormBase::parseField
virtual bool parseField(const QXmppDataForm::Field &)
Definition QXmppDataFormBase.cpp:215
QXmppExtensibleDataFormBase::setUnknownFields
void setUnknownFields(const QList< QXmppDataForm::Field > &unknownFields)
Definition QXmppDataFormBase.cpp:186
QXmppExtensibleDataFormBase::unknownFields
QList< QXmppDataForm::Field > unknownFields() const
Definition QXmppDataFormBase.cpp:178