From 0b2467475a306e4e2559c982ceb688a3c03668f8 Mon Sep 17 00:00:00 2001 From: Pcornat Date: Wed, 30 Oct 2024 16:43:07 +0100 Subject: [PATCH 1/7] Creating easily objects --- include/generic_object.hpp | 17 +++-------------- src/generic_object.cpp | 8 -------- 2 files changed, 3 insertions(+), 22 deletions(-) diff --git a/include/generic_object.hpp b/include/generic_object.hpp index 0d2e45d..3c5aa94 100644 --- a/include/generic_object.hpp +++ b/include/generic_object.hpp @@ -68,13 +68,11 @@ namespace character { using billyObjects = std::unique_ptr; - static billyObjects new_object(const billyEnums &inputObject) noexcept; - class Weapons final : virtual public GenericObject { public: const weapons type{ weapons::Sword }; - friend billyObjects new_object(const billyEnums &inputObject) noexcept; + explicit Weapons(const weapons type) : type(type) {} Weapons() = delete; @@ -91,16 +89,13 @@ namespace character { [[nodiscard]] std::string_view to_string() const noexcept final; [[nodiscard]] billyEnums get_type() const noexcept final { return type; } - - private: - explicit Weapons(const weapons type) : type(type) {} }; class Equipments final : virtual public GenericObject { public: const equipments type{ equipments::Chainmail }; - friend billyObjects new_object(const billyEnums &inputObject) noexcept; + explicit Equipments(const equipments type) : type(type) {} Equipments() = delete; @@ -117,16 +112,13 @@ namespace character { [[nodiscard]] std::string_view to_string() const noexcept final; [[nodiscard]] billyEnums get_type() const noexcept final { return type; } - - private: - explicit Equipments(const equipments type) : type(type) {} }; class Tools final : virtual public GenericObject { public: const tools type{ tools::Fourche }; - friend billyObjects new_object(const billyEnums &inputObject) noexcept; + explicit Tools(const tools type) : type(type) {} Tools() = delete; @@ -143,9 +135,6 @@ namespace character { [[nodiscard]] std::string_view to_string() const noexcept final; [[nodiscard]] billyEnums get_type() const noexcept final { return type; } - - private: - explicit Tools(const tools type) : type(type) {} }; } // character diff --git a/src/generic_object.cpp b/src/generic_object.cpp index a09f2c9..5bc6bc9 100644 --- a/src/generic_object.cpp +++ b/src/generic_object.cpp @@ -165,12 +165,4 @@ namespace character { return sackOfGrain; } } - - billyObjects new_object(const billyEnums &inputObject) noexcept { - return std::visit(overloaded{ - [](const weapons input) { return billyObjects{ new Weapons{ input }}; }, - [](const equipments input) { return billyObjects{ new Equipments{ input }}; }, - [](const tools input) { return billyObjects{ new Tools{ input }}; } - }, inputObject); - } } From 965d0c1a35a46da1944f17c2274d5f88b5209b7d Mon Sep 17 00:00:00 2001 From: Pcornat Date: Wed, 30 Oct 2024 16:43:48 +0100 Subject: [PATCH 2/7] Correct type used in character_sheet.hpp --- include/character_sheet.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/character_sheet.hpp b/include/character_sheet.hpp index 339a97c..8fd3dec 100644 --- a/include/character_sheet.hpp +++ b/include/character_sheet.hpp @@ -32,7 +32,7 @@ namespace character { BillyObjects::container objects; - std::unordered_set available_objects{ + std::unordered_set available_objects{ BillyObjects::all_objects.cbegin(), BillyObjects::all_objects.cend() }; From 40f692ea5ecbfd5b588bb99f82103711943f428b Mon Sep 17 00:00:00 2001 From: Pcornat Date: Wed, 30 Oct 2024 17:04:21 +0100 Subject: [PATCH 3/7] Correct code, I suppose. It compiles. --- include/billy_objects.hpp | 97 +++------------ src/billy_objects.cpp | 254 ++++++++------------------------------ 2 files changed, 69 insertions(+), 282 deletions(-) diff --git a/include/billy_objects.hpp b/include/billy_objects.hpp index 8e3125d..2d2e7d2 100644 --- a/include/billy_objects.hpp +++ b/include/billy_objects.hpp @@ -10,46 +10,21 @@ #include #include #include +#include #include #include "characteristic/characteristic.hpp" +#include "generic_object.hpp" -// helper type for the visitor -template -struct overloaded : Ts ... { using Ts::operator()...; }; -template -overloaded(Ts...) -> overloaded; namespace character { class CharacterSheet; - enum class weapons : std::uint8_t { - Sword = 0, - Lance = 1, - Morgenstern = 2, - Bow = 3 - }; - - enum class equipments : std::uint8_t { - Chainmail = 4, - CookingPot = 5, - PamphletTourist = 6, - MedicKit = 7 - }; - - enum class tools : std::uint8_t { - Fourche = 8, - Dagger = 9, - RockClimbingKit = 10, - SackOfGrain = 11 - }; - class BillyObjects final { public: static constexpr std::size_t max_num_obj{ 3 }; - using billyObject = std::variant; - using container = ankerl::svector; + using container = std::unordered_map; - static constexpr std::array all_objects{ + static constexpr std::array all_objects{ weapons::Sword, weapons::Lance, weapons::Morgenstern, @@ -66,22 +41,7 @@ namespace character { static constexpr std::string_view json_key{ "billy_objects" }; - static constexpr std::string_view sword{ "Sword" }; - static constexpr std::string_view lance{ "Lance" }; - static constexpr std::string_view morgenstern{ "Morgenstern" }; - static constexpr std::string_view bow{ "Bow" }; - - static constexpr std::string_view chainmail{ "Chainmail" }; - static constexpr std::string_view cooking_pot{ "Cooking pot" }; - static constexpr std::string_view pamphlet_tourist{ "Touristic pamphlet" }; - static constexpr std::string_view medic_kit{ "Medic kit" }; - - static constexpr std::string_view fourche{ "Fourche" }; - static constexpr std::string_view dagger{ "Dagger" }; - static constexpr std::string_view rock_climbing_kit{ "Rock climbing kit" }; - static constexpr std::string_view sack_of_grain{ "Sack of grain" }; - - static std::string_view billy_object_to_string(const billyObject &object) noexcept; + static std::string_view billy_object_to_string(const billyObjects &object) noexcept; static void to_json(json &j, const BillyObjects::container &billy); @@ -91,47 +51,30 @@ namespace character { ~BillyObjects() noexcept = default; - [[nodiscard]] bool push_object(const billyObject &object, CharacterSheet &sheet) noexcept; + [[nodiscard]] bool insert_object(CharacterSheet &sheet, const billyEnums objType) noexcept; - void pop_object(CharacterSheet &sheet) noexcept; - - [[nodiscard]] static ankerl::svector check_conformity(const CharacterSheet &sheet) noexcept; + void erase_object(CharacterSheet &sheet, const billyEnums objToErase) noexcept; [[nodiscard]] const std::plus &get_plus_operation() const { return plus; } [[nodiscard]] const std::minus &get_minus_operation() const { return minus; } - private: - std::plus plus; - std::minus minus; - - static void change_carac_weapon(const weapons &arg, - CharacterSheet &sheet, - characteristic::Characteristic &localHabilete, - characteristic::Characteristic &localAdresse, - characteristic::Characteristic &localEndurance, - const std::function &operation) noexcept; - - static void change_carac_equipment(const equipments &arg, - CharacterSheet &sheet, - characteristic::Characteristic &localHabilete, - characteristic::Characteristic &localAdresse, - characteristic::Characteristic &localEndurance, - characteristic::Characteristic &localChance, - const std::function &primary, - const std::function &complement) noexcept; - - static void change_carac_tools(const tools &arg, - CharacterSheet &sheet, - characteristic::Characteristic &localHabilete, - characteristic::Characteristic &localAdresse, - characteristic::Characteristic &localEndurance, - characteristic::Characteristic &localChance, - const std::function &operation) noexcept; - static void check_dagger_conditions(const CharacterSheet &sheet, characteristic::Characteristic &localHabilete, const std::function &operation); + + private: + std::plus plus; + + std::minus minus; + + static void change_carac(const billyObjects &arg, + CharacterSheet &sheet, + characteristic::Characteristic &localHabilete, + characteristic::Characteristic &localAdresse, + characteristic::Characteristic &localEndurance, + characteristic::Characteristic &localChance, + const std::function &operation) noexcept; }; } diff --git a/src/billy_objects.cpp b/src/billy_objects.cpp index b4488e8..395600f 100644 --- a/src/billy_objects.cpp +++ b/src/billy_objects.cpp @@ -17,10 +17,16 @@ constexpr std::uint32_t toolsHash = const_hash("tools"); namespace character { using characteristic::Characteristic; - bool BillyObjects::push_object(const billyObject &object, CharacterSheet &sheet) noexcept { + + bool BillyObjects::insert_object(CharacterSheet &sheet, const billyEnums objType) noexcept { if (sheet.objects.size() < 3) { - sheet.objects.emplace_back(object); - sheet.available_objects.erase(object); + sheet.objects.emplace(objType, std::visit(overloaded{ + [](const weapons &arg) { return std::unique_ptr(std::make_unique(arg)); }, + [](const equipments &arg) { return std::unique_ptr(std::make_unique(arg)); }, + [](const tools &arg) { return std::unique_ptr(std::make_unique(arg)); }, + }, objType)); + const auto &object = sheet.objects[objType]; + sheet.available_objects.erase(objType); auto &local_habilete = static_cast(sheet.habilete); auto &local_adresse = static_cast(sheet.adresse); @@ -28,42 +34,21 @@ namespace character { auto &local_chance = static_cast(sheet.chance); std::visit(overloaded{ - [&](const weapons &arg) { - ++sheet.nb_weapons; - change_carac_weapon(arg, sheet, local_habilete, local_adresse, local_endurance, plus); - }, - [&](const equipments &arg) { - ++sheet.nb_equipments; - change_carac_equipment(arg, - sheet, - local_habilete, - local_adresse, - local_endurance, - local_chance, - plus, - minus); - }, - [&](const tools &arg) { - ++sheet.nb_tools; - change_carac_tools(arg, - sheet, - local_habilete, - local_adresse, - local_endurance, - local_chance, - plus); - }, - }, object); + [&](const weapons &arg) { ++sheet.nb_weapons; }, + [&](const equipments &arg) { ++sheet.nb_equipments; }, + [&](const tools &arg) { ++sheet.nb_tools; }, + }, objType); + change_carac(object, sheet, local_habilete, local_adresse, local_endurance, local_chance, plus); return true; } return false; } - void BillyObjects::pop_object(CharacterSheet &sheet) noexcept { + void BillyObjects::erase_object(CharacterSheet &sheet, const billyEnums objToErase) noexcept { if (!sheet.objects.empty()) { - const billyObject obj = sheet.objects.back(); - sheet.objects.pop_back(); - sheet.available_objects.insert(obj); + const billyObjects obj{ sheet.objects[objToErase].release() }; + sheet.objects.erase(objToErase); + sheet.available_objects.insert(objToErase); auto &local_habilete = static_cast(sheet.habilete); auto &local_adresse = static_cast(sheet.adresse); @@ -71,171 +56,49 @@ namespace character { auto &local_chance = static_cast(sheet.chance); std::visit(overloaded{ - [&](const weapons &arg) { - --sheet.nb_weapons; - change_carac_weapon(arg, sheet, local_habilete, local_adresse, local_endurance, minus); - }, - [&](const equipments &arg) { - --sheet.nb_equipments; - change_carac_equipment(arg, - sheet, - local_habilete, - local_adresse, - local_endurance, - local_chance, - minus, - plus); - }, - [&](const tools &arg) { - --sheet.nb_tools; - change_carac_tools(arg, - sheet, - local_habilete, - local_adresse, - local_endurance, - local_chance, - minus); - } - }, obj); + [&](const weapons &arg) { --sheet.nb_weapons; }, + [&](const equipments &arg) { --sheet.nb_equipments; }, + [&](const tools &arg) { --sheet.nb_tools; } + }, objToErase); + change_carac(obj, sheet, local_habilete, local_adresse, local_endurance, local_chance, minus); } } - void BillyObjects::change_carac_tools(const tools &arg, + void BillyObjects::change_carac(const billyObjects &arg, CharacterSheet &sheet, Characteristic &localHabilete, Characteristic &localAdresse, Characteristic &localEndurance, Characteristic &localChance, const std::function &operation) noexcept { - switch (arg) { - case tools::Fourche: - localHabilete.materiel = operation(localHabilete.materiel, 1); - localEndurance.materiel = operation(localEndurance.materiel, 3); - break; - case tools::Dagger: - sheet.critique = operation(sheet.critique, 6); - break; - case tools::RockClimbingKit: - localAdresse.materiel = operation(localAdresse.materiel, 1); - break; - case tools::SackOfGrain: - localEndurance.materiel = operation(localEndurance.materiel, 2); - localChance.materiel = operation(localChance.materiel, 2); - break; - } - } - - void BillyObjects::change_carac_equipment(const equipments &arg, - CharacterSheet &sheet, - Characteristic &localHabilete, - Characteristic &localAdresse, - Characteristic &localEndurance, - Characteristic &localChance, - const std::function &primary, - const std::function &complement) noexcept { - switch (arg) { - case equipments::Chainmail: - localHabilete.materiel = complement(localHabilete.materiel, 1); - localAdresse.materiel = complement(localAdresse.materiel, 1); - localEndurance.materiel = primary(localEndurance.materiel, 1); - sheet.armor = primary(sheet.armor, 2); - break; - case equipments::CookingPot: - localEndurance.materiel = primary(localEndurance.materiel, 2); - sheet.armor = primary(sheet.armor, 1); - break; - case equipments::PamphletTourist: - localChance.materiel = primary(localChance.materiel, 4); - break; - case equipments::MedicKit: - localChance.materiel = primary(localChance.materiel, 1); - break; - } - } - - void BillyObjects::change_carac_weapon(const weapons &arg, - CharacterSheet &sheet, - Characteristic &localHabilete, - Characteristic &localAdresse, - Characteristic &localEndurance, - const std::function &operation) noexcept { - switch (arg) { - case weapons::Sword: - localHabilete.materiel = operation(localHabilete.materiel, 4); - break; - case weapons::Lance: - localHabilete.materiel = operation(localHabilete.materiel, 3); - localAdresse.materiel = operation(localAdresse.materiel, 1); - break; - case weapons::Morgenstern: - localHabilete.materiel = operation(localHabilete.materiel, 1); - localEndurance.materiel = operation(localEndurance.materiel, 1); - sheet.damage = operation(sheet.damage, 1); - break; - case weapons::Bow: - localHabilete.materiel = operation(localHabilete.materiel, 3); - localAdresse.materiel = operation(localAdresse.materiel, 1); - break; - } + localHabilete.materiel = operation(localHabilete.materiel, arg->add_materiel(localHabilete.type)); + localAdresse.materiel = operation(localAdresse.materiel, arg->add_materiel(localAdresse.type)); + localEndurance.materiel = operation(localEndurance.materiel, arg->add_materiel(localEndurance.type)); + localChance.materiel = operation(localChance.materiel, arg->add_materiel(localChance.type)); + sheet.armor = operation(sheet.armor, arg->add_armor()); + sheet.damage = operation(sheet.armor, arg->add_damage()); + sheet.critique = operation(sheet.critique, arg->add_critique()); } void BillyObjects::check_dagger_conditions(const CharacterSheet &sheet, Characteristic &localHabilete, const std::function &operation) { - int count_weapons = 0; - bool is_there_bow = false; - std::for_each(sheet.objects.cbegin(), sheet.objects.cend(), [&](const billyObject &object) -> void { - if (const weapons *p = std::get_if(std::addressof(object)); p != nullptr) { - ++count_weapons; - if (*p == weapons::Bow) { - is_there_bow = true; - } + if (const auto it_dagger = sheet.objects.find(tools::Dagger); it_dagger != sheet.objects.cend()) { + const std::size_t count_weapons = std::count_if(sheet.objects.cbegin(), + sheet.objects.cend(), + [](container::const_reference node) { + return std::get_if(&node.first) != nullptr; + }); + const bool is_there_bow = sheet.objects.find(weapons::Bow) != sheet.objects.cend(); + if (count_weapons > 1 || is_there_bow) { + localHabilete.materiel = operation(localHabilete.materiel, + -it_dagger->second->add_materiel(localHabilete.type)); } - }); - if (count_weapons < 2 && !is_there_bow) { - localHabilete.materiel = operation(localHabilete.materiel, 1); } } - std::string_view BillyObjects::billy_object_to_string(const billyObject &object) noexcept { - return std::visit(overloaded{ - [](const weapons &arg) { - switch (arg) { - case weapons::Sword: - return sword; - case weapons::Lance: - return lance; - case weapons::Morgenstern: - return morgenstern; - case weapons::Bow: - return bow; - } - }, - [](const equipments &arg) { - switch (arg) { - case equipments::Chainmail: - return chainmail; - case equipments::CookingPot: - return cooking_pot; - case equipments::MedicKit: - return medic_kit; - case equipments::PamphletTourist: - return pamphlet_tourist; - } - }, - [](const tools &arg) { - switch (arg) { - case tools::Fourche: - return fourche; - case tools::Dagger: - return dagger; - case tools::RockClimbingKit: - return rock_climbing_kit; - case tools::SackOfGrain: - return sack_of_grain; - } - } - }, object); + std::string_view BillyObjects::billy_object_to_string(const billyObjects &object) noexcept { + return object->to_string(); } void BillyObjects::from_json(const json &j, BillyObjects::container &billy) { @@ -244,20 +107,21 @@ namespace character { const std::uint8_t value = element[1].get(); switch (key) { case weaponsHash: - billy.push_back(static_cast(value)); + billy.emplace(static_cast(value), std::make_unique(static_cast(value))); break; case equipmentHash: - billy.push_back(static_cast(value)); + billy.emplace(static_cast(value), + std::make_unique(static_cast(value))); break; case toolsHash: - billy.push_back(static_cast(value)); + billy.emplace(static_cast(value), std::make_unique(static_cast(value))); break; } } } void BillyObjects::to_json(json &j, const BillyObjects::container &billy) { - for (const auto &object: billy) { + for (const auto &[object_type, _]: billy) { std::visit(overloaded{ [&j](const weapons weapon) { j.emplace_back(std::pair{ weaponsHash, static_cast(weapon) }); @@ -268,27 +132,7 @@ namespace character { [&j](const tools tool) { j.emplace_back(std::pair{ toolsHash, static_cast(tool) }); } - }, object); + }, object_type); } } - - ankerl::svector BillyObjects::check_conformity(const CharacterSheet &sheet) noexcept { - ankerl::svector output; - std::transform(sheet.get_objects().cbegin(), - sheet.get_objects().cend(), std::back_inserter(output), - [&sheet](const billyObject &object) -> bool { - return std::visit(overloaded{ - [](const weapons weapon) { return false; }, - [](const equipments equipment) { return false; }, - [](const tools tool) { return false; }, - }, object); - }); - const int total = std::accumulate(sheet.get_objects().cbegin(), - sheet.get_objects().cend(), - 0, - [](const int a, const billyObject &object) -> int { - return 0; - }); - return output; - } } \ No newline at end of file From 1f18499228ebb5830ad947e4a326878755aee998 Mon Sep 17 00:00:00 2001 From: Pcornat Date: Wed, 30 Oct 2024 17:04:49 +0100 Subject: [PATCH 4/7] Adding BillySheet as a library --- CMakeLists.txt | 4 ++-- Unit testing/CMakeLists.txt | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index eca249a..a6e059b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -77,8 +77,6 @@ if (${ENABLE_COVERAGE}) list(APPEND LINKER_FLAGS gcov) endif () -add_subdirectory("Unit testing") - add_library(BillySheet SHARED ${SOURCES}) target_include_directories(BillySheet PRIVATE include include/imgui external/ImFileDialog) @@ -117,3 +115,5 @@ target_link_libraries(BillySheet glfw nlohmann_json::nlohmann_json ${LINKER_FLAGS} ) + +add_subdirectory("Unit testing") diff --git a/Unit testing/CMakeLists.txt b/Unit testing/CMakeLists.txt index d8086de..554d3fd 100644 --- a/Unit testing/CMakeLists.txt +++ b/Unit testing/CMakeLists.txt @@ -6,7 +6,6 @@ include(../external/catch2/extras/Catch.cmake) add_executable(UnitTest adummy.cpp characteristics_tests.cpp - ../src/billy_objects.cpp billy_objects_tests.cpp ) @@ -19,9 +18,9 @@ set_target_properties(Catch2 UnitTest svector PROPERTIES ) target_include_directories(UnitTest PRIVATE ${CMAKE_SOURCE_DIR}/include) target_compile_definitions(UnitTest PRIVATE ${DEF_COMP}) -target_compile_options(UnitTest PRIVATE ${COMPILE_FLAGS}) +target_compile_options(UnitTest PRIVATE ${COMPILE_FLAGS} -fdiagnostics-all-candidates) target_link_options(UnitTest PRIVATE ${LINKER_OPTIONS}) -target_link_libraries(UnitTest ${LINKER_FLAGS} spdlog::spdlog_header_only Catch2::Catch2WithMain nlohmann_json::nlohmann_json svector::svector) +target_link_libraries(UnitTest ${LINKER_FLAGS} spdlog::spdlog_header_only Catch2::Catch2WithMain nlohmann_json::nlohmann_json svector::svector BillySheet) enable_testing() catch_discover_tests(UnitTest WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} REPORTER junit OUTPUT_DIR ${CMAKE_SOURCE_DIR} OUTPUT_PREFIX cppspec- OUTPUT_SUFFIX .xml) From e2d598d5d034a14e1c46feb13c783fb06cc15908 Mon Sep 17 00:00:00 2001 From: Pcornat Date: Wed, 30 Oct 2024 19:07:23 +0100 Subject: [PATCH 5/7] Cleaner includes --- include/billy_objects.hpp | 5 +++-- include/character_sheet.hpp | 2 +- include/{characteristic => }/characteristic.hpp | 0 include/generic_object.hpp | 13 ++++++++----- src/billy_objects.cpp | 6 +----- src/character_sheet.cpp | 1 - src/generic_object.cpp | 7 ++++--- 7 files changed, 17 insertions(+), 17 deletions(-) rename include/{characteristic => }/characteristic.hpp (100%) delete mode 100644 src/character_sheet.cpp diff --git a/include/billy_objects.hpp b/include/billy_objects.hpp index 2d2e7d2..6f8462b 100644 --- a/include/billy_objects.hpp +++ b/include/billy_objects.hpp @@ -11,13 +11,14 @@ #include #include #include -#include -#include "characteristic/characteristic.hpp" #include "generic_object.hpp" namespace character { class CharacterSheet; + namespace characteristic{ + class Characteristic; + } class BillyObjects final { public: diff --git a/include/character_sheet.hpp b/include/character_sheet.hpp index 8fd3dec..c9ec58a 100644 --- a/include/character_sheet.hpp +++ b/include/character_sheet.hpp @@ -1,7 +1,7 @@ #ifndef BILLYSHEET_CHARACTER_SHEET_HPP #define BILLYSHEET_CHARACTER_SHEET_HPP -#include "characteristic/characteristic.hpp" +#include "characteristic.hpp" #include "billy_objects.hpp" #include diff --git a/include/characteristic/characteristic.hpp b/include/characteristic.hpp similarity index 100% rename from include/characteristic/characteristic.hpp rename to include/characteristic.hpp diff --git a/include/generic_object.hpp b/include/generic_object.hpp index 3c5aa94..56d5fb0 100644 --- a/include/generic_object.hpp +++ b/include/generic_object.hpp @@ -9,7 +9,6 @@ #include #include #include -#include "characteristic/characteristic.hpp" // helper type for the visitor template @@ -18,6 +17,10 @@ template overloaded(Ts...) -> overloaded; namespace character { + namespace characteristic { + enum class characType : std::uint8_t; + } + using namespace std::string_literals; enum class weapons : std::uint8_t { @@ -59,7 +62,7 @@ namespace character { [[nodiscard]] virtual std::uint32_t add_damage() const noexcept = 0; - [[nodiscard]] virtual std::int32_t add_materiel(const characteristic::characType inType) const noexcept = 0; + [[nodiscard]] virtual std::int32_t add_materiel(const characteristic::characType &inType) const noexcept = 0; [[nodiscard]] virtual std::string_view to_string() const noexcept = 0; @@ -84,7 +87,7 @@ namespace character { [[nodiscard]] std::uint32_t add_damage() const noexcept final; - [[nodiscard]] std::int32_t add_materiel(const characteristic::characType inType) const noexcept final; + [[nodiscard]] std::int32_t add_materiel(const characteristic::characType &inType) const noexcept final; [[nodiscard]] std::string_view to_string() const noexcept final; @@ -107,7 +110,7 @@ namespace character { [[nodiscard]] std::uint32_t add_damage() const noexcept final; - [[nodiscard]] std::int32_t add_materiel(const characteristic::characType inType) const noexcept final; + [[nodiscard]] std::int32_t add_materiel(const characteristic::characType &inType) const noexcept final; [[nodiscard]] std::string_view to_string() const noexcept final; @@ -130,7 +133,7 @@ namespace character { [[nodiscard]] std::uint32_t add_damage() const noexcept final; - [[nodiscard]] std::int32_t add_materiel(const characteristic::characType inType) const noexcept final; + [[nodiscard]] std::int32_t add_materiel(const characteristic::characType &inType) const noexcept final; [[nodiscard]] std::string_view to_string() const noexcept final; diff --git a/src/billy_objects.cpp b/src/billy_objects.cpp index 395600f..d2d40f4 100644 --- a/src/billy_objects.cpp +++ b/src/billy_objects.cpp @@ -1,9 +1,5 @@ -// -// Created by postaron on 23/02/24. -// #include "billy_objects.hpp" -#include -#include "characteristic/characteristic.hpp" +#include "characteristic.hpp" #include "character_sheet.hpp" std::uint32_t constexpr const_hash(const char *input) { diff --git a/src/character_sheet.cpp b/src/character_sheet.cpp deleted file mode 100644 index aae9853..0000000 --- a/src/character_sheet.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "character_sheet.hpp" diff --git a/src/generic_object.cpp b/src/generic_object.cpp index 5bc6bc9..c0536df 100644 --- a/src/generic_object.cpp +++ b/src/generic_object.cpp @@ -3,6 +3,7 @@ // #include "generic_object.hpp" +#include "characteristic.hpp" namespace character { using characteristic::characType; @@ -30,7 +31,7 @@ namespace character { std::uint32_t Weapons::add_damage() const noexcept { return type == weapons::Morgenstern ? 1 : 0; } - std::int32_t Weapons::add_materiel(const characteristic::characType inType) const noexcept { + std::int32_t Weapons::add_materiel(const characteristic::characType &inType) const noexcept { switch (type) { case weapons::Sword: if (inType == characType::Habilete) { @@ -93,7 +94,7 @@ namespace character { std::uint32_t Equipments::add_damage() const noexcept { return 0; } - std::int32_t Equipments::add_materiel(const characteristic::characType inType) const noexcept { + std::int32_t Equipments::add_materiel(const characteristic::characType &inType) const noexcept { switch (type) { case equipments::Chainmail: if (inType == characType::Habilete || @@ -129,7 +130,7 @@ namespace character { std::uint32_t Tools::add_damage() const noexcept { return 0; } - std::int32_t Tools::add_materiel(const characteristic::characType inType) const noexcept { + std::int32_t Tools::add_materiel(const characteristic::characType &inType) const noexcept { switch (type) { case tools::Fourche: switch (inType) { From 9d57971414dd181abbb0d172e768e05608ff6166 Mon Sep 17 00:00:00 2001 From: Pcornat Date: Wed, 30 Oct 2024 20:04:07 +0100 Subject: [PATCH 6/7] Working correctly --- .idea/cmake.xml | 6 +++--- include/billy_objects.hpp | 3 ++- src/billy_objects.cpp | 1 + 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/.idea/cmake.xml b/.idea/cmake.xml index 4f2501f..221a8cc 100644 --- a/.idea/cmake.xml +++ b/.idea/cmake.xml @@ -2,9 +2,9 @@ - - - + + + \ No newline at end of file diff --git a/include/billy_objects.hpp b/include/billy_objects.hpp index 6f8462b..7a9e765 100644 --- a/include/billy_objects.hpp +++ b/include/billy_objects.hpp @@ -11,6 +11,7 @@ #include #include #include +#include #include "generic_object.hpp" @@ -22,7 +23,7 @@ namespace character { class BillyObjects final { public: - static constexpr std::size_t max_num_obj{ 3 }; + using json = nlohmann::json; using container = std::unordered_map; static constexpr std::array all_objects{ diff --git a/src/billy_objects.cpp b/src/billy_objects.cpp index d2d40f4..6b7ce6e 100644 --- a/src/billy_objects.cpp +++ b/src/billy_objects.cpp @@ -1,6 +1,7 @@ #include "billy_objects.hpp" #include "characteristic.hpp" #include "character_sheet.hpp" +#include std::uint32_t constexpr const_hash(const char *input) { return *input ? static_cast(*input) + 33 * const_hash(input + 1) : 5381; From 2b59a832a458f9a5cb14073b67533d8f9f33abb0 Mon Sep 17 00:00:00 2001 From: Pcornat Date: Wed, 30 Oct 2024 20:35:38 +0100 Subject: [PATCH 7/7] Removing useless submodules and exporting targets --- .gitmodules | 9 ------ CMakeLists.txt | 79 +++++++++++++++++++++++++++--------------------- Config.cmake.in | 7 +++++ external/json | 1 - external/spdlog | 1 - external/svector | 1 - 6 files changed, 51 insertions(+), 47 deletions(-) create mode 100644 Config.cmake.in delete mode 160000 external/json delete mode 160000 external/spdlog delete mode 160000 external/svector diff --git a/.gitmodules b/.gitmodules index 10ec53e..e540fcd 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,12 +1,3 @@ -[submodule "external/spdlog"] - path = external/spdlog - url = https://github.com/gabime/spdlog.git -[submodule "external/json"] - path = external/json - url = https://github.com/nlohmann/json.git [submodule "external/catch2"] path = external/catch2 url = https://github.com/catchorg/Catch2.git -[submodule "external/svector"] - path = external/svector - url = https://github.com/martinus/svector.git diff --git a/CMakeLists.txt b/CMakeLists.txt index a6e059b..1258457 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,10 @@ cmake_minimum_required(VERSION 3.19 FATAL_ERROR) project(BillySheet LANGUAGES CXX) +include(GNUInstallDirs) +include(CMakePackageConfigHelpers) +include(FetchContent) + file(GLOB_RECURSE SOURCE_HEADERS include/*.h include/*.hpp) file(GLOB_RECURSE SOURCE_FILES src/*.cpp) @@ -10,6 +14,9 @@ set(SOURCES ${SOURCE_FILES} ) +find_package(PkgConfig REQUIRED) +pkg_check_modules(Jemalloc REQUIRED IMPORTED_TARGET jemalloc) + find_program(CCACHE_FOUND ccache) if (CCACHE_FOUND) message(STATUS "ccache found !") @@ -21,20 +28,11 @@ else () message(STATUS "ccache not found") endif () -add_subdirectory(external/svector) - -option(SPDLOG_ENABLE_PCH "Build static or shared library using precompiled header to speed up compilation time" ON) -option(SPDLOG_BUILD_WARNINGS "Enable compiler warnings" ON) -option(SPDLOG_PREVENT_CHILD_FD "Prevent from child processes to inherit log file descriptors" ON) -option(SPDLOG_NO_THREAD_ID "prevent spdlog from querying the thread id on each log call if thread id is not needed" ON) -option(SPDLOG_NO_TLS "prevent spdlog from using thread local storage" ON) -option(SPDLOG_NO_ATOMIC_LEVELS "prevent spdlog from using of std::atomic log levels (use only if your code never modifies log levels concurrently" ON) -add_subdirectory(external/spdlog) - +fetchcontent_declare(json URL https://github.com/nlohmann/json/releases/download/v3.11.3/json.tar.xz) set(JSON_BuildTests OFF CACHE INTERNAL "") option(JSON_ImplicitConversions "Enable implicit conversions." OFF) option(JSON_SystemInclude "Include as system headers (skip for clang-tidy)." ON) -add_subdirectory(external/json) +fetchcontent_makeavailable(json) option(CATCH_INSTALL_DOCS "Install documentation alongside library" OFF) option(CATCH_INSTALL_EXTRAS "Install extras alongside library" OFF) @@ -64,10 +62,6 @@ set(LINKER_OPTIONS -fdevirtualize-at-ltrans ) -set(LINKER_FLAGS - jemalloc -) - option(ENABLE_COVERAGE "Enabling coverage" OFF) if (${ENABLE_COVERAGE}) @@ -79,41 +73,56 @@ endif () add_library(BillySheet SHARED ${SOURCES}) -target_include_directories(BillySheet PRIVATE include include/imgui external/ImFileDialog) +target_include_directories(BillySheet + PUBLIC + $ + $ +) -set_target_properties(BillySheet spdlog svector PROPERTIES +set_target_properties(BillySheet PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF INTERPROCEDURAL_OPTIMIZATION ON - # UNITY_BUILD ON + UNITY_BUILD ON + PUBLIC_HEADER "include/billy_objects.hpp;include/character_sheet.hpp;include/generic_object.hpp;include/characteristic.hpp" ) -set_target_properties(spdlog svector PROPERTIES UNITY_BUILD ON) - target_compile_definitions(BillySheet PRIVATE $<$:_GLIBCXX_DEBUG> - $<$:SPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_DEBUG> - $<$:SPDLOG_ACTIVE_LEVEL=SPDLOG_LEVEL_ERROR>) +) -target_compile_definitions(spdlog PRIVATE $<$:_GLIBCXX_DEBUG>) -target_compile_definitions(svector INTERFACE $<$:_GLIBCXX_DEBUG>) - -target_compile_options(spdlog PRIVATE ${COMPILE_FLAGS}) -target_compile_options(svector INTERFACE ${COMPILE_FLAGS}) target_compile_options(BillySheet PRIVATE ${COMPILE_FLAGS}) -target_link_options(spdlog PRIVATE ${LINKER_OPTIONS}) -target_link_options(svector INTERFACE ${LINKER_OPTIONS}) target_link_options(BillySheet PRIVATE ${LINKER_OPTIONS}) -target_link_libraries(spdlog PRIVATE ${LINKER_FLAGS}) -target_link_libraries(svector INTERFACE ${LINKER_FLAGS}) -target_link_libraries(BillySheet glfw - spdlog::spdlog_header_only - svector::svector +target_link_libraries(BillySheet PUBLIC nlohmann_json::nlohmann_json - ${LINKER_FLAGS} + PkgConfig::Jemalloc +) + +install(TARGETS BillySheet nlohmann_json + EXPORT billySheetTargets + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/billySheet" +) + +install(EXPORT billySheetTargets + FILE billySheetTargets.cmake + NAMESPACE billySheet:: + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/billySheet +) + +configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in + "${CMAKE_CURRENT_BINARY_DIR}/billySheetConfig.cmake" + INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/billySheet +) + +install(FILES + "${CMAKE_CURRENT_BINARY_DIR}/billySheetConfig.cmake" + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/billySheet ) add_subdirectory("Unit testing") diff --git a/Config.cmake.in b/Config.cmake.in new file mode 100644 index 0000000..849e51d --- /dev/null +++ b/Config.cmake.in @@ -0,0 +1,7 @@ +@PACKAGE_INIT@ + +include("${CMAKE_CURRENT_LIST_DIR}/billySheetTargets.cmake") +include(CMakeFindDependencyMacro) +find_dependency(PkgConfig REQUIRED) + +check_required_components(billySheet) \ No newline at end of file diff --git a/external/json b/external/json deleted file mode 160000 index 9cca280..0000000 --- a/external/json +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 9cca280a4d0ccf0c08f47a99aa71d1b0e52f8d03 diff --git a/external/spdlog b/external/spdlog deleted file mode 160000 index 27cb4c7..0000000 --- a/external/spdlog +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 27cb4c76708608465c413f6d0e6b8d99a4d84302 diff --git a/external/svector b/external/svector deleted file mode 160000 index cbeced4..0000000 --- a/external/svector +++ /dev/null @@ -1 +0,0 @@ -Subproject commit cbeced42e061da03b881ba1d09621d4604de030a