summaryrefslogtreecommitdiffhomepage
path: root/serialize/magnum-vector2i.cpp
blob: 0b52d5084b0f26b94c3add53365dd09a5d34e25f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "magnum-vector2i.hpp"
#include "compat/exception.hpp"
#include <nlohmann/json.hpp>

using namespace floormat;

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>>;