From 58a95b07908d9f1afa1bd8f99d26ccac24ff3b89 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Sun, 24 Jun 2018 09:22:54 +0900 Subject: converters.h: Support QSet<> Also: toJson is no more (was #if 0'd anyway). --- lib/converters.h | 51 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 12 deletions(-) (limited to 'lib/converters.h') diff --git a/lib/converters.h b/lib/converters.h index 4a206a4c..28bb14cf 100644 --- a/lib/converters.h +++ b/lib/converters.h @@ -25,7 +25,6 @@ #include #include -#include #if 0 // Waiting for C++17 #include @@ -147,16 +146,16 @@ namespace QMatrixClient } template - inline QJsonObject toJson(const QHash& hashMap) + inline QJsonObject toJson(const QSet& set) { QJsonObject json; - for (auto it = hashMap.begin(); it != hashMap.end(); ++it) - json.insert(it.key(), toJson(it.value())); + for (auto e: set) + json.insert(toJson(e), QJsonObject{}); return json; } template - inline QJsonObject toJson(const std::unordered_map& hashMap) + inline QJsonObject toJson(const QHash& hashMap) { QJsonObject json; for (auto it = hashMap.begin(); it != hashMap.end(); ++it) @@ -164,16 +163,14 @@ namespace QMatrixClient return json; } -#if 0 template - inline auto toJson(const optional& optVal) + inline QJsonObject toJson(const std::unordered_map& hashMap) { - if (optVal) - return toJson(optVal.value()); - - return decltype(toJson(std::declval()))(); + QJsonObject json; + for (auto it = hashMap.begin(); it != hashMap.end(); ++it) + json.insert(it.key(), toJson(it.value())); + return json; } -#endif template struct FromJson @@ -301,6 +298,18 @@ namespace QMatrixClient QMap operator()(const QJsonValue& jv) const; }; + template struct FromJson> + { + auto operator()(const QJsonValue& jv) const + { + const auto json = jv.toObject(); + QSet s; s.reserve(json.size()); + for (auto it = json.begin(); it != json.end(); ++it) + s.insert(it.key()); + return s; + } + }; + template struct FromJson> { auto operator()(const QJsonValue& jv) const @@ -393,6 +402,24 @@ namespace QMatrixClient AddNode::impl(container, key, QString{}); } }; + +#if 0 + // This is a special one that unfolds optional<> + template + struct AddNode, Force> + { + template + static void impl(ContT& container, + const QString& key, const OptionalT& value) + { + if (value) + AddNode::impl(container, key, value.value()); + else if (Force) // Edge case, no value but must put something + AddNode::impl(container, key, QString{}); + } + }; +#endif + } // namespace _impl static constexpr bool IfNotEmpty = false; -- cgit v1.2.3