summaryrefslogtreecommitdiffhomepage
path: root/serialize
diff options
context:
space:
mode:
Diffstat (limited to 'serialize')
-rw-r--r--serialize/anim.cpp1
-rw-r--r--serialize/anim.hpp6
-rw-r--r--serialize/magnum-vector2i.hpp6
-rw-r--r--serialize/string.cpp39
-rw-r--r--serialize/string.hpp31
-rw-r--r--serialize/tile-atlas.cpp5
6 files changed, 79 insertions, 9 deletions
diff --git a/serialize/anim.cpp b/serialize/anim.cpp
index 18bca85a..76d72485 100644
--- a/serialize/anim.cpp
+++ b/serialize/anim.cpp
@@ -1,5 +1,6 @@
#include "serialize/magnum-vector2i.hpp"
#include "serialize/anim.hpp"
+#include "serialize/string.hpp"
#include <tuple>
#include <filesystem>
diff --git a/serialize/anim.hpp b/serialize/anim.hpp
index 9e5bf081..536824d3 100644
--- a/serialize/anim.hpp
+++ b/serialize/anim.hpp
@@ -1,8 +1,6 @@
#pragma once
-#include <tuple>
#include <vector>
-#include <string>
#include <Magnum/Magnum.h>
#include <Magnum/Math/Vector2.h>
#include <nlohmann/json_fwd.hpp>
@@ -22,7 +20,7 @@ enum class anim_direction : unsigned char
struct anim_group final
{
- std::string name;
+ String name;
std::vector<anim_frame> frames;
Vector2i ground;
};
@@ -31,7 +29,7 @@ struct anim final
{
static constexpr int default_fps = 24;
- std::string object_name, anim_name;
+ String object_name, anim_name;
std::vector<anim_group> groups;
int nframes = 0;
int width = 0, height = 0;
diff --git a/serialize/magnum-vector2i.hpp b/serialize/magnum-vector2i.hpp
index 02e1fdd1..0b440c78 100644
--- a/serialize/magnum-vector2i.hpp
+++ b/serialize/magnum-vector2i.hpp
@@ -1,6 +1,6 @@
#include "compat/assert.hpp"
+#include "serialize/string.hpp"
#include <cstdio>
-#include <string>
#include <Magnum/Math/Vector2.h>
#include <nlohmann/json.hpp>
@@ -20,12 +20,12 @@ struct adl_serializer<Magnum::Math::Vector2<t>> final
}
static void from_json(const json& j, Magnum::Math::Vector2<t>& val)
{
- std::string str = j;
+ Corrade::Containers::StringView 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);
+ int ret = std::sscanf(str.data(), format_string, &x, &y, &n);
if (ret != 2 || (std::size_t)n != str.size() || x != (t)x || y != (t)y)
fm_abort("failed to parse Vector2");
val = { (t)x, (t)y };
diff --git a/serialize/string.cpp b/serialize/string.cpp
new file mode 100644
index 00000000..0c0fa74e
--- /dev/null
+++ b/serialize/string.cpp
@@ -0,0 +1,39 @@
+#include "string.hpp"
+#include <Corrade/Containers/String.h>
+#include <string_view>
+#include <nlohmann/json.hpp>
+
+using String = Corrade::Containers::String;
+using StringView = Corrade::Containers::StringView;
+
+namespace nlohmann {
+
+void adl_serializer<String>::to_json(json& j, const String& val)
+{
+ using nlohmann::to_json;
+ to_json(j, std::string_view { val.cbegin(), val.cend() });
+}
+
+void adl_serializer<String>::from_json(const json& j, String& val)
+{
+ using nlohmann::from_json;
+ std::string_view str;
+ from_json(j, str);
+ val = { str.cbegin(), str.size() };
+}
+
+void adl_serializer<StringView>::to_json(json& j, const StringView& val)
+{
+ using nlohmann::to_json;
+ to_json(j, std::string_view { val.cbegin(), val.cend() });
+}
+
+void adl_serializer<StringView>::from_json(const json& j, StringView& val)
+{
+ using nlohmann::from_json;
+ std::string_view str;
+ from_json(j, str);
+ val = { str.cbegin(), str.size() };
+}
+
+} // namespace nlohmann
diff --git a/serialize/string.hpp b/serialize/string.hpp
new file mode 100644
index 00000000..a3923527
--- /dev/null
+++ b/serialize/string.hpp
@@ -0,0 +1,31 @@
+#pragma once
+
+#include <nlohmann/json_fwd.hpp>
+
+namespace Corrade::Containers {
+
+template<typename T> class BasicStringView;
+class String;
+using StringView = BasicStringView<const char>;
+
+} // namespace Corrade::Containers
+
+namespace floormat {
+
+} // namespace floormat
+
+namespace nlohmann {
+
+template<>
+struct adl_serializer<Corrade::Containers::String> {
+ static void to_json(json& j, const Corrade::Containers::String& val);
+ static void from_json(const json& j, Corrade::Containers::String& val);
+};
+
+template<>
+struct adl_serializer<Corrade::Containers::StringView> {
+ static void to_json(json& j, const Corrade::Containers::StringView& val);
+ static void from_json(const json& j, Corrade::Containers::StringView& val);
+};
+
+} // namespace nlohmann
diff --git a/serialize/tile-atlas.cpp b/serialize/tile-atlas.cpp
index 874a7054..711d5fdd 100644
--- a/serialize/tile-atlas.cpp
+++ b/serialize/tile-atlas.cpp
@@ -1,6 +1,7 @@
#include "src/tile-atlas.hpp"
#include "serialize/tile-atlas.hpp"
#include "serialize/magnum-vector2i.hpp"
+#include "serialize/string.hpp"
#include "loader.hpp"
#include <tuple>
@@ -10,7 +11,7 @@ using namespace floormat;
namespace nlohmann {
-using proxy_atlas = std::tuple<std::string, Vector2ub>;
+using proxy_atlas = std::tuple<StringView, Vector2ub>;
void adl_serializer<std::shared_ptr<tile_atlas>>::to_json(json& j, const std::shared_ptr<const tile_atlas>& x)
{
@@ -18,7 +19,7 @@ void adl_serializer<std::shared_ptr<tile_atlas>>::to_json(json& j, const std::sh
if (!x)
j = nullptr;
else
- to_json(j, proxy_atlas{x->name(), x->num_tiles2()});
+ to_json(j, std::tuple<StringView, Vector2ub>{x->name(), x->num_tiles2()});
}
void adl_serializer<std::shared_ptr<tile_atlas>>::from_json(const json& j, std::shared_ptr<tile_atlas>& x)