diff --git a/CMakeLists.txt b/CMakeLists.txt index 072630a..7437b73 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,6 +33,7 @@ qt_add_executable(appscheincommander iconutils.h iconutils.cpp presetstepsmodel.h presetstepsmodel.cpp patternmaker.h patternmaker.cpp + expected.hpp ) qt_add_qml_module(appscheincommander diff --git a/projectloader.cpp b/projectloader.cpp index 947571a..c01c05c 100644 --- a/projectloader.cpp +++ b/projectloader.cpp @@ -127,7 +127,7 @@ std::expected load(const QJsonArray &json) { if (res.has_value()) { result.push_back(res.value()); } else { - return std::unexpected(QString("%1: %2").arg(i).arg(res.error())); + return tl::make_unexpected(QString("%1: %2").arg(i).arg(res.error())); } } @@ -139,7 +139,7 @@ std::expected load(const QJsonArray &json) { QVector3D vec; if (json.size() != 3) { - return std::unexpected("Vector has wrong number of components"); + return tl::make_unexpected("Vector has wrong number of components"); } for (size_t i = 0; i < json.size(); i++) { @@ -147,7 +147,7 @@ std::expected load(const QJsonArray &json) { if (auto val = load(el); val) { vec[i] = val.value(); } else { - return std::unexpected(QString("%1: %2").arg(i).arg(val.error())); + return tl::make_unexpected(QString("%1: %2").arg(i).arg(val.error())); } } @@ -157,7 +157,7 @@ std::expected load(const QJsonArray &json) { template std::expected load(const QJsonValue &json) { if (!json.isArray()) - return std::unexpected("Not a JSON array"); + return tl::make_unexpected("Not a JSON array"); return load(json.toArray()); } @@ -166,7 +166,7 @@ std::expected load(const QJsonValue &json) { if (json.isDouble()) { return (T)json.toDouble(); } else { - return std::unexpected(QString("Not a number")); + return tl::make_unexpected(QString("Not a number")); } } @@ -175,19 +175,19 @@ std::expected load(const QJsonValue &json) { if (json.isString()) { return json.toString(); } else { - return std::unexpected(QString("Not a string")); + return tl::make_unexpected(QString("Not a string")); } } template std::expected load(const QJsonValue &json) { if (!json.isString()) { - return std::unexpected("Not a JSON string"); + return tl::make_unexpected("Not a JSON string"); } QMetaEnum me = QMetaEnum::fromType(); if (!me.isValid()) { - return std::unexpected("Invalid QMetaEnum"); + return tl::make_unexpected("Invalid QMetaEnum"); } bool ok = false; @@ -195,7 +195,7 @@ std::expected load(const QJsonValue &json) { if (ok) { return (T)value; } else { - return std::unexpected(QString("No value found for enum key %1").arg(json.toString())); + return tl::make_unexpected(QString("No value found for enum key %1").arg(json.toString())); } } @@ -204,14 +204,14 @@ std::expected loadIfExists(const QJsonObject &json, const QString &k if (json.contains(key)) { return load(json[key]); } else { - return std::unexpected(QString("does not exist")); + return tl::make_unexpected(QString("does not exist")); } } template std::expected load(const QJsonObject &json) { T t; - std::optional> error; + std::optional> error; iterateMembers(t, [&json, &error] (const char *name, auto &field) { if (error) @@ -220,7 +220,7 @@ std::expected load(const QJsonObject &json) { if (auto result = loadIfExists>(json, name)) { field = std::move(result.value()); } else { - error = std::unexpected(QString("%1: %2").arg(name).arg(result.error())); + error = tl::make_unexpected(QString("%1: %2").arg(name).arg(result.error())); } }); @@ -233,7 +233,7 @@ std::expected load(const QJsonObject &json) { template std::expected load(const QJsonValue &json) { if (!json.isObject()) - return std::unexpected("Not a JSON object"); + return tl::make_unexpected("Not a JSON object"); return load(json.toObject()); } @@ -241,7 +241,7 @@ std::expected load(const QJsonValue &json) { std::expected loadProject(const QJsonDocument &json) { if (!json.isObject()) { - return std::unexpected("JsonDocument is not an object"); + return tl::make_unexpected("JsonDocument is not an object"); } return load(json.object()); @@ -270,20 +270,20 @@ template std::expected save(const T &val) { QMetaEnum me = QMetaEnum::fromType(); if (!me.isValid()) { - return std::unexpected("Invalid QMetaEnum"); + return tl::make_unexpected("Invalid QMetaEnum"); } if (auto key = me.valueToKey((int)val); key) { return QJsonValue(key); } else { - return std::unexpected(QString("No key found for enum value %1").arg((int)val)); + return tl::make_unexpected(QString("No key found for enum value %1").arg((int)val)); } } template std::expected save(const T &val) { QJsonObject json; - std::optional> error; + std::optional> error; iterateMembers(val, [&json, &error] (const char *name, auto &field) { if (error) @@ -292,7 +292,7 @@ std::expected save(const T &val) { if (auto result = save>(field)) { json[name] = result.value(); } else { - error = std::unexpected(QString("%1: %2").arg(name).arg(result.error())); + error = tl::make_unexpected(QString("%1: %2").arg(name).arg(result.error())); } }); @@ -312,7 +312,7 @@ std::expected save(const T &val) { if (json) { arr.push_back(json.value()); } else { - return std::unexpected(QString("%1: %2").arg(i).arg(json.error())); + return tl::make_unexpected(QString("%1: %2").arg(i).arg(json.error())); } } @@ -325,7 +325,7 @@ std::expected saveProject(const LightProject &val) { if (auto project = save(val); project) { return QJsonDocument(project.value()); } else { - return std::unexpected(QString("Failed to save project: %1").arg(project.error())); + return tl::make_unexpected(QString("Failed to save project: %1").arg(project.error())); } } diff --git a/projectloader.h b/projectloader.h index fce8a37..a1c8d59 100644 --- a/projectloader.h +++ b/projectloader.h @@ -1,13 +1,23 @@ #ifndef PROJECTLOADER_H #define PROJECTLOADER_H -#include +#include "expected.hpp" #include #include #include "lightproject.h" + +namespace std { + template + using expected = tl::expected; +} + + + + + namespace ProjectLoader { std::expected loadProject(const QJsonDocument &json);