Add Dictionary.merge()

(cherry picked from commit a0915e6dee)
This commit is contained in:
kobewi 2022-04-04 18:46:55 +02:00 committed by Rémi Verschelde
parent 7882b1ea05
commit 460fd7c03b
4 changed files with 18 additions and 0 deletions

View file

@ -197,6 +197,14 @@ void Dictionary::clear() {
_p->variant_map.clear();
}
void Dictionary::merge(const Dictionary &p_dictionary, bool p_overwrite) {
for (OrderedHashMap<Variant, Variant, VariantHasher, VariantComparator>::Element E = p_dictionary._p->variant_map.front(); E; E = E.next()) {
if (p_overwrite || !has(E.key())) {
this->operator[](E.key()) = E.value();
}
}
}
void Dictionary::_unref() const {
ERR_FAIL_COND(!_p);
if (_p->refcount.unref()) {

View file

@ -62,6 +62,7 @@ public:
int size() const;
bool empty() const;
void clear();
void merge(const Dictionary &p_dictionary, bool p_overwrite = false);
bool has(const Variant &p_key) const;
bool has_all(const Array &p_keys) const;

View file

@ -549,6 +549,7 @@ struct _VariantCall {
VCALL_LOCALMEM0R(Dictionary, size);
VCALL_LOCALMEM0R(Dictionary, empty);
VCALL_LOCALMEM0(Dictionary, clear);
VCALL_LOCALMEM2(Dictionary, merge);
VCALL_LOCALMEM1R(Dictionary, has);
VCALL_LOCALMEM1R(Dictionary, has_all);
VCALL_LOCALMEM1R(Dictionary, erase);
@ -1919,6 +1920,7 @@ void register_variant_methods() {
ADDFUNC0R(DICTIONARY, INT, Dictionary, size, varray());
ADDFUNC0R(DICTIONARY, BOOL, Dictionary, empty, varray());
ADDFUNC0NC(DICTIONARY, NIL, Dictionary, clear, varray());
ADDFUNC2NC(DICTIONARY, NIL, Dictionary, merge, DICTIONARY, "dictionary", BOOL, "overwrite", varray(false));
ADDFUNC1R(DICTIONARY, BOOL, Dictionary, has, NIL, "key", varray());
ADDFUNC1R(DICTIONARY, BOOL, Dictionary, has_all, ARRAY, "keys", varray());
ADDFUNC1RNC(DICTIONARY, BOOL, Dictionary, erase, NIL, "key", varray());

View file

@ -164,6 +164,13 @@
Returns the list of keys in the [Dictionary].
</description>
</method>
<method name="merge">
<argument index="0" name="dictionary" type="Dictionary" />
<argument index="1" name="overwrite" type="bool" default="false" />
<description>
Adds elements from [code]dictionary[/code] to this [Dictionary]. By default, duplicate keys will not be copied over, unless [code]overwrite[/code] is [code]true[/code].
</description>
</method>
<method name="size">
<return type="int" />
<description>