From f741f2d77096689703dc11e776b133e4b9d9a615 Mon Sep 17 00:00:00 2001 From: NiskuT Date: Thu, 26 Jan 2023 21:09:29 +0100 Subject: [PATCH] bug-9: remove serialize for variant --- src/shared/Serialization.cpp | 96 ++++++++++++++++++++++-------------- 1 file changed, 60 insertions(+), 36 deletions(-) diff --git a/src/shared/Serialization.cpp b/src/shared/Serialization.cpp index 97303f2a..7ef36d92 100644 --- a/src/shared/Serialization.cpp +++ b/src/shared/Serialization.cpp @@ -45,7 +45,29 @@ void Hexagon::save(Archive &ar, const unsigned int version) const { ar << level; ar << elementsList.size(); - ar << elementsList; + + for (auto &element : elementsList) + { + ar << element->index(); + switch (element->index()) + { + case 0: + ar << std::get(*element); + break; + case 1: + ar << std::get(*element); + break; + case 2: + ar << std::get(*element); + break; + case 3: + ar << std::get(*element); + break; + case 4: + ar << std::get(*element); + break; + } + } } template @@ -54,9 +76,45 @@ void Hexagon::load(Archive &ar, const unsigned int version) ar >> level; size_t size = 0; ar >> size; + /* std::generate_n(std::back_inserter(elementsList), size, []() { return std::make_shared(); }); - ar >> elementsList; + ar >> elementsList;*/ + for (size_t i = 0; i < size; i++) + { + int index = 0; + ar >> index; + if (index == 0) + { + Caravan caravan; + ar >> caravan; + elementsList.push_back(std::make_shared(caravan)); + } + else if (index == 1) + { + Barbarian barbarian; + ar >> barbarian; + elementsList.push_back(std::make_shared(barbarian)); + } + else if (index == 2) + { + BarbarianVillage barbarianVillage; + ar >> barbarianVillage; + elementsList.push_back(std::make_shared(barbarianVillage)); + } + else if (index == 3) + { + ControlPawn controlPawn; + ar >> controlPawn; + elementsList.push_back(std::make_shared(controlPawn)); + } + else if (index == 4) + { + City city; + ar >> city; + elementsList.push_back(std::make_shared(city)); + } + } } template @@ -113,37 +171,3 @@ void Caravan::serialize(Archive &ar, const unsigned int version) ar &used; ar &player; } - -namespace boost -{ - namespace serialization - { - template - void save(Archive &ar, const std::variant &obj, const unsigned int version) - { - boost::variant v; - std::visit([&](const auto &arg) - { v = arg; }, - obj); - - ar &v; - } - - template - void load(Archive &ar, std::variant &obj, const unsigned int version) - { - boost::variant v; - ar &v; - - boost::apply_visitor([&](auto &arg) - { obj = arg; }, - v); - } - - template - void serialize(Archive &ar, std::variant &t, const unsigned int file_version) - { - split_free(ar, t, file_version); - } - } -}