From 437dd5940bad6133561cb896cd558881fa5c8b44 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Thu, 6 Oct 2022 17:30:31 +0200 Subject: a --- serialize/anim.cpp | 8 +++--- serialize/anim.hpp | 4 +-- serialize/json-helper.hpp | 59 ++++++++++++++++++--------------------------- serialize/magnum-vector.hpp | 20 +++++++++++---- 4 files changed, 45 insertions(+), 46 deletions(-) (limited to 'serialize') diff --git a/serialize/anim.cpp b/serialize/anim.cpp index b7d3ce95..dffe2449 100644 --- a/serialize/anim.cpp +++ b/serialize/anim.cpp @@ -23,14 +23,14 @@ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(anim, name, nframes, actionframe, fps, groups # pragma clang diagnostic pop #endif -std::tuple anim::from_json(const std::filesystem::path& pathname) noexcept +std::tuple anim::from_json(const std::filesystem::path& pathname) { - return json_helper::from_json(pathname); + return json_helper::from_json(pathname); } -bool anim::to_json(const std::filesystem::path& pathname) const noexcept +bool anim::to_json(const std::filesystem::path& pathname) const { - return json_helper::to_json(*this, pathname); + return json_helper::to_json(*this, pathname); } } // namespace Magnum::Examples::Serialize diff --git a/serialize/anim.hpp b/serialize/anim.hpp index f03e3c8c..726efa44 100644 --- a/serialize/anim.hpp +++ b/serialize/anim.hpp @@ -32,8 +32,8 @@ struct anim_group final struct anim final { - static std::tuple from_json(const std::filesystem::path& pathname) noexcept; - [[nodiscard]] bool to_json(const std::filesystem::path& pathname) const noexcept; + static std::tuple from_json(const std::filesystem::path& pathname); + [[nodiscard]] bool to_json(const std::filesystem::path& pathname) const; static constexpr int default_fps = 24; std::string name; diff --git a/serialize/json-helper.hpp b/serialize/json-helper.hpp index af6fd211..4a9ee0ac 100644 --- a/serialize/json-helper.hpp +++ b/serialize/json-helper.hpp @@ -6,57 +6,46 @@ #include #include -template struct json_helper final { - [[nodiscard]] static std::tuple from_json(const std::filesystem::path& pathname) noexcept; - [[nodiscard]] static bool to_json(const t& self, const std::filesystem::path& pathname) noexcept; + template + [[nodiscard]] + static std::tuple from_json(const std::filesystem::path& pathname); + + template + [[nodiscard]] + static bool to_json(const t& self, const std::filesystem::path& pathname); }; template -std::tuple json_helper::from_json(const std::filesystem::path& pathname) noexcept { +std::tuple json_helper::from_json(const std::filesystem::path& pathname) { using namespace nlohmann; using Corrade::Utility::Error; std::ifstream s; s.exceptions(s.exceptions() | std::ios::failbit | std::ios::badbit); - try { - s.open(pathname, std::ios_base::in); - } catch (const std::ios::failure& e) { - Error{Error::Flag::NoSpace} << "failed to open '" << pathname << "': " << e.what(); - return { {}, false }; - } + s.open(pathname, std::ios_base::in); t ret; - try { - json j; - s >> j; - using nlohmann::from_json; - from_json(j, ret); - } catch (const std::exception& e) { - Error{Error::Flag::NoSpace} << "failed to parse '" << pathname << "': " << e.what(); - return { {}, false }; - } + json j; + s >> j; + using nlohmann::from_json; + from_json(j, ret); return { std::move(ret), true }; } template -bool json_helper::to_json(const t& self, const std::filesystem::path& pathname) noexcept { +bool json_helper::to_json(const t& self, const std::filesystem::path& pathname) { using Corrade::Utility::Error; - try { - nlohmann::json j(self); + nlohmann::json j = self; - std::ofstream s; - s.exceptions(s.exceptions() | std::ios::failbit | std::ios::badbit); - try { - s.open(pathname, std::ios_base::out | std::ios_base::trunc); - } catch (const std::ios::failure& e) { - Error{Error::Flag::NoSpace} << "failed to open '" << pathname << "' for writing: " << e.what(); - return false; - } - s << j.dump(4); - s.flush(); - } catch (const std::exception& e) { - Error{Error::Flag::NoSpace} << "failed writing to '" << pathname << "': " << e.what(); + std::ofstream s; + s.exceptions(s.exceptions() | std::ios::failbit | std::ios::badbit); + try { + s.open(pathname, std::ios_base::out | std::ios_base::trunc); + } catch (const std::ios::failure& e) { + Error{Error::Flag::NoSpace} << "failed to open '" << pathname << "' for writing: " << e.what(); return false; } - + s << j.dump(4); + s.flush(); return true; } + diff --git a/serialize/magnum-vector.hpp b/serialize/magnum-vector.hpp index 3e95c8eb..eed37708 100644 --- a/serialize/magnum-vector.hpp +++ b/serialize/magnum-vector.hpp @@ -1,25 +1,35 @@ #pragma once #include #include +#include +#include +#include +#include #include namespace nlohmann { template -struct adl_serializer> final { - static void to_json(json& j, const Magnum::Math::Vector2& val) +struct adl_serializer> { + static void to_json(json& j, const Magnum::Math::Vector& val) { std::array array{}; - for (std::size_t i; i < std::size(val); i++) + for (std::size_t i = 0; i < N; i++) array[i] = val[i]; j = array; } - static void from_json(const json& j, Magnum::Math::Vector2& val) + static void from_json(const json& j, Magnum::Math::Vector& val) { std::array array = j; - for (std::size_t i; i < std::size(val); i++) + for (std::size_t i = 0; i < N; i++) val[i] = array[i]; } }; +template struct adl_serializer> : adl_serializer> {}; +template struct adl_serializer> : adl_serializer> {}; +template struct adl_serializer> : adl_serializer> {}; +template struct adl_serializer> : adl_serializer> {}; +template struct adl_serializer> : adl_serializer> {}; + } // namespace nlohmann -- cgit v1.2.3