2018-07-18 23:07:57 +02:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
using System.Runtime.InteropServices;
|
2021-07-28 15:44:09 +02:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2018-07-18 23:07:57 +02:00
|
|
|
|
2018-08-25 17:16:16 +02:00
|
|
|
namespace Godot.Collections
|
2018-07-18 23:07:57 +02:00
|
|
|
{
|
|
|
|
class DictionarySafeHandle : SafeHandle
|
|
|
|
{
|
|
|
|
public DictionarySafeHandle(IntPtr handle) : base(IntPtr.Zero, true)
|
|
|
|
{
|
|
|
|
this.handle = handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override bool IsInvalid
|
|
|
|
{
|
2020-04-23 02:19:32 +02:00
|
|
|
get { return handle == IntPtr.Zero; }
|
2018-07-18 23:07:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override bool ReleaseHandle()
|
|
|
|
{
|
|
|
|
Dictionary.godot_icall_Dictionary_Dtor(handle);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-28 15:44:09 +02:00
|
|
|
/// <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>
|
2018-07-18 23:07:57 +02:00
|
|
|
public class Dictionary :
|
2019-03-04 21:31:04 +01:00
|
|
|
IDictionary,
|
2018-07-18 23:07:57 +02:00
|
|
|
IDisposable
|
|
|
|
{
|
|
|
|
DictionarySafeHandle safeHandle;
|
|
|
|
bool disposed = false;
|
|
|
|
|
2021-07-28 15:44:09 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Constructs a new empty <see cref="Dictionary"/>.
|
|
|
|
/// </summary>
|
2018-07-18 23:07:57 +02:00
|
|
|
public Dictionary()
|
|
|
|
{
|
|
|
|
safeHandle = new DictionarySafeHandle(godot_icall_Dictionary_Ctor());
|
|
|
|
}
|
|
|
|
|
2021-07-28 15:44:09 +02:00
|
|
|
/// <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>
|
2019-03-05 18:52:19 +01:00
|
|
|
public Dictionary(IDictionary dictionary) : this()
|
|
|
|
{
|
|
|
|
if (dictionary == null)
|
|
|
|
throw new NullReferenceException($"Parameter '{nameof(dictionary)} cannot be null.'");
|
|
|
|
|
2020-04-23 02:19:32 +02:00
|
|
|
foreach (DictionaryEntry entry in dictionary)
|
|
|
|
Add(entry.Key, entry.Value);
|
2019-03-05 18:52:19 +01:00
|
|
|
}
|
|
|
|
|
2018-07-18 23:07:57 +02:00
|
|
|
internal Dictionary(DictionarySafeHandle handle)
|
|
|
|
{
|
|
|
|
safeHandle = handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal Dictionary(IntPtr handle)
|
|
|
|
{
|
|
|
|
safeHandle = new DictionarySafeHandle(handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal IntPtr GetPtr()
|
|
|
|
{
|
2019-02-19 00:37:10 +01:00
|
|
|
if (disposed)
|
|
|
|
throw new ObjectDisposedException(GetType().FullName);
|
|
|
|
|
2018-07-18 23:07:57 +02:00
|
|
|
return safeHandle.DangerousGetHandle();
|
|
|
|
}
|
|
|
|
|
2021-07-28 15:44:09 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Disposes of this <see cref="Dictionary"/>.
|
|
|
|
/// </summary>
|
2018-07-18 23:07:57 +02:00
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
if (disposed)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (safeHandle != null)
|
|
|
|
{
|
|
|
|
safeHandle.Dispose();
|
|
|
|
safeHandle = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
disposed = true;
|
|
|
|
}
|
|
|
|
|
2021-07-28 15:44:09 +02:00
|
|
|
/// <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>
|
2019-12-04 14:54:33 +01:00
|
|
|
public Dictionary Duplicate(bool deep = false)
|
|
|
|
{
|
|
|
|
return new Dictionary(godot_icall_Dictionary_Duplicate(GetPtr(), deep));
|
|
|
|
}
|
|
|
|
|
2019-03-04 21:31:04 +01:00
|
|
|
// IDictionary
|
2018-07-18 23:07:57 +02:00
|
|
|
|
2021-07-28 15:44:09 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the collection of keys in this <see cref="Dictionary"/>.
|
|
|
|
/// </summary>
|
2019-03-04 21:31:04 +01:00
|
|
|
public ICollection Keys
|
2018-07-18 23:07:57 +02:00
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
IntPtr handle = godot_icall_Dictionary_Keys(GetPtr());
|
|
|
|
return new Array(new ArraySafeHandle(handle));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-28 15:44:09 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the collection of elements in this <see cref="Dictionary"/>.
|
|
|
|
/// </summary>
|
2019-03-04 21:31:04 +01:00
|
|
|
public ICollection Values
|
2018-07-18 23:07:57 +02:00
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
IntPtr handle = godot_icall_Dictionary_Values(GetPtr());
|
|
|
|
return new Array(new ArraySafeHandle(handle));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-28 16:55:29 +02:00
|
|
|
private (Array keys, Array values, int count) GetKeyValuePairs()
|
|
|
|
{
|
|
|
|
int count = godot_icall_Dictionary_KeyValuePairs(GetPtr(), out IntPtr keysHandle, out IntPtr valuesHandle);
|
|
|
|
Array keys = new Array(new ArraySafeHandle(keysHandle));
|
|
|
|
Array values = new Array(new ArraySafeHandle(valuesHandle));
|
|
|
|
return (keys, values, count);
|
|
|
|
}
|
|
|
|
|
2021-07-28 15:44:09 +02:00
|
|
|
bool IDictionary.IsFixedSize => false;
|
2018-07-18 23:07:57 +02:00
|
|
|
|
2021-07-28 15:44:09 +02:00
|
|
|
bool IDictionary.IsReadOnly => false;
|
2018-07-18 23:07:57 +02:00
|
|
|
|
2021-07-28 15:44:09 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Returns the object at the given <paramref name="key"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The object at the given <paramref name="key"/>.</value>
|
2019-03-04 21:31:04 +01:00
|
|
|
public object this[object key]
|
2018-07-18 23:07:57 +02:00
|
|
|
{
|
2019-03-04 21:31:04 +01:00
|
|
|
get => godot_icall_Dictionary_GetValue(GetPtr(), key);
|
|
|
|
set => godot_icall_Dictionary_SetValue(GetPtr(), key, value);
|
2018-07-18 23:07:57 +02:00
|
|
|
}
|
|
|
|
|
2021-07-28 15:44:09 +02:00
|
|
|
/// <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>
|
2019-03-04 21:31:04 +01:00
|
|
|
public void Add(object key, object value) => godot_icall_Dictionary_Add(GetPtr(), key, value);
|
2018-07-18 23:07:57 +02:00
|
|
|
|
2021-07-28 15:44:09 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Erases all items from this <see cref="Dictionary"/>.
|
|
|
|
/// </summary>
|
2019-03-04 21:31:04 +01:00
|
|
|
public void Clear() => godot_icall_Dictionary_Clear(GetPtr());
|
2018-07-18 23:07:57 +02:00
|
|
|
|
2021-07-28 15:44:09 +02:00
|
|
|
/// <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>
|
2019-03-04 21:31:04 +01:00
|
|
|
public bool Contains(object key) => godot_icall_Dictionary_ContainsKey(GetPtr(), key);
|
2018-07-18 23:07:57 +02:00
|
|
|
|
2021-07-28 15:44:09 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Gets an enumerator for this <see cref="Dictionary"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>An enumerator.</returns>
|
2019-03-04 21:31:04 +01:00
|
|
|
public IDictionaryEnumerator GetEnumerator() => new DictionaryEnumerator(this);
|
|
|
|
|
2021-07-28 15:44:09 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Removes an element from this <see cref="Dictionary"/> by key.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="key">The key of the element to remove.</param>
|
2019-03-04 21:31:04 +01:00
|
|
|
public void Remove(object key) => godot_icall_Dictionary_RemoveKey(GetPtr(), key);
|
|
|
|
|
|
|
|
// ICollection
|
|
|
|
|
2021-07-28 15:44:09 +02:00
|
|
|
object ICollection.SyncRoot => this;
|
2018-07-18 23:07:57 +02:00
|
|
|
|
2021-07-28 15:44:09 +02:00
|
|
|
bool ICollection.IsSynchronized => false;
|
2019-03-04 21:31:04 +01:00
|
|
|
|
2021-07-28 15:44:09 +02:00
|
|
|
/// <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>
|
2019-03-04 21:31:04 +01:00
|
|
|
public int Count => godot_icall_Dictionary_Count(GetPtr());
|
|
|
|
|
2021-07-28 15:44:09 +02:00
|
|
|
/// <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>
|
2019-03-04 21:31:04 +01:00
|
|
|
public void CopyTo(System.Array array, int index)
|
2018-07-18 23:07:57 +02:00
|
|
|
{
|
2019-03-04 21:31:04 +01:00
|
|
|
if (array == null)
|
|
|
|
throw new ArgumentNullException(nameof(array), "Value cannot be null.");
|
|
|
|
|
|
|
|
if (index < 0)
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(index), "Number was less than the array's lower bound in the first dimension.");
|
|
|
|
|
2021-07-28 16:55:29 +02:00
|
|
|
var (keys, values, count) = GetKeyValuePairs();
|
2018-07-18 23:07:57 +02:00
|
|
|
|
2019-03-04 21:31:04 +01:00
|
|
|
if (array.Length < (index + count))
|
|
|
|
throw new ArgumentException("Destination array was not long enough. Check destIndex and length, and the array's lower bounds.");
|
|
|
|
|
2018-07-18 23:07:57 +02:00
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
{
|
2019-03-04 21:31:04 +01:00
|
|
|
array.SetValue(new DictionaryEntry(keys[i], values[i]), index);
|
|
|
|
index++;
|
2018-07-18 23:07:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 21:31:04 +01:00
|
|
|
// IEnumerable
|
|
|
|
|
|
|
|
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
|
|
|
|
|
|
|
private class DictionaryEnumerator : IDictionaryEnumerator
|
2018-07-18 23:07:57 +02:00
|
|
|
{
|
2021-07-28 16:55:29 +02:00
|
|
|
private readonly Dictionary dictionary;
|
2021-07-28 15:44:09 +02:00
|
|
|
private readonly int count;
|
|
|
|
private int index = -1;
|
2021-07-28 16:55:29 +02:00
|
|
|
private bool dirty = true;
|
|
|
|
|
|
|
|
private DictionaryEntry entry;
|
2018-07-18 23:07:57 +02:00
|
|
|
|
2019-03-04 21:31:04 +01:00
|
|
|
public DictionaryEnumerator(Dictionary dictionary)
|
2018-07-18 23:07:57 +02:00
|
|
|
{
|
2021-07-28 16:55:29 +02:00
|
|
|
this.dictionary = dictionary;
|
2019-03-04 21:31:04 +01:00
|
|
|
count = dictionary.Count;
|
2018-07-18 23:07:57 +02:00
|
|
|
}
|
|
|
|
|
2019-03-04 21:31:04 +01:00
|
|
|
public object Current => Entry;
|
2018-07-18 23:07:57 +02:00
|
|
|
|
2021-07-28 16:55:29 +02:00
|
|
|
public DictionaryEntry Entry
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
if (dirty)
|
|
|
|
{
|
|
|
|
UpdateEntry();
|
|
|
|
}
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void UpdateEntry()
|
|
|
|
{
|
|
|
|
dirty = false;
|
|
|
|
godot_icall_Dictionary_KeyValuePairAt(dictionary.GetPtr(), index, out object key, out object value);
|
|
|
|
entry = new DictionaryEntry(key, value);
|
|
|
|
}
|
2018-07-18 23:07:57 +02:00
|
|
|
|
2019-03-04 21:31:04 +01:00
|
|
|
public object Key => Entry.Key;
|
2018-07-18 23:07:57 +02:00
|
|
|
|
2019-03-04 21:31:04 +01:00
|
|
|
public object Value => Entry.Value;
|
|
|
|
|
|
|
|
public bool MoveNext()
|
|
|
|
{
|
|
|
|
index++;
|
2021-07-28 16:55:29 +02:00
|
|
|
dirty = true;
|
2019-03-04 21:31:04 +01:00
|
|
|
return index < count;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Reset()
|
|
|
|
{
|
|
|
|
index = -1;
|
2021-07-28 16:55:29 +02:00
|
|
|
dirty = true;
|
2019-03-04 21:31:04 +01:00
|
|
|
}
|
2018-07-18 23:07:57 +02:00
|
|
|
}
|
|
|
|
|
2021-07-28 15:44:09 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Converts this <see cref="Dictionary"/> to a string.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>A string representation of this dictionary.</returns>
|
2019-04-18 14:48:10 +02:00
|
|
|
public override string ToString()
|
|
|
|
{
|
|
|
|
return godot_icall_Dictionary_ToString(GetPtr());
|
|
|
|
}
|
|
|
|
|
2018-09-04 05:40:41 +02:00
|
|
|
[MethodImpl(MethodImplOptions.InternalCall)]
|
|
|
|
internal extern static IntPtr godot_icall_Dictionary_Ctor();
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.InternalCall)]
|
|
|
|
internal extern static void godot_icall_Dictionary_Dtor(IntPtr ptr);
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.InternalCall)]
|
|
|
|
internal extern static object godot_icall_Dictionary_GetValue(IntPtr ptr, object key);
|
|
|
|
|
2018-09-27 00:11:31 +02:00
|
|
|
[MethodImpl(MethodImplOptions.InternalCall)]
|
|
|
|
internal extern static object godot_icall_Dictionary_GetValue_Generic(IntPtr ptr, object key, int valTypeEncoding, IntPtr valTypeClass);
|
|
|
|
|
2018-09-04 05:40:41 +02:00
|
|
|
[MethodImpl(MethodImplOptions.InternalCall)]
|
|
|
|
internal extern static void godot_icall_Dictionary_SetValue(IntPtr ptr, object key, object value);
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.InternalCall)]
|
|
|
|
internal extern static IntPtr godot_icall_Dictionary_Keys(IntPtr ptr);
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.InternalCall)]
|
|
|
|
internal extern static IntPtr godot_icall_Dictionary_Values(IntPtr ptr);
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.InternalCall)]
|
|
|
|
internal extern static int godot_icall_Dictionary_Count(IntPtr ptr);
|
|
|
|
|
2021-07-28 16:55:29 +02:00
|
|
|
[MethodImpl(MethodImplOptions.InternalCall)]
|
|
|
|
internal extern static int godot_icall_Dictionary_KeyValuePairs(IntPtr ptr, out IntPtr keys, out IntPtr values);
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.InternalCall)]
|
|
|
|
internal extern static void godot_icall_Dictionary_KeyValuePairAt(IntPtr ptr, int index, out object key, out object value);
|
|
|
|
|
2018-09-04 05:40:41 +02:00
|
|
|
[MethodImpl(MethodImplOptions.InternalCall)]
|
|
|
|
internal extern static void godot_icall_Dictionary_Add(IntPtr ptr, object key, object value);
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.InternalCall)]
|
|
|
|
internal extern static void godot_icall_Dictionary_Clear(IntPtr ptr);
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.InternalCall)]
|
|
|
|
internal extern static bool godot_icall_Dictionary_Contains(IntPtr ptr, object key, object value);
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.InternalCall)]
|
|
|
|
internal extern static bool godot_icall_Dictionary_ContainsKey(IntPtr ptr, object key);
|
|
|
|
|
2019-12-04 14:54:33 +01:00
|
|
|
[MethodImpl(MethodImplOptions.InternalCall)]
|
|
|
|
internal extern static IntPtr godot_icall_Dictionary_Duplicate(IntPtr ptr, bool deep);
|
|
|
|
|
2018-09-04 05:40:41 +02:00
|
|
|
[MethodImpl(MethodImplOptions.InternalCall)]
|
|
|
|
internal extern static bool godot_icall_Dictionary_RemoveKey(IntPtr ptr, object key);
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.InternalCall)]
|
|
|
|
internal extern static bool godot_icall_Dictionary_Remove(IntPtr ptr, object key, object value);
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.InternalCall)]
|
|
|
|
internal extern static bool godot_icall_Dictionary_TryGetValue(IntPtr ptr, object key, out object value);
|
2018-09-27 00:11:31 +02:00
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.InternalCall)]
|
|
|
|
internal extern static bool godot_icall_Dictionary_TryGetValue_Generic(IntPtr ptr, object key, out object value, int valTypeEncoding, IntPtr valTypeClass);
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.InternalCall)]
|
|
|
|
internal extern static void godot_icall_Dictionary_Generic_GetValueTypeInfo(Type valueType, out int valTypeEncoding, out IntPtr valTypeClass);
|
2019-04-18 14:48:10 +02:00
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.InternalCall)]
|
|
|
|
internal extern static string godot_icall_Dictionary_ToString(IntPtr ptr);
|
2018-09-04 05:40:41 +02:00
|
|
|
}
|
2018-07-18 23:07:57 +02:00
|
|
|
|
2021-07-28 15:44:09 +02:00
|
|
|
/// <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>
|
2018-07-18 23:07:57 +02:00
|
|
|
public class Dictionary<TKey, TValue> :
|
2019-03-04 21:31:04 +01:00
|
|
|
IDictionary<TKey, TValue>
|
2018-07-18 23:07:57 +02:00
|
|
|
{
|
2021-07-28 15:44:09 +02:00
|
|
|
private readonly Dictionary objectDict;
|
2018-07-18 23:07:57 +02:00
|
|
|
|
2018-09-27 00:11:31 +02:00
|
|
|
internal static int valTypeEncoding;
|
|
|
|
internal static IntPtr valTypeClass;
|
|
|
|
|
|
|
|
static Dictionary()
|
|
|
|
{
|
|
|
|
Dictionary.godot_icall_Dictionary_Generic_GetValueTypeInfo(typeof(TValue), out valTypeEncoding, out valTypeClass);
|
|
|
|
}
|
|
|
|
|
2021-07-28 15:44:09 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Constructs a new empty <see cref="Dictionary{TKey, TValue}"/>.
|
|
|
|
/// </summary>
|
2018-07-18 23:07:57 +02:00
|
|
|
public Dictionary()
|
|
|
|
{
|
|
|
|
objectDict = new Dictionary();
|
|
|
|
}
|
|
|
|
|
2021-07-28 15:44:09 +02:00
|
|
|
/// <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>
|
2019-03-05 18:52:19 +01:00
|
|
|
public Dictionary(IDictionary<TKey, TValue> dictionary)
|
|
|
|
{
|
|
|
|
objectDict = new Dictionary();
|
|
|
|
|
|
|
|
if (dictionary == null)
|
|
|
|
throw new NullReferenceException($"Parameter '{nameof(dictionary)} cannot be null.'");
|
|
|
|
|
|
|
|
// TODO: Can be optimized
|
|
|
|
|
|
|
|
IntPtr godotDictionaryPtr = GetPtr();
|
|
|
|
|
|
|
|
foreach (KeyValuePair<TKey, TValue> entry in dictionary)
|
|
|
|
{
|
|
|
|
Dictionary.godot_icall_Dictionary_Add(godotDictionaryPtr, entry.Key, entry.Value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-28 15:44:09 +02:00
|
|
|
/// <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>
|
2018-07-18 23:07:57 +02:00
|
|
|
public Dictionary(Dictionary dictionary)
|
|
|
|
{
|
|
|
|
objectDict = dictionary;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal Dictionary(IntPtr handle)
|
|
|
|
{
|
|
|
|
objectDict = new Dictionary(handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal Dictionary(DictionarySafeHandle handle)
|
|
|
|
{
|
|
|
|
objectDict = new Dictionary(handle);
|
|
|
|
}
|
|
|
|
|
2021-07-28 15:44:09 +02:00
|
|
|
/// <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>
|
2018-07-18 23:07:57 +02:00
|
|
|
public static explicit operator Dictionary(Dictionary<TKey, TValue> from)
|
|
|
|
{
|
|
|
|
return from.objectDict;
|
|
|
|
}
|
|
|
|
|
2019-03-04 21:31:04 +01:00
|
|
|
internal IntPtr GetPtr()
|
|
|
|
{
|
|
|
|
return objectDict.GetPtr();
|
|
|
|
}
|
|
|
|
|
2021-07-28 15:44:09 +02:00
|
|
|
/// <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>
|
2019-12-04 14:54:33 +01:00
|
|
|
public Dictionary<TKey, TValue> Duplicate(bool deep = false)
|
|
|
|
{
|
|
|
|
return new Dictionary<TKey, TValue>(objectDict.Duplicate(deep));
|
|
|
|
}
|
|
|
|
|
2019-03-04 21:31:04 +01:00
|
|
|
// IDictionary<TKey, TValue>
|
|
|
|
|
2021-07-28 15:44:09 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Returns the value at the given <paramref name="key"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The value at the given <paramref name="key"/>.</value>
|
2018-07-18 23:07:57 +02:00
|
|
|
public TValue this[TKey key]
|
|
|
|
{
|
2020-04-23 02:19:32 +02:00
|
|
|
get { return (TValue)Dictionary.godot_icall_Dictionary_GetValue_Generic(objectDict.GetPtr(), key, valTypeEncoding, valTypeClass); }
|
|
|
|
set { objectDict[key] = value; }
|
2018-07-18 23:07:57 +02:00
|
|
|
}
|
|
|
|
|
2021-07-28 15:44:09 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the collection of keys in this <see cref="Dictionary{TKey, TValue}"/>.
|
|
|
|
/// </summary>
|
2018-07-18 23:07:57 +02:00
|
|
|
public ICollection<TKey> Keys
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
IntPtr handle = Dictionary.godot_icall_Dictionary_Keys(objectDict.GetPtr());
|
|
|
|
return new Array<TKey>(new ArraySafeHandle(handle));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-28 15:44:09 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the collection of elements in this <see cref="Dictionary{TKey, TValue}"/>.
|
|
|
|
/// </summary>
|
2018-07-18 23:07:57 +02:00
|
|
|
public ICollection<TValue> Values
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
IntPtr handle = Dictionary.godot_icall_Dictionary_Values(objectDict.GetPtr());
|
|
|
|
return new Array<TValue>(new ArraySafeHandle(handle));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-28 16:55:29 +02:00
|
|
|
private KeyValuePair<TKey, TValue> GetKeyValuePair(int index)
|
|
|
|
{
|
|
|
|
Dictionary.godot_icall_Dictionary_KeyValuePairAt(GetPtr(), index, out object key, out object value);
|
|
|
|
return new KeyValuePair<TKey, TValue>((TKey)key, (TValue)value);
|
|
|
|
}
|
|
|
|
|
2021-07-28 15:44:09 +02:00
|
|
|
/// <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>
|
2019-03-04 21:31:04 +01:00
|
|
|
public void Add(TKey key, TValue value)
|
|
|
|
{
|
|
|
|
objectDict.Add(key, value);
|
|
|
|
}
|
|
|
|
|
2021-07-28 15:44:09 +02:00
|
|
|
/// <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>
|
2019-03-04 21:31:04 +01:00
|
|
|
public bool ContainsKey(TKey key)
|
|
|
|
{
|
|
|
|
return objectDict.Contains(key);
|
|
|
|
}
|
|
|
|
|
2021-07-28 15:44:09 +02:00
|
|
|
/// <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>
|
2019-03-04 21:31:04 +01:00
|
|
|
public bool Remove(TKey key)
|
|
|
|
{
|
|
|
|
return Dictionary.godot_icall_Dictionary_RemoveKey(GetPtr(), key);
|
|
|
|
}
|
|
|
|
|
2021-07-28 15:44:09 +02:00
|
|
|
/// <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)
|
2019-03-04 21:31:04 +01:00
|
|
|
{
|
2021-07-28 15:44:09 +02:00
|
|
|
bool found = Dictionary.godot_icall_Dictionary_TryGetValue_Generic(GetPtr(), key, out object retValue, valTypeEncoding, valTypeClass);
|
|
|
|
value = found ? (TValue)retValue : default;
|
2019-03-04 21:31:04 +01:00
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ICollection<KeyValuePair<TKey, TValue>>
|
|
|
|
|
2021-07-28 15:44:09 +02:00
|
|
|
/// <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>
|
2018-07-18 23:07:57 +02:00
|
|
|
public int Count
|
|
|
|
{
|
2020-04-23 02:19:32 +02:00
|
|
|
get { return objectDict.Count; }
|
2018-07-18 23:07:57 +02:00
|
|
|
}
|
|
|
|
|
2021-07-28 15:44:09 +02:00
|
|
|
bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly => false;
|
2018-07-18 23:07:57 +02:00
|
|
|
|
2021-07-28 15:44:09 +02:00
|
|
|
void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item)
|
2018-07-18 23:07:57 +02:00
|
|
|
{
|
|
|
|
objectDict.Add(item.Key, item.Value);
|
|
|
|
}
|
|
|
|
|
2021-07-28 15:44:09 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Erases all the items from this <see cref="Dictionary{TKey, TValue}"/>.
|
|
|
|
/// </summary>
|
2018-07-18 23:07:57 +02:00
|
|
|
public void Clear()
|
|
|
|
{
|
|
|
|
objectDict.Clear();
|
|
|
|
}
|
|
|
|
|
2021-07-28 15:44:09 +02:00
|
|
|
bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item)
|
2018-07-18 23:07:57 +02:00
|
|
|
{
|
|
|
|
return objectDict.Contains(new KeyValuePair<object, object>(item.Key, item.Value));
|
|
|
|
}
|
|
|
|
|
2021-07-28 15:44:09 +02:00
|
|
|
/// <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>
|
2018-07-18 23:07:57 +02:00
|
|
|
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
|
|
|
|
{
|
2019-03-04 21:31:04 +01:00
|
|
|
if (array == null)
|
|
|
|
throw new ArgumentNullException(nameof(array), "Value cannot be null.");
|
|
|
|
|
|
|
|
if (arrayIndex < 0)
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(arrayIndex), "Number was less than the array's lower bound in the first dimension.");
|
|
|
|
|
2018-07-18 23:07:57 +02:00
|
|
|
int count = Count;
|
|
|
|
|
2019-03-04 21:31:04 +01:00
|
|
|
if (array.Length < (arrayIndex + count))
|
|
|
|
throw new ArgumentException("Destination array was not long enough. Check destIndex and length, and the array's lower bounds.");
|
|
|
|
|
2018-07-18 23:07:57 +02:00
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
{
|
2021-07-28 16:55:29 +02:00
|
|
|
array[arrayIndex] = GetKeyValuePair(i);
|
2018-07-18 23:07:57 +02:00
|
|
|
arrayIndex++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-28 15:44:09 +02:00
|
|
|
bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item)
|
2019-03-04 21:31:04 +01:00
|
|
|
{
|
2020-04-23 02:19:32 +02:00
|
|
|
return Dictionary.godot_icall_Dictionary_Remove(GetPtr(), item.Key, item.Value);
|
|
|
|
;
|
2019-03-04 21:31:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// IEnumerable<KeyValuePair<TKey, TValue>>
|
|
|
|
|
2021-07-28 15:44:09 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Gets an enumerator for this <see cref="Dictionary{TKey, TValue}"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>An enumerator.</returns>
|
2018-07-18 23:07:57 +02:00
|
|
|
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
|
|
|
|
{
|
2021-07-28 16:55:29 +02:00
|
|
|
for (int i = 0; i < Count; i++)
|
2018-07-18 23:07:57 +02:00
|
|
|
{
|
2021-07-28 16:55:29 +02:00
|
|
|
yield return GetKeyValuePair(i);
|
2018-07-18 23:07:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
|
|
{
|
|
|
|
return GetEnumerator();
|
|
|
|
}
|
2019-04-18 14:48:10 +02:00
|
|
|
|
2021-07-28 15:44:09 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Converts this <see cref="Dictionary{TKey, TValue}"/> to a string.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>A string representation of this dictionary.</returns>
|
2019-04-18 14:48:10 +02:00
|
|
|
public override string ToString() => objectDict.ToString();
|
2018-07-18 23:07:57 +02:00
|
|
|
}
|
|
|
|
}
|