From 38440a724acf8836e74e5bca29105e493f36eb7c Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Sun, 3 Jun 2018 17:12:35 +0900 Subject: csapi + converters: Support variant types (using QVariant) This mandated some rearrangement of toJson() overloads and FromJson<> specializations for QVariant* types - instead of variant_converters.h they are now in converters.cpp. --- lib/connection.cpp | 2 +- lib/converters.cpp | 59 ++++++++++++++++++++++++++++++++++++++++ lib/converters.h | 70 +++++++++++++++++++++++++++++++++--------------- lib/csapi/gtad.yaml | 9 ++++--- lib/room.cpp | 2 +- lib/variant_converters.h | 56 -------------------------------------- 6 files changed, 114 insertions(+), 84 deletions(-) create mode 100644 lib/converters.cpp delete mode 100644 lib/variant_converters.h (limited to 'lib') diff --git a/lib/connection.cpp b/lib/connection.cpp index dae7b663..8adb18b5 100644 --- a/lib/connection.cpp +++ b/lib/connection.cpp @@ -23,6 +23,7 @@ #include "events/directchatevent.h" #include "room.h" #include "settings.h" +#include "converters.h" #include "csapi/login.h" #include "csapi/logout.h" #include "csapi/receipts.h" @@ -33,7 +34,6 @@ #include "jobs/syncjob.h" #include "jobs/mediathumbnailjob.h" #include "jobs/downloadfilejob.h" -#include "variant_converters.h" #include #include diff --git a/lib/converters.cpp b/lib/converters.cpp new file mode 100644 index 00000000..f726db4c --- /dev/null +++ b/lib/converters.cpp @@ -0,0 +1,59 @@ +/****************************************************************************** +* Copyright (C) 2018 Kitsune Ral +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU Lesser General Public +* License as published by the Free Software Foundation; either +* version 2.1 of the License, or (at your option) any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this library; if not, write to the Free Software +* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "converters.h" + +#include + +using namespace QMatrixClient; + +QJsonValue toJson(const QVariant& v) +{ + return QJsonValue::fromVariant(v); +} + +QJsonObject toJson(const QVariantMap& map) +{ + return QJsonObject::fromVariantMap(map); +} + +#if (QT_VERSION >= QT_VERSION_CHECK(5, 5, 0)) +QJsonObject toJson(const QVariantHash& hMap) +{ + return QJsonObject::fromVariantHash(hMap); +} +#endif + +QVariant FromJson::operator()(const QJsonValue& jv) const +{ + return jv.toVariant(); +} + +QMap +FromJson>::operator()(const QJsonValue& jv) const +{ + return jv.toObject().toVariantMap(); +} + +#if (QT_VERSION >= QT_VERSION_CHECK(5, 5, 0)) +QHash +FromJson>::operator()(const QJsonValue& jv) const +{ + return jv.toObject().toVariantHash(); +} +#endif diff --git a/lib/converters.h b/lib/converters.h index 87d5ad7e..ff06e232 100644 --- a/lib/converters.h +++ b/lib/converters.h @@ -43,6 +43,8 @@ namespace std }; } +class QVariant; + namespace QMatrixClient { struct NoneTag {}; @@ -87,12 +89,29 @@ namespace QMatrixClient // This catches anything implicitly convertible to QJsonValue/Object/Array - inline QJsonValue toJson(const QJsonValue& val) { return val; } - inline QJsonObject toJson(const QJsonObject& o) { return o; } - inline QJsonArray toJson(const QJsonArray& arr) { return arr; } - // Special-case QStrings so that we could use isEmpty() on them - // (see _impl::AddNote<> below) - inline QString toJson(const QString& s) { return s; } + inline auto toJson(const QJsonValue& val) { return val; } + inline auto toJson(const QJsonObject& o) { return o; } + inline auto toJson(const QJsonArray& arr) { return arr; } + // Special-case QStrings and bools to avoid ambiguity between QJsonValue + // and QVariant (also, QString.isEmpty() is used in _impl::AddNote<> below) + inline auto toJson(const QString& s) { return s; } + inline QJsonValue toJson(bool b) { return b; } + + inline QJsonArray toJson(const QStringList& strings) + { + return QJsonArray::fromStringList(strings); + } + + inline QString toJson(const QByteArray& bytes) + { + return bytes.constData(); + } + + QJsonValue toJson(const QVariant& v); + QJsonObject toJson(const QMap& map); +#if (QT_VERSION >= QT_VERSION_CHECK(5, 5, 0)) + QJsonObject toJson(const QHash& hMap); +#endif template inline QJsonArray toJson(const std::vector& vals) @@ -112,16 +131,6 @@ namespace QMatrixClient return ar; } - inline QJsonArray toJson(const QStringList& strings) - { - return QJsonArray::fromStringList(strings); - } - - inline QString toJson(const QByteArray& bytes) - { - return bytes.constData(); - } - template inline QJsonObject toJson(const QHash& hashMap) { @@ -230,6 +239,19 @@ namespace QMatrixClient } }; + template <> struct FromJson + { + auto operator()(const QJsonValue& jv) const + { + return fromJson(jv).toLatin1(); + } + }; + + template <> struct FromJson + { + QVariant operator()(const QJsonValue& jv) const; + }; + template struct FromJson> { auto operator()(const QJsonValue& jv) const @@ -269,12 +291,9 @@ namespace QMatrixClient template <> struct FromJson : FromJson> { }; - template <> struct FromJson + template <> struct FromJson> { - auto operator()(const QJsonValue& jv) const - { - return fromJson(jv).toLatin1(); - } + QMap operator()(const QJsonValue& jv) const; }; template struct FromJson> @@ -289,6 +308,13 @@ namespace QMatrixClient } }; +#if (QT_VERSION >= QT_VERSION_CHECK(5, 5, 0)) + template <> struct FromJson> + { + QHash operator()(const QJsonValue& jv) const; + }; +#endif + template struct FromJson> { auto operator()(const QJsonValue& jv) const @@ -325,7 +351,7 @@ namespace QMatrixClient o.insert(std::move(key), std::forward(value)); } }; - } + } // namespace _impl static constexpr bool IfNotEmpty = false; diff --git a/lib/csapi/gtad.yaml b/lib/csapi/gtad.yaml index e3afd473..fada9c83 100644 --- a/lib/csapi/gtad.yaml +++ b/lib/csapi/gtad.yaml @@ -50,7 +50,7 @@ analyzer: type: QDateTime initializer: QDateTime::fromString("{{defaultValue}}") imports: - - //: + - //: &QString type: QString initializer: QStringLiteral("{{defaultValue}}") isString: @@ -68,7 +68,7 @@ analyzer: - /event.yaml$/: { type: EventPtr, imports: '"events/event.h"' } - /m\.room\.member$/: pass # This $ref is only used in an array, see below - - //: # Apply "avoidCopy" to all other ref'ed types + - //: *UseOmittable # Also apply "avoidCopy" to all other ref'ed types - array: - string: { type: QStringList, imports: } - +set: { moveOnly: } @@ -96,9 +96,10 @@ analyzer: imports: - //: type: QVariantHash - imports: '"variant_converters.h"' + imports: - variant: # A sequence `type` (multitype) in OpenAPI - { type: QVariant, imports: '"variant_converters.h"' } + - "string|null": *QString + - //: { type: QVariant, imports: } - schema: # Properties of inline structure definitions *UseOmittable diff --git a/lib/room.cpp b/lib/room.cpp index 9bcf0704..63b5e76d 100644 --- a/lib/room.cpp +++ b/lib/room.cpp @@ -40,7 +40,7 @@ #include "avatar.h" #include "connection.h" #include "user.h" -#include "variant_converters.h" +#include "converters.h" #include #include // for efficient string concats (operator%) diff --git a/lib/variant_converters.h b/lib/variant_converters.h deleted file mode 100644 index 76c395e1..00000000 --- a/lib/variant_converters.h +++ /dev/null @@ -1,56 +0,0 @@ -/****************************************************************************** -* Copyright (C) 2018 Kitsune Ral -* -* This library is free software; you can redistribute it and/or -* modify it under the terms of the GNU Lesser General Public -* License as published by the Free Software Foundation; either -* version 2.1 of the License, or (at your option) any later version. -* -* This library is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -* Lesser General Public License for more details. -* -* You should have received a copy of the GNU Lesser General Public -* License along with this library; if not, write to the Free Software -* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -*/ - -#pragma once - -#include - -#include "converters.h" - -namespace QMatrixClient -{ - inline QJsonObject toJson(const QVariantMap& map) - { - return QJsonObject::fromVariantMap(map); - } - -#if (QT_VERSION >= QT_VERSION_CHECK(5, 5, 0)) - inline QJsonObject toJson(const QVariantHash& hMap) - { - return QJsonObject::fromVariantHash(hMap); - } -#endif - - template <> struct FromJson - { - auto operator()(const QJsonValue& jv) const - { - return jv.toObject().toVariantMap(); - } - }; - -#if (QT_VERSION >= QT_VERSION_CHECK(5, 5, 0)) - template <> struct FromJson - { - auto operator()(const QJsonValue& jv) const - { - return jv.toObject().toVariantHash(); - } - }; -#endif -} -- cgit v1.2.3