Add documentation to Dictionary in C#

Adds documentation to `Godot.Collections.Dictionary` in C#.

(cherry picked from commit 0669ffcd15)
This commit is contained in:
Raul Santos 2021-07-28 15:44:09 +02:00 committed by Rémi Verschelde
parent 320579b9fd
commit ca32c18458
No known key found for this signature in database
GPG Key ID: C3336907360768E1
1 changed files with 168 additions and 20 deletions

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Collections; using System.Collections;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Diagnostics.CodeAnalysis;
namespace Godot.Collections namespace Godot.Collections
{ {
@ -25,6 +26,11 @@ namespace Godot.Collections
} }
} }
/// <summary>
/// Wrapper around Godot's Dictionary class, a dictionary of Variant
/// typed elements allocated in the engine in C++. Useful when
/// interfacing with the engine.
/// </summary>
public class Dictionary : public class Dictionary :
IDictionary, IDictionary,
IDisposable IDisposable
@ -32,11 +38,19 @@ namespace Godot.Collections
DictionarySafeHandle safeHandle; DictionarySafeHandle safeHandle;
bool disposed = false; bool disposed = false;
/// <summary>
/// Constructs a new empty <see cref="Dictionary"/>.
/// </summary>
public Dictionary() public Dictionary()
{ {
safeHandle = new DictionarySafeHandle(godot_icall_Dictionary_Ctor()); safeHandle = new DictionarySafeHandle(godot_icall_Dictionary_Ctor());
} }
/// <summary>
/// Constructs a new <see cref="Dictionary"/> from the given dictionary's elements.
/// </summary>
/// <param name="dictionary">The dictionary to construct from.</param>
/// <returns>A new Godot Dictionary.</returns>
public Dictionary(IDictionary dictionary) : this() public Dictionary(IDictionary dictionary) : this()
{ {
if (dictionary == null) if (dictionary == null)
@ -64,6 +78,9 @@ namespace Godot.Collections
return safeHandle.DangerousGetHandle(); return safeHandle.DangerousGetHandle();
} }
/// <summary>
/// Disposes of this <see cref="Dictionary"/>.
/// </summary>
public void Dispose() public void Dispose()
{ {
if (disposed) if (disposed)
@ -78,6 +95,11 @@ namespace Godot.Collections
disposed = true; disposed = true;
} }
/// <summary>
/// Duplicates this <see cref="Dictionary"/>.
/// </summary>
/// <param name="deep">If <see langword="true"/>, performs a deep copy.</param>
/// <returns>A new Godot Dictionary.</returns>
public Dictionary Duplicate(bool deep = false) public Dictionary Duplicate(bool deep = false)
{ {
return new Dictionary(godot_icall_Dictionary_Duplicate(GetPtr(), deep)); return new Dictionary(godot_icall_Dictionary_Duplicate(GetPtr(), deep));
@ -85,6 +107,9 @@ namespace Godot.Collections
// IDictionary // IDictionary
/// <summary>
/// Gets the collection of keys in this <see cref="Dictionary"/>.
/// </summary>
public ICollection Keys public ICollection Keys
{ {
get get
@ -94,6 +119,9 @@ namespace Godot.Collections
} }
} }
/// <summary>
/// Gets the collection of elements in this <see cref="Dictionary"/>.
/// </summary>
public ICollection Values public ICollection Values
{ {
get get
@ -103,34 +131,71 @@ namespace Godot.Collections
} }
} }
public bool IsFixedSize => false; bool IDictionary.IsFixedSize => false;
public bool IsReadOnly => false; bool IDictionary.IsReadOnly => false;
/// <summary>
/// Returns the object at the given <paramref name="key"/>.
/// </summary>
/// <value>The object at the given <paramref name="key"/>.</value>
public object this[object key] public object this[object key]
{ {
get => godot_icall_Dictionary_GetValue(GetPtr(), key); get => godot_icall_Dictionary_GetValue(GetPtr(), key);
set => godot_icall_Dictionary_SetValue(GetPtr(), key, value); set => godot_icall_Dictionary_SetValue(GetPtr(), key, value);
} }
/// <summary>
/// Adds an object <paramref name="value"/> at key <paramref name="key"/>
/// to this <see cref="Dictionary"/>.
/// </summary>
/// <param name="key">The key at which to add the object.</param>
/// <param name="value">The object to add.</param>
public void Add(object key, object value) => godot_icall_Dictionary_Add(GetPtr(), key, value); public void Add(object key, object value) => godot_icall_Dictionary_Add(GetPtr(), key, value);
/// <summary>
/// Erases all items from this <see cref="Dictionary"/>.
/// </summary>
public void Clear() => godot_icall_Dictionary_Clear(GetPtr()); public void Clear() => godot_icall_Dictionary_Clear(GetPtr());
/// <summary>
/// Checks if this <see cref="Dictionary"/> contains the given key.
/// </summary>
/// <param name="key">The key to look for.</param>
/// <returns>Whether or not this dictionary contains the given key.</returns>
public bool Contains(object key) => godot_icall_Dictionary_ContainsKey(GetPtr(), key); public bool Contains(object key) => godot_icall_Dictionary_ContainsKey(GetPtr(), key);
/// <summary>
/// Gets an enumerator for this <see cref="Dictionary"/>.
/// </summary>
/// <returns>An enumerator.</returns>
public IDictionaryEnumerator GetEnumerator() => new DictionaryEnumerator(this); public IDictionaryEnumerator GetEnumerator() => new DictionaryEnumerator(this);
/// <summary>
/// Removes an element from this <see cref="Dictionary"/> by key.
/// </summary>
/// <param name="key">The key of the element to remove.</param>
public void Remove(object key) => godot_icall_Dictionary_RemoveKey(GetPtr(), key); public void Remove(object key) => godot_icall_Dictionary_RemoveKey(GetPtr(), key);
// ICollection // ICollection
public object SyncRoot => this; object ICollection.SyncRoot => this;
public bool IsSynchronized => false; bool ICollection.IsSynchronized => false;
/// <summary>
/// Returns the number of elements in this <see cref="Dictionary"/>.
/// This is also known as the size or length of the dictionary.
/// </summary>
/// <returns>The number of elements.</returns>
public int Count => godot_icall_Dictionary_Count(GetPtr()); public int Count => godot_icall_Dictionary_Count(GetPtr());
/// <summary>
/// Copies the elements of this <see cref="Dictionary"/> to the given
/// untyped C# array, starting at the given index.
/// </summary>
/// <param name="array">The array to copy to.</param>
/// <param name="index">The index to start at.</param>
public void CopyTo(System.Array array, int index) public void CopyTo(System.Array array, int index)
{ {
// TODO Can be done with single internal call // TODO Can be done with single internal call
@ -161,10 +226,10 @@ namespace Godot.Collections
private class DictionaryEnumerator : IDictionaryEnumerator private class DictionaryEnumerator : IDictionaryEnumerator
{ {
Array keys; private readonly Array keys;
Array values; private readonly Array values;
int count; private readonly int count;
int index = -1; private int index = -1;
public DictionaryEnumerator(Dictionary dictionary) public DictionaryEnumerator(Dictionary dictionary)
{ {
@ -196,6 +261,10 @@ namespace Godot.Collections
} }
} }
/// <summary>
/// Converts this <see cref="Dictionary"/> to a string.
/// </summary>
/// <returns>A string representation of this dictionary.</returns>
public override string ToString() public override string ToString()
{ {
return godot_icall_Dictionary_ToString(GetPtr()); return godot_icall_Dictionary_ToString(GetPtr());
@ -259,10 +328,18 @@ namespace Godot.Collections
internal extern static string godot_icall_Dictionary_ToString(IntPtr ptr); internal extern static string godot_icall_Dictionary_ToString(IntPtr ptr);
} }
/// <summary>
/// Typed wrapper around Godot's Dictionary class, a dictionary of Variant
/// typed elements allocated in the engine in C++. Useful when
/// interfacing with the engine. Otherwise prefer .NET collections
/// such as <see cref="System.Collections.Generic.Dictionary{TKey, TValue}"/>.
/// </summary>
/// <typeparam name="TKey">The type of the dictionary's keys.</typeparam>
/// <typeparam name="TValue">The type of the dictionary's values.</typeparam>
public class Dictionary<TKey, TValue> : public class Dictionary<TKey, TValue> :
IDictionary<TKey, TValue> IDictionary<TKey, TValue>
{ {
Dictionary objectDict; private readonly Dictionary objectDict;
internal static int valTypeEncoding; internal static int valTypeEncoding;
internal static IntPtr valTypeClass; internal static IntPtr valTypeClass;
@ -272,11 +349,19 @@ namespace Godot.Collections
Dictionary.godot_icall_Dictionary_Generic_GetValueTypeInfo(typeof(TValue), out valTypeEncoding, out valTypeClass); Dictionary.godot_icall_Dictionary_Generic_GetValueTypeInfo(typeof(TValue), out valTypeEncoding, out valTypeClass);
} }
/// <summary>
/// Constructs a new empty <see cref="Dictionary{TKey, TValue}"/>.
/// </summary>
public Dictionary() public Dictionary()
{ {
objectDict = new Dictionary(); objectDict = new Dictionary();
} }
/// <summary>
/// Constructs a new <see cref="Dictionary{TKey, TValue}"/> from the given dictionary's elements.
/// </summary>
/// <param name="dictionary">The dictionary to construct from.</param>
/// <returns>A new Godot Dictionary.</returns>
public Dictionary(IDictionary<TKey, TValue> dictionary) public Dictionary(IDictionary<TKey, TValue> dictionary)
{ {
objectDict = new Dictionary(); objectDict = new Dictionary();
@ -294,6 +379,11 @@ namespace Godot.Collections
} }
} }
/// <summary>
/// Constructs a new <see cref="Dictionary{TKey, TValue}"/> from the given dictionary's elements.
/// </summary>
/// <param name="dictionary">The dictionary to construct from.</param>
/// <returns>A new Godot Dictionary.</returns>
public Dictionary(Dictionary dictionary) public Dictionary(Dictionary dictionary)
{ {
objectDict = dictionary; objectDict = dictionary;
@ -309,6 +399,10 @@ namespace Godot.Collections
objectDict = new Dictionary(handle); objectDict = new Dictionary(handle);
} }
/// <summary>
/// Converts this typed <see cref="Dictionary{TKey, TValue}"/> to an untyped <see cref="Dictionary"/>.
/// </summary>
/// <param name="from">The typed dictionary to convert.</param>
public static explicit operator Dictionary(Dictionary<TKey, TValue> from) public static explicit operator Dictionary(Dictionary<TKey, TValue> from)
{ {
return from.objectDict; return from.objectDict;
@ -319,6 +413,11 @@ namespace Godot.Collections
return objectDict.GetPtr(); return objectDict.GetPtr();
} }
/// <summary>
/// Duplicates this <see cref="Dictionary{TKey, TValue}"/>.
/// </summary>
/// <param name="deep">If <see langword="true"/>, performs a deep copy.</param>
/// <returns>A new Godot Dictionary.</returns>
public Dictionary<TKey, TValue> Duplicate(bool deep = false) public Dictionary<TKey, TValue> Duplicate(bool deep = false)
{ {
return new Dictionary<TKey, TValue>(objectDict.Duplicate(deep)); return new Dictionary<TKey, TValue>(objectDict.Duplicate(deep));
@ -326,12 +425,19 @@ namespace Godot.Collections
// IDictionary<TKey, TValue> // IDictionary<TKey, TValue>
/// <summary>
/// Returns the value at the given <paramref name="key"/>.
/// </summary>
/// <value>The value at the given <paramref name="key"/>.</value>
public TValue this[TKey key] public TValue this[TKey key]
{ {
get { return (TValue)Dictionary.godot_icall_Dictionary_GetValue_Generic(objectDict.GetPtr(), key, valTypeEncoding, valTypeClass); } get { return (TValue)Dictionary.godot_icall_Dictionary_GetValue_Generic(objectDict.GetPtr(), key, valTypeEncoding, valTypeClass); }
set { objectDict[key] = value; } set { objectDict[key] = value; }
} }
/// <summary>
/// Gets the collection of keys in this <see cref="Dictionary{TKey, TValue}"/>.
/// </summary>
public ICollection<TKey> Keys public ICollection<TKey> Keys
{ {
get get
@ -341,6 +447,9 @@ namespace Godot.Collections
} }
} }
/// <summary>
/// Gets the collection of elements in this <see cref="Dictionary{TKey, TValue}"/>.
/// </summary>
public ICollection<TValue> Values public ICollection<TValue> Values
{ {
get get
@ -350,56 +459,87 @@ namespace Godot.Collections
} }
} }
/// <summary>
/// Adds an object <paramref name="value"/> at key <paramref name="key"/>
/// to this <see cref="Dictionary{TKey, TValue}"/>.
/// </summary>
/// <param name="key">The key at which to add the object.</param>
/// <param name="value">The object to add.</param>
public void Add(TKey key, TValue value) public void Add(TKey key, TValue value)
{ {
objectDict.Add(key, value); objectDict.Add(key, value);
} }
/// <summary>
/// Checks if this <see cref="Dictionary{TKey, TValue}"/> contains the given key.
/// </summary>
/// <param name="key">The key to look for.</param>
/// <returns>Whether or not this dictionary contains the given key.</returns>
public bool ContainsKey(TKey key) public bool ContainsKey(TKey key)
{ {
return objectDict.Contains(key); return objectDict.Contains(key);
} }
/// <summary>
/// Removes an element from this <see cref="Dictionary{TKey, TValue}"/> by key.
/// </summary>
/// <param name="key">The key of the element to remove.</param>
public bool Remove(TKey key) public bool Remove(TKey key)
{ {
return Dictionary.godot_icall_Dictionary_RemoveKey(GetPtr(), key); return Dictionary.godot_icall_Dictionary_RemoveKey(GetPtr(), key);
} }
public bool TryGetValue(TKey key, out TValue value) /// <summary>
/// Gets the object at the given <paramref name="key"/>.
/// </summary>
/// <param name="key">The key of the element to get.</param>
/// <param name="value">The value at the given <paramref name="key"/>.</param>
/// <returns>If an object was found for the given <paramref name="key"/>.</returns>
public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
{ {
object retValue; bool found = Dictionary.godot_icall_Dictionary_TryGetValue_Generic(GetPtr(), key, out object retValue, valTypeEncoding, valTypeClass);
bool found = Dictionary.godot_icall_Dictionary_TryGetValue_Generic(GetPtr(), key, out retValue, valTypeEncoding, valTypeClass); value = found ? (TValue)retValue : default;
value = found ? (TValue)retValue : default(TValue);
return found; return found;
} }
// ICollection<KeyValuePair<TKey, TValue>> // ICollection<KeyValuePair<TKey, TValue>>
/// <summary>
/// Returns the number of elements in this <see cref="Dictionary{TKey, TValue}"/>.
/// This is also known as the size or length of the dictionary.
/// </summary>
/// <returns>The number of elements.</returns>
public int Count public int Count
{ {
get { return objectDict.Count; } get { return objectDict.Count; }
} }
public bool IsReadOnly bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly => false;
{
get { return objectDict.IsReadOnly; }
}
public void Add(KeyValuePair<TKey, TValue> item) void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item)
{ {
objectDict.Add(item.Key, item.Value); objectDict.Add(item.Key, item.Value);
} }
/// <summary>
/// Erases all the items from this <see cref="Dictionary{TKey, TValue}"/>.
/// </summary>
public void Clear() public void Clear()
{ {
objectDict.Clear(); objectDict.Clear();
} }
public bool Contains(KeyValuePair<TKey, TValue> item) bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item)
{ {
return objectDict.Contains(new KeyValuePair<object, object>(item.Key, item.Value)); return objectDict.Contains(new KeyValuePair<object, object>(item.Key, item.Value));
} }
/// <summary>
/// Copies the elements of this <see cref="Dictionary{TKey, TValue}"/> to the given
/// untyped C# array, starting at the given index.
/// </summary>
/// <param name="array">The array to copy to.</param>
/// <param name="arrayIndex">The index to start at.</param>
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{ {
if (array == null) if (array == null)
@ -424,7 +564,7 @@ namespace Godot.Collections
} }
} }
public bool Remove(KeyValuePair<TKey, TValue> item) bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item)
{ {
return Dictionary.godot_icall_Dictionary_Remove(GetPtr(), item.Key, item.Value); return Dictionary.godot_icall_Dictionary_Remove(GetPtr(), item.Key, item.Value);
; ;
@ -432,6 +572,10 @@ namespace Godot.Collections
// IEnumerable<KeyValuePair<TKey, TValue>> // IEnumerable<KeyValuePair<TKey, TValue>>
/// <summary>
/// Gets an enumerator for this <see cref="Dictionary{TKey, TValue}"/>.
/// </summary>
/// <returns>An enumerator.</returns>
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{ {
// TODO 3 internal calls, can reduce to 1 // TODO 3 internal calls, can reduce to 1
@ -451,6 +595,10 @@ namespace Godot.Collections
return GetEnumerator(); return GetEnumerator();
} }
/// <summary>
/// Converts this <see cref="Dictionary{TKey, TValue}"/> to a string.
/// </summary>
/// <returns>A string representation of this dictionary.</returns>
public override string ToString() => objectDict.ToString(); public override string ToString() => objectDict.ToString();
} }
} }