From 1f298cef08037d14d1283dd8777050038fac37ca Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 26 Jun 2021 13:36:21 +0200 Subject: [PATCH] Fixes for FindPreEvolution from C Interface --- CInterface/Library/SpeciesLibrary.cpp | 16 +++++++++++----- src/Library/Species/SpeciesLibrary.cpp | 12 ++++++++---- src/Library/Species/SpeciesLibrary.hpp | 2 ++ 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/CInterface/Library/SpeciesLibrary.cpp b/CInterface/Library/SpeciesLibrary.cpp index 763201c..cf5cdaf 100644 --- a/CInterface/Library/SpeciesLibrary.cpp +++ b/CInterface/Library/SpeciesLibrary.cpp @@ -2,10 +2,16 @@ #include "../Core.hpp" using namespace PkmnLib::Library; -export const PokemonSpecies* PkmnLib_SpeciesLibrary_FindPreEvolution(const SpeciesLibrary* p, +export const SpeciesLibrary* PkmnLib_SpeciesLibrary_Construct(size_t initialCapacity) { + return new SpeciesLibrary(initialCapacity); +}; + +export u8 PkmnLib_SpeciesLibrary_FindPreEvolution(const PokemonSpecies*& out, const SpeciesLibrary* p, const PokemonSpecies* species) { - auto v = p->FindPreEvolution(species); - if (!v.has_value()) - return nullptr; - return v.value().GetRaw(); + Try( + auto v = p->FindPreEvolution(species); + if (!v.has_value()) + out = nullptr; + out = v.value().GetRaw(); + ) } \ No newline at end of file diff --git a/src/Library/Species/SpeciesLibrary.cpp b/src/Library/Species/SpeciesLibrary.cpp index 3dbbf94..6c20560 100644 --- a/src/Library/Species/SpeciesLibrary.cpp +++ b/src/Library/Species/SpeciesLibrary.cpp @@ -2,15 +2,19 @@ namespace PkmnLib::Library { std::optional> SpeciesLibrary::FindPreEvolution(const ArbUt::BorrowedPtr& species) const noexcept { - if (_preEvolutionCache.Has(species)) { - return _preEvolutionCache[species]; + auto tryGet = _preEvolutionCache.TryGet(species); + if (tryGet.has_value()) { + return tryGet->get(); } for (const auto& s : _values) { - auto* pkmn = (PokemonSpecies*)s.second.get(); + const auto* pkmn = dynamic_cast(s.second.get()); + if (pkmn == nullptr){ + continue; + } for (const auto& evo : pkmn->GetEvolutions()) { if (evo->GetNewSpecies() == species) { auto* non_const = const_cast(this); - non_const->_preEvolutionCache[species] = pkmn; + non_const->_preEvolutionCache.Insert(species, pkmn); return pkmn; } } diff --git a/src/Library/Species/SpeciesLibrary.hpp b/src/Library/Species/SpeciesLibrary.hpp index a696109..a82825c 100644 --- a/src/Library/Species/SpeciesLibrary.hpp +++ b/src/Library/Species/SpeciesLibrary.hpp @@ -10,6 +10,8 @@ namespace PkmnLib::Library { _preEvolutionCache; public: + SpeciesLibrary(size_t initialCapacity = 32) : CreatureLib::Library::SpeciesLibrary(initialCapacity) {} + inline std::optional> TryGet(const ArbUt::BasicStringView& name) const { auto res = CreatureLib::Library::SpeciesLibrary::TryGet(name);