diff options
Diffstat (limited to 'serialize')
-rw-r--r-- | serialize/ground-atlas.cpp | 7 | ||||
-rw-r--r-- | serialize/ground-atlas.hpp | 7 | ||||
-rw-r--r-- | serialize/old-savegame.cpp | 5 | ||||
-rw-r--r-- | serialize/savegame.cpp | 22 | ||||
-rw-r--r-- | serialize/scenery.cpp | 1 | ||||
-rw-r--r-- | serialize/tile.cpp | 25 | ||||
-rw-r--r-- | serialize/tile.hpp | 6 | ||||
-rw-r--r-- | serialize/wall-atlas.hpp | 1 |
8 files changed, 48 insertions, 26 deletions
diff --git a/serialize/ground-atlas.cpp b/serialize/ground-atlas.cpp index d563bb05..49bcf7b9 100644 --- a/serialize/ground-atlas.cpp +++ b/serialize/ground-atlas.cpp @@ -1,5 +1,6 @@ #include "ground-atlas.hpp" #include "compat/exception.hpp" +#include "compat/borrowed-ptr.inl" #include "src/ground-atlas.hpp" #include "src/ground-def.hpp" #include "loader/loader.hpp" @@ -49,15 +50,15 @@ void adl_serializer<ground_cell>::from_json(const json& j, ground_cell& val) } -void adl_serializer<std::shared_ptr<ground_atlas>>::to_json(json& j, const std::shared_ptr<const ground_atlas>& x) +void adl_serializer<bptr<ground_atlas>>::to_json(json& j, const bptr<const ground_atlas>& x) { j = std::tuple<StringView, Vector2ub, pass_mode>{x->name(), x->num_tiles2(), x->pass_mode()}; } -void adl_serializer<std::shared_ptr<ground_atlas>>::from_json(const json& j, std::shared_ptr<ground_atlas>& val) +void adl_serializer<bptr<ground_atlas>>::from_json(const json& j, bptr<ground_atlas>& val) { ground_def def = j; - val = std::make_shared<ground_atlas>(move(def), loader.texture(loader.GROUND_TILESET_PATH, def.name)); + val = bptr<ground_atlas>(InPlace, move(def), loader.texture(loader.GROUND_TILESET_PATH, def.name)); } } // namespace nlohmann diff --git a/serialize/ground-atlas.hpp b/serialize/ground-atlas.hpp index 20b50537..e74792a5 100644 --- a/serialize/ground-atlas.hpp +++ b/serialize/ground-atlas.hpp @@ -1,4 +1,5 @@ #pragma once +#include "compat/borrowed-ptr.hpp" #include <nlohmann/json_fwd.hpp> namespace floormat { @@ -18,9 +19,9 @@ struct adl_serializer<floormat::ground_def> final { }; template<> -struct adl_serializer<std::shared_ptr<floormat::ground_atlas>> final { - static void to_json(json& j, const std::shared_ptr<const floormat::ground_atlas>& x); - static void from_json(const json& j, std::shared_ptr<floormat::ground_atlas>& x); +struct adl_serializer<floormat::bptr<floormat::ground_atlas>> final { + static void to_json(json& j, const floormat::bptr<const floormat::ground_atlas>& x); + static void from_json(const json& j, floormat::bptr<floormat::ground_atlas>& x); }; template<> diff --git a/serialize/old-savegame.cpp b/serialize/old-savegame.cpp index 073756d2..8e4998b0 100644 --- a/serialize/old-savegame.cpp +++ b/serialize/old-savegame.cpp @@ -18,7 +18,6 @@ #include <cerrno> #include <cstring> #include <concepts> -#include <memory> #include <vector> @@ -80,7 +79,9 @@ constexpr inline uint8_t meta_short_scenery_bit = highbits<uint8_t, 1, 0>; } // namespace -template<typename T> concept object_subtype = std::is_base_of_v<object, T> || std::is_base_of_v<object_proto, T>; +template<typename T> concept object_subtype = requires { + requires std::is_base_of_v<object, T> || std::is_base_of_v<object_proto, T>; +}; enum : tilemeta { meta_ground = 1 << 2, diff --git a/serialize/savegame.cpp b/serialize/savegame.cpp index f7893094..d279bd9e 100644 --- a/serialize/savegame.cpp +++ b/serialize/savegame.cpp @@ -5,6 +5,7 @@ #include "compat/strerror.hpp" #include "compat/hash.hpp" #include "compat/exception.hpp" +#include "compat/borrowed-ptr.inl" #include "src/ground-atlas.hpp" #include "src/wall-atlas.hpp" @@ -23,7 +24,6 @@ #include <cstdio> #include <compare> #include <concepts> -#include <memory> #include <vector> #include <algorithm> #include <Corrade/Utility/Path.h> @@ -402,7 +402,7 @@ struct writer final : visitor_<writer, true, true> template<typename F> static void visit(const local_coords& pt, F&& f) { visit(pt.to_index(), f); } template<typename F> void visit(StringView name, F&& f) { visit(intern_string(name), f); } - template<typename F> void visit(qual<std::shared_ptr<anim_atlas>>& a, atlas_type type, F&& f) + template<typename F> void visit(qual<bptr<anim_atlas>>& a, atlas_type type, F&& f) { atlasid id = intern_atlas(a, type); visit(id, f); } template<typename F> void write_scenery_proto(const scenery& obj, F&& f) @@ -483,7 +483,7 @@ ok: void(); visit(intern_string(name), f); } - template<typename T> [[nodiscard]] atlasid intern_atlas(const std::shared_ptr<T>& atlas_, atlas_type type) + template<typename T> [[nodiscard]] atlasid intern_atlas(const bptr<T>& atlas_, atlas_type type) { const void* atlas = atlas_.get(); atlas_array.reserve(vector_initial_size); @@ -512,7 +512,7 @@ ok: void(); } } - template<typename T> atlasid maybe_intern_atlas(const std::shared_ptr<T>& atlas, atlas_type type) + template<typename T> atlasid maybe_intern_atlas(const bptr<T>& atlas, atlas_type type) { if (!atlas) return null<atlasid>; @@ -533,7 +533,7 @@ ok: void(); template<typename F> void serialize_objects_(chunk& c, F&& f) { uint32_t count = 0; - for (const std::shared_ptr<object>& obj : c.objects()) + for (const bptr<object>& obj : c.objects()) { if (obj->ephemeral) continue; @@ -541,7 +541,7 @@ ok: void(); } visit(count, f); - for (const std::shared_ptr<object>& obj : c.objects()) + for (const bptr<object>& obj : c.objects()) { fm_assert(obj != nullptr); if (obj->ephemeral) @@ -798,7 +798,7 @@ struct reader final : visitor_<reader<IsNewest>, false, IsNewest> template<typename F> static void visit(local_coords& pt, F&& f) { uint8_t i; f(i); pt = local_coords{i}; } - template<typename F> void visit(std::shared_ptr<anim_atlas>& a, atlas_type type, F&& f) + template<typename F> void visit(bptr<anim_atlas>& a, atlas_type type, F&& f) { atlasid id = (atlasid)-1; f(id); @@ -819,7 +819,7 @@ struct reader final : visitor_<reader<IsNewest>, false, IsNewest> } template<typename F> - void read_scenery(std::shared_ptr<scenery>& ret, const object_proto& pʹ, const object_header_s& h, F&& f) + void read_scenery(bptr<scenery>& ret, const object_proto& pʹ, const object_header_s& h, F&& f) { const auto coord = global_coords{h.ch->coord(), h.tile}; auto sc_type = scenery_type::none; @@ -852,7 +852,7 @@ ok: } template<typename Obj, typename Proto, typename Header> - std::shared_ptr<object> make_object(const object_header_s& h0, object_proto&& p0, Header&& h, auto&& f) + bptr<object> make_object(const object_header_s& h0, object_proto&& p0, Header&& h, auto&& f) { fm_soft_assert(h0.id != 0); @@ -867,7 +867,7 @@ ok: template<typename F> void read_object(chunk* ch, F&& f) { - std::shared_ptr<object> obj; + bptr<object> obj; object_id id = 0; auto type = object_type::none; local_coords tile; @@ -899,7 +899,7 @@ ok: obj = make_object<light, light_proto, std::nullptr_t>(s, move(p), {}, f); goto ok; case object_type::scenery: { - std::shared_ptr<scenery> objʹ; + bptr<scenery> objʹ; read_scenery(objʹ, move(p), s, f); obj = move(objʹ); goto ok; diff --git a/serialize/scenery.cpp b/serialize/scenery.cpp index edcd57c7..e278d408 100644 --- a/serialize/scenery.cpp +++ b/serialize/scenery.cpp @@ -1,6 +1,7 @@ #include "scenery.hpp" #include "compat/overloaded.hpp" #include "compat/exception.hpp" +#include "compat/borrowed-ptr.inl" #include "src/anim-atlas.hpp" #include "compat/assert.hpp" #include "loader/loader.hpp" diff --git a/serialize/tile.cpp b/serialize/tile.cpp index cd9064a8..1cbf8d1b 100644 --- a/serialize/tile.cpp +++ b/serialize/tile.cpp @@ -1,7 +1,8 @@ -#include "serialize/tile.hpp" +#include "tile.hpp" +#include "ground-atlas.hpp" +#include "compat/borrowed-ptr.inl" #include "src/tile.hpp" #include "src/global-coords.hpp" -#include "serialize/ground-atlas.hpp" #include "src/ground-atlas.hpp" #include <tuple> #include <nlohmann/json.hpp> @@ -10,8 +11,6 @@ namespace floormat { using nlohmann::json; -NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(tile_image_proto, atlas, variant) - inline void to_json(json& j, const tile_image_ref& val) { j = tile_image_proto(val); } inline void from_json(const json& j, tile_image_ref& val) { val = tile_image_proto(j); } @@ -28,13 +27,27 @@ inline void from_json(const json& j, global_coords& coord) { std::tuple<chunk_co } // namespace floormat -using namespace floormat; - namespace nlohmann { +using namespace floormat; + void adl_serializer<tile_image_ref>::to_json(json& j, const tile_image_ref& val) { using nlohmann::to_json; if (val.atlas) to_json(j, val); else j = nullptr; } void adl_serializer<tile_image_ref>::from_json(const json& j, tile_image_ref& val) { using nlohmann::from_json; if (j.is_null()) val = {}; else from_json(j, val); } +void adl_serializer<tile_image_proto>::to_json(json& j, const floormat::tile_image_proto& val) +{ + using nlohmann::to_json; + j["atlas"] = val.atlas; + j["variant"] = val.variant; +} + +void adl_serializer<tile_image_proto>::from_json(const json& j, floormat::tile_image_proto& val) +{ + using nlohmann::from_json; + val.atlas = j["atlas"]; + val.variant = j["variant"]; +} + void adl_serializer<local_coords>::to_json(json& j, const local_coords& val) { using nlohmann::to_json; to_json(j, local_coords_{val.x, val.y}); } void adl_serializer<local_coords>::from_json(const json& j, local_coords& val) { using nlohmann::from_json; local_coords_ proxy{}; from_json(j, proxy); val = {proxy.x, proxy.y}; } diff --git a/serialize/tile.hpp b/serialize/tile.hpp index ded1a479..239fc41f 100644 --- a/serialize/tile.hpp +++ b/serialize/tile.hpp @@ -20,6 +20,12 @@ struct adl_serializer<floormat::tile_image_ref> { }; template<> +struct adl_serializer<floormat::tile_image_proto> { + static void to_json(json& j, const floormat::tile_image_proto& val); + static void from_json(const json& j, floormat::tile_image_proto& val); +}; + +template<> struct adl_serializer<floormat::local_coords> { static void to_json(json& j, const floormat::local_coords& val); static void from_json(const json& j, floormat::local_coords& val); diff --git a/serialize/wall-atlas.hpp b/serialize/wall-atlas.hpp index 5ce61c74..29539350 100644 --- a/serialize/wall-atlas.hpp +++ b/serialize/wall-atlas.hpp @@ -1,6 +1,5 @@ #pragma once #include "src/wall-atlas.hpp" -#include <memory> #include <Corrade/Containers/Array.h> #include <nlohmann/json_fwd.hpp> |