Makes TryGetVariant use an std::optional
continuous-integration/drone/push Build is passing Details

Signed-off-by: Deukhoofd <Deukhoofd@gmail.com>
This commit is contained in:
Deukhoofd 2020-12-12 14:12:50 +01:00
parent 4367d1f5cf
commit 29d6632472
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
3 changed files with 25 additions and 23 deletions

View File

@ -31,17 +31,19 @@ export bool CreatureLib_CreatureSpecies_HasVariantWithHash(const CreatureSpecies
}
export bool CreatureLib_CreatureSpecies_TryGetVariant(const CreatureSpecies* p, const char* name,
const SpeciesVariant*& out) {
ArbUt::BorrowedPtr<const SpeciesVariant> o;
auto res = p->TryGetVariant(ArbUt::StringView::CalculateHash(name), o);
out = o.GetRaw();
return res;
auto res = p->TryGetVariant(ArbUt::StringView::CalculateHash(name));
if (!res.has_value())
return false;
out = res.value();
return true;
}
export bool CreatureLib_CreatureSpecies_TryGetVariantWithHash(const CreatureSpecies* p, uint32_t hash,
const SpeciesVariant*& out) {
ArbUt::BorrowedPtr<const SpeciesVariant> o;
auto res = p->TryGetVariant(hash, o);
out = o.GetRaw();
return res;
auto res = p->TryGetVariant(hash);
if (!res.has_value())
return false;
out = res.value();
return true;
}
export uint8_t CreatureLib_CreatureSpecies_GetVariant(const SpeciesVariant*& out, const CreatureSpecies* p,

View File

@ -32,16 +32,15 @@ struct CreatureSpecies::impl {
return _variantsLookup.Has(key);
}
[[nodiscard]] inline bool HasVariant(uint32_t hash) const noexcept { return _variantsLookup.Has(hash); }
[[nodiscard]] inline bool TryGetVariant(const ArbUt::BasicStringView& name,
ArbUt::BorrowedPtr<const SpeciesVariant>& out) const noexcept {
return TryGetVariant(name.GetHash(), out);
[[nodiscard]] inline std::optional<ArbUt::BorrowedPtr<const SpeciesVariant>>
TryGetVariant(const ArbUt::BasicStringView& name) const noexcept {
return TryGetVariant(name.GetHash());
}
[[nodiscard]] bool TryGetVariant(uint32_t hash, ArbUt::BorrowedPtr<const SpeciesVariant>& out) const noexcept {
[[nodiscard]] std::optional<ArbUt::BorrowedPtr<const SpeciesVariant>> TryGetVariant(uint32_t hash) const noexcept {
auto find = _variantsLookup.GetStdMap().find(hash);
if (find == _variantsLookup.end())
return false;
out = std::get<1>(*find);
return true;
return {};
return std::get<1>(*find);
}
[[nodiscard]] inline ArbUt::BorrowedPtr<const SpeciesVariant> GetVariant(const ArbUt::BasicStringView& key) const {
return _variantsLookup.Get(key);
@ -95,12 +94,12 @@ ImplGetter(const ArbUt::StringView&, GetName);
bool CreatureSpecies::HasVariant(const ArbUt::BasicStringView& key) const noexcept { return _impl->HasVariant(key); }
bool CreatureSpecies::HasVariant(uint32_t hash) const noexcept { return _impl->HasVariant(hash); }
bool CreatureSpecies::TryGetVariant(const ArbUt::BasicStringView& name,
ArbUt::BorrowedPtr<const SpeciesVariant>& out) const noexcept {
return _impl->TryGetVariant(name, out);
std::optional<ArbUt::BorrowedPtr<const SpeciesVariant>>
CreatureSpecies::TryGetVariant(const ArbUt::BasicStringView& name) const noexcept {
return _impl->TryGetVariant(name);
}
bool CreatureSpecies::TryGetVariant(uint32_t hash, ArbUt::BorrowedPtr<const SpeciesVariant>& out) const noexcept {
return _impl->TryGetVariant(hash, out);
std::optional<ArbUt::BorrowedPtr<const SpeciesVariant>> CreatureSpecies::TryGetVariant(uint32_t hash) const noexcept {
return _impl->TryGetVariant(hash);
}
ArbUt::BorrowedPtr<const SpeciesVariant> CreatureSpecies::GetVariant(const ArbUt::BasicStringView& key) const {
return _impl->GetVariant(key);

View File

@ -60,14 +60,15 @@ namespace CreatureLib::Library {
/// @param name The name of the variant that's being looked for.
/// @param out If a variant is found, it will be put in this variable. If not, this will remain unchanged.
/// @return True if the species contains the variant, false otherwise.
[[nodiscard]] bool TryGetVariant(const ArbUt::BasicStringView& name,
ArbUt::BorrowedPtr<const SpeciesVariant>& out) const noexcept;
[[nodiscard]] std::optional<ArbUt::BorrowedPtr<const SpeciesVariant>>
TryGetVariant(const ArbUt::BasicStringView& name) const noexcept;
/// @brief Try to get a variant of the species with a specific name.
/// @param name The string hash of the variant that's being looked for. This hash can be retrieved from the
/// StringView class.
/// @param out If a variant is found, it will be put in this variable. If not, this will remain unchanged.
/// @return True if the species contains the variant, false otherwise.
[[nodiscard]] bool TryGetVariant(uint32_t hash, ArbUt::BorrowedPtr<const SpeciesVariant>& out) const noexcept;
[[nodiscard]] std::optional<ArbUt::BorrowedPtr<const SpeciesVariant>>
TryGetVariant(uint32_t hash) const noexcept;
/// @brief Returns a variant with a specific name. Throws if no variant with the name exists.
/// @param key The name of the variant that's being looked for.
/// @return The specified variant.