From d13f6bb870cdda71257f665367be8ef9fca86255 Mon Sep 17 00:00:00 2001 From: Mikaƫl Capelle Date: Tue, 17 May 2022 11:47:01 +0200 Subject: Apply clang-format. --- src/json.h | 185 +++++++++++++++++++++++++++++-------------------------------- 1 file changed, 87 insertions(+), 98 deletions(-) (limited to 'src/json.h') diff --git a/src/json.h b/src/json.h index d182f330..3b7ae2cf 100644 --- a/src/json.h +++ b/src/json.h @@ -1,143 +1,133 @@ #ifndef MODORGANIZER_JSON_INCLUDED #define MODORGANIZER_JSON_INCLUDED -#include +#include #include #include -#include #include +#include namespace json { -class failed {}; - +class failed +{}; namespace details { -QString typeName(const QJsonValue& v) -{ - if (v.isUndefined()) { - return "undefined"; - } else if (v.isNull()) { - return "null"; - } else if (v.isArray()) { - return "an array"; - } else if (v.isBool()) { - return "a bool"; - } else if (v.isDouble()) { - return "a double"; - } else if (v.isObject()) { - return "an object"; - } else if (v.isString()) { - return "a string"; - } else { - return "an unknown type"; + QString typeName(const QJsonValue& v) + { + if (v.isUndefined()) { + return "undefined"; + } else if (v.isNull()) { + return "null"; + } else if (v.isArray()) { + return "an array"; + } else if (v.isBool()) { + return "a bool"; + } else if (v.isDouble()) { + return "a double"; + } else if (v.isObject()) { + return "an object"; + } else if (v.isString()) { + return "a string"; + } else { + return "an unknown type"; + } } -} -QString typeName(const QJsonDocument& doc) -{ - if (doc.isEmpty()) { - return "empty"; - } else if (doc.isNull()) { - return "null"; - } else if (doc.isArray()) { - return "an array"; - } else if (doc.isObject()) { - return "an object"; - } else { - return "an unknown type"; + QString typeName(const QJsonDocument& doc) + { + if (doc.isEmpty()) { + return "empty"; + } else if (doc.isNull()) { + return "null"; + } else if (doc.isArray()) { + return "an array"; + } else if (doc.isObject()) { + return "an object"; + } else { + return "an unknown type"; + } } -} + template + T convert(const QJsonValue& v) = delete; -template -T convert(const QJsonValue& v) = delete; + template <> + bool convert(const QJsonValue& v) + { + if (!v.isBool()) { + throw failed(); + } -template <> -bool convert(const QJsonValue& v) -{ - if (!v.isBool()) { - throw failed(); + return v.toBool(); } - return v.toBool(); -} + template <> + QJsonObject convert(const QJsonValue& v) + { + if (!v.isObject()) { + throw failed(); + } -template <> -QJsonObject convert(const QJsonValue& v) -{ - if (!v.isObject()) { - throw failed(); + return v.toObject(); } - return v.toObject(); -} + template <> + QString convert(const QJsonValue& v) + { + if (!v.isString()) { + throw failed(); + } -template <> -QString convert(const QJsonValue& v) -{ - if (!v.isString()) { - throw failed(); + return v.toString(); } - return v.toString(); -} + template <> + QJsonArray convert(const QJsonValue& v) + { + if (!v.isArray()) { + throw failed(); + } -template <> -QJsonArray convert(const QJsonValue& v) -{ - if (!v.isArray()) { - throw failed(); + return v.toArray(); } - return v.toArray(); -} + template <> + qint64 convert(const QJsonValue& v) + { + if (!v.isDouble()) { + throw failed(); + } -template <> -qint64 convert(const QJsonValue& v) -{ - if (!v.isDouble()) { - throw failed(); + return static_cast(v.toDouble()); } - return static_cast(v.toDouble()); -} - -} // namespace - +} // namespace details template T convert(const QJsonValue& value, const char* what) { - try - { + try { return details::convert(value); - } - catch(failed&) - { - MOBase::log::error( - "'{}' is a {}, not a {}", - what, details::typeName(value), typeid(T).name); + } catch (failed&) { + MOBase::log::error("'{}' is a {}, not a {}", what, details::typeName(value), + typeid(T).name); throw; } } template -T convertWarn(const QJsonValue& value, const char* what, T def={}) +T convertWarn(const QJsonValue& value, const char* what, T def = {}) { - try - { + try { return details::convert(value); - } - catch(failed&) - { - MOBase::log::warn( - "'{}' is a {}, not a {}", - what, details::typeName(value), typeid(T).name()); + } catch (failed&) { + MOBase::log::warn("'{}' is a {}, not a {}", what, details::typeName(value), + typeid(T).name()); return def; } @@ -155,7 +145,7 @@ T get(const QJsonObject& o, const char* e) } template -T getWarn(const QJsonObject& o, const char* e, T def={}) +T getWarn(const QJsonObject& o, const char* e, T def = {}) { if (!o.contains(e)) { MOBase::log::warn("property '{}' is missing", e); @@ -166,7 +156,7 @@ T getWarn(const QJsonObject& o, const char* e, T def={}) } template -T getOpt(const QJsonObject& o, const char* e, T def={}) +T getOpt(const QJsonObject& o, const char* e, T def = {}) { if (!o.contains(e)) { return def; @@ -175,7 +165,6 @@ T getOpt(const QJsonObject& o, const char* e, T def={}) return convertWarn(o[e], e); } - template void requireObject(const Value& v, const char* what) { @@ -185,6 +174,6 @@ void requireObject(const Value& v, const char* what) } } -} // namespace +} // namespace json -#endif // MODORGANIZER_JSON_INCLUDED +#endif // MODORGANIZER_JSON_INCLUDED -- cgit v1.3.1