From a55f4043329ba12dfdf17e3c250cdb947e808876 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Tue, 9 Apr 2024 16:40:30 +0200 Subject: w --- serialize/savegame.cpp | 88 ++++++++++++++++++-------------------------------- 1 file changed, 31 insertions(+), 57 deletions(-) diff --git a/serialize/savegame.cpp b/serialize/savegame.cpp index 0773d78c..32c9e60e 100644 --- a/serialize/savegame.cpp +++ b/serialize/savegame.cpp @@ -93,6 +93,30 @@ struct buffer } }; +struct size_counter +{ + size_t& size; + + template requires (std::is_arithmetic_v && std::is_fundamental_v) + CORRADE_ALWAYS_INLINE void operator()(T) { size += sizeof(T); } +}; + +struct byte_writer +{ + binary_writer& s; + + template requires (std::is_fundamental_v && std::is_arithmetic_v) + CORRADE_ALWAYS_INLINE void operator()(T value) { s << value; } +}; + +struct byte_reader +{ + binary_reader& s; + + template requires (std::is_fundamental_v && std::is_arithmetic_v) + CORRADE_ALWAYS_INLINE void operator()(T& value) { value << s; } +}; + struct object_header_s { object_id& id; @@ -170,8 +194,9 @@ struct visitor_ { auto& self = derived(); - fm_soft_assert(s.id != 0); f(s.id); + fm_soft_assert(s.id != 0); + visit(s.type, f); if (s.type >= object_type::COUNT || s.type == object_type::none) [[unlikely]] fm_throw("invalid object type {}"_cf, (int)s.type); @@ -375,42 +400,11 @@ struct writer final : visitor_ explicit writer(const world& w) : w{ w } {} - struct size_counter - { - size_t& size; - - template - requires (std::is_arithmetic_v && std::is_fundamental_v) - void operator()(T) { size += sizeof(T); } - }; - - struct byte_writer - { - binary_writer& s; - - template - requires (std::is_fundamental_v && std::is_arithmetic_v) - void operator()(T value) - { - s << value; - } - }; + template static void visit(const local_coords& pt, F&& f) { f(pt.to_index()); } + template void visit(StringView name, F&& f) { f(intern_string(name)); } template void visit(qual>& a, atlas_type type, F&& f) - { - atlasid id = intern_atlas(a, type); - visit(id, f); - } - - template static void visit(const local_coords& pt, F&& f) - { - f(pt.to_index()); - } - - template void visit(StringView name, F&& f) - { - f(intern_string(name)); - } + { atlasid id = intern_atlas(a, type); visit(id, f); } template void write_scenery_proto(const scenery& obj, F&& f) // todo! replace scenery::subtype with inheritance! { @@ -789,31 +783,11 @@ struct reader final : visitor_ reader(class world& w, loader_policy policy) : w{w}, asset_policy{policy} {} - struct byte_reader - { - binary_reader& s; - - template - requires (std::is_fundamental_v && std::is_arithmetic_v) - void operator()(T& value) - { - value << s; - } - }; - template void visit(String& str, F&& f) - { - atlasid id; - f(id); - str = get_string(id); - } + { atlasid id; f(id); str = get_string(id); } template static void visit(local_coords& pt, F&& f) - { - uint8_t i; - f(i); - pt = local_coords{i}; - } + { uint8_t i; f(i); pt = local_coords{i}; } template void visit(std::shared_ptr& a, atlas_type type, F&& f) { -- cgit v1.2.3