summaryrefslogtreecommitdiffhomepage
path: root/serialize
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2023-11-05 03:19:54 +0100
committerStanislaw Halik <sthalik@misaki.pl>2023-11-05 03:19:54 +0100
commitfaaacb7924962acd6b865224e42d5f08625914cf (patch)
treefa926ea00a96aacc417ee16b244b317860143647 /serialize
parentd4780b4f3ed6c46f5f75e2e53869e33e1fa45856 (diff)
serialize: move vector2i impl from header
Diffstat (limited to 'serialize')
-rw-r--r--serialize/magnum-vector2i.cpp47
-rw-r--r--serialize/magnum-vector2i.hpp37
2 files changed, 58 insertions, 26 deletions
diff --git a/serialize/magnum-vector2i.cpp b/serialize/magnum-vector2i.cpp
index 215253f2..0b52d508 100644
--- a/serialize/magnum-vector2i.cpp
+++ b/serialize/magnum-vector2i.cpp
@@ -1,5 +1,6 @@
#include "magnum-vector2i.hpp"
#include "compat/exception.hpp"
+#include <nlohmann/json.hpp>
using namespace floormat;
@@ -7,3 +8,49 @@ void floormat::Serialize::throw_failed_to_parse_vector2(const std::string& str)
{
fm_throw("failed to parse Vector2 '{}'"_cf, str);
}
+
+void floormat::Serialize::throw_vector2_overflow(const std::string& str)
+{
+ fm_throw("numeric overflow in Vector2 '{}'"_cf, str);
+}
+
+using namespace nlohmann;
+
+template<typename T>
+requires std::is_integral_v<T>
+void nlohmann::adl_serializer<Magnum::Math::Vector2<T>>::to_json(json& j, const Magnum::Math::Vector2<T>& val)
+{
+ char buf[64];
+ using type = std::conditional_t<std::is_signed_v<T>, intmax_t, 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;
+}
+
+template<typename T>
+requires std::is_integral_v<T>
+void nlohmann::adl_serializer<Magnum::Math::Vector2<T>>::from_json(const json& j, Magnum::Math::Vector2<T>& val)
+{
+ using namespace floormat;
+ std::string str = j;
+ using type = std::conditional_t<std::is_signed_v<T>, intmax_t, 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.data(), format_string, &x, &y, &n);
+ if (ret != 2 || (size_t)n != str.size() || x != (T)x || y != (T)y)
+ floormat::Serialize::throw_failed_to_parse_vector2(str);
+ if constexpr(sizeof(T) < sizeof(type))
+ if (x != (type)(T)x || y != (type)(T)y)
+ floormat::Serialize::throw_vector2_overflow(str);
+ val = { (T)x, (T)y };
+}
+
+template struct nlohmann::adl_serializer<Magnum::Math::Vector2<uint8_t>>;
+template struct nlohmann::adl_serializer<Magnum::Math::Vector2<uint16_t>>;
+template struct nlohmann::adl_serializer<Magnum::Math::Vector2<uint32_t>>;
+template struct nlohmann::adl_serializer<Magnum::Math::Vector2<uint64_t>>;
+template struct nlohmann::adl_serializer<Magnum::Math::Vector2<int8_t>>;
+template struct nlohmann::adl_serializer<Magnum::Math::Vector2<int16_t>>;
+template struct nlohmann::adl_serializer<Magnum::Math::Vector2<int32_t>>;
+template struct nlohmann::adl_serializer<Magnum::Math::Vector2<int64_t>>;
diff --git a/serialize/magnum-vector2i.hpp b/serialize/magnum-vector2i.hpp
index 5f313920..ddb79e98 100644
--- a/serialize/magnum-vector2i.hpp
+++ b/serialize/magnum-vector2i.hpp
@@ -1,38 +1,23 @@
#pragma once
+#include "compat/assert.hpp"
#include <cstdio>
#include <string>
#include <Magnum/Math/Vector2.h>
-#include <nlohmann/json.hpp>
+#include <nlohmann/json_fwd.hpp>
-namespace floormat::Serialize { [[noreturn]] void throw_failed_to_parse_vector2(const std::string& str); }
+namespace floormat::Serialize {
+ [[noreturn]] void throw_failed_to_parse_vector2(const std::string& str);
+ [[noreturn]] void throw_vector2_overflow(const std::string& str);
+}
namespace nlohmann {
-template<typename t>
-requires std::is_integral_v<t>
-struct adl_serializer<Magnum::Math::Vector2<t>> final
+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>, intmax_t, 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)
- {
- using namespace floormat;
- std::string str = j;
- using type = std::conditional_t<std::is_signed_v<t>, intmax_t, 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.data(), format_string, &x, &y, &n);
- if (ret != 2 || (size_t)n != str.size() || x != (t)x || y != (t)y)
- floormat::Serialize::throw_failed_to_parse_vector2(str);
- val = { (t)x, (t)y };
- }
+ static void to_json(json& j, const Magnum::Math::Vector2<T>& val);
+ static void from_json(const json& j, Magnum::Math::Vector2<T>& val);
};
} // namespace nlohmann