From aacc4bcb4a487871daae6717f77605aaba444341 Mon Sep 17 00:00:00 2001 From: Marc Deop Date: Sat, 2 Mar 2019 12:26:57 +0100 Subject: style: apply .clang-format to all .cpp and .h files --- lib/converters.h | 239 ++++++++++++++++++++++++------------------------------- 1 file changed, 104 insertions(+), 135 deletions(-) (limited to 'lib/converters.h') diff --git a/lib/converters.h b/lib/converters.h index af2be645..68a841cf 100644 --- a/lib/converters.h +++ b/lib/converters.h @@ -1,31 +1,31 @@ /****************************************************************************** -* 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 -*/ + * 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 "util.h" -#include +#include #include // Includes #include -#include -#include +#include #include +#include #include #include @@ -38,35 +38,29 @@ using optional = std::experimental::optional; #endif // Enable std::unordered_map -namespace std -{ - template <> struct hash - { +namespace std { + template <> struct hash { size_t operator()(const QString& s) const Q_DECL_NOEXCEPT { return qHash(s #if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)) - , uint(qGlobalQHashSeed()) + , + uint(qGlobalQHashSeed()) #endif - ); + ); } }; } class QVariant; -namespace QMatrixClient -{ - template - struct JsonObjectConverter - { +namespace QMatrixClient { + template struct JsonObjectConverter { static void dumpTo(QJsonObject& jo, const T& pod) { jo = pod; } static void fillFrom(const QJsonObject& jo, T& pod) { pod = jo; } }; - template - struct JsonConverter - { + template struct JsonConverter { static QJsonObject dump(const T& pod) { QJsonObject jo; @@ -83,39 +77,33 @@ namespace QMatrixClient static T load(const QJsonDocument& jd) { return doLoad(jd.object()); } }; - template - inline auto toJson(const T& pod) + template inline auto toJson(const T& pod) { return JsonConverter::dump(pod); } - template - inline auto fillJson(QJsonObject& json, const T& data) + template inline auto fillJson(QJsonObject& json, const T& data) { JsonObjectConverter::dumpTo(json, data); } - template - inline auto fromJson(const QJsonValue& jv) + template inline auto fromJson(const QJsonValue& jv) { return JsonConverter::load(jv); } - template - inline T fromJson(const QJsonDocument& jd) + template inline T fromJson(const QJsonDocument& jd) { return JsonConverter::load(jd); } - template - inline void fromJson(const QJsonValue& jv, T& pod) + template inline void fromJson(const QJsonValue& jv, T& pod) { if (!jv.isUndefined()) pod = fromJson(jv); } - template - inline void fromJson(const QJsonDocument& jd, T& pod) + template inline void fromJson(const QJsonDocument& jd, T& pod) { pod = fromJson(jd); } @@ -130,8 +118,7 @@ namespace QMatrixClient pod = fromJson(jv); } - template - inline void fillFromJson(const QJsonValue& jv, T& pod) + template inline void fillFromJson(const QJsonValue& jv, T& pod) { if (jv.isObject()) JsonObjectConverter::fillFrom(jv.toObject(), pod); @@ -139,59 +126,49 @@ namespace QMatrixClient // JsonConverter<> specialisations - template - struct TrivialJsonDumper - { + template struct TrivialJsonDumper { // Works for: QJsonValue (and all things it can consume), // QJsonObject, QJsonArray static auto dump(const T& val) { return val; } }; - template <> struct JsonConverter : public TrivialJsonDumper - { + template <> struct JsonConverter : public TrivialJsonDumper { static auto load(const QJsonValue& jv) { return jv.toBool(); } }; - template <> struct JsonConverter : public TrivialJsonDumper - { + template <> struct JsonConverter : public TrivialJsonDumper { static auto load(const QJsonValue& jv) { return jv.toInt(); } }; - template <> struct JsonConverter - : public TrivialJsonDumper - { + template <> + struct JsonConverter : public TrivialJsonDumper { static auto load(const QJsonValue& jv) { return jv.toDouble(); } }; - template <> struct JsonConverter : public TrivialJsonDumper - { + template <> struct JsonConverter : public TrivialJsonDumper { static auto load(const QJsonValue& jv) { return float(jv.toDouble()); } }; - template <> struct JsonConverter - : public TrivialJsonDumper - { + template <> + struct JsonConverter : public TrivialJsonDumper { static auto load(const QJsonValue& jv) { return qint64(jv.toDouble()); } }; - template <> struct JsonConverter - : public TrivialJsonDumper - { + template <> + struct JsonConverter : public TrivialJsonDumper { static auto load(const QJsonValue& jv) { return jv.toString(); } }; - template <> struct JsonConverter - { + template <> struct JsonConverter { static auto dump(const QDateTime& val) = delete; // not provided yet static auto load(const QJsonValue& jv) { - return QDateTime::fromMSecsSinceEpoch( - fromJson(jv), Qt::UTC); + return QDateTime::fromMSecsSinceEpoch(fromJson(jv), + Qt::UTC); } }; - template <> struct JsonConverter - { + template <> struct JsonConverter { static auto dump(const QDate& val) = delete; // not provided yet static auto load(const QJsonValue& jv) { @@ -199,14 +176,12 @@ namespace QMatrixClient } }; - template <> struct JsonConverter - : public TrivialJsonDumper - { + template <> + struct JsonConverter : public TrivialJsonDumper { static auto load(const QJsonValue& jv) { return jv.toArray(); } }; - template <> struct JsonConverter - { + template <> struct JsonConverter { static QString dump(const QByteArray& ba) { return ba.constData(); } static auto load(const QJsonValue& jv) { @@ -214,19 +189,16 @@ namespace QMatrixClient } }; - template <> struct JsonConverter - { + template <> struct JsonConverter { static QJsonValue dump(const QVariant& v); static QVariant load(const QJsonValue& jv); }; - template - struct JsonArrayConverter - { + template + struct JsonArrayConverter { static void dumpTo(QJsonArray& ar, const VectorT& vals) { - for (const auto& v: vals) + for (const auto& v : vals) ar.push_back(toJson(v)); } static auto dump(const VectorT& vals) @@ -237,8 +209,9 @@ namespace QMatrixClient } static auto load(const QJsonArray& ja) { - VectorT vect; vect.reserve(typename VectorT::size_type(ja.size())); - for (const auto& i: ja) + VectorT vect; + vect.reserve(typename VectorT::size_type(ja.size())); + for (const auto& i : ja) vect.push_back(fromJson(i)); return vect; } @@ -246,33 +219,32 @@ namespace QMatrixClient static auto load(const QJsonDocument& jd) { return load(jd.array()); } }; - template struct JsonConverter> - : public JsonArrayConverter> - { }; + template + struct JsonConverter> + : public JsonArrayConverter> { + }; - template struct JsonConverter> - : public JsonArrayConverter> - { }; + template + struct JsonConverter> : public JsonArrayConverter> { + }; - template struct JsonConverter> - : public JsonArrayConverter> - { }; + template + struct JsonConverter> : public JsonArrayConverter> { + }; - template <> struct JsonConverter - : public JsonConverter> - { + template <> + struct JsonConverter : public JsonConverter> { static auto dump(const QStringList& sl) { return QJsonArray::fromStringList(sl); } }; - template <> struct JsonObjectConverter> - { + template <> struct JsonObjectConverter> { static void dumpTo(QJsonObject& json, const QSet& s) { - for (const auto& e: s) - json.insert(toJson(e), QJsonObject{}); + for (const auto& e : s) + json.insert(toJson(e), QJsonObject {}); } static auto fillFrom(const QJsonObject& json, QSet& s) { @@ -283,9 +255,7 @@ namespace QMatrixClient } }; - template - struct HashMapFromJson - { + template struct HashMapFromJson { static void dumpTo(QJsonObject& json, const HashMapT& hashMap) { for (auto it = hashMap.begin(); it != hashMap.end(); ++it) @@ -296,19 +266,19 @@ namespace QMatrixClient h.reserve(jo.size()); for (auto it = jo.begin(); it != jo.end(); ++it) h[it.key()] = - fromJson(it.value()); + fromJson(it.value()); } }; template struct JsonObjectConverter> - : public HashMapFromJson> - { }; + : public HashMapFromJson> { + }; template struct JsonObjectConverter> - : public HashMapFromJson> - { }; + : public HashMapFromJson> { + }; // We could use std::conditional<> below but QT_VERSION* macros in C++ code // cause (kinda valid but useless and noisy) compiler warnings about @@ -319,35 +289,38 @@ namespace QMatrixClient #else QVariantMap; #endif - template <> struct JsonConverter - { + template <> struct JsonConverter { static QJsonObject dump(const variant_map_t& vh); static QVariantHash load(const QJsonValue& jv); }; // Conditional insertion into a QJsonObject - namespace _impl - { + namespace _impl { template inline void addTo(QJsonObject& o, const QString& k, ValT&& v) - { o.insert(k, toJson(v)); } + { + o.insert(k, toJson(v)); + } template inline void addTo(QUrlQuery& q, const QString& k, ValT&& v) - { q.addQueryItem(k, QStringLiteral("%1").arg(v)); } + { + q.addQueryItem(k, QStringLiteral("%1").arg(v)); + } // OpenAPI is entirely JSON-based, which means representing bools as // textual true/false, rather than 1/0. inline void addTo(QUrlQuery& q, const QString& k, bool v) { - q.addQueryItem(k, v ? QStringLiteral("true") - : QStringLiteral("false")); + q.addQueryItem( + k, v ? QStringLiteral("true") : QStringLiteral("false")); } - inline void addTo(QUrlQuery& q, const QString& k, const QStringList& vals) + inline void addTo(QUrlQuery& q, const QString& k, + const QStringList& vals) { - for (const auto& v: vals) + for (const auto& v : vals) q.addQueryItem(k, v); } @@ -359,8 +332,7 @@ namespace QMatrixClient // This one is for types that don't have isEmpty() template - struct AddNode - { + struct AddNode { template static void impl(ContT& container, const QString& key, ForwardedT&& value) @@ -371,31 +343,28 @@ namespace QMatrixClient // This one is for types that have isEmpty() template - struct AddNode().isEmpty())> - { + struct AddNode().isEmpty())> { template static void impl(ContT& container, const QString& key, ForwardedT&& value) { if (!value.isEmpty()) - AddNode::impl(container, - key, std::forward(value)); + AddNode::impl(container, key, + std::forward(value)); } }; // This is a special one that unfolds Omittable<> template - struct AddNode, Force> - { + struct AddNode, Force> { template - static void impl(ContT& container, - const QString& key, const OmittableT& value) + static void impl(ContT& container, const QString& key, + const OmittableT& value) { if (!value.omitted()) AddNode::impl(container, key, value.value()); else if (Force) // Edge case, no value but must put something - AddNode::impl(container, key, QString{}); + AddNode::impl(container, key, QString {}); } }; @@ -416,14 +385,14 @@ namespace QMatrixClient }; #endif - } // namespace _impl + } // namespace _impl static constexpr bool IfNotEmpty = false; template inline void addParam(ContT& container, const QString& key, ValT&& value) { - _impl::AddNode, Force> - ::impl(container, key, std::forward(value)); + _impl::AddNode, Force>::impl( + container, key, std::forward(value)); } -} // namespace QMatrixClient +} // namespace QMatrixClient -- cgit v1.2.3