Fixes for FindPreEvolution from C Interface
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2021-06-26 13:36:21 +02:00
parent 94de55f933
commit 1f298cef08
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
3 changed files with 21 additions and 9 deletions

View File

@ -2,10 +2,16 @@
#include "../Core.hpp" #include "../Core.hpp"
using namespace PkmnLib::Library; 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) { const PokemonSpecies* species) {
Try(
auto v = p->FindPreEvolution(species); auto v = p->FindPreEvolution(species);
if (!v.has_value()) if (!v.has_value())
return nullptr; out = nullptr;
return v.value().GetRaw(); out = v.value().GetRaw();
)
} }

View File

@ -2,15 +2,19 @@
namespace PkmnLib::Library { namespace PkmnLib::Library {
std::optional<ArbUt::BorrowedPtr<const PokemonSpecies>> std::optional<ArbUt::BorrowedPtr<const PokemonSpecies>>
SpeciesLibrary::FindPreEvolution(const ArbUt::BorrowedPtr<const PokemonSpecies>& species) const noexcept { SpeciesLibrary::FindPreEvolution(const ArbUt::BorrowedPtr<const PokemonSpecies>& species) const noexcept {
if (_preEvolutionCache.Has(species)) { auto tryGet = _preEvolutionCache.TryGet(species);
return _preEvolutionCache[species]; if (tryGet.has_value()) {
return tryGet->get();
} }
for (const auto& s : _values) { for (const auto& s : _values) {
auto* pkmn = (PokemonSpecies*)s.second.get(); const auto* pkmn = dynamic_cast<const PokemonSpecies*>(s.second.get());
if (pkmn == nullptr){
continue;
}
for (const auto& evo : pkmn->GetEvolutions()) { for (const auto& evo : pkmn->GetEvolutions()) {
if (evo->GetNewSpecies() == species) { if (evo->GetNewSpecies() == species) {
auto* non_const = const_cast<SpeciesLibrary*>(this); auto* non_const = const_cast<SpeciesLibrary*>(this);
non_const->_preEvolutionCache[species] = pkmn; non_const->_preEvolutionCache.Insert(species, pkmn);
return pkmn; return pkmn;
} }
} }

View File

@ -10,6 +10,8 @@ namespace PkmnLib::Library {
_preEvolutionCache; _preEvolutionCache;
public: public:
SpeciesLibrary(size_t initialCapacity = 32) : CreatureLib::Library::SpeciesLibrary(initialCapacity) {}
inline std::optional<ArbUt::BorrowedPtr<const PokemonSpecies>> inline std::optional<ArbUt::BorrowedPtr<const PokemonSpecies>>
TryGet(const ArbUt::BasicStringView& name) const { TryGet(const ArbUt::BasicStringView& name) const {
auto res = CreatureLib::Library::SpeciesLibrary::TryGet(name); auto res = CreatureLib::Library::SpeciesLibrary::TryGet(name);