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) { \
ArbUt::BorrowedPtr<const returnType> o; \
auto v = p->TryGet(ArbUt::StringView::CalculateHash(name), o); \
out = o.GetRaw(); \
return v; \
auto v = p->TryGet(ArbUt::StringView::CalculateHash(name)); \
if (v.has_value()) { \
out = nullptr; \
return false; \
} else { \
out = v.value(); \
return true; \
} \
} \
\
export bool simpleName##_TryGetWithHash(fullname* p, uint32_t hashedKey, const returnType*& out) { \
ArbUt::BorrowedPtr<const returnType> o; \
auto v = p->TryGet(hashedKey, o); \
out = o.GetRaw(); \
return v; \
auto v = p->TryGet(hashedKey); \
if (v.has_value()) { \
out = nullptr; \
return false; \
} else { \
out = v.value(); \
return true; \
} \
} \
\
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;
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() << "'.");
}
heldItem = val.value();
}
auto experience = _library->GetGrowthRateLibrary()->CalculateExperience(species->GetGrowthRate(), _level);

View File

@ -265,18 +265,18 @@ namespace CreatureLib::Battling {
return variant;
}
void Creature::SetHeldItem(const ArbUt::BasicStringView& itemName) {
ArbUt::BorrowedPtr<const Library::Item> item;
if (!_library->GetItemLibrary()->TryGet(itemName.GetHash(), item)) {
auto v = _library->GetItemLibrary()->TryGet(itemName.GetHash());
if (!v.has_value()) {
THROW("Item not found '" << itemName.c_str() << "'.");
}
_heldItem = item;
_heldItem = v.value();
}
void Creature::SetHeldItem(uint32_t itemNameHash) {
ArbUt::BorrowedPtr<const Library::Item> item;
if (!_library->GetItemLibrary()->TryGet(itemNameHash, item)) {
auto v = _library->GetItemLibrary()->TryGet(itemNameHash);
if (!v.has_value()) {
THROW("Item not found.");
}
_heldItem = item;
_heldItem = v.value();
}
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 {
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);
if (find == _values.GetStdMap().end())
return false;
out = std::get<1>(*find);
return true;
return {};
return std::get<1>(*find);
}
[[nodiscard]] inline ArbUt::BorrowedPtr<const T> Get(const ArbUt::BasicStringView& name) const {