summaryrefslogtreecommitdiffhomepage
path: root/serialize
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-10-06 15:54:10 +0200
committerStanislaw Halik <sthalik@misaki.pl>2022-10-06 15:54:10 +0200
commitded69f52906990cf975a62c0efbaca4b6cfa5e88 (patch)
treebca2790e5d9d45ccef12e9da6cf4b125912f12db /serialize
parent896e072d8bd94020c883e994451b86675ecdced7 (diff)
a
Diffstat (limited to 'serialize')
-rw-r--r--serialize/anim.cpp4
-rw-r--r--serialize/json-helper.hpp (renamed from serialize/helper.hpp)2
-rw-r--r--serialize/magnum-vector.hpp51
-rw-r--r--serialize/magnum-vector2i.hpp42
-rw-r--r--serialize/tile-atlas.cpp2
-rw-r--r--serialize/tile-atlas.hpp1
6 files changed, 62 insertions, 40 deletions
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 <tuple>
diff --git a/serialize/helper.hpp b/serialize/json-helper.hpp
index f16ed60c..af6fd211 100644
--- a/serialize/helper.hpp
+++ b/serialize/json-helper.hpp
@@ -41,7 +41,7 @@ template<typename t>
bool json_helper<t>::to_json(const t& self, const std::filesystem::path& pathname) noexcept {
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);
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 <cstdio>
-#include <string>
-#include <exception>
+#pragma once
#include <Magnum/Magnum.h>
-#include <Magnum/Math/Vector2.h>
+#include <Magnum/Math/Vector.h>
#include <nlohmann/json.hpp>
namespace nlohmann {
-template<typename t>
-requires std::is_integral_v<t>
-struct adl_serializer<Magnum::Math::Vector2<t>> final {
- static void to_json(json& j, const Magnum::Math::Vector2<t>& x);
- static void from_json(const json& j, Magnum::Math::Vector2<t>& x);
-};
-
-template<typename t>
-requires std::is_integral_v<t>
-void adl_serializer<Magnum::Math::Vector2<t>>::to_json(json& j, const Magnum::Math::Vector2<t>& val)
-{
- char buf[64];
- snprintf(buf, sizeof(buf), "%d x %d", val[0], val[1]);
- j = buf;
-}
-
-template<typename t>
-requires std::is_integral_v<t>
-void adl_serializer<Magnum::Math::Vector2<t>>::from_json(const json& j, Magnum::Math::Vector2<t>& 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<std::size_t N, typename T>
+struct adl_serializer<Magnum::Math::Vector<N, T>> final {
+ static void to_json(json& j, const Magnum::Math::Vector2<T>& 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<T, N> 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<T>& val)
+ {
+ std::array<T, N> 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 <cstdio>
+#include <string>
+#include <limits>
+#include <exception>
+#include <Magnum/Magnum.h>
+#include <Magnum/Math/Vector2.h>
+#include <nlohmann/json.hpp>
+
+namespace nlohmann {
+
+template<typename t>
+requires std::is_integral_v<t>
+struct adl_serializer<Magnum::Math::Vector2<t>> final {
+ static void to_json(json& j, const Magnum::Math::Vector2<t>& val)
+ {
+ char buf[64];
+ using type = std::conditional_t<std::is_signed_v<t>, std::intmax_t, std::uintmax_t>;
+ constexpr auto format_string = std::is_signed_v<t> ? "%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<t>& val)
+ {
+ std::string str = j;
+ using type = std::conditional_t<std::is_signed_v<t>, std::intmax_t, std::uintmax_t>;
+ constexpr auto format_string = std::is_signed_v<t> ? "%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 <nlohmann/json.hpp>
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 <memory>
#include <nlohmann/json_fwd.hpp>