From db50c7a6a38eff251a14d57dcb9ae551ffe3a1d6 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Fri, 2 Dec 2022 16:01:28 +0100 Subject: loader, serialize: use more exception handling --- serialize/magnum-vector2i.hpp | 5 +++-- serialize/scenery.cpp | 11 ++++++----- 2 files changed, 9 insertions(+), 7 deletions(-) (limited to 'serialize') diff --git a/serialize/magnum-vector2i.hpp b/serialize/magnum-vector2i.hpp index a57429dc..f63bd130 100644 --- a/serialize/magnum-vector2i.hpp +++ b/serialize/magnum-vector2i.hpp @@ -1,5 +1,5 @@ #pragma once -#include "compat/assert.hpp" +#include "compat/exception.hpp" #include #include #include @@ -21,6 +21,7 @@ struct adl_serializer> final } static void from_json(const json& j, Magnum::Math::Vector2& val) { + using namespace floormat; std::string str = j; using type = std::conditional_t, std::intmax_t, std::uintmax_t>; constexpr auto format_string = std::is_signed_v ? "%jd x %jd%n" : "%ju x %ju%n"; @@ -28,7 +29,7 @@ struct adl_serializer> final int n = 0; int ret = std::sscanf(str.data(), format_string, &x, &y, &n); if (ret != 2 || (std::size_t)n != str.size() || x != (t)x || y != (t)y) - fm_abort("failed to parse Vector2 '%s'", str.data()); + fm_throw("failed to parse Vector2 '{}'"_cf, str); val = { (t)x, (t)y }; } }; diff --git a/serialize/scenery.cpp b/serialize/scenery.cpp index ccc80270..434ffefa 100644 --- a/serialize/scenery.cpp +++ b/serialize/scenery.cpp @@ -1,4 +1,5 @@ #include "scenery.hpp" +#include "compat/exception.hpp" #include "anim-atlas.hpp" #include "compat/assert.hpp" #include "loader/loader.hpp" @@ -42,7 +43,7 @@ auto foo_from_string(StringView str, const T(&map)[N], const char* desc) for (const auto& [value, str2] : map) if (str2 == str) return value; - fm_abort("wrong %s string '%s'", desc, str.data()); + fm_throw("wrong {} string '{}'"_cf, desc, str); } template @@ -51,7 +52,7 @@ StringView foo_to_string(auto type, const T(&map)[N], const char* desc) for (const auto& [type2, str] : map) if (type2 == type) return str; - fm_abort("wrong %s enum '%zu'", desc, (std::size_t)type); + fm_throw("wrong {} enum '{}'"_cf, desc, (std::size_t)type); } } // namespace @@ -101,7 +102,7 @@ void adl_serializer::from_json(const json& j, scenery_proto& val) }; StringView atlas_name = j["atlas-name"]; - fm_assert(!atlas_name.isEmpty()); + fm_soft_assert(!atlas_name.isEmpty()); val.atlas = loader.anim_atlas(atlas_name, loader_::SCENERY_PATH); auto& f = val.frame; f = {}; @@ -115,7 +116,7 @@ void adl_serializer::from_json(const json& j, scenery_proto& val) switch (type) { default: - fm_abort("unhandled scenery type '%u'", (unsigned)type); + fm_throw("unhandled scenery type '{}'"_cf, (unsigned)type); case scenery_type::generic: f = { scenery::generic, *val.atlas, r, frame, pass, active }; break; @@ -126,7 +127,7 @@ void adl_serializer::from_json(const json& j, scenery_proto& val) void adl_serializer::to_json(json& j, const serialized_scenery& val) { - fm_assert(val.proto.atlas); + fm_soft_assert(val.proto.atlas); j = val.proto; const auto name = !val.name.isEmpty() ? StringView{val.name} : val.proto.atlas->name(); j["name"] = name; -- cgit v1.2.3