From 12eca108a07cde1606fca3e472aed58749407699 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sat, 5 Nov 2022 19:29:40 +0100 Subject: replace std::string with Corrade's String --- editor/editor.cpp | 5 ++++- editor/editor.hpp | 5 ++--- serialize/anim.cpp | 1 + serialize/anim.hpp | 6 ++---- serialize/magnum-vector2i.hpp | 6 +++--- serialize/string.cpp | 39 +++++++++++++++++++++++++++++++++++++++ serialize/string.hpp | 31 +++++++++++++++++++++++++++++++ serialize/tile-atlas.cpp | 5 +++-- src/precomp.hpp | 1 - 9 files changed, 85 insertions(+), 14 deletions(-) create mode 100644 serialize/string.cpp create mode 100644 serialize/string.hpp 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 +#include #include #include @@ -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>; - for (auto& atlas : json_helper::from_json(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(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 #include #include -#include #include -#include +#include namespace floormat { @@ -32,7 +31,7 @@ private: sel_none, sel_tile, sel_perm, }; - std::string _name; + String _name; std::map> _atlases; tile_image_proto _selected_tile; std::tuple, std::vector> _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 #include 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 #include -#include #include #include #include @@ -22,7 +20,7 @@ enum class anim_direction : unsigned char struct anim_group final { - std::string name; + String name; std::vector 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 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 -#include #include #include @@ -20,12 +20,12 @@ struct adl_serializer> final } static void from_json(const json& j, Magnum::Math::Vector2& val) { - std::string str = j; + Corrade::Containers::StringView str = j; using type = std::conditional_t, std::intmax_t, std::uintmax_t>; constexpr auto format_string = std::is_signed_v ? "%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 +#include +#include + +using String = Corrade::Containers::String; +using StringView = Corrade::Containers::StringView; + +namespace nlohmann { + +void adl_serializer::to_json(json& j, const String& val) +{ + using nlohmann::to_json; + to_json(j, std::string_view { val.cbegin(), val.cend() }); +} + +void adl_serializer::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::to_json(json& j, const StringView& val) +{ + using nlohmann::to_json; + to_json(j, std::string_view { val.cbegin(), val.cend() }); +} + +void adl_serializer::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 + +namespace Corrade::Containers { + +template class BasicStringView; +class String; +using StringView = BasicStringView; + +} // namespace Corrade::Containers + +namespace floormat { + +} // namespace floormat + +namespace nlohmann { + +template<> +struct adl_serializer { + 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 { + 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 @@ -10,7 +11,7 @@ using namespace floormat; namespace nlohmann { -using proxy_atlas = std::tuple; +using proxy_atlas = std::tuple; void adl_serializer>::to_json(json& j, const std::shared_ptr& x) { @@ -18,7 +19,7 @@ void adl_serializer>::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{x->name(), x->num_tiles2()}); } void adl_serializer>::from_json(const json& j, std::shared_ptr& 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 #include #include -#include // TODO #include #include // TODO maybe remove stl -- cgit v1.2.3