1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 08:47:17 +01:00

Add retrieval with fall-back.

This commit is contained in:
Sandu Liviu Catalin 2021-07-04 03:41:53 +03:00
parent 812cbcf332
commit 9e0071567e
2 changed files with 14 additions and 0 deletions

View File

@ -23,6 +23,7 @@ void Register_Dictionary(HSQUIRRELVM vm, Table & ns)
// Member Methods
.Func(_SC("Get"), &SqDictionary::Get)
.Func(_SC("Set"), &SqDictionary::Set)
.Func(_SC("GetOr"), &SqDictionary::GetOr)
.Func(_SC("Clear"), &SqDictionary::Clear)
.Func(_SC("Erase"), &SqDictionary::Erase)
.Func(_SC("Contains"), &SqDictionary::Contains)

View File

@ -150,6 +150,19 @@ struct SqDictionary
SQ_UNREACHABLE
}
/* --------------------------------------------------------------------------------------------
* Retrieve a value from the container or a fall-back if it doesn't exist.
*/
SQMOD_NODISCARD LightObj & GetOr(SqKeyHash k, LightObj & v)
{
for (auto & e : mC)
{
if (e.first == k.mH) return e.second;
}
// Use fall-back
return v;
}
/* --------------------------------------------------------------------------------------------
* Modify a value from the container.
*/