From 1e0d00d3b76d98188010c468c822580e7a05df5f Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 4 Apr 2020 13:37:06 +0200 Subject: [PATCH] Revert "Use smart pointers for basic libraries." This reverts commit 214ff819 --- CInterface/Library/DataLibrary.cpp | 7 ++-- src/Battling/Flow/TurnHandler.cpp | 2 +- src/Battling/Library/BattleLibrary.cpp | 50 ++++++++++++++++++-------- src/Battling/Library/BattleLibrary.hpp | 50 ++++++++++---------------- src/Battling/Models/Creature.cpp | 2 +- src/Library/DataLibrary.cpp | 41 ++++++++++++++++----- src/Library/DataLibrary.hpp | 44 +++++++++++------------ tests/BattleTests/TurnOrderTests.cpp | 8 ++--- tests/TestLibrary/TestLibrary.cpp | 13 +++---- 9 files changed, 121 insertions(+), 96 deletions(-) diff --git a/CInterface/Library/DataLibrary.cpp b/CInterface/Library/DataLibrary.cpp index 829b96e..c0513ed 100644 --- a/CInterface/Library/DataLibrary.cpp +++ b/CInterface/Library/DataLibrary.cpp @@ -5,16 +5,13 @@ using namespace CreatureLib::Library; export uint8_t CreatureLib_DataLibrary_Construct(const DataLibrary*& out, LibrarySettings* settings, SpeciesLibrary* species, AttackLibrary* attacks, ItemLibrary* items, GrowthRateLibrary* growthRates, TypeLibrary* typeLibrary) { - Try(out = new DataLibrary( - std::unique_ptr(settings), std::unique_ptr(species), - std::unique_ptr(attacks), std::unique_ptr(items), - std::unique_ptr(growthRates), std::unique_ptr(typeLibrary));) + Try(out = new DataLibrary(settings, species, attacks, items, growthRates, typeLibrary);) } export void CreatureLib_DataLibrary_Destruct(const DataLibrary* p) { delete p; } #define SIMPLE_GET_FUNC(type, name, returnType) \ - export returnType CreatureLib_##type##_##name(const type* p) { return p->name().get(); } + export returnType CreatureLib_##type##_##name(const type* p) { return p->name(); } SIMPLE_GET_FUNC(DataLibrary, GetSettings, const LibrarySettings*); SIMPLE_GET_FUNC(DataLibrary, GetSpeciesLibrary, const SpeciesLibrary*); diff --git a/src/Battling/Flow/TurnHandler.cpp b/src/Battling/Flow/TurnHandler.cpp index 317c05a..e502ee3 100644 --- a/src/Battling/Flow/TurnHandler.cpp +++ b/src/Battling/Flow/TurnHandler.cpp @@ -134,7 +134,7 @@ void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, Creature* targe auto attackData = attack->GetAttack()->GetAttack(); auto library = user->GetBattle()->GetLibrary(); AssertNotNull(library) - auto& dmgLibrary = library->GetDamageLibrary(); + auto dmgLibrary = library->GetDamageLibrary(); for (uint8_t hitIndex = 0; hitIndex < numHits; hitIndex++) { if (user->IsFainted()) { break; diff --git a/src/Battling/Library/BattleLibrary.cpp b/src/Battling/Library/BattleLibrary.cpp index 417e6cb..bcd14b6 100644 --- a/src/Battling/Library/BattleLibrary.cpp +++ b/src/Battling/Library/BattleLibrary.cpp @@ -2,24 +2,46 @@ using namespace CreatureLib::Battling; -BattleLibrary::BattleLibrary(const Library::DataLibrary* staticLib, - std::unique_ptr statCalculator, - std::unique_ptr damageLibrary, - std::unique_ptr experienceLibrary, - std::unique_ptr scriptResolver, std::unique_ptr miscLibrary) - : _staticLib(staticLib), _statCalculator(std::move(statCalculator)), _damageLibrary(std::move(damageLibrary)), - _experienceLibrary(std::move(experienceLibrary)), _scriptResolver(std::move(scriptResolver)), - _miscLibrary(std::move(miscLibrary)) {} +BattleLibrary::BattleLibrary(const CreatureLib::Library::DataLibrary* staticLib, BattleStatCalculator* statCalculator, + DamageLibrary* damageLibrary, ExperienceLibrary* experienceLibrary, + ScriptResolver* scriptResolver, MiscLibrary* miscLibrary) + : _staticLib(staticLib), _statCalculator(statCalculator), _damageLibrary(damageLibrary), + _experienceLibrary(experienceLibrary), _scriptResolver(scriptResolver), _miscLibrary(miscLibrary) {} -BattleLibrary::~BattleLibrary() { delete _staticLib; } - -const std::unique_ptr& BattleLibrary::GetStatCalculator() const noexcept { - return _statCalculator; +BattleLibrary::~BattleLibrary() { + delete _staticLib; + delete _statCalculator; + delete _damageLibrary; + delete _experienceLibrary; + delete _scriptResolver; + delete _miscLibrary; } -const std::unique_ptr& BattleLibrary::GetDamageLibrary() const noexcept { return _damageLibrary; } +const CreatureLib::Library::LibrarySettings* BattleLibrary::GetSettings() const noexcept { + return _staticLib->GetSettings(); +} -const std::unique_ptr& BattleLibrary::GetMiscLibrary() const noexcept { return _miscLibrary; } +const BattleStatCalculator* BattleLibrary::GetStatCalculator() const noexcept { return _statCalculator; } + +const CreatureLib::Library::SpeciesLibrary* BattleLibrary::GetSpeciesLibrary() const noexcept { + return _staticLib->GetSpeciesLibrary(); +} + +const CreatureLib::Library::ItemLibrary* BattleLibrary::GetItemLibrary() const noexcept { + return _staticLib->GetItemLibrary(); +} + +const CreatureLib::Library::AttackLibrary* BattleLibrary::GetAttackLibrary() const noexcept { + return _staticLib->GetAttackLibrary(); +} + +const CreatureLib::Library::TypeLibrary* BattleLibrary::GetTypeLibrary() const noexcept { + return _staticLib->GetTypeLibrary(); +} + +const DamageLibrary* BattleLibrary::GetDamageLibrary() const noexcept { return _damageLibrary; } + +const MiscLibrary* BattleLibrary::GetMiscLibrary() const noexcept { return _miscLibrary; } Script* BattleLibrary::LoadScript(ScriptCategory category, const ConstString& scriptName) const { return _scriptResolver->LoadScript(category, scriptName); diff --git a/src/Battling/Library/BattleLibrary.hpp b/src/Battling/Library/BattleLibrary.hpp index 4396c67..cc5ecd3 100644 --- a/src/Battling/Library/BattleLibrary.hpp +++ b/src/Battling/Library/BattleLibrary.hpp @@ -1,7 +1,6 @@ #ifndef CREATURELIB_BATTLELIBRARY_HPP #define CREATURELIB_BATTLELIBRARY_HPP -#include #include "../../Library/DataLibrary.hpp" #include "../ScriptHandling/ScriptResolver.hpp" #include "BattleStatCalculator.hpp" @@ -13,45 +12,32 @@ namespace CreatureLib::Battling { class BattleLibrary { protected: const Library::DataLibrary* _staticLib = nullptr; - const std::unique_ptr _statCalculator; - const std::unique_ptr _damageLibrary = nullptr; - const std::unique_ptr _experienceLibrary = nullptr; - const std::unique_ptr _scriptResolver = nullptr; - const std::unique_ptr _miscLibrary = nullptr; + BattleStatCalculator* _statCalculator = nullptr; + DamageLibrary* _damageLibrary = nullptr; + ExperienceLibrary* _experienceLibrary = nullptr; + ScriptResolver* _scriptResolver = nullptr; + MiscLibrary* _miscLibrary = nullptr; public: - BattleLibrary(const Library::DataLibrary* staticLib, std::unique_ptr statCalculator, - std::unique_ptr damageLibrary, - std::unique_ptr experienceLibrary, - std::unique_ptr scriptResolver, std::unique_ptr miscLibrary); + BattleLibrary(const Library::DataLibrary* staticLib, BattleStatCalculator* statCalculator, + DamageLibrary* damageLibrary, ExperienceLibrary* experienceLibrary, + ScriptResolver* scriptResolver, MiscLibrary* miscLibrary); ~BattleLibrary(); inline const Library::DataLibrary* GetStaticLib() const noexcept { return _staticLib; } - [[nodiscard]] const std::unique_ptr& GetSettings() const noexcept { - return _staticLib->GetSettings(); - } - [[nodiscard]] const std::unique_ptr& GetSpeciesLibrary() const noexcept { - return _staticLib->GetSpeciesLibrary(); - } - [[nodiscard]] const std::unique_ptr& GetItemLibrary() const noexcept { - return _staticLib->GetItemLibrary(); - } - [[nodiscard]] const std::unique_ptr& GetAttackLibrary() const noexcept { - return _staticLib->GetAttackLibrary(); - } - [[nodiscard]] const std::unique_ptr& GetTypeLibrary() const noexcept { - return _staticLib->GetTypeLibrary(); - } - [[nodiscard]] const std::unique_ptr& GetGrowthRateLibrary() const noexcept { + [[nodiscard]] const Library::LibrarySettings* GetSettings() const noexcept; + [[nodiscard]] const Library::SpeciesLibrary* GetSpeciesLibrary() const noexcept; + [[nodiscard]] const Library::ItemLibrary* GetItemLibrary() const noexcept; + [[nodiscard]] const Library::AttackLibrary* GetAttackLibrary() const noexcept; + [[nodiscard]] const Library::TypeLibrary* GetTypeLibrary() const noexcept; + [[nodiscard]] const Library::GrowthRateLibrary* GetGrowthRateLibrary() const noexcept { return _staticLib->GetGrowthRates(); } - [[nodiscard]] const std::unique_ptr& GetStatCalculator() const noexcept; - [[nodiscard]] const std::unique_ptr& GetDamageLibrary() const noexcept; - [[nodiscard]] const std::unique_ptr& GetMiscLibrary() const noexcept; - [[nodiscard]] const std::unique_ptr& GetExperienceLibrary() const noexcept { - return _experienceLibrary; - } + [[nodiscard]] const BattleStatCalculator* GetStatCalculator() const noexcept; + [[nodiscard]] const DamageLibrary* GetDamageLibrary() const noexcept; + [[nodiscard]] const MiscLibrary* GetMiscLibrary() const noexcept; + [[nodiscard]] const ExperienceLibrary* GetExperienceLibrary() const noexcept { return _experienceLibrary; } [[nodiscard]] Script* LoadScript(ScriptCategory category, const ConstString& scriptName) const; }; diff --git a/src/Battling/Models/Creature.cpp b/src/Battling/Models/Creature.cpp index f98b3cf..b682aea 100644 --- a/src/Battling/Models/Creature.cpp +++ b/src/Battling/Models/Creature.cpp @@ -70,7 +70,7 @@ uint32_t Battling::Creature::GetBaseStat(Library::Statistic stat) const noexcept int8_t Battling::Creature::GetStatBoost(Library::Statistic stat) const noexcept { return _statBoost.GetStat(stat); } void Battling::Creature::RecalculateFlatStats() { - auto& statCalc = this->_library->GetStatCalculator(); + auto statCalc = this->_library->GetStatCalculator(); this->_flatStats = statCalc->CalculateFlatStats(this); RecalculateBoostedStats(); } diff --git a/src/Library/DataLibrary.cpp b/src/Library/DataLibrary.cpp index 9762449..eefd277 100644 --- a/src/Library/DataLibrary.cpp +++ b/src/Library/DataLibrary.cpp @@ -1,17 +1,40 @@ #include "DataLibrary.hpp" -CreatureLib::Library::DataLibrary::DataLibrary(std::unique_ptr settings, - std::unique_ptr species, - std::unique_ptr attacks, - std::unique_ptr items, - std::unique_ptr growthRates, - std::unique_ptr typeLibrary) - : _settings(std::move(settings)), _species(std::move(species)), _attacks(std::move(attacks)), - _items(std::move(items)), _growthRates(std::move(growthRates)), _typeLibrary(std::move(typeLibrary)) { +CreatureLib::Library::DataLibrary::DataLibrary(LibrarySettings* settings, CreatureLib::Library::SpeciesLibrary* species, + CreatureLib::Library::AttackLibrary* attacks, + CreatureLib::Library::ItemLibrary* items, + CreatureLib::Library::GrowthRateLibrary* growthRates, + TypeLibrary* typeLibrary) + : _settings(settings), _species(species), _attacks(attacks), _items(items), _growthRates(growthRates), + _typeLibrary(typeLibrary) { AssertNotNull(_settings) AssertNotNull(_species) AssertNotNull(_attacks) AssertNotNull(_items) AssertNotNull(_growthRates) AssertNotNull(_typeLibrary) -} \ No newline at end of file +} + +const CreatureLib::Library::LibrarySettings* CreatureLib::Library::DataLibrary::GetSettings() const noexcept { + return _settings; +} + +const CreatureLib::Library::SpeciesLibrary* CreatureLib::Library::DataLibrary::GetSpeciesLibrary() const noexcept { + return _species; +} + +const CreatureLib::Library::AttackLibrary* CreatureLib::Library::DataLibrary::GetAttackLibrary() const noexcept { + return _attacks; +} + +const CreatureLib::Library::ItemLibrary* CreatureLib::Library::DataLibrary::GetItemLibrary() const noexcept { + return _items; +} + +const CreatureLib::Library::GrowthRateLibrary* CreatureLib::Library::DataLibrary::GetGrowthRates() const noexcept { + return _growthRates; +} + +const CreatureLib::Library::TypeLibrary* CreatureLib::Library::DataLibrary::GetTypeLibrary() const noexcept { + return _typeLibrary; +} diff --git a/src/Library/DataLibrary.hpp b/src/Library/DataLibrary.hpp index fd9a283..1a2dc2b 100644 --- a/src/Library/DataLibrary.hpp +++ b/src/Library/DataLibrary.hpp @@ -1,7 +1,6 @@ #ifndef CREATURELIB_DATALIBRARY_HPP #define CREATURELIB_DATALIBRARY_HPP -#include #include "AttackLibrary.hpp" #include "GrowthRates/GrowthRateLibrary.hpp" #include "ItemLibrary.hpp" @@ -15,31 +14,32 @@ namespace CreatureLib::Library { */ class DataLibrary { private: - const std::unique_ptr _settings; - const std::unique_ptr _species; - const std::unique_ptr _attacks; - const std::unique_ptr _items; - const std::unique_ptr _growthRates; - const std::unique_ptr _typeLibrary; + const LibrarySettings* _settings; + const SpeciesLibrary* _species; + const AttackLibrary* _attacks; + const ItemLibrary* _items; + const GrowthRateLibrary* _growthRates; + const TypeLibrary* _typeLibrary; public: - DataLibrary(std::unique_ptr settings, std::unique_ptr species, - std::unique_ptr attacks, std::unique_ptr items, - std::unique_ptr growthRates, - std::unique_ptr typeLibrary); + DataLibrary(LibrarySettings* settings, CreatureLib::Library::SpeciesLibrary* species, + CreatureLib::Library::AttackLibrary* attacks, CreatureLib::Library::ItemLibrary* items, + CreatureLib::Library::GrowthRateLibrary* growthRates, TypeLibrary* typeLibrary); - virtual ~DataLibrary() {} + virtual ~DataLibrary() { + delete _species; + delete _attacks; + delete _items; + delete _growthRates; + delete _typeLibrary; + } - [[nodiscard]] const std::unique_ptr& GetSettings() const noexcept { return _settings; } - [[nodiscard]] const std::unique_ptr& GetSpeciesLibrary() const noexcept { - return _species; - } - [[nodiscard]] const std::unique_ptr& GetAttackLibrary() const noexcept { return _attacks; } - [[nodiscard]] const std::unique_ptr& GetItemLibrary() const noexcept { return _items; } - [[nodiscard]] const std::unique_ptr& GetGrowthRates() const noexcept { - return _growthRates; - } - [[nodiscard]] const std::unique_ptr& GetTypeLibrary() const noexcept { return _typeLibrary; } + [[nodiscard]] const LibrarySettings* GetSettings() const noexcept; + [[nodiscard]] const SpeciesLibrary* GetSpeciesLibrary() const noexcept; + [[nodiscard]] const AttackLibrary* GetAttackLibrary() const noexcept; + [[nodiscard]] const ItemLibrary* GetItemLibrary() const noexcept; + [[nodiscard]] const GrowthRateLibrary* GetGrowthRates() const noexcept; + [[nodiscard]] const TypeLibrary* GetTypeLibrary() const noexcept; }; } diff --git a/tests/BattleTests/TurnOrderTests.cpp b/tests/BattleTests/TurnOrderTests.cpp index f441ed7..5e5da82 100644 --- a/tests/BattleTests/TurnOrderTests.cpp +++ b/tests/BattleTests/TurnOrderTests.cpp @@ -29,7 +29,7 @@ TEST_CASE("Turn ordering: Attack before pass", "[Battling]") { } TEST_CASE("Turn ordering: High priority goes before no priority", "[Battling]") { - auto& l = TestLibrary::Get()->GetAttackLibrary(); + auto l = TestLibrary::Get()->GetAttackLibrary(); auto a1 = new LearnedAttack(l->Get("standard"_cnc), AttackLearnMethod::Unknown); auto a2 = new LearnedAttack(l->Get("highPriority"_cnc), AttackLearnMethod::Unknown); auto choice1 = new AttackTurnChoice(nullptr, a1, CreatureIndex(0, 0)); @@ -51,7 +51,7 @@ TEST_CASE("Turn ordering: High priority goes before no priority", "[Battling]") } TEST_CASE("Turn ordering: Higher priority goes before high priority", "[Battling]") { - auto& l = TestLibrary::Get()->GetAttackLibrary(); + auto l = TestLibrary::Get()->GetAttackLibrary(); auto a1 = new LearnedAttack(l->Get("highPriority"_cnc), AttackLearnMethod::Unknown); auto a2 = new LearnedAttack(l->Get("higherPriority"_cnc), AttackLearnMethod::Unknown); auto choice1 = new AttackTurnChoice(nullptr, a1, CreatureIndex(0, 0)); @@ -73,7 +73,7 @@ TEST_CASE("Turn ordering: Higher priority goes before high priority", "[Battling } TEST_CASE("Turn ordering: High priority goes before low priority", "[Battling]") { - auto& l = TestLibrary::Get()->GetAttackLibrary(); + auto l = TestLibrary::Get()->GetAttackLibrary(); auto a1 = new LearnedAttack(l->Get("lowPriority"_cnc), AttackLearnMethod::Unknown); auto a2 = new LearnedAttack(l->Get("higherPriority"_cnc), AttackLearnMethod::Unknown); auto choice1 = new AttackTurnChoice(nullptr, a1, CreatureIndex(0, 0)); @@ -95,7 +95,7 @@ TEST_CASE("Turn ordering: High priority goes before low priority", "[Battling]") } TEST_CASE("Turn ordering: No priority goes before low priority", "[Battling]") { - auto& l = TestLibrary::Get()->GetAttackLibrary(); + auto l = TestLibrary::Get()->GetAttackLibrary(); auto a1 = new LearnedAttack(l->Get("lowPriority"_cnc), AttackLearnMethod::Unknown); auto a2 = new LearnedAttack(l->Get("standard"_cnc), AttackLearnMethod::Unknown); auto choice1 = new AttackTurnChoice(nullptr, a1, CreatureIndex(0, 0)); diff --git a/tests/TestLibrary/TestLibrary.cpp b/tests/TestLibrary/TestLibrary.cpp index 6413ee3..cde89a4 100644 --- a/tests/TestLibrary/TestLibrary.cpp +++ b/tests/TestLibrary/TestLibrary.cpp @@ -9,14 +9,11 @@ BattleLibrary* TestLibrary::_library = nullptr; BattleLibrary* TestLibrary::Get() { if (TestLibrary::_library == nullptr) { - auto l = new DataLibrary( - std::make_unique(100, 4), std::unique_ptr(BuildSpeciesLibrary()), - std::unique_ptr(BuildAttackLibrary()), std::unique_ptr(BuildItemLibrary()), - std::unique_ptr(BuildGrowthRateLibrary()), - std::unique_ptr(BuildTypeLibrary())); - auto battleLib = new BattleLibrary(l, std::make_unique(), - std::make_unique(), std::make_unique(), - std::make_unique(), std::make_unique()); + auto l = new DataLibrary(new LibrarySettings(100, 4), BuildSpeciesLibrary(), BuildAttackLibrary(), + BuildItemLibrary(), BuildGrowthRateLibrary(), BuildTypeLibrary()); + auto statCalc = new BattleStatCalculator(); + auto battleLib = new BattleLibrary(l, statCalc, new DamageLibrary(), new ExperienceLibrary(), + new ScriptResolver(), new MiscLibrary()); TestLibrary::_library = battleLib; } return TestLibrary::_library;