/****************************************************************************** * Copyright (C) 2017 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 // Includes #include #include #include #if 0 // Waiting for C++17 #include template using optional = std::experimental::optional; #endif // Enable std::unordered_map namespace std { template <> struct hash { size_t operator()(const QString& s) const Q_DECL_NOEXCEPT { return qHash(s, uint(qGlobalQHashSeed())); } }; } namespace QMatrixClient { struct NoneTag {}; constexpr NoneTag none {}; /** A crude substitute for `optional` while we're not C++17 * * Only works with default-constructible types. */ template class Omittable { public: explicit Omittable() : Omittable(none) { } Omittable(NoneTag) : _omitted(true) { } Omittable(const T& val) : _value(val), _omitted(false) { } Omittable(T&& val) : _value(std::move(val)), _omitted(false) { } Omittable& operator=(const T& val) { _value = val; _omitted = false; return *this; } Omittable& operator=(T&& val) { _value = std::move(val); _omitted = false; return *this; } bool omitted() const { return _omitted; } const T& value() const { return _value; } T& value() { return _value; } T&& release() { _omitted = true; return std::move(_value); } operator bool() const { return !_omitted; } private: T _value; bool _omitted; }; // 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; } template inline QJsonArray toJson(const std::vector& vals) { QJsonArray ar; for (const auto& v: vals) ar.push_back(toJson(v)); return ar; } template inline QJsonArray toJson(const QVector& vals) { QJsonArray ar; for (const auto& v: vals) ar.push_back(toJson(v)); 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) { QJsonObject json; for (auto it = hashMap.begin(); it != hashMap.end(); ++it) json.insert(it.key(), toJson(it.value())); return json; } template inline QJsonObject toJson(const std::unordered_map& hashMap) { QJsonObject json; for (auto it = hashMap.begin(); it != hashMap.end(); ++it) json.insert(it.key(), toJson(it.value())); return json; } template inline auto toJson(const Omittable& omittable) -> decltype(toJson(omittable.value())) { if (omittable) return toJson(omittable.value()); return {}; } #if 0 template inline auto toJson(const optional& optVal) { if (optVal) return toJson(optVal.value()); return decltype(toJson(std::declval()))(); } #endif template struct FromJson { T operator()(const QJsonValue& jv) const { return static_cast(jv); } }; template inline T fromJson(const QJsonValue& jv) { return FromJson()(jv); } template <> struct FromJson { auto operator()(const QJsonValue& jv) const { return jv.toBool(); } }; template <> struct FromJson { auto operator()(const QJsonValue& jv) const { return jv.toInt(); } }; template <> struct FromJson { auto operator()(const QJsonValue& jv) const { return jv.toDouble(); } }; template <> struct FromJson { auto operator()(const QJsonValue& jv) const { return qint64(jv.toDouble()); } }; template <> struct FromJson { auto operator()(const QJsonValue& jv) const { return jv.toString(); } }; template <> struct FromJson { auto operator()(const QJsonValue& jv) const { return QDateTime::fromMSecsSinceEpoch(fromJson(jv), Qt::UTC); } }; template <> struct FromJson { auto operator()(const QJsonValue& jv) const { return fromJson(jv).date(); } }; template <> struct FromJson { auto operator()(const QJsonValue& jv) const { return jv.toObject(); } }; template <> struct FromJson { auto operator()(const QJsonValue& jv) const { return jv.toArray(); } }; template struct FromJson> { auto operator()(const QJsonValue& jv) const { using size_type = typename std::vector::size_type; const auto jsonArray = jv.toArray(); std::vector vect; vect.resize(size_type(jsonArray.size())); std::transform(jsonArray.begin(), jsonArray.end(), vect.begin(), FromJson()); return vect; } }; template struct FromJson> { auto operator()(const QJsonValue& jv) const { const auto jsonArray = jv.toArray(); QVector vect; vect.resize(jsonArray.size()); std::transform(jsonArray.begin(), jsonArray.end(), vect.begin(), FromJson()); return vect; } }; template struct FromJson> { auto operator()(const QJsonValue& jv) const { const auto jsonArray = jv.toArray(); QList sl; sl.reserve(jsonArray.size()); std::transform(jsonArray.begin(), jsonArray.end(), std::back_inserter(sl), FromJson()); return sl; } }; template <> struct FromJson : FromJson> { }; template <> struct FromJson { auto operator()(const QJsonValue& jv) const { return fromJson(jv).toLatin1(); } }; template struct FromJson> { auto operator()(const QJsonValue& jv) const { const auto json = jv.toObject(); QHash h; h.reserve(json.size()); for (auto it = json.begin(); it != json.end(); ++it) h.insert(it.key(), fromJson(it.value())); return h; } }; template struct FromJson> { auto operator()(const QJsonValue& jv) const { const auto json = jv.toObject(); std::unordered_map h; h.reserve(size_t(json.size())); for (auto it = json.begin(); it != json.end(); ++it) h.insert(std::make_pair(it.key(), fromJson(it.value()))); return h; } }; // Conditional insertion into a QJsonObject namespace _impl { // This one is for types that don't have isEmpty() template struct AddNode { static void impl(QJsonObject& o, QString key, JsonT&& value) { o.insert(std::move(key), std::forward(value)); } }; // This one is for types that have isEmpty() template struct AddNode().isEmpty())> { static void impl(QJsonObject& o, QString key, JsonT&& value) { if (!value.isEmpty()) o.insert(std::move(key), std::forward(value)); } }; } static constexpr bool IfNotEmpty = false; template inline void addToJson(QJsonObject& o, QString key, ValT&& value) { auto&& json = toJson(std::forward(value)); // QJsonValue doesn't have isEmpty and consumes all other types // (QString, QJsonObject etc.). _impl::AddNode> ::impl(o, std::move(key), std::move(json)); } } // namespace QMatrixClient