aboutsummaryrefslogtreecommitdiff
path: root/lib/converters.h
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2018-06-24 09:22:54 +0900
committerKitsune Ral <Kitsune-Ral@users.sf.net>2018-06-24 09:22:54 +0900
commit58a95b07908d9f1afa1bd8f99d26ccac24ff3b89 (patch)
tree0276560f650609b2b5bee71b0cac0234881ef24f /lib/converters.h
parentfe4bedeb349ed867feba7cb3c996a97f726d2083 (diff)
downloadlibquotient-58a95b07908d9f1afa1bd8f99d26ccac24ff3b89.tar.gz
libquotient-58a95b07908d9f1afa1bd8f99d26ccac24ff3b89.zip
converters.h: Support QSet<>
Also: toJson<optional> is no more (was #if 0'd anyway).
Diffstat (limited to 'lib/converters.h')
-rw-r--r--lib/converters.h51
1 files changed, 39 insertions, 12 deletions
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 <unordered_map>
#include <vector>
-#include <functional>
#if 0 // Waiting for C++17
#include <experimental/optional>
@@ -147,16 +146,16 @@ namespace QMatrixClient
}
template <typename T>
- inline QJsonObject toJson(const QHash<QString, T>& hashMap)
+ inline QJsonObject toJson(const QSet<T>& 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 <typename T>
- inline QJsonObject toJson(const std::unordered_map<QString, T>& hashMap)
+ inline QJsonObject toJson(const QHash<QString, T>& hashMap)
{
QJsonObject json;
for (auto it = hashMap.begin(); it != hashMap.end(); ++it)
@@ -164,16 +163,14 @@ namespace QMatrixClient
return json;
}
-#if 0
template <typename T>
- inline auto toJson(const optional<T>& optVal)
+ inline QJsonObject toJson(const std::unordered_map<QString, T>& hashMap)
{
- if (optVal)
- return toJson(optVal.value());
-
- return decltype(toJson(std::declval<T>()))();
+ QJsonObject json;
+ for (auto it = hashMap.begin(); it != hashMap.end(); ++it)
+ json.insert(it.key(), toJson(it.value()));
+ return json;
}
-#endif
template <typename T>
struct FromJson
@@ -301,6 +298,18 @@ namespace QMatrixClient
QMap<QString, QVariant> operator()(const QJsonValue& jv) const;
};
+ template <typename T> struct FromJson<QSet<T>>
+ {
+ auto operator()(const QJsonValue& jv) const
+ {
+ const auto json = jv.toObject();
+ QSet<T> s; s.reserve(json.size());
+ for (auto it = json.begin(); it != json.end(); ++it)
+ s.insert(it.key());
+ return s;
+ }
+ };
+
template <typename T> struct FromJson<QHash<QString, T>>
{
auto operator()(const QJsonValue& jv) const
@@ -393,6 +402,24 @@ namespace QMatrixClient
AddNode<ValT>::impl(container, key, QString{});
}
};
+
+#if 0
+ // This is a special one that unfolds optional<>
+ template <typename ValT, bool Force>
+ struct AddNode<optional<ValT>, Force>
+ {
+ template <typename ContT, typename OptionalT>
+ static void impl(ContT& container,
+ const QString& key, const OptionalT& value)
+ {
+ if (value)
+ AddNode<ValT>::impl(container, key, value.value());
+ else if (Force) // Edge case, no value but must put something
+ AddNode<ValT>::impl(container, key, QString{});
+ }
+ };
+#endif
+
} // namespace _impl
static constexpr bool IfNotEmpty = false;