From 95ef4dc84fde414130cf5993f9648bd5d213b362 Mon Sep 17 00:00:00 2001 From: Raul Santos Date: Tue, 24 Jan 2023 17:02:19 +0100 Subject: Sync C# Dictionary with Core - Implement `ICollection` methods explicitly. - Add `Merge` method. - Add `RecursiveEqual` method. - Update documentation. --- .../glue/GodotSharp/GodotSharp/Core/Dictionary.cs | 115 ++++++++++++++++++--- .../GodotSharp/Core/NativeInterop/NativeFuncs.cs | 4 + modules/mono/glue/runtime_interop.cpp | 10 ++ 3 files changed, 117 insertions(+), 12 deletions(-) (limited to 'modules/mono') diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Dictionary.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Dictionary.cs index b5a8742d3d..505763e6a2 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Dictionary.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Dictionary.cs @@ -68,7 +68,15 @@ namespace Godot.Collections } /// - /// Duplicates this . + /// Returns a copy of the . + /// If is , a deep copy is performed: + /// all nested arrays and dictionaries are duplicated and will not be shared with + /// the original dictionary. If , a shallow copy is made and + /// references to the original nested arrays and dictionaries are kept, so that + /// modifying a sub-array or dictionary in the copy will also impact those + /// referenced in the source dictionary. Note that any derived + /// elements will be shallow copied regardless of the + /// setting. /// /// If , performs a deep copy. /// A new Godot Dictionary. @@ -80,6 +88,39 @@ namespace Godot.Collections return CreateTakingOwnershipOfDisposableValue(newDictionary); } + /// + /// Adds entries from to this dictionary. + /// By default, duplicate keys are not copied over, unless + /// is . + /// + /// Dictionary to copy entries from. + /// If duplicate keys should be copied over as well. + public void Merge(Dictionary dictionary, bool overwrite = false) + { + var self = (godot_dictionary)NativeValue; + var other = (godot_dictionary)dictionary.NativeValue; + NativeFuncs.godotsharp_dictionary_merge(ref self, in other, overwrite.ToGodotBool()); + } + + /// + /// Compares this against the + /// recursively. Returns if the + /// two dictionaries contain the same keys and values. The order of the entries + /// does not matter. + /// otherwise. + /// + /// The other dictionary to compare against. + /// + /// if the dictionaries contain the same keys and values, + /// otherwise. + /// + public bool RecursiveEqual(Dictionary other) + { + var self = (godot_dictionary)NativeValue; + var otherVariant = (godot_dictionary)other.NativeValue; + return NativeFuncs.godotsharp_dictionary_recursive_equal(ref self, otherVariant).ToBool(); + } + // IDictionary /// @@ -134,6 +175,9 @@ namespace Godot.Collections /// /// Returns the value at the given . /// + /// + /// An entry for does not exist in the dictionary. + /// /// The value at the given . public Variant this[Variant key] { @@ -163,6 +207,9 @@ namespace Godot.Collections /// Adds an value at key /// to this . /// + /// + /// An entry for already exists in the dictionary. + /// /// The key at which to add the value. /// The value to add. public void Add(Variant key, Variant value) @@ -181,7 +228,7 @@ namespace Godot.Collections => Add(item.Key, item.Value); /// - /// Erases all items from this . + /// Clears the dictionary, removing all entries from it. /// public void Clear() { @@ -200,7 +247,7 @@ namespace Godot.Collections return NativeFuncs.godotsharp_dictionary_contains_key(ref self, (godot_variant)key.NativeVar).ToBool(); } - public bool Contains(KeyValuePair item) + bool ICollection>.Contains(KeyValuePair item) { godot_variant variantKey = (godot_variant)item.Key.NativeVar; var self = (godot_dictionary)NativeValue; @@ -227,7 +274,7 @@ namespace Godot.Collections return NativeFuncs.godotsharp_dictionary_remove_key(ref self, (godot_variant)key.NativeVar).ToBool(); } - public bool Remove(KeyValuePair item) + bool ICollection>.Remove(KeyValuePair item) { godot_variant variantKey = (godot_variant)item.Key.NativeVar; var self = (godot_dictionary)NativeValue; @@ -266,6 +313,14 @@ namespace Godot.Collections bool ICollection>.IsReadOnly => false; + /// + /// Gets the value for the given in the dictionary. + /// Returns if an entry for the given key exists in + /// the dictionary; otherwise, returns . + /// + /// The key of the element to get. + /// The value at the given . + /// If an entry was found for the given . public bool TryGetValue(Variant key, out Variant value) { var self = (godot_dictionary)NativeValue; @@ -283,7 +338,7 @@ namespace Godot.Collections /// /// The array to copy to. /// The index to start at. - public void CopyTo(KeyValuePair[] array, int arrayIndex) + void ICollection>.CopyTo(KeyValuePair[] array, int arrayIndex) { if (array == null) throw new ArgumentNullException(nameof(array), "Value cannot be null."); @@ -433,15 +488,49 @@ namespace Godot.Collections } /// - /// Duplicates this . + /// Returns a copy of the . + /// If is , a deep copy is performed: + /// all nested arrays and dictionaries are duplicated and will not be shared with + /// the original dictionary. If , a shallow copy is made and + /// references to the original nested arrays and dictionaries are kept, so that + /// modifying a sub-array or dictionary in the copy will also impact those + /// referenced in the source dictionary. Note that any derived + /// elements will be shallow copied regardless of the + /// setting. /// - /// If , performs a deep copy. - /// A new Godot Dictionary. public Dictionary Duplicate(bool deep = false) { return new Dictionary(_underlyingDict.Duplicate(deep)); } + /// + /// Adds entries from to this dictionary. + /// By default, duplicate keys are not copied over, unless + /// is . + /// + /// Dictionary to copy entries from. + /// If duplicate keys should be copied over as well. + public void Merge(Dictionary dictionary, bool overwrite = false) + { + _underlyingDict.Merge(dictionary._underlyingDict, overwrite); + } + + /// + /// Compares this against the + /// recursively. Returns if the + /// two dictionaries contain the same keys and values. The order of the entries does not matter. + /// otherwise. + /// + /// The other dictionary to compare against. + /// + /// if the dictionaries contain the same keys and values, + /// otherwise. + /// + public bool RecursiveEqual(Dictionary other) + { + return _underlyingDict.RecursiveEqual(other._underlyingDict); + } + // IDictionary /// @@ -565,11 +654,13 @@ namespace Godot.Collections } /// - /// Gets the object at the given . + /// Gets the value for the given in the dictionary. + /// Returns if an entry for the given key exists in + /// the dictionary; otherwise, returns . /// /// The key of the element to get. /// The value at the given . - /// If an object was found for the given . + /// If an entry was found for the given . public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value) { using var variantKey = VariantUtils.CreateFrom(key); @@ -598,7 +689,7 @@ namespace Godot.Collections => Add(item.Key, item.Value); /// - /// Erases all the items from this . + /// Clears the dictionary, removing all entries from it. /// public void Clear() => _underlyingDict.Clear(); @@ -625,7 +716,7 @@ namespace Godot.Collections /// /// The array to copy to. /// The index to start at. - public void CopyTo(KeyValuePair[] array, int arrayIndex) + void ICollection>.CopyTo(KeyValuePair[] array, int arrayIndex) { if (array == null) throw new ArgumentNullException(nameof(array), "Value cannot be null."); diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/NativeFuncs.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/NativeFuncs.cs index 57488bd586..53ca29d7dc 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/NativeFuncs.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/NativeFuncs.cs @@ -409,6 +409,10 @@ namespace Godot.NativeInterop public static partial void godotsharp_dictionary_duplicate(ref godot_dictionary p_self, godot_bool p_deep, out godot_dictionary r_dest); + public static partial void godotsharp_dictionary_merge(ref godot_dictionary p_self, in godot_dictionary p_dictionary, godot_bool p_overwrite); + + public static partial godot_bool godotsharp_dictionary_recursive_equal(ref godot_dictionary p_self, in godot_dictionary p_other); + public static partial godot_bool godotsharp_dictionary_remove_key(ref godot_dictionary p_self, in godot_variant p_key); diff --git a/modules/mono/glue/runtime_interop.cpp b/modules/mono/glue/runtime_interop.cpp index f0ea0313ea..4caeb76408 100644 --- a/modules/mono/glue/runtime_interop.cpp +++ b/modules/mono/glue/runtime_interop.cpp @@ -1066,6 +1066,14 @@ void godotsharp_dictionary_duplicate(const Dictionary *p_self, bool p_deep, Dict memnew_placement(r_dest, Dictionary(p_self->duplicate(p_deep))); } +void godotsharp_dictionary_merge(Dictionary *p_self, const Dictionary *p_dictionary, bool p_overwrite) { + p_self->merge(*p_dictionary, p_overwrite); +} + +bool godotsharp_dictionary_recursive_equal(const Dictionary *p_self, const Dictionary *p_other) { + return p_self->recursive_equal(*p_other, 0); +} + bool godotsharp_dictionary_remove_key(Dictionary *p_self, const Variant *p_key) { return p_self->erase(*p_key); } @@ -1455,6 +1463,8 @@ static const void *unmanaged_callbacks[]{ (void *)godotsharp_dictionary_clear, (void *)godotsharp_dictionary_contains_key, (void *)godotsharp_dictionary_duplicate, + (void *)godotsharp_dictionary_merge, + (void *)godotsharp_dictionary_recursive_equal, (void *)godotsharp_dictionary_remove_key, (void *)godotsharp_dictionary_to_string, (void *)godotsharp_string_simplify_path, -- cgit v1.2.3