Add a get_or_add method to Dictionary

Co-authored-by: Aaron Franke <arnfranke@yahoo.com>
This commit is contained in:
SysError99 2024-02-15 05:03:39 +07:00 committed by Relintai
parent da78ed110a
commit 487b454506
4 changed files with 20 additions and 0 deletions

View File

@ -117,6 +117,15 @@ Variant Dictionary::get(const Variant &p_key, const Variant &p_default) const {
return *result; return *result;
} }
Variant Dictionary::get_or_add(const Variant &p_key, const Variant &p_default) {
const Variant *result = getptr(p_key);
if (!result) {
operator[](p_key) = p_default;
return p_default;
}
return *result;
}
int Dictionary::size() const { int Dictionary::size() const {
return _p->variant_map.size(); return _p->variant_map.size();
} }

View File

@ -59,6 +59,7 @@ public:
Variant get_valid(const Variant &p_key) const; Variant get_valid(const Variant &p_key) const;
Variant get(const Variant &p_key, const Variant &p_default) const; Variant get(const Variant &p_key, const Variant &p_default) const;
Variant get_or_add(const Variant &p_key, const Variant &p_default);
int size() const; int size() const;
bool empty() const; bool empty() const;

View File

@ -870,6 +870,7 @@ struct _VariantCall {
VCALL_LOCALMEM0R(Dictionary, values); VCALL_LOCALMEM0R(Dictionary, values);
VCALL_LOCALMEM1R(Dictionary, duplicate); VCALL_LOCALMEM1R(Dictionary, duplicate);
VCALL_LOCALMEM2R(Dictionary, get); VCALL_LOCALMEM2R(Dictionary, get);
VCALL_LOCALMEM2R(Dictionary, get_or_add);
VCALL_LOCALMEM2(Array, set); VCALL_LOCALMEM2(Array, set);
VCALL_LOCALMEM1R(Array, get); VCALL_LOCALMEM1R(Array, get);
@ -2945,6 +2946,7 @@ void register_variant_methods() {
ADDFUNC0R(DICTIONARY, ARRAY, Dictionary, values, varray()); ADDFUNC0R(DICTIONARY, ARRAY, Dictionary, values, varray());
ADDFUNC1R(DICTIONARY, DICTIONARY, Dictionary, duplicate, BOOL, "deep", varray(false)); ADDFUNC1R(DICTIONARY, DICTIONARY, Dictionary, duplicate, BOOL, "deep", varray(false));
ADDFUNC2R(DICTIONARY, NIL, Dictionary, get, NIL, "key", NIL, "default", varray(Variant())); ADDFUNC2R(DICTIONARY, NIL, Dictionary, get, NIL, "key", NIL, "default", varray(Variant()));
ADDFUNC2R(DICTIONARY, NIL, Dictionary, get_or_add, NIL, "key", NIL, "default", varray(Variant()));
ADDFUNC0R(ARRAY, INT, Array, size, varray()); ADDFUNC0R(ARRAY, INT, Array, size, varray());
ADDFUNC0R(ARRAY, BOOL, Array, empty, varray()); ADDFUNC0R(ARRAY, BOOL, Array, empty, varray());

View File

@ -131,6 +131,14 @@
Returns the current value for the specified key in the [Dictionary]. If the key does not exist, the method returns the value of the optional default argument, or [code]null[/code] if it is omitted. Returns the current value for the specified key in the [Dictionary]. If the key does not exist, the method returns the value of the optional default argument, or [code]null[/code] if it is omitted.
</description> </description>
</method> </method>
<method name="get_or_add">
<return type="Variant" />
<argument index="0" name="key" type="Variant" />
<argument index="1" name="default" type="Variant" default="null" />
<description>
Gets a value and ensures the key is set. If the [code]key[/code] exists in the dictionary, this behaves like [method get]. Otherwise, the [code]default[/code] value is inserted into the dictionary and returned.
</description>
</method>
<method name="has"> <method name="has">
<return type="bool" /> <return type="bool" />
<argument index="0" name="key" type="Variant" /> <argument index="0" name="key" type="Variant" />