summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-11-10 21:27:08 +0100
committerStanislaw Halik <sthalik@misaki.pl>2022-11-10 22:17:43 +0100
commitee1a527191db646850ee919b8fe3a6f8cb6cd693 (patch)
treea1ff23b8c0f93d689d51d3837caefaa72b9480cd
parent0e23ba9e5a565e34fee0f024e29ce162f420ec22 (diff)
get rid of std::string
-rw-r--r--anim-crop-tool/main.cpp12
-rw-r--r--editor/tile-editor.cpp17
-rw-r--r--editor/tile-editor.hpp5
-rw-r--r--loader/loader-impl.cpp7
-rw-r--r--serialize/anim.cpp1
-rw-r--r--serialize/anim.hpp6
-rw-r--r--serialize/corrade-string.cpp31
-rw-r--r--serialize/corrade-string.hpp18
-rw-r--r--serialize/tile-atlas.cpp5
-rw-r--r--src/anim-atlas.hpp4
-rw-r--r--src/precomp.hpp3
-rw-r--r--src/tile-atlas.cpp1
-rw-r--r--src/tile-atlas.hpp7
13 files changed, 78 insertions, 39 deletions
diff --git a/anim-crop-tool/main.cpp b/anim-crop-tool/main.cpp
index ea0614ca..ae3c2af3 100644
--- a/anim-crop-tool/main.cpp
+++ b/anim-crop-tool/main.cpp
@@ -211,8 +211,8 @@ static std::tuple<options, Arguments, bool> parse_cmdline(int argc, const char*
opts.width = w;
if (auto h = args.value<unsigned>("height"); h != 0)
opts.height = h;
- opts.output_dir = args.value<std::string>("output");
- opts.input_file = args.value<std::string>("input");
+ opts.output_dir = args.value<StringView>("output");
+ opts.input_file = args.value<StringView>("input");
opts.input_dir = Path::split(opts.input_file).first();
if (opts.output_dir.isEmpty())
@@ -227,17 +227,17 @@ static std::tuple<options, Arguments, bool> parse_cmdline(int argc, const char*
return EX_USAGE;
}
-[[nodiscard]] static bool check_atlas_name(const std::string& str) noexcept
+[[nodiscard]] static bool check_atlas_name(StringView str) noexcept
{
constexpr auto npos = std::string::npos;
- if (str.empty())
+ if (str.isEmpty())
return false;
if (str[0] == '.' || str[0] == '\\' || str[0] == '/')
return false;
- if (str.find('"') != npos || str.find('\'') != npos)
+ if (str.find('"') || str.find('\''))
return false;
- if (str.find("/.") != npos || str.find("\\.") != npos)
+ if (str.find("/.") || str.find("\\."))
return false; // NOLINT(readability-simplify-boolean-expr)
return true;
diff --git a/editor/tile-editor.cpp b/editor/tile-editor.cpp
index 2a0e9abc..f396d64a 100644
--- a/editor/tile-editor.cpp
+++ b/editor/tile-editor.cpp
@@ -6,7 +6,6 @@
#include "random.hpp"
#include "serialize/json-helper.hpp"
#include "serialize/tile-atlas.hpp"
-#include <Corrade/Containers/StringStl.h>
#include <Corrade/Utility/Path.h>
namespace floormat {
@@ -27,26 +26,22 @@ void tile_editor::load_atlases()
if (auto x = name.findLast('.'); x)
name = name.prefix(x.data());
auto& [_, vec] = _permutation;
- vec.reserve((std::size_t)atlas->num_tiles());
- _atlases[name] = std::move(atlas);
+ vec.reserve(atlas->num_tiles());
+ _atlases[name] = atlas;
}
}
std::shared_ptr<tile_atlas> tile_editor::maybe_atlas(StringView str)
{
- auto it = std::find_if(_atlases.begin(), _atlases.end(), [&](const auto& tuple) -> bool {
- const auto& [x, _] = tuple;
- return StringView{x} == str;
- });
- if (it == _atlases.end())
- return nullptr;
- else
+ if (auto it = _atlases.find(str); it != _atlases.end())
return it->second;
+ else
+ return nullptr;
}
std::shared_ptr<tile_atlas> tile_editor::atlas(StringView str)
{
- if (auto ptr = maybe_atlas(str); ptr)
+ if (auto ptr = maybe_atlas(str))
return ptr;
else
fm_abort("no such atlas: %s", str.cbegin());
diff --git a/editor/tile-editor.hpp b/editor/tile-editor.hpp
index 53b12e55..9f4bb2db 100644
--- a/editor/tile-editor.hpp
+++ b/editor/tile-editor.hpp
@@ -4,10 +4,9 @@
#include "src/tile-image.hpp"
#include "global-coords.hpp"
#include <vector>
-#include <string>
#include <map>
#include <memory>
-#include <Corrade/Containers/StringView.h>
+#include <Corrade/Containers/String.h>
namespace floormat {
@@ -25,7 +24,7 @@ private:
std::vector<decltype(tile_image_proto::variant)> variant;
};
- std::string _name;
+ String _name;
std::map<StringView, std::shared_ptr<tile_atlas>> _atlases;
tile_image_proto _selected_tile;
tuple _permutation;
diff --git a/loader/loader-impl.cpp b/loader/loader-impl.cpp
index 03144f30..5d137eca 100644
--- a/loader/loader-impl.cpp
+++ b/loader/loader-impl.cpp
@@ -12,7 +12,6 @@
#include <Corrade/Containers/ArrayViewStl.h>
#include <Corrade/Containers/StringView.h>
#include <Corrade/Containers/StringStlHash.h>
-#include <Corrade/Containers/StringStlView.h>
#include <Corrade/PluginManager/PluginManager.h>
#include <Corrade/Utility/Resource.h>
#include <Corrade/Utility/Path.h>
@@ -41,7 +40,7 @@ struct loader_impl final : loader_
Containers::Pointer<Trade::AbstractImporter> tga_importer =
importer_plugins.loadAndInstantiate("TgaImporter");
- std::unordered_map<std::string, std::shared_ptr<struct tile_atlas>> tile_atlas_map;
+ std::unordered_map<StringView, std::shared_ptr<struct tile_atlas>> tile_atlas_map;
std::unordered_map<StringView, std::shared_ptr<struct anim_atlas>> anim_atlas_map;
std::vector<String> anim_atlases;
@@ -79,7 +78,7 @@ std::shared_ptr<tile_atlas> loader_impl::tile_atlas(StringView name, Vector2ub s
return it->second;
auto image = texture(FM_IMAGE_PATH, name);
auto atlas = std::make_shared<struct tile_atlas>(name, image, size);
- tile_atlas_map[name] = atlas;
+ tile_atlas_map[atlas->name()] = atlas;
return atlas;
}
@@ -140,7 +139,7 @@ std::shared_ptr<anim_atlas> loader_impl::anim_atlas(StringView name)
auto anim_info = json_helper::from_json<Serialize::anim>(Path::splitExtension(path).first() + ".json");
auto tex = texture("", path);
- fm_assert(!anim_info.anim_name.empty() && !anim_info.object_name.empty());
+ fm_assert(!anim_info.anim_name.isEmpty() && !anim_info.object_name.isEmpty());
fm_assert(anim_info.pixel_size.product() > 0);
fm_assert(!anim_info.groups.empty());
fm_assert(anim_info.nframes > 0);
diff --git a/serialize/anim.cpp b/serialize/anim.cpp
index 79608de0..b6882f1d 100644
--- a/serialize/anim.cpp
+++ b/serialize/anim.cpp
@@ -1,4 +1,5 @@
#include "serialize/magnum-vector2i.hpp"
+#include "serialize/corrade-string.hpp"
#include "serialize/anim.hpp"
namespace floormat::Serialize {
diff --git a/serialize/anim.hpp b/serialize/anim.hpp
index fa6b6d22..9d6db66c 100644
--- a/serialize/anim.hpp
+++ b/serialize/anim.hpp
@@ -1,7 +1,7 @@
#pragma once
#include <vector>
-#include <string>
+#include <Corrade/Containers/String.h>
#include <Magnum/Magnum.h>
#include <Magnum/Math/Vector2.h>
#include <nlohmann/json_fwd.hpp>
@@ -22,7 +22,7 @@ enum class anim_direction : unsigned char
struct anim_group final
{
- std::string name;
+ String name;
std::vector<anim_frame> frames;
Vector2ui ground;
};
@@ -31,7 +31,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;
Vector2ui pixel_size;
std::size_t nframes = 0, width = 0, height = 0, fps = default_fps, actionframe = 0;
diff --git a/serialize/corrade-string.cpp b/serialize/corrade-string.cpp
new file mode 100644
index 00000000..16ae23e1
--- /dev/null
+++ b/serialize/corrade-string.cpp
@@ -0,0 +1,31 @@
+#include "corrade-string.hpp"
+#include <nlohmann/json.hpp>
+#include <string_view>
+
+using namespace floormat;
+
+namespace nlohmann {
+
+void adl_serializer<String>::to_json(json& j, const String& val)
+{
+ std::string_view s{val.data(), val.size()};
+ using nlohmann::to_json;
+ to_json(j, s);
+}
+
+void adl_serializer<String>::from_json(const json& j, String& val)
+{
+ using nlohmann::from_json;
+ std::string_view s;
+ from_json(j, s);
+ val = String{s.data(), s.size()};
+}
+
+void adl_serializer<StringView>::to_json(json& j, StringView val)
+{
+ std::string_view s{val.data(), val.size()};
+ using nlohmann::to_json;
+ to_json(j, s);
+}
+
+} // namespace nlohmann
diff --git a/serialize/corrade-string.hpp b/serialize/corrade-string.hpp
new file mode 100644
index 00000000..81922367
--- /dev/null
+++ b/serialize/corrade-string.hpp
@@ -0,0 +1,18 @@
+#pragma once
+#include <Corrade/Containers/String.h>
+#include <nlohmann/json_fwd.hpp>
+
+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, Corrade::Containers::StringView val);
+};
+
+} // namespace nlohmann
diff --git a/serialize/tile-atlas.cpp b/serialize/tile-atlas.cpp
index 8caaedd3..451fb48c 100644
--- a/serialize/tile-atlas.cpp
+++ b/serialize/tile-atlas.cpp
@@ -1,5 +1,6 @@
#include "src/tile-atlas.hpp"
#include "serialize/tile-atlas.hpp"
+#include "serialize/corrade-string.hpp"
#include "serialize/magnum-vector2i.hpp"
#include "loader.hpp"
#include <tuple>
@@ -10,8 +11,6 @@ using namespace floormat;
namespace nlohmann {
-using proxy_atlas = std::tuple<std::string, Vector2ub>;
-
void adl_serializer<std::shared_ptr<tile_atlas>>::to_json(json& j, const std::shared_ptr<const tile_atlas>& x)
{
using nlohmann::to_json;
@@ -27,7 +26,7 @@ void adl_serializer<std::shared_ptr<tile_atlas>>::from_json(const json& j, std::
x = nullptr;
else
{
- proxy_atlas proxy = j;
+ std::tuple<String, Vector2ub> proxy = j;
const auto& [name, num_tiles] = proxy;
x = loader.tile_atlas(name, num_tiles);
}
diff --git a/src/anim-atlas.hpp b/src/anim-atlas.hpp
index 40cc15fe..d05bbe77 100644
--- a/src/anim-atlas.hpp
+++ b/src/anim-atlas.hpp
@@ -3,7 +3,7 @@
#include "scenery.hpp"
#include "serialize/anim.hpp"
#include <array>
-#include <string>
+#include <Corrade/Containers/String.h>
#include <Magnum/Math/Vector2.h>
#include <Magnum/ImageView.h>
#include <Magnum/GL/Texture.h>
@@ -38,7 +38,7 @@ struct anim_atlas final
private:
GL::Texture2D _tex;
- std::string _name;
+ String _name;
anim_info _info;
std::array<std::uint8_t, (std::size_t)rotation::COUNT> _group_indices = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
diff --git a/src/precomp.hpp b/src/precomp.hpp
index 5d46092e..5ec264a4 100644
--- a/src/precomp.hpp
+++ b/src/precomp.hpp
@@ -29,14 +29,13 @@
#include <optional>
#include <vector>
#include <unordered_map>
-#include <string>
#include <Corrade/Containers/Array.h>
#include <Corrade/Containers/ArrayView.h>
#include <Corrade/Containers/StringView.h>
#include <Corrade/Containers/Pointer.h>
#include <Corrade/Containers/ArrayViewStl.h> // TODO maybe remove stl
-#include <Corrade/Containers/StringStl.h> // TODO remove stl
+#include <Corrade/Containers/StringStlHash.h>
#include <Corrade/Utility/DebugStl.h>
#include <Magnum/Magnum.h>
diff --git a/src/tile-atlas.cpp b/src/tile-atlas.cpp
index 15bb9439..44efd45b 100644
--- a/src/tile-atlas.cpp
+++ b/src/tile-atlas.cpp
@@ -3,7 +3,6 @@
#include "compat/assert.hpp"
#include "tile-image.hpp"
#include <limits>
-#include <Corrade/Containers/StringStl.h>
#include <Magnum/Math/Color.h>
#include <Magnum/ImageView.h>
#include <Magnum/GL/TextureFormat.h>
diff --git a/src/tile-atlas.hpp b/src/tile-atlas.hpp
index 5856f3bb..55546017 100644
--- a/src/tile-atlas.hpp
+++ b/src/tile-atlas.hpp
@@ -1,10 +1,9 @@
#pragma once
+#include <array>
+#include <memory>
#include <Corrade/Containers/String.h>
#include <Magnum/Magnum.h>
#include <Magnum/GL/Texture.h>
-#include <array>
-#include <string>
-#include <memory>
namespace floormat {
@@ -32,7 +31,7 @@ private:
std::unique_ptr<const texcoords[]> texcoords_;
GL::Texture2D tex_;
- std::string name_;
+ String name_;
Vector2ui size_;
Vector2ub dims_;
};