mirror of
https://github.com/VCMP-SqMod/SqMod.git
synced 2024-11-08 00:37:15 +01:00
Add retrieval with fall-back.
This commit is contained in:
parent
812cbcf332
commit
9e0071567e
@ -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)
|
||||
|
@ -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.
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user