From ded69f52906990cf975a62c0efbaca4b6cfa5e88 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Thu, 6 Oct 2022 15:54:10 +0200 Subject: a --- serialize/anim.cpp | 4 +-- serialize/helper.hpp | 62 ------------------------------------------- serialize/json-helper.hpp | 62 +++++++++++++++++++++++++++++++++++++++++++ serialize/magnum-vector.hpp | 51 +++++++++++------------------------ serialize/magnum-vector2i.hpp | 42 +++++++++++++++++++++++++++++ serialize/tile-atlas.cpp | 2 +- serialize/tile-atlas.hpp | 1 - 7 files changed, 123 insertions(+), 101 deletions(-) delete mode 100644 serialize/helper.hpp create mode 100644 serialize/json-helper.hpp create mode 100644 serialize/magnum-vector2i.hpp (limited to 'serialize') diff --git a/serialize/anim.cpp b/serialize/anim.cpp index 78c871a8..b7d3ce95 100644 --- a/serialize/anim.cpp +++ b/serialize/anim.cpp @@ -1,5 +1,5 @@ -#include "serialize/magnum-vector.hpp" -#include "serialize/helper.hpp" +#include "serialize/magnum-vector2i.hpp" +#include "serialize/json-helper.hpp" #include "serialize/anim.hpp" #include diff --git a/serialize/helper.hpp b/serialize/helper.hpp deleted file mode 100644 index f16ed60c..00000000 --- a/serialize/helper.hpp +++ /dev/null @@ -1,62 +0,0 @@ -#pragma once -#include -#include -#include -#include -#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 -std::tuple json_helper::from_json(const std::filesystem::path& pathname) noexcept { - 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 }; - } - 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 }; - } - return { std::move(ret), true }; -} - -template -bool json_helper::to_json(const t& self, const std::filesystem::path& pathname) noexcept { - using Corrade::Utility::Error; - try { - 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(); - return false; - } - - return true; -} diff --git a/serialize/json-helper.hpp b/serialize/json-helper.hpp new file mode 100644 index 00000000..af6fd211 --- /dev/null +++ b/serialize/json-helper.hpp @@ -0,0 +1,62 @@ +#pragma once +#include +#include +#include +#include +#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 +std::tuple json_helper::from_json(const std::filesystem::path& pathname) noexcept { + 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 }; + } + 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 }; + } + return { std::move(ret), true }; +} + +template +bool json_helper::to_json(const t& self, const std::filesystem::path& pathname) noexcept { + using Corrade::Utility::Error; + try { + 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(); + return false; + } + + return true; +} diff --git a/serialize/magnum-vector.hpp b/serialize/magnum-vector.hpp index c4a2c36c..3e95c8eb 100644 --- a/serialize/magnum-vector.hpp +++ b/serialize/magnum-vector.hpp @@ -1,44 +1,25 @@ -#include -#include -#include +#pragma once #include -#include +#include #include namespace nlohmann { -template -requires std::is_integral_v -struct adl_serializer> final { - static void to_json(json& j, const Magnum::Math::Vector2& x); - static void from_json(const json& j, Magnum::Math::Vector2& x); -}; - -template -requires std::is_integral_v -void adl_serializer>::to_json(json& j, const Magnum::Math::Vector2& val) -{ - char buf[64]; - snprintf(buf, sizeof(buf), "%d x %d", val[0], val[1]); - j = buf; -} - -template -requires std::is_integral_v -void adl_serializer>::from_json(const json& j, Magnum::Math::Vector2& val) -{ - std::string str = j; - long long x = 0, y = 0; int n = 0; - int ret = std::sscanf(str.c_str(), "%lld x %lld%n", &x, &y, &n); - if (ret != 2 || (std::size_t)n != str.size()) +template +struct adl_serializer> final { + static void to_json(json& j, const Magnum::Math::Vector2& val) { - std::string msg; msg.reserve(64 + str.size()); - msg += "failed to parse string '"; - msg += str; - msg += "' as Magnum::Vector2i"; - throw std::invalid_argument(msg); + std::array array{}; + for (std::size_t i; i < std::size(val); i++) + array[i] = val[i]; + j = array; } - val = { (t)x, (t)y }; -} + static void from_json(const json& j, Magnum::Math::Vector2& val) + { + std::array array = j; + for (std::size_t i; i < std::size(val); i++) + val[i] = array[i]; + } +}; } // namespace nlohmann diff --git a/serialize/magnum-vector2i.hpp b/serialize/magnum-vector2i.hpp new file mode 100644 index 00000000..eb445e21 --- /dev/null +++ b/serialize/magnum-vector2i.hpp @@ -0,0 +1,42 @@ +#include +#include +#include +#include +#include +#include +#include + +namespace nlohmann { + +template +requires std::is_integral_v +struct adl_serializer> final { + static void to_json(json& j, const Magnum::Math::Vector2& val) + { + char buf[64]; + using type = std::conditional_t, std::intmax_t, std::uintmax_t>; + constexpr auto format_string = std::is_signed_v ? "%jd x %jd" : "%ju x %ju"; + snprintf(buf, sizeof(buf), format_string, (type)val[0], (type)val[1]); + j = buf; + } + static void from_json(const json& j, Magnum::Math::Vector2& val) + { + 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"; + type x = 0, y = 0; + int n = 0; + int ret = std::sscanf(str.c_str(), format_string, &x, &y, &n); + if (ret != 2 || (std::size_t)n != str.size() || x != (type)x || y != (type)y) + { + std::string msg; msg.reserve(128); + msg += "failed to parse string '"; + msg += str; + msg += "' as Magnum::Vector2i"; + throw std::invalid_argument(msg); + } + val = { (t)x, (t)y }; + } +}; + +} // namespace nlohmann diff --git a/serialize/tile-atlas.cpp b/serialize/tile-atlas.cpp index 1aa0c14d..c7e52057 100644 --- a/serialize/tile-atlas.cpp +++ b/serialize/tile-atlas.cpp @@ -1,6 +1,6 @@ #include "src/tile-atlas.hpp" #include "serialize/tile-atlas.hpp" -#include "serialize/magnum-vector.hpp" +#include "serialize/magnum-vector2i.hpp" #include "loader.hpp" #include diff --git a/serialize/tile-atlas.hpp b/serialize/tile-atlas.hpp index c49b8a72..8dfa8c86 100644 --- a/serialize/tile-atlas.hpp +++ b/serialize/tile-atlas.hpp @@ -1,5 +1,4 @@ #pragma once -#include "loader.hpp" #include #include -- cgit v1.2.3