Use std::optional for BaseLibrary TryGet.
continuous-integration/drone/push Build is passing Details

Signed-off-by: Deukhoofd <Deukhoofd@gmail.com>
This commit is contained in:
Deukhoofd 2020-12-12 12:42:28 +01:00
parent 5c39694f19
commit 185ec40ba5
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
4 changed files with 28 additions and 17 deletions

View File

@ -17,16 +17,26 @@
\ \
export bool simpleName##_TryGet(fullname* p, const char* name, const returnType*& out) { \ export bool simpleName##_TryGet(fullname* p, const char* name, const returnType*& out) { \
ArbUt::BorrowedPtr<const returnType> o; \ ArbUt::BorrowedPtr<const returnType> o; \
auto v = p->TryGet(ArbUt::StringView::CalculateHash(name), o); \ auto v = p->TryGet(ArbUt::StringView::CalculateHash(name)); \
out = o.GetRaw(); \ if (v.has_value()) { \
return v; \ out = nullptr; \
return false; \
} else { \
out = v.value(); \
return true; \
} \
} \ } \
\ \
export bool simpleName##_TryGetWithHash(fullname* p, uint32_t hashedKey, const returnType*& out) { \ export bool simpleName##_TryGetWithHash(fullname* p, uint32_t hashedKey, const returnType*& out) { \
ArbUt::BorrowedPtr<const returnType> o; \ ArbUt::BorrowedPtr<const returnType> o; \
auto v = p->TryGet(hashedKey, o); \ auto v = p->TryGet(hashedKey); \
out = o.GetRaw(); \ if (v.has_value()) { \
return v; \ out = nullptr; \
return false; \
} else { \
out = v.value(); \
return true; \
} \
} \ } \
\ \
export uint8_t simpleName##_Get(fullname* p, const char* name, const returnType*& out) { \ export uint8_t simpleName##_Get(fullname* p, const char* name, const returnType*& out) { \

View File

@ -48,9 +48,11 @@ Creature* CreateCreature::Create() {
} }
ArbUt::BorrowedPtr<const Library::Item> heldItem; ArbUt::BorrowedPtr<const Library::Item> heldItem;
if (!this->_heldItem.IsEmpty()) { if (!this->_heldItem.IsEmpty()) {
if (!_library->GetItemLibrary()->TryGet(this->_heldItem.GetHash(), heldItem)) { auto val = _library->GetItemLibrary()->TryGet(this->_heldItem.GetHash());
if (!val.has_value()) {
THROW("Invalid held item '" << this->_heldItem.c_str() << "'."); THROW("Invalid held item '" << this->_heldItem.c_str() << "'.");
} }
heldItem = val.value();
} }
auto experience = _library->GetGrowthRateLibrary()->CalculateExperience(species->GetGrowthRate(), _level); auto experience = _library->GetGrowthRateLibrary()->CalculateExperience(species->GetGrowthRate(), _level);

View File

@ -265,18 +265,18 @@ namespace CreatureLib::Battling {
return variant; return variant;
} }
void Creature::SetHeldItem(const ArbUt::BasicStringView& itemName) { void Creature::SetHeldItem(const ArbUt::BasicStringView& itemName) {
ArbUt::BorrowedPtr<const Library::Item> item; auto v = _library->GetItemLibrary()->TryGet(itemName.GetHash());
if (!_library->GetItemLibrary()->TryGet(itemName.GetHash(), item)) { if (!v.has_value()) {
THROW("Item not found '" << itemName.c_str() << "'."); THROW("Item not found '" << itemName.c_str() << "'.");
} }
_heldItem = item; _heldItem = v.value();
} }
void Creature::SetHeldItem(uint32_t itemNameHash) { void Creature::SetHeldItem(uint32_t itemNameHash) {
ArbUt::BorrowedPtr<const Library::Item> item; auto v = _library->GetItemLibrary()->TryGet(itemNameHash);
if (!_library->GetItemLibrary()->TryGet(itemNameHash, item)) { if (!v.has_value()) {
THROW("Item not found."); THROW("Item not found.");
} }
_heldItem = item; _heldItem = v.value();
} }
void Creature::AddVolatileScript(const ArbUt::StringView& name) { void Creature::AddVolatileScript(const ArbUt::StringView& name) {

View File

@ -39,12 +39,11 @@ namespace CreatureLib::Library {
bool TryGet(const ArbUt::BasicStringView& name, ArbUt::BorrowedPtr<const T>& out) const noexcept { bool TryGet(const ArbUt::BasicStringView& name, ArbUt::BorrowedPtr<const T>& out) const noexcept {
return TryGet(name.GetHash(), out); return TryGet(name.GetHash(), out);
} }
bool TryGet(uint32_t hashedKey, ArbUt::BorrowedPtr<const T>& out) const noexcept { std::optional<ArbUt::BorrowedPtr<const T>> TryGet(uint32_t hashedKey) const noexcept {
auto find = _values.GetStdMap().find(hashedKey); auto find = _values.GetStdMap().find(hashedKey);
if (find == _values.GetStdMap().end()) if (find == _values.GetStdMap().end())
return false; return {};
out = std::get<1>(*find); return std::get<1>(*find);
return true;
} }
[[nodiscard]] inline ArbUt::BorrowedPtr<const T> Get(const ArbUt::BasicStringView& name) const { [[nodiscard]] inline ArbUt::BorrowedPtr<const T> Get(const ArbUt::BasicStringView& name) const {