diff options
Diffstat (limited to 'lib/converters.h')
-rw-r--r-- | lib/converters.h | 97 |
1 files changed, 33 insertions, 64 deletions
diff --git a/lib/converters.h b/lib/converters.h index aa07261d..0085fa4b 100644 --- a/lib/converters.h +++ b/lib/converters.h @@ -38,11 +38,9 @@ using optional = std::experimental::optional<T>; #endif // Enable std::unordered_map<QString, T> -namespace std -{ +namespace std { template <> -struct hash<QString> -{ +struct hash<QString> { size_t operator()(const QString& s) const Q_DECL_NOEXCEPT { return qHash(s @@ -57,18 +55,15 @@ struct hash<QString> class QVariant; -namespace QMatrixClient -{ +namespace QMatrixClient { template <typename T> -struct JsonObjectConverter -{ +struct JsonObjectConverter { static void dumpTo(QJsonObject& jo, const T& pod) { jo = pod.toJson(); } static void fillFrom(const QJsonObject& jo, T& pod) { pod = T(jo); } }; template <typename T> -struct JsonConverter -{ +struct JsonConverter { static QJsonObject dump(const T& pod) { QJsonObject jo; @@ -139,52 +134,44 @@ inline void fillFromJson(const QJsonValue& jv, T& pod) // JsonConverter<> specialisations template <typename T> -struct TrivialJsonDumper -{ +struct TrivialJsonDumper { // Works for: QJsonValue (and all things it can consume), // QJsonObject, QJsonArray static auto dump(const T& val) { return val; } }; template <> -struct JsonConverter<bool> : public TrivialJsonDumper<bool> -{ +struct JsonConverter<bool> : public TrivialJsonDumper<bool> { static auto load(const QJsonValue& jv) { return jv.toBool(); } }; template <> -struct JsonConverter<int> : public TrivialJsonDumper<int> -{ +struct JsonConverter<int> : public TrivialJsonDumper<int> { static auto load(const QJsonValue& jv) { return jv.toInt(); } }; template <> -struct JsonConverter<double> : public TrivialJsonDumper<double> -{ +struct JsonConverter<double> : public TrivialJsonDumper<double> { static auto load(const QJsonValue& jv) { return jv.toDouble(); } }; template <> -struct JsonConverter<float> : public TrivialJsonDumper<float> -{ +struct JsonConverter<float> : public TrivialJsonDumper<float> { static auto load(const QJsonValue& jv) { return float(jv.toDouble()); } }; template <> -struct JsonConverter<qint64> : public TrivialJsonDumper<qint64> -{ +struct JsonConverter<qint64> : public TrivialJsonDumper<qint64> { static auto load(const QJsonValue& jv) { return qint64(jv.toDouble()); } }; template <> -struct JsonConverter<QString> : public TrivialJsonDumper<QString> -{ +struct JsonConverter<QString> : public TrivialJsonDumper<QString> { static auto load(const QJsonValue& jv) { return jv.toString(); } }; template <> -struct JsonConverter<QDateTime> -{ +struct JsonConverter<QDateTime> { static auto dump(const QDateTime& val) = delete; // not provided yet static auto load(const QJsonValue& jv) { @@ -193,8 +180,7 @@ struct JsonConverter<QDateTime> }; template <> -struct JsonConverter<QDate> -{ +struct JsonConverter<QDate> { static auto dump(const QDate& val) = delete; // not provided yet static auto load(const QJsonValue& jv) { @@ -203,14 +189,12 @@ struct JsonConverter<QDate> }; template <> -struct JsonConverter<QJsonArray> : public TrivialJsonDumper<QJsonArray> -{ +struct JsonConverter<QJsonArray> : public TrivialJsonDumper<QJsonArray> { static auto load(const QJsonValue& jv) { return jv.toArray(); } }; template <> -struct JsonConverter<QByteArray> -{ +struct JsonConverter<QByteArray> { static QString dump(const QByteArray& ba) { return ba.constData(); } static auto load(const QJsonValue& jv) { @@ -219,15 +203,13 @@ struct JsonConverter<QByteArray> }; template <> -struct JsonConverter<QVariant> -{ +struct JsonConverter<QVariant> { static QJsonValue dump(const QVariant& v); static QVariant load(const QJsonValue& jv); }; template <typename T> -struct JsonConverter<Omittable<T>> -{ +struct JsonConverter<Omittable<T>> { static QJsonValue dump(const Omittable<T>& from) { return from.omitted() ? QJsonValue() : toJson(from.value()); @@ -241,8 +223,7 @@ struct JsonConverter<Omittable<T>> }; template <typename VectorT, typename T = typename VectorT::value_type> -struct JsonArrayConverter -{ +struct JsonArrayConverter { static void dumpTo(QJsonArray& ar, const VectorT& vals) { for (const auto& v : vals) @@ -267,20 +248,17 @@ struct JsonArrayConverter }; template <typename T> -struct JsonConverter<std::vector<T>> : public JsonArrayConverter<std::vector<T>> -{}; +struct JsonConverter<std::vector<T>> + : public JsonArrayConverter<std::vector<T>> {}; template <typename T> -struct JsonConverter<QVector<T>> : public JsonArrayConverter<QVector<T>> -{}; +struct JsonConverter<QVector<T>> : public JsonArrayConverter<QVector<T>> {}; template <typename T> -struct JsonConverter<QList<T>> : public JsonArrayConverter<QList<T>> -{}; +struct JsonConverter<QList<T>> : public JsonArrayConverter<QList<T>> {}; template <> -struct JsonConverter<QStringList> : public JsonConverter<QList<QString>> -{ +struct JsonConverter<QStringList> : public JsonConverter<QList<QString>> { static auto dump(const QStringList& sl) { return QJsonArray::fromStringList(sl); @@ -288,8 +266,7 @@ struct JsonConverter<QStringList> : public JsonConverter<QList<QString>> }; template <> -struct JsonObjectConverter<QSet<QString>> -{ +struct JsonObjectConverter<QSet<QString>> { static void dumpTo(QJsonObject& json, const QSet<QString>& s) { for (const auto& e : s) @@ -305,8 +282,7 @@ struct JsonObjectConverter<QSet<QString>> }; template <typename HashMapT> -struct HashMapFromJson -{ +struct HashMapFromJson { static void dumpTo(QJsonObject& json, const HashMapT& hashMap) { for (auto it = hashMap.begin(); it != hashMap.end(); ++it) @@ -322,13 +298,11 @@ struct HashMapFromJson template <typename T> struct JsonObjectConverter<std::unordered_map<QString, T>> - : public HashMapFromJson<std::unordered_map<QString, T>> -{}; + : public HashMapFromJson<std::unordered_map<QString, T>> {}; template <typename T> struct JsonObjectConverter<QHash<QString, T>> - : public HashMapFromJson<QHash<QString, T>> -{}; + : public HashMapFromJson<QHash<QString, T>> {}; // We could use std::conditional<> below but QT_VERSION* macros in C++ code // cause (kinda valid but useless and noisy) compiler warnings about @@ -340,16 +314,14 @@ using variant_map_t = QVariantMap; #endif template <> -struct JsonConverter<variant_map_t> -{ +struct JsonConverter<variant_map_t> { static QJsonObject dump(const variant_map_t& vh); static QVariantHash load(const QJsonValue& jv); }; // Conditional insertion into a QJsonObject -namespace _impl -{ +namespace _impl { template <typename ValT> inline void addTo(QJsonObject& o, const QString& k, ValT&& v) { @@ -384,8 +356,7 @@ namespace _impl // This one is for types that don't have isEmpty() and for all types // when Force is true template <typename ValT, bool Force = true, typename = bool> - struct AddNode - { + struct AddNode { template <typename ContT, typename ForwardedT> static void impl(ContT& container, const QString& key, ForwardedT&& value) @@ -396,8 +367,7 @@ namespace _impl // This one is for types that have isEmpty() when Force is false template <typename ValT> - struct AddNode<ValT, false, decltype(std::declval<ValT>().isEmpty())> - { + struct AddNode<ValT, false, decltype(std::declval<ValT>().isEmpty())> { template <typename ContT, typename ForwardedT> static void impl(ContT& container, const QString& key, ForwardedT&& value) @@ -409,8 +379,7 @@ namespace _impl // This one unfolds Omittable<> (also only when Force is false) template <typename ValT> - struct AddNode<Omittable<ValT>, false> - { + struct AddNode<Omittable<ValT>, false> { template <typename ContT, typename OmittableT> static void impl(ContT& container, const QString& key, const OmittableT& value) |