summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-11-05 19:29:40 +0100
committerStanislaw Halik <sthalik@misaki.pl>2022-11-05 19:29:40 +0100
commit12eca108a07cde1606fca3e472aed58749407699 (patch)
tree44ac90dffca8cde0fc06800c5546cd12f5cd9249
parent28f5a46ba0058752d4e92cc66e0260f4ed2b8cf3 (diff)
replace std::string with Corrade's String
-rw-r--r--editor/editor.cpp5
-rw-r--r--editor/editor.hpp5
-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
-rw-r--r--src/precomp.hpp1
9 files changed, 85 insertions, 14 deletions
diff --git a/editor/editor.cpp b/editor/editor.cpp
index 3586a3bf..499dd441 100644
--- a/editor/editor.cpp
+++ b/editor/editor.cpp
@@ -11,6 +11,7 @@
#include <Corrade/Containers/StringView.h>
+#include <string_view>
#include <vector>
#include <filesystem>
@@ -25,7 +26,9 @@ void tile_editor::load_atlases()
{
static const std::filesystem::path image_path{IMAGE_PATH, std::filesystem::path::generic_format};
using atlas_array = std::vector<std::shared_ptr<tile_atlas>>;
- for (auto& atlas : json_helper::from_json<atlas_array>(image_path/(_name + ".json")))
+ const String filename = _name + ".json";
+ const auto filename_view = std::string_view{filename.cbegin(), filename.cend()};
+ for (auto& atlas : json_helper::from_json<atlas_array>(image_path/filename_view))
{
StringView name = atlas->name();
if (auto x = name.findLast('.'); x)
diff --git a/editor/editor.hpp b/editor/editor.hpp
index 869307c7..09277590 100644
--- a/editor/editor.hpp
+++ b/editor/editor.hpp
@@ -9,9 +9,8 @@
#include <optional>
#include <vector>
#include <map>
-#include <string>
#include <memory>
-#include <Corrade/Containers/StringView.h>
+#include <Corrade/Containers/String.h>
namespace floormat {
@@ -32,7 +31,7 @@ private:
sel_none, sel_tile, sel_perm,
};
- std::string _name;
+ String _name;
std::map<std::string, std::shared_ptr<tile_atlas>> _atlases;
tile_image_proto _selected_tile;
std::tuple<std::shared_ptr<tile_atlas>, std::vector<decltype(tile_image_proto::variant)>> _permutation;
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)
diff --git a/src/precomp.hpp b/src/precomp.hpp
index 8d3e474e..f35b2563 100644
--- a/src/precomp.hpp
+++ b/src/precomp.hpp
@@ -26,7 +26,6 @@
#include <optional>
#include <vector>
#include <unordered_map>
-#include <string> // TODO
#include <Corrade/Containers/Array.h>
#include <Corrade/Containers/ArrayViewStl.h> // TODO maybe remove stl