From 29d663247212513b5af47c5042529607a2a3976f Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 12 Dec 2020 14:12:50 +0100 Subject: [PATCH] Makes TryGetVariant use an std::optional Signed-off-by: Deukhoofd --- CInterface/Library/CreatureSpecies.cpp | 18 ++++++++------- src/Library/CreatureData/CreatureSpecies.cpp | 23 ++++++++++---------- src/Library/CreatureData/CreatureSpecies.hpp | 7 +++--- 3 files changed, 25 insertions(+), 23 deletions(-) diff --git a/CInterface/Library/CreatureSpecies.cpp b/CInterface/Library/CreatureSpecies.cpp index 2e0102c..93f2376 100644 --- a/CInterface/Library/CreatureSpecies.cpp +++ b/CInterface/Library/CreatureSpecies.cpp @@ -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 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 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, diff --git a/src/Library/CreatureData/CreatureSpecies.cpp b/src/Library/CreatureData/CreatureSpecies.cpp index 35cdad4..50e00ab 100644 --- a/src/Library/CreatureData/CreatureSpecies.cpp +++ b/src/Library/CreatureData/CreatureSpecies.cpp @@ -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& out) const noexcept { - return TryGetVariant(name.GetHash(), out); + [[nodiscard]] inline std::optional> + TryGetVariant(const ArbUt::BasicStringView& name) const noexcept { + return TryGetVariant(name.GetHash()); } - [[nodiscard]] bool TryGetVariant(uint32_t hash, ArbUt::BorrowedPtr& out) const noexcept { + [[nodiscard]] std::optional> 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 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& out) const noexcept { - return _impl->TryGetVariant(name, out); +std::optional> +CreatureSpecies::TryGetVariant(const ArbUt::BasicStringView& name) const noexcept { + return _impl->TryGetVariant(name); } -bool CreatureSpecies::TryGetVariant(uint32_t hash, ArbUt::BorrowedPtr& out) const noexcept { - return _impl->TryGetVariant(hash, out); +std::optional> CreatureSpecies::TryGetVariant(uint32_t hash) const noexcept { + return _impl->TryGetVariant(hash); } ArbUt::BorrowedPtr CreatureSpecies::GetVariant(const ArbUt::BasicStringView& key) const { return _impl->GetVariant(key); diff --git a/src/Library/CreatureData/CreatureSpecies.hpp b/src/Library/CreatureData/CreatureSpecies.hpp index d699a3e..895ea44 100644 --- a/src/Library/CreatureData/CreatureSpecies.hpp +++ b/src/Library/CreatureData/CreatureSpecies.hpp @@ -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& out) const noexcept; + [[nodiscard]] std::optional> + 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& out) const noexcept; + [[nodiscard]] std::optional> + 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.