From 02d7895033c4ab8a3fb02eddb17ac5622f8cdaef Mon Sep 17 00:00:00 2001 From: Pcornat Date: Wed, 4 Feb 2026 16:03:51 +0100 Subject: [PATCH 1/3] C++23 --- CMakeLists.txt | 2 +- Unit testing/sheet_tests.cpp | 2 +- src/billy_objects.cpp | 55 ++++++++++++++++++++---------------- 3 files changed, 32 insertions(+), 27 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a0393f0..e386176 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -81,7 +81,7 @@ target_include_directories(BillySheet target_include_directories(BillySheet INTERFACE $) set_target_properties(BillySheet PROPERTIES - CXX_STANDARD 17 + CXX_STANDARD 23 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF INTERPROCEDURAL_OPTIMIZATION ON diff --git a/Unit testing/sheet_tests.cpp b/Unit testing/sheet_tests.cpp index 39de7ec..19814c7 100644 --- a/Unit testing/sheet_tests.cpp +++ b/Unit testing/sheet_tests.cpp @@ -22,7 +22,7 @@ TEST_CASE("[E] Serialize sheet", "[serialize][0]") { } TEST_CASE("[F] Deserialize sheet", "[deserialize][0]") { - const auto deserializer = []() { + const auto deserializer = [] { std::ifstream file{ "character_sheet.json" }; return json::parse(file); }(); diff --git a/src/billy_objects.cpp b/src/billy_objects.cpp index 2beca1f..d8cba8b 100644 --- a/src/billy_objects.cpp +++ b/src/billy_objects.cpp @@ -46,9 +46,9 @@ namespace character { auto &local_chance = static_cast(sheet.chance); std::visit(overloaded{ - [&](const weapons &arg) { ++sheet.nb_weapons; }, - [&](const equipments &arg) { ++sheet.nb_equipments; }, - [&](const tools &arg) { ++sheet.nb_tools; }, + [&](const weapons &) { ++sheet.nb_weapons; }, + [&](const equipments &) { ++sheet.nb_equipments; }, + [&](const tools &) { ++sheet.nb_tools; }, }, objType); change_carac(object, sheet, local_habilete, local_adresse, local_endurance, local_chance, plus); @@ -99,13 +99,13 @@ namespace character { void BillyObjects::check_dagger_conditions(CharacterSheet &sheet) { if (const auto it_dagger = sheet.get_objects().find(tools::Dagger); it_dagger != sheet.get_objects().cend()) { - const std::size_t count_weapons = std::count_if(sheet.get_objects().cbegin(), - sheet.get_objects().cend(), - [](container::const_reference node) { - return std::get_if(&node.first) != nullptr; - } + const std::size_t count_weapons = std::ranges::count_if(sheet.get_objects(), + [](container::const_reference node) { + return std::get_if(&node.first) != + nullptr; + } ); - if (count_weapons > 1 || sheet.get_objects().find(weapons::Bow) != sheet.get_objects().cend()) { + if (count_weapons > 1 || sheet.get_objects().contains(weapons::Bow)) { sheet.habilete.materiel -= it_dagger->second->add_materiel(sheet.get_habilete().type); } } @@ -130,26 +130,31 @@ namespace character { case toolsHash: billy.emplace(static_cast(value), std::make_unique(static_cast(value))); break; - default: - throw std::logic_error("Wrong hash for from_json in BillyObjects::from_json"); } } } void BillyObjects::to_json(json &j, const container &billy) { - for (const auto &[object_type, _]: billy) { - std::visit(overloaded{ - [&j](const weapons weapon) { - j.emplace_back(std::pair{ weaponsHash, static_cast(weapon) }); - }, - [&j](const equipments equipment) { - j.emplace_back(std::pair{ equipmentHash, static_cast(equipment) }); - }, - [&j](const tools tool) { - j.emplace_back(std::pair{ toolsHash, static_cast(tool) }); - } - }, - object_type); - } + std::ranges::for_each(billy | std::views::keys, + [&j](const auto &obj) { + std::visit(overloaded{ + [&j](const weapons weapon) { + j.emplace_back(std::pair{ + weaponsHash, static_cast(weapon) + }); + }, + [&j](const equipments equipment) { + j.emplace_back(std::pair{ + equipmentHash, static_cast(equipment) + }); + }, + [&j](const tools tool) { + j.emplace_back(std::pair{ + toolsHash, static_cast(tool) + }); + } + }, + obj); + }); } } From 27b3812725bcffa94fcf2cb735f2c2ae79f26e9e Mon Sep 17 00:00:00 2001 From: Pcornat Date: Wed, 4 Feb 2026 16:04:08 +0100 Subject: [PATCH 2/3] Simplify --- include/character_sheet.hpp | 43 ++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/include/character_sheet.hpp b/include/character_sheet.hpp index 09f710a..ee54cf3 100644 --- a/include/character_sheet.hpp +++ b/include/character_sheet.hpp @@ -80,15 +80,10 @@ namespace character { [[nodiscard]] const BillyObjects::container &get_objects() const { return objects; } [[nodiscard]] classe get_current_class() const { - if (nb_weapons >= 2) { - return classe::Guerrier; - } else if (nb_equipments >= 2) { - return classe::Prudent; - } else if (nb_tools >= 2) { - return classe::Paysan; - } else { - return classe::Debrouillard; - } + if (nb_weapons >= 2) { return classe::Guerrier; } + if (nb_equipments >= 2) { return classe::Prudent; } + if (nb_tools >= 2) { return classe::Paysan; } + return classe::Debrouillard; } [[nodiscard]] std::uint32_t get_health_point() const { return health_point; } @@ -126,24 +121,24 @@ namespace character { }; static void to_json(json &j, const CharacterSheet &billy) { - j["caractere"] = billy.get_caractere(); - j["adresse"] = billy.get_adresse(); - j["endurance"] = billy.get_endurance(); - j["chance"] = billy.get_chance(); - j["habilete"] = billy.get_habilete(); - j["classe"] = billy.get_current_class(); - j["health_point"] = billy.get_health_point(); - j["armor"] = billy.get_armor(); - j["damage"] = billy.get_damage(); - j["glory"] = billy.get_glory(); - j["money"] = billy.get_money(); - j["nb_weapons"] = billy.get_nb_weapons(); - j["nb_equipments"] = billy.get_nb_equipments(); - j["nb_tools"] = billy.get_nb_tools(); + j.emplace("caractere", billy.get_caractere()); + j.emplace("adresse", billy.get_adresse()); + j.emplace("endurance", billy.get_endurance()); + j.emplace("chance", billy.get_chance()); + j.emplace("habilete", billy.get_habilete()); + j.emplace("classe", billy.get_current_class()); + j.emplace("health_point", billy.get_health_point()); + j.emplace("armor", billy.get_armor()); + j.emplace("damage", billy.get_damage()); + j.emplace("glory", billy.get_glory()); + j.emplace("money", billy.get_money()); + j.emplace("nb_weapons", billy.get_nb_weapons()); + j.emplace("nb_equipments", billy.get_nb_equipments()); + j.emplace("nb_tools", billy.get_nb_tools()); j.emplace(std::pair{ BillyObjects::json_key, json::array() }); BillyObjects::to_json(j.at(BillyObjects::json_key), billy.get_objects()); } } -#endif //BILLYSHEET_CHARACTER_SHEET_HPP \ No newline at end of file +#endif //BILLYSHEET_CHARACTER_SHEET_HPP From 9d8156291721a30abff8082ad0809163637c4876 Mon Sep 17 00:00:00 2001 From: Pcornat Date: Wed, 4 Feb 2026 16:04:20 +0100 Subject: [PATCH 3/3] New test case to cover --- Unit testing/billy_objects_tests.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Unit testing/billy_objects_tests.cpp b/Unit testing/billy_objects_tests.cpp index d1b73ab..d762848 100644 --- a/Unit testing/billy_objects_tests.cpp +++ b/Unit testing/billy_objects_tests.cpp @@ -165,6 +165,19 @@ TEST_CASE("[D] Double erase no throw", "[pushpop][1]") { REQUIRE_FALSE(gestionnaire.erase_object(sheet, weapons::Bow)); } +TEST_CASE("[D] Insert no more than 3", "[pushpop][2]") { + BillyObjects gestionnaire{}; + CharacterSheet sheet; + REQUIRE(sheet.get_objects().empty()); + + REQUIRE_NOTHROW(gestionnaire.insert_object(sheet, weapons::Sword)); + REQUIRE(gestionnaire.insert_object(sheet, weapons::Morgenstern)); + REQUIRE(gestionnaire.insert_object(sheet, weapons::Bow)); + REQUIRE(sheet.get_objects().size() == 3); + REQUIRE_FALSE(gestionnaire.insert_object(sheet, equipments::Chainmail)); + REQUIRE(sheet.get_objects().size() == 3); +} + TEST_CASE("[D] Printing Billy's objects", "[printing]") { for (const auto &object: BillyObjects::all_objects) { const std::unique_ptr obj = test::get_obj(object);