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"
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();
)
}

View File

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

View File

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