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, export bool CreatureLib_CreatureSpecies_TryGetVariant(const CreatureSpecies* p, const char* name,
const SpeciesVariant*& out) { const SpeciesVariant*& out) {
ArbUt::BorrowedPtr<const SpeciesVariant> o; auto res = p->TryGetVariant(ArbUt::StringView::CalculateHash(name));
auto res = p->TryGetVariant(ArbUt::StringView::CalculateHash(name), o); if (!res.has_value())
out = o.GetRaw(); return false;
return res; out = res.value();
return true;
} }
export bool CreatureLib_CreatureSpecies_TryGetVariantWithHash(const CreatureSpecies* p, uint32_t hash, export bool CreatureLib_CreatureSpecies_TryGetVariantWithHash(const CreatureSpecies* p, uint32_t hash,
const SpeciesVariant*& out) { const SpeciesVariant*& out) {
ArbUt::BorrowedPtr<const SpeciesVariant> o; auto res = p->TryGetVariant(hash);
auto res = p->TryGetVariant(hash, o); if (!res.has_value())
out = o.GetRaw(); return false;
return res; out = res.value();
return true;
} }
export uint8_t CreatureLib_CreatureSpecies_GetVariant(const SpeciesVariant*& out, const CreatureSpecies* p, 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); return _variantsLookup.Has(key);
} }
[[nodiscard]] inline bool HasVariant(uint32_t hash) const noexcept { return _variantsLookup.Has(hash); } [[nodiscard]] inline bool HasVariant(uint32_t hash) const noexcept { return _variantsLookup.Has(hash); }
[[nodiscard]] inline bool TryGetVariant(const ArbUt::BasicStringView& name, [[nodiscard]] inline std::optional<ArbUt::BorrowedPtr<const SpeciesVariant>>
ArbUt::BorrowedPtr<const SpeciesVariant>& out) const noexcept { TryGetVariant(const ArbUt::BasicStringView& name) const noexcept {
return TryGetVariant(name.GetHash(), out); 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); auto find = _variantsLookup.GetStdMap().find(hash);
if (find == _variantsLookup.end()) if (find == _variantsLookup.end())
return false; return {};
out = std::get<1>(*find); return std::get<1>(*find);
return true;
} }
[[nodiscard]] inline ArbUt::BorrowedPtr<const SpeciesVariant> GetVariant(const ArbUt::BasicStringView& key) const { [[nodiscard]] inline ArbUt::BorrowedPtr<const SpeciesVariant> GetVariant(const ArbUt::BasicStringView& key) const {
return _variantsLookup.Get(key); 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(const ArbUt::BasicStringView& key) const noexcept { return _impl->HasVariant(key); }
bool CreatureSpecies::HasVariant(uint32_t hash) const noexcept { return _impl->HasVariant(hash); } bool CreatureSpecies::HasVariant(uint32_t hash) const noexcept { return _impl->HasVariant(hash); }
bool CreatureSpecies::TryGetVariant(const ArbUt::BasicStringView& name, std::optional<ArbUt::BorrowedPtr<const SpeciesVariant>>
ArbUt::BorrowedPtr<const SpeciesVariant>& out) const noexcept { CreatureSpecies::TryGetVariant(const ArbUt::BasicStringView& name) const noexcept {
return _impl->TryGetVariant(name, out); return _impl->TryGetVariant(name);
} }
bool CreatureSpecies::TryGetVariant(uint32_t hash, ArbUt::BorrowedPtr<const SpeciesVariant>& out) const noexcept { std::optional<ArbUt::BorrowedPtr<const SpeciesVariant>> CreatureSpecies::TryGetVariant(uint32_t hash) const noexcept {
return _impl->TryGetVariant(hash, out); return _impl->TryGetVariant(hash);
} }
ArbUt::BorrowedPtr<const SpeciesVariant> CreatureSpecies::GetVariant(const ArbUt::BasicStringView& key) const { ArbUt::BorrowedPtr<const SpeciesVariant> CreatureSpecies::GetVariant(const ArbUt::BasicStringView& key) const {
return _impl->GetVariant(key); 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 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. /// @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. /// @return True if the species contains the variant, false otherwise.
[[nodiscard]] bool TryGetVariant(const ArbUt::BasicStringView& name, [[nodiscard]] std::optional<ArbUt::BorrowedPtr<const SpeciesVariant>>
ArbUt::BorrowedPtr<const SpeciesVariant>& out) const noexcept; TryGetVariant(const ArbUt::BasicStringView& name) const noexcept;
/// @brief Try to get a variant of the species with a specific name. /// @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 /// @param name The string hash of the variant that's being looked for. This hash can be retrieved from the
/// StringView class. /// StringView class.
/// @param out If a variant is found, it will be put in this variable. If not, this will remain unchanged. /// @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. /// @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. /// @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. /// @param key The name of the variant that's being looked for.
/// @return The specified variant. /// @return The specified variant.