From 2d3a2fc63b829c893afd481b404a0b3ce4f7882f Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Tue, 2 Jun 2020 20:37:21 +0200 Subject: [PATCH] Smart pointers for most library and battle classes. --- CInterface/Battling/ExperienceLibrary.cpp | 4 +-- CInterface/Battling/Pokemon.cpp | 11 ++++++-- CInterface/Library/EvolutionData.cpp | 6 ++-- CInterface/Library/NatureLibrary.cpp | 2 +- CInterface/Library/PokemonLibrary.cpp | 2 +- CInterface/Library/PokemonSpecies.cpp | 2 +- src/Battling/Battle/Battle.cpp | 5 ++-- src/Battling/Battle/Battle.hpp | 2 +- src/Battling/Library/BattleLibrary.hpp | 4 ++- src/Battling/Library/DamageLibrary.cpp | 2 +- src/Battling/Library/ExperienceLibrary.cpp | 9 +++--- src/Battling/Library/ExperienceLibrary.hpp | 5 ++-- src/Battling/Library/MiscLibrary.hpp | 20 ++++++------- src/Battling/Pokemon/CreatePokemon.cpp | 6 ++-- src/Battling/Pokemon/CreatePokemon.hpp | 2 +- src/Battling/Pokemon/Pokemon.hpp | 18 ++++++------ src/Battling/Pokemon/PokemonParty.hpp | 4 +-- src/Library/Evolutions/EvolutionData.hpp | 24 ++++++++-------- src/Library/Natures/NatureLibrary.hpp | 33 +++++++++------------- src/Library/PokemonLibrary.hpp | 6 ++-- src/Library/Species/PokemonSpecies.hpp | 11 +++----- tests/LibraryTests/SpeciesTests.cpp | 2 +- 22 files changed, 91 insertions(+), 89 deletions(-) diff --git a/CInterface/Battling/ExperienceLibrary.cpp b/CInterface/Battling/ExperienceLibrary.cpp index e8609f9..f0382a2 100644 --- a/CInterface/Battling/ExperienceLibrary.cpp +++ b/CInterface/Battling/ExperienceLibrary.cpp @@ -8,8 +8,8 @@ export uint8_t PkmnLib_ExperienceLibrary_HandleExperienceGain(ExperienceLibrary* CreatureLib::Battling::Creature* faintedMon, CreatureLib::Battling::Creature* const* opponents, size_t numberOfOpponents) { - Try(p->HandleExperienceGain( - faintedMon, std::unordered_set(opponents, opponents + numberOfOpponents));) + Try(p->HandleExperienceGain(faintedMon, std::unordered_set>( + opponents, opponents + numberOfOpponents));) } export void PkmnLib_ExperienceLibrary_Destruct(ExperienceLibrary* p) { delete p; } diff --git a/CInterface/Battling/Pokemon.cpp b/CInterface/Battling/Pokemon.cpp index 186fe09..fd1c293 100644 --- a/CInterface/Battling/Pokemon.cpp +++ b/CInterface/Battling/Pokemon.cpp @@ -12,7 +12,7 @@ export Pokemon* PkmnLib_Pokemon_Construct(const BattleLibrary* library, const Pk uint8_t spIv, uint8_t hpEv, uint8_t attEv, uint8_t defEv, uint8_t sAtEv, uint8_t sDeEv, uint8_t spEv, const PkmnLib::Library::Nature* nature) { std::string nick(nickname); - ArbUt::List cMoves(moves, moves + moveCount); + std::vector cMoves(moves, moves + moveCount); return new Pokemon(library, species, forme, level, experience, uid, gender, coloring, heldItem, nick, CreatureLib::Library::TalentIndex(hiddenAbility, abilityIndex), cMoves, @@ -24,9 +24,11 @@ export void PkmnLib_Pokemon_Destruct(const Pokemon* p) { delete p; } #define SIMPLE_GET_FUNC(type, name, returnType) \ export returnType PkmnLib_##type##_##name(const PkmnLib::Battling::type* p) { return p->name(); } +#define SIMPLE_GET_FUNC_SMART_PTR(type, name, returnType) \ + export returnType PkmnLib_##type##_##name(const PkmnLib::Battling::type* p) { return p->name().operator->(); } SIMPLE_GET_FUNC(Pokemon, IsShiny, bool) -SIMPLE_GET_FUNC(Pokemon, GetNature, const PkmnLib::Library::Nature*) +SIMPLE_GET_FUNC_SMART_PTR(Pokemon, GetNature, const PkmnLib::Library::Nature*) #undef SIMPLE_GET_FUNC @@ -35,4 +37,7 @@ export uint8_t PkmnLib_Pokemon_GetIndividualValue(const Pokemon* p, CreatureLib: } export uint8_t PkmnLib_Pokemon_GetEffortValue(const Pokemon* p, CreatureLib::Library::Statistic stat) { return p->GetEffortValue(stat); -} \ No newline at end of file +} + +#undef SIMPLE_GET_FUNC +#undef SIMPLE_GET_FUNC_SMART_PTR \ No newline at end of file diff --git a/CInterface/Library/EvolutionData.cpp b/CInterface/Library/EvolutionData.cpp index e6a0830..7a608b2 100644 --- a/CInterface/Library/EvolutionData.cpp +++ b/CInterface/Library/EvolutionData.cpp @@ -49,16 +49,16 @@ export const EvolutionData* PkmnLib_Evolution_CreateTradeWithSpeciesEvolution(co } export const EvolutionData* PkmnLib_Evolution_CreateCustomEvolution(const CreatureLib::Library::EffectParameter** data, size_t dataLength, const PokemonSpecies* into) { - auto list = ArbUt::List(data, data + dataLength); + auto list = std::vector(data, data + dataLength); return EvolutionData::CreateCustomEvolution(list, into); } export EvolutionMethod PkmnLib_Evolution_GetMethod(const EvolutionData* data) { return data->GetMethod(); } export const PokemonSpecies* PkmnLib_Evolution_GetNewSpecies(const EvolutionData* data) { - return data->GetNewSpecies(); + return data->GetNewSpecies().GetRaw(); } export size_t PkmnLib_Evolution_GetDataCount(const EvolutionData* data) { return data->GetDataCount(); } export uint8_t PkmnLib_Evolution_GetData(const EvolutionData* data, size_t index, const CreatureLib::Library::EffectParameter*& out) { - Try(out = data->GetData(index)); + Try(out = data->GetData(index).GetRaw()); } diff --git a/CInterface/Library/NatureLibrary.cpp b/CInterface/Library/NatureLibrary.cpp index 55f140e..b050c62 100644 --- a/CInterface/Library/NatureLibrary.cpp +++ b/CInterface/Library/NatureLibrary.cpp @@ -11,7 +11,7 @@ export uint8_t PkmnLib_NatureLibrary_LoadNature(NatureLibrary* p, const char* na Try(p->LoadNature(ArbUt::CaseInsensitiveConstString(name), nature);) } export uint8_t PkmnLib_NatureLibrary_GetNatureByName(NatureLibrary* p, const char* name, const Nature*& out) { - Try(out = p->GetNatureByName(ArbUt::CaseInsensitiveConstString(name))); + Try(out = p->GetNatureByName(ArbUt::CaseInsensitiveConstString(name)).GetRaw()); } export const char* PkmnLib_NatureLibrary_GetRandomNatureName(NatureLibrary* p, ArbUt::Random* rand) { diff --git a/CInterface/Library/PokemonLibrary.cpp b/CInterface/Library/PokemonLibrary.cpp index 3ac6f34..e4520b2 100644 --- a/CInterface/Library/PokemonLibrary.cpp +++ b/CInterface/Library/PokemonLibrary.cpp @@ -12,5 +12,5 @@ export uint8_t PkmnLib_PokemonLibrary_Construct(PokemonLibrary*& out, PkmnLib::L export void PkmnLib_PokemonLibrary_Destruct(const PokemonLibrary* p) { delete p; } export const NatureLibrary* PkmnLib_PokemonLibrary_GetNatureLibrary(const PokemonLibrary* p) { - return p->GetNatureLibrary(); + return p->GetNatureLibrary().GetRaw(); } \ No newline at end of file diff --git a/CInterface/Library/PokemonSpecies.cpp b/CInterface/Library/PokemonSpecies.cpp index 631f4c3..72bac22 100644 --- a/CInterface/Library/PokemonSpecies.cpp +++ b/CInterface/Library/PokemonSpecies.cpp @@ -24,5 +24,5 @@ export void PkmnLib_PokemonSpecies_AddEvolution(PokemonSpecies* p, EvolutionData export size_t PkmnLib_PokemonSpecies_GetEvolutionCount(const PokemonSpecies* p) { return p->GetEvolutions().Count(); } export uint8_t PkmnLib_PokemonSpecies_GetEvolution(const PokemonSpecies* p, size_t index, const EvolutionData*& out) { - Try(out = p->GetEvolutions().At(index)); + Try(out = p->GetEvolutions().At(index).GetRaw()); } \ No newline at end of file diff --git a/src/Battling/Battle/Battle.cpp b/src/Battling/Battle/Battle.cpp index f9d8f75..51f9488 100644 --- a/src/Battling/Battle/Battle.cpp +++ b/src/Battling/Battle/Battle.cpp @@ -2,14 +2,13 @@ void PkmnLib::Battling::Battle::SetWeather(const ArbUt::CaseInsensitiveConstString& name) { if (_weatherScript != nullptr) { _weatherScript->OnRemove(); - delete _weatherScript; } - _weatherScript = _library->LoadScript(static_cast(PkmnScriptCategory::Weather), name); + _weatherScript = std::unique_ptr( + _library->LoadScript(static_cast(PkmnScriptCategory::Weather), name)); _eventHook.TriggerEvent(new WeatherChangeEvent(name)); } void PkmnLib::Battling::Battle::ClearWeather() { _weatherScript->OnRemove(); - delete _weatherScript; _weatherScript = nullptr; _eventHook.TriggerEvent(new WeatherChangeEvent(""_cnc)); } diff --git a/src/Battling/Battle/Battle.hpp b/src/Battling/Battle/Battle.hpp index 903c24d..39c18f1 100644 --- a/src/Battling/Battle/Battle.hpp +++ b/src/Battling/Battle/Battle.hpp @@ -9,7 +9,7 @@ namespace PkmnLib::Battling { class Battle : public CreatureLib::Battling::Battle { private: - CreatureLib::Battling::Script* _weatherScript = nullptr; + std::unique_ptr _weatherScript = nullptr; public: Battle(const BattleLibrary* library, const ArbUt::List& parties, diff --git a/src/Battling/Library/BattleLibrary.hpp b/src/Battling/Library/BattleLibrary.hpp index 1fc7cdd..e998425 100644 --- a/src/Battling/Library/BattleLibrary.hpp +++ b/src/Battling/Library/BattleLibrary.hpp @@ -40,7 +40,9 @@ namespace PkmnLib::Battling { return reinterpret_cast&>(_staticLib->GetItemLibrary()); } - const Library::NatureLibrary* GetNatureLibrary() const { return GetStaticLib()->GetNatureLibrary(); } + const ArbUt::BorrowedPtr GetNatureLibrary() const { + return GetStaticLib()->GetNatureLibrary(); + } static CreatureLib::Battling::ScriptResolver* CreateScriptResolver(); }; diff --git a/src/Battling/Library/DamageLibrary.cpp b/src/Battling/Library/DamageLibrary.cpp index f76579e..e202fa2 100644 --- a/src/Battling/Library/DamageLibrary.cpp +++ b/src/Battling/Library/DamageLibrary.cpp @@ -28,7 +28,7 @@ float PkmnLib::Battling::DamageLibrary::GetStatModifier(CreatureLib::Battling::E // HOOK: allow overriding for which users stat we use. CreatureLib::Library::Statistic offensiveStat; CreatureLib::Library::Statistic defensiveStat; - auto learnedMove = static_cast(attack->GetAttack()); + auto learnedMove = attack->GetAttack().ForceAs(); auto moveData = learnedMove->GetMoveData(); if (moveData->GetCategory() == Library::MoveCategory::Physical) { offensiveStat = Library::Statistic::PhysicalAttack; diff --git a/src/Battling/Library/ExperienceLibrary.cpp b/src/Battling/Library/ExperienceLibrary.cpp index 6fb43e9..118bad6 100644 --- a/src/Battling/Library/ExperienceLibrary.cpp +++ b/src/Battling/Library/ExperienceLibrary.cpp @@ -3,20 +3,21 @@ #include "../PkmnScriptHook.hpp" #include "../Pokemon/Pokemon.hpp" -static inline uint32_t CalculateDynamicExperience(uint8_t level, float v1, CreatureLib::Battling::Creature* op, - CreatureLib::Battling::Creature* faintedMon) { +static inline uint32_t CalculateDynamicExperience(uint8_t level, float v1, + ArbUt::BorrowedPtr op, + ArbUt::BorrowedPtr faintedMon) { float a = 2 * level + 10; float b = level + op->GetLevel() + 10; float v2 = (a * a * sqrt(a)) / (b * b * sqrt(b)); uint32_t experienceGain = v1 * v2 + 1; // TODO: Check owner and international - PKMN_HOOK(ModifyExperienceGain, op, faintedMon, op, &experienceGain); + PKMN_HOOK(ModifyExperienceGain, op, faintedMon.GetRaw(), op.GetRaw(), &experienceGain); return experienceGain; } void PkmnLib::Battling::ExperienceLibrary::HandleExperienceGain( CreatureLib::Battling::Creature* faintedMon, - const std::unordered_set& opponents) const { + const std::unordered_set>& opponents) const { auto fainted = dynamic_cast(faintedMon); auto expGain = fainted->GetForme()->GetBaseExperience(); diff --git a/src/Battling/Library/ExperienceLibrary.hpp b/src/Battling/Library/ExperienceLibrary.hpp index 98e053f..1e61c54 100644 --- a/src/Battling/Library/ExperienceLibrary.hpp +++ b/src/Battling/Library/ExperienceLibrary.hpp @@ -5,8 +5,9 @@ namespace PkmnLib::Battling { class ExperienceLibrary : public CreatureLib::Battling::ExperienceLibrary { public: - void HandleExperienceGain(CreatureLib::Battling::Creature* faintedMon, - const std::unordered_set& opponents) const override; + void HandleExperienceGain( + CreatureLib::Battling::Creature* faintedMon, + const std::unordered_set>& opponents) const override; }; } diff --git a/src/Battling/Library/MiscLibrary.hpp b/src/Battling/Library/MiscLibrary.hpp index dec22d5..b365254 100644 --- a/src/Battling/Library/MiscLibrary.hpp +++ b/src/Battling/Library/MiscLibrary.hpp @@ -7,24 +7,22 @@ namespace PkmnLib::Battling { class MiscLibrary : public CreatureLib::Battling::MiscLibrary { - PkmnLib::Library::MoveData* _struggleData = - new PkmnLib::Library::MoveData("struggle"_cnc, 0, PkmnLib::Library::MoveCategory::Physical, 50, 255, 10, - CreatureLib::Library::AttackTarget::Any, 0, - new CreatureLib::Library::SecondaryEffect(-1, "struggle"_cnc, {}), {}); + std::unique_ptr _struggleData = std::make_unique( + "struggle"_cnc, 0, PkmnLib::Library::MoveCategory::Physical, 50, 255, 10, + CreatureLib::Library::AttackTarget::Any, 0, + new CreatureLib::Library::SecondaryEffect(-1, "struggle"_cnc, {}), std::unordered_set()); ; - CreatureLib::Battling::LearnedAttack* _struggle = - new PkmnLib::Battling::LearnedMove(_struggleData, CreatureLib::Battling::AttackLearnMethod::Unknown); + std::unique_ptr _struggle = + std::make_unique(_struggleData, + CreatureLib::Battling::AttackLearnMethod::Unknown); - [[nodiscard]] CreatureLib::Battling::LearnedAttack* GetReplacementAttack() const { + [[nodiscard]] ArbUt::BorrowedPtr GetReplacementAttack() const { _struggle->RestoreAllUses(); return _struggle; } public: - ~MiscLibrary() override { - delete _struggle; - delete _struggleData; - }; + ~MiscLibrary() override = default; bool IsCritical(CreatureLib::Battling::ExecutingAttack* attack, CreatureLib::Battling::Creature* target, uint8_t hit) const override; bool CanFlee(CreatureLib::Battling::FleeTurnChoice* switchChoice) const override; diff --git a/src/Battling/Pokemon/CreatePokemon.cpp b/src/Battling/Pokemon/CreatePokemon.cpp index 75df60e..a7ae631 100644 --- a/src/Battling/Pokemon/CreatePokemon.cpp +++ b/src/Battling/Pokemon/CreatePokemon.cpp @@ -74,14 +74,14 @@ PkmnLib::Battling::Pokemon* PkmnLib::Battling::CreatePokemon::Build() { } auto experience = _library->GetGrowthRateLibrary()->CalculateExperience(species->GetGrowthRate(), _level); - auto attacks = ArbUt::List(_attacks.Count()); + auto attacks = std::vector(_attacks.Count()); for (size_t i = 0; i < _attacks.Count(); i++) { auto& kv = _attacks[i]; auto move = kv.Move; if (move != nullptr) - attacks.Append(new LearnedMove(move, kv.LearnMethod)); + attacks[i] = new LearnedMove(move, kv.LearnMethod); else - attacks.Append(nullptr); + attacks[i] = nullptr; } auto ivs = CreatureLib::Library::StatisticSet(_ivHp, _ivAttack, _ivDefense, _ivSpAtt, _ivSpDef, _ivSpeed); auto evs = CreatureLib::Library::StatisticSet(_evHp, _evAttack, _evDefense, _evSpAtt, _evSpDef, _evSpeed); diff --git a/src/Battling/Pokemon/CreatePokemon.hpp b/src/Battling/Pokemon/CreatePokemon.hpp index 3baeb52..6df8d7b 100644 --- a/src/Battling/Pokemon/CreatePokemon.hpp +++ b/src/Battling/Pokemon/CreatePokemon.hpp @@ -6,7 +6,7 @@ namespace PkmnLib::Battling { class CreatePokemon { private: - const BattleLibrary* _library; + ArbUt::BorrowedPtr _library; ArbUt::CaseInsensitiveConstString _species = ""_cnc; ArbUt::CaseInsensitiveConstString _forme = "default"_cnc; uint8_t _level; diff --git a/src/Battling/Pokemon/Pokemon.hpp b/src/Battling/Pokemon/Pokemon.hpp index c757718..a6d60c3 100644 --- a/src/Battling/Pokemon/Pokemon.hpp +++ b/src/Battling/Pokemon/Pokemon.hpp @@ -13,21 +13,23 @@ namespace PkmnLib::Battling { CreatureLib::Library::StatisticSet _individualValues; CreatureLib::Library::StatisticSet _effortValues; - const PkmnLib::Library::Nature* _nature; + ArbUt::BorrowedPtr _nature; - const BattleLibrary* GetLibrary() const { return reinterpret_cast(_library); } + ArbUt::BorrowedPtr GetLibrary() const { return _library.ForceAs(); } public: - Pokemon(const BattleLibrary* library, const ArbUt::BorrowedPtr& species, + Pokemon(ArbUt::BorrowedPtr library, + const ArbUt::BorrowedPtr& species, const ArbUt::BorrowedPtr& forme, uint8_t level, uint32_t experience, uint32_t uid, CreatureLib::Library::Gender gender, uint8_t coloring, ArbUt::BorrowedPtr heldItem, const std::string& nickname, const CreatureLib::Library::TalentIndex& talent, - const ArbUt::List& moves, + const std::vector& moves, CreatureLib::Library::StatisticSet individualValues, - CreatureLib::Library::StatisticSet effortValues, const PkmnLib::Library::Nature* nature, - bool allowedExperienceGain = true) - : CreatureLib::Battling::Creature(library, species.As(), + CreatureLib::Library::StatisticSet effortValues, + ArbUt::BorrowedPtr nature, bool allowedExperienceGain = true) + : CreatureLib::Battling::Creature(library.ForceAs(), + species.ForceAs(), forme.As(), level, experience, uid, gender, coloring, heldItem.As(), nickname, talent, moves, allowedExperienceGain), @@ -43,7 +45,7 @@ namespace PkmnLib::Battling { return reinterpret_cast&>(_attacks); } - inline const Library::Nature* GetNature() const noexcept { return _nature; } + inline const ArbUt::BorrowedPtr& GetNature() const noexcept { return _nature; } inline uint8_t GetIndividualValue(CreatureLib::Library::Statistic stat) const { return _individualValues.GetStat(stat); } diff --git a/src/Battling/Pokemon/PokemonParty.hpp b/src/Battling/Pokemon/PokemonParty.hpp index 2ed8e4d..1ebb8f8 100644 --- a/src/Battling/Pokemon/PokemonParty.hpp +++ b/src/Battling/Pokemon/PokemonParty.hpp @@ -12,8 +12,8 @@ namespace PkmnLib::Battling { PokemonParty(std::initializer_list party) : CreatureLib::Battling::CreatureParty(party) {} - Pokemon* GetAtIndex(int index) const { - return dynamic_cast(CreatureLib::Battling::CreatureParty::GetAtIndex(index)); + ArbUt::BorrowedPtr GetAtIndex(int index) const { + return CreatureLib::Battling::CreatureParty::GetAtIndex(index).As(); } }; } diff --git a/src/Library/Evolutions/EvolutionData.hpp b/src/Library/Evolutions/EvolutionData.hpp index 31e1abf..cb652dd 100644 --- a/src/Library/Evolutions/EvolutionData.hpp +++ b/src/Library/Evolutions/EvolutionData.hpp @@ -2,12 +2,15 @@ #define PKMNLIB_EVOLUTIONDATA_HPP #include +#include +#include #include #include #include #include #include "../TimeOfDay.hpp" #include "EvolutionMethod.hpp" + namespace PkmnLib::Library { class PokemonSpecies; class MoveData; @@ -15,20 +18,16 @@ namespace PkmnLib::Library { class EvolutionData { private: - const PokemonSpecies* _evolvesInto; + ArbUt::BorrowedPtr _evolvesInto; EvolutionMethod _method; - ArbUt::List _evolutionData; + ArbUt::UniquePtrList _evolutionData; - EvolutionData(EvolutionMethod method, ArbUt::List data, + EvolutionData(EvolutionMethod method, std::vector data, const PokemonSpecies* next) : _evolvesInto(next), _method(method), _evolutionData(std::move(data)) {} public: - ~EvolutionData() { - for (auto v : _evolutionData) { - delete v; - } - } + ~EvolutionData() = default; static inline const EvolutionData* CreateLevelEvolution(uint8_t level, const PokemonSpecies* into) { return new EvolutionData(EvolutionMethod::Level, @@ -67,15 +66,18 @@ namespace PkmnLib::Library { static const EvolutionData* CreateTradeWithSpeciesEvolution(const PokemonSpecies* traded, const PokemonSpecies* into); static inline const EvolutionData* - CreateCustomEvolution(const ArbUt::List& data, + CreateCustomEvolution(const std::vector& data, const PokemonSpecies* into) { return new EvolutionData(EvolutionMethod::Custom, data, into); } - [[nodiscard]] inline const PokemonSpecies* GetNewSpecies() const noexcept { return _evolvesInto; } + [[nodiscard]] inline ArbUt::BorrowedPtr GetNewSpecies() const noexcept { + return _evolvesInto; + } [[nodiscard]] inline EvolutionMethod GetMethod() const noexcept { return _method; } [[nodiscard]] inline size_t GetDataCount() const noexcept { return _evolutionData.Count(); } - [[nodiscard]] inline const CreatureLib::Library::EffectParameter* GetData(size_t index) const { + [[nodiscard]] inline ArbUt::BorrowedPtr + GetData(size_t index) const { return _evolutionData.At(index); } }; diff --git a/src/Library/Natures/NatureLibrary.hpp b/src/Library/Natures/NatureLibrary.hpp index d781aec..d03b132 100644 --- a/src/Library/Natures/NatureLibrary.hpp +++ b/src/Library/Natures/NatureLibrary.hpp @@ -4,49 +4,44 @@ #include #include #include +#include #include #include #include #include #include "Nature.hpp" + namespace PkmnLib::Library { class NatureLibrary { private: - ArbUt::Dictionary _items; + std::unordered_map> _items; public: - explicit NatureLibrary(size_t size = 32) noexcept - : _items(ArbUt::Dictionary(size)) {} + explicit NatureLibrary(size_t size = 32) noexcept : _items(size) {} - ~NatureLibrary() { - for (auto& kv : _items) { - delete kv.second; - } - } + ~NatureLibrary() = default; inline void LoadNature(const ArbUt::CaseInsensitiveConstString& name, const Nature* nature) { - _items.Insert(name, nature); + _items.insert({name, std::unique_ptr(nature)}); } - inline const Nature* GetNatureByName(const ArbUt::CaseInsensitiveConstString& name) const { - return _items[name]; + inline ArbUt::BorrowedPtr GetNatureByName(const ArbUt::CaseInsensitiveConstString& name) const { + return _items.at(name); } inline const ArbUt::CaseInsensitiveConstString& GetRandomNatureName(ArbUt::Random rand = ArbUt::Random()) const { - auto i = rand.Get(_items.Count()); - auto& map = _items.GetStdMap(); - return std::next(std::begin(map), i)->first; + auto i = rand.Get(_items.size()); + return std::next(std::begin(_items), i)->first; } inline const ArbUt::CaseInsensitiveConstString& GetRandomNatureName(ArbUt::Random* rand) const { - auto i = rand->Get(_items.Count()); - auto& map = _items.GetStdMap(); - return std::next(std::begin(map), i)->first; + auto i = rand->Get(_items.size()); + return std::next(std::begin(_items), i)->first; } - inline ArbUt::CaseInsensitiveConstString GetNatureName(const Nature* nature) { + inline ArbUt::CaseInsensitiveConstString GetNatureName(ArbUt::BorrowedPtr nature) { for (const auto& v : _items) { - if (v.second == nature) { + if (v.second.get() == nature.GetRaw()) { return v.first; } } diff --git a/src/Library/PokemonLibrary.hpp b/src/Library/PokemonLibrary.hpp index 9f702d6..976c489 100644 --- a/src/Library/PokemonLibrary.hpp +++ b/src/Library/PokemonLibrary.hpp @@ -11,10 +11,10 @@ namespace PkmnLib::Library { class PokemonLibrary : public CreatureLib::Library::DataLibrary { private: - const NatureLibrary* _natures; + std::unique_ptr _natures; public: - ~PokemonLibrary() override { delete _natures; } + ~PokemonLibrary() override = default; PokemonLibrary(PkmnLib::Library::LibrarySettings* settings, SpeciesLibrary* species, MoveLibrary* moves, ItemLibrary* items, CreatureLib::Library::GrowthRateLibrary* growthRates, @@ -37,7 +37,7 @@ namespace PkmnLib::Library { return (const std::unique_ptr&)CreatureLib::Library::DataLibrary::GetItemLibrary(); } - [[nodiscard]] const NatureLibrary* GetNatureLibrary() const noexcept { return _natures; } + [[nodiscard]] ArbUt::BorrowedPtr GetNatureLibrary() const noexcept { return _natures; } }; } diff --git a/src/Library/Species/PokemonSpecies.hpp b/src/Library/Species/PokemonSpecies.hpp index a9ee7f7..d025601 100644 --- a/src/Library/Species/PokemonSpecies.hpp +++ b/src/Library/Species/PokemonSpecies.hpp @@ -1,5 +1,6 @@ #ifndef PKMNLIB_POKEMONSPECIES_HPP #define PKMNLIB_POKEMONSPECIES_HPP +#include #include #include "../Evolutions/EvolutionData.hpp" #include "PokemonForme.hpp" @@ -8,7 +9,7 @@ namespace PkmnLib::Library { class PokemonSpecies : public CreatureLib::Library::CreatureSpecies { private: uint8_t _baseHappiness; - ArbUt::List _evolutions; + ArbUt::UniquePtrList _evolutions; public: PokemonSpecies(uint16_t id, const ArbUt::CaseInsensitiveConstString& name, const PokemonForme* defaultForme, @@ -17,11 +18,7 @@ namespace PkmnLib::Library { : CreatureSpecies(id, name, defaultForme, genderRatio, growthRate, captureRate), _baseHappiness(baseHappiness) {} - ~PokemonSpecies() override { - for (auto evo : _evolutions) { - delete evo; - } - } + ~PokemonSpecies() override = default; inline uint8_t GetBaseHappiness() const { return _baseHappiness; } @@ -44,7 +41,7 @@ namespace PkmnLib::Library { } inline void AddEvolution(const EvolutionData* data) noexcept { _evolutions.Append(data); } - const ArbUt::List& GetEvolutions() const noexcept { return _evolutions; } + const ArbUt::UniquePtrList& GetEvolutions() const noexcept { return _evolutions; } }; } diff --git a/tests/LibraryTests/SpeciesTests.cpp b/tests/LibraryTests/SpeciesTests.cpp index 7233412..62e50d2 100644 --- a/tests/LibraryTests/SpeciesTests.cpp +++ b/tests/LibraryTests/SpeciesTests.cpp @@ -145,7 +145,7 @@ TEST_CASE("Able to set and get evolution", "library") { 0.5f, "testGrowthRate"_cnc, 100, 100); species->AddEvolution(PkmnLib::Library::EvolutionData::CreateLevelEvolution(16, species2)); - auto evolutions = species->GetEvolutions(); + auto& evolutions = species->GetEvolutions(); REQUIRE(evolutions.Count() == 1); auto evo = evolutions[0]; CHECK(evo->GetMethod() == PkmnLib::Library::EvolutionMethod::Level);