diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2022-10-06 15:54:10 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2022-10-06 15:54:10 +0200 |
commit | ded69f52906990cf975a62c0efbaca4b6cfa5e88 (patch) | |
tree | bca2790e5d9d45ccef12e9da6cf4b125912f12db /serialize/magnum-vector.hpp | |
parent | 896e072d8bd94020c883e994451b86675ecdced7 (diff) |
a
Diffstat (limited to 'serialize/magnum-vector.hpp')
-rw-r--r-- | serialize/magnum-vector.hpp | 51 |
1 files changed, 16 insertions, 35 deletions
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 |