Array.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4. using System.Runtime.CompilerServices;
  5. using System.Runtime.InteropServices;
  6. namespace Godot.Collections
  7. {
  8. class ArraySafeHandle : SafeHandle
  9. {
  10. public ArraySafeHandle(IntPtr handle) : base(IntPtr.Zero, true)
  11. {
  12. this.handle = handle;
  13. }
  14. public override bool IsInvalid
  15. {
  16. get { return handle == IntPtr.Zero; }
  17. }
  18. protected override bool ReleaseHandle()
  19. {
  20. Array.godot_icall_Array_Dtor(handle);
  21. return true;
  22. }
  23. }
  24. public class Array : IList, IDisposable
  25. {
  26. ArraySafeHandle safeHandle;
  27. bool disposed = false;
  28. public Array()
  29. {
  30. safeHandle = new ArraySafeHandle(godot_icall_Array_Ctor());
  31. }
  32. public Array(IEnumerable collection) : this()
  33. {
  34. if (collection == null)
  35. throw new NullReferenceException($"Parameter '{nameof(collection)} cannot be null.'");
  36. foreach (object element in collection)
  37. Add(element);
  38. }
  39. public Array(params object[] array) : this()
  40. {
  41. if (array == null)
  42. {
  43. throw new NullReferenceException($"Parameter '{nameof(array)} cannot be null.'");
  44. }
  45. safeHandle = new ArraySafeHandle(godot_icall_Array_Ctor_MonoArray(array));
  46. }
  47. internal Array(ArraySafeHandle handle)
  48. {
  49. safeHandle = handle;
  50. }
  51. internal Array(IntPtr handle)
  52. {
  53. safeHandle = new ArraySafeHandle(handle);
  54. }
  55. internal IntPtr GetPtr()
  56. {
  57. if (disposed)
  58. throw new ObjectDisposedException(GetType().FullName);
  59. return safeHandle.DangerousGetHandle();
  60. }
  61. public Array Duplicate(bool deep = false)
  62. {
  63. return new Array(godot_icall_Array_Duplicate(GetPtr(), deep));
  64. }
  65. public Error Resize(int newSize)
  66. {
  67. return godot_icall_Array_Resize(GetPtr(), newSize);
  68. }
  69. public static Array operator +(Array left, Array right)
  70. {
  71. return new Array(godot_icall_Array_Concatenate(left.GetPtr(), right.GetPtr()));
  72. }
  73. // IDisposable
  74. public void Dispose()
  75. {
  76. if (disposed)
  77. return;
  78. if (safeHandle != null)
  79. {
  80. safeHandle.Dispose();
  81. safeHandle = null;
  82. }
  83. disposed = true;
  84. }
  85. // IList
  86. public bool IsReadOnly => false;
  87. public bool IsFixedSize => false;
  88. public object this[int index]
  89. {
  90. get => godot_icall_Array_At(GetPtr(), index);
  91. set => godot_icall_Array_SetAt(GetPtr(), index, value);
  92. }
  93. public int Add(object value) => godot_icall_Array_Add(GetPtr(), value);
  94. public bool Contains(object value) => godot_icall_Array_Contains(GetPtr(), value);
  95. public void Clear() => godot_icall_Array_Clear(GetPtr());
  96. public int IndexOf(object value) => godot_icall_Array_IndexOf(GetPtr(), value);
  97. public void Insert(int index, object value) => godot_icall_Array_Insert(GetPtr(), index, value);
  98. public void Remove(object value) => godot_icall_Array_Remove(GetPtr(), value);
  99. public void RemoveAt(int index) => godot_icall_Array_RemoveAt(GetPtr(), index);
  100. // ICollection
  101. public int Count => godot_icall_Array_Count(GetPtr());
  102. public object SyncRoot => this;
  103. public bool IsSynchronized => false;
  104. public void CopyTo(System.Array array, int index)
  105. {
  106. if (array == null)
  107. throw new ArgumentNullException(nameof(array), "Value cannot be null.");
  108. if (index < 0)
  109. throw new ArgumentOutOfRangeException(nameof(index), "Number was less than the array's lower bound in the first dimension.");
  110. // Internal call may throw ArgumentException
  111. godot_icall_Array_CopyTo(GetPtr(), array, index);
  112. }
  113. // IEnumerable
  114. public IEnumerator GetEnumerator()
  115. {
  116. int count = Count;
  117. for (int i = 0; i < count; i++)
  118. {
  119. yield return this[i];
  120. }
  121. }
  122. public override string ToString()
  123. {
  124. return godot_icall_Array_ToString(GetPtr());
  125. }
  126. [MethodImpl(MethodImplOptions.InternalCall)]
  127. internal extern static IntPtr godot_icall_Array_Ctor();
  128. [MethodImpl(MethodImplOptions.InternalCall)]
  129. internal extern static IntPtr godot_icall_Array_Ctor_MonoArray(System.Array array);
  130. [MethodImpl(MethodImplOptions.InternalCall)]
  131. internal extern static void godot_icall_Array_Dtor(IntPtr ptr);
  132. [MethodImpl(MethodImplOptions.InternalCall)]
  133. internal extern static object godot_icall_Array_At(IntPtr ptr, int index);
  134. [MethodImpl(MethodImplOptions.InternalCall)]
  135. internal extern static object godot_icall_Array_At_Generic(IntPtr ptr, int index, int elemTypeEncoding, IntPtr elemTypeClass);
  136. [MethodImpl(MethodImplOptions.InternalCall)]
  137. internal extern static void godot_icall_Array_SetAt(IntPtr ptr, int index, object value);
  138. [MethodImpl(MethodImplOptions.InternalCall)]
  139. internal extern static int godot_icall_Array_Count(IntPtr ptr);
  140. [MethodImpl(MethodImplOptions.InternalCall)]
  141. internal extern static int godot_icall_Array_Add(IntPtr ptr, object item);
  142. [MethodImpl(MethodImplOptions.InternalCall)]
  143. internal extern static void godot_icall_Array_Clear(IntPtr ptr);
  144. [MethodImpl(MethodImplOptions.InternalCall)]
  145. internal extern static IntPtr godot_icall_Array_Concatenate(IntPtr left, IntPtr right);
  146. [MethodImpl(MethodImplOptions.InternalCall)]
  147. internal extern static bool godot_icall_Array_Contains(IntPtr ptr, object item);
  148. [MethodImpl(MethodImplOptions.InternalCall)]
  149. internal extern static void godot_icall_Array_CopyTo(IntPtr ptr, System.Array array, int arrayIndex);
  150. [MethodImpl(MethodImplOptions.InternalCall)]
  151. internal extern static IntPtr godot_icall_Array_Duplicate(IntPtr ptr, bool deep);
  152. [MethodImpl(MethodImplOptions.InternalCall)]
  153. internal extern static int godot_icall_Array_IndexOf(IntPtr ptr, object item);
  154. [MethodImpl(MethodImplOptions.InternalCall)]
  155. internal extern static void godot_icall_Array_Insert(IntPtr ptr, int index, object item);
  156. [MethodImpl(MethodImplOptions.InternalCall)]
  157. internal extern static bool godot_icall_Array_Remove(IntPtr ptr, object item);
  158. [MethodImpl(MethodImplOptions.InternalCall)]
  159. internal extern static void godot_icall_Array_RemoveAt(IntPtr ptr, int index);
  160. [MethodImpl(MethodImplOptions.InternalCall)]
  161. internal extern static Error godot_icall_Array_Resize(IntPtr ptr, int newSize);
  162. [MethodImpl(MethodImplOptions.InternalCall)]
  163. internal extern static void godot_icall_Array_Generic_GetElementTypeInfo(Type elemType, out int elemTypeEncoding, out IntPtr elemTypeClass);
  164. [MethodImpl(MethodImplOptions.InternalCall)]
  165. internal extern static string godot_icall_Array_ToString(IntPtr ptr);
  166. }
  167. public class Array<T> : IList<T>, ICollection<T>, IEnumerable<T>
  168. {
  169. Array objectArray;
  170. internal static int elemTypeEncoding;
  171. internal static IntPtr elemTypeClass;
  172. static Array()
  173. {
  174. Array.godot_icall_Array_Generic_GetElementTypeInfo(typeof(T), out elemTypeEncoding, out elemTypeClass);
  175. }
  176. public Array()
  177. {
  178. objectArray = new Array();
  179. }
  180. public Array(IEnumerable<T> collection)
  181. {
  182. if (collection == null)
  183. throw new NullReferenceException($"Parameter '{nameof(collection)} cannot be null.'");
  184. objectArray = new Array(collection);
  185. }
  186. public Array(params T[] array) : this()
  187. {
  188. if (array == null)
  189. {
  190. throw new NullReferenceException($"Parameter '{nameof(array)} cannot be null.'");
  191. }
  192. objectArray = new Array(array);
  193. }
  194. public Array(Array array)
  195. {
  196. objectArray = array;
  197. }
  198. internal Array(IntPtr handle)
  199. {
  200. objectArray = new Array(handle);
  201. }
  202. internal Array(ArraySafeHandle handle)
  203. {
  204. objectArray = new Array(handle);
  205. }
  206. internal IntPtr GetPtr()
  207. {
  208. return objectArray.GetPtr();
  209. }
  210. public static explicit operator Array(Array<T> from)
  211. {
  212. return from.objectArray;
  213. }
  214. public Array<T> Duplicate(bool deep = false)
  215. {
  216. return new Array<T>(objectArray.Duplicate(deep));
  217. }
  218. public Error Resize(int newSize)
  219. {
  220. return objectArray.Resize(newSize);
  221. }
  222. public static Array<T> operator +(Array<T> left, Array<T> right)
  223. {
  224. return new Array<T>(left.objectArray + right.objectArray);
  225. }
  226. // IList<T>
  227. public T this[int index]
  228. {
  229. get { return (T)Array.godot_icall_Array_At_Generic(GetPtr(), index, elemTypeEncoding, elemTypeClass); }
  230. set { objectArray[index] = value; }
  231. }
  232. public int IndexOf(T item)
  233. {
  234. return objectArray.IndexOf(item);
  235. }
  236. public void Insert(int index, T item)
  237. {
  238. objectArray.Insert(index, item);
  239. }
  240. public void RemoveAt(int index)
  241. {
  242. objectArray.RemoveAt(index);
  243. }
  244. // ICollection<T>
  245. public int Count
  246. {
  247. get { return objectArray.Count; }
  248. }
  249. public bool IsReadOnly
  250. {
  251. get { return objectArray.IsReadOnly; }
  252. }
  253. public void Add(T item)
  254. {
  255. objectArray.Add(item);
  256. }
  257. public void Clear()
  258. {
  259. objectArray.Clear();
  260. }
  261. public bool Contains(T item)
  262. {
  263. return objectArray.Contains(item);
  264. }
  265. public void CopyTo(T[] array, int arrayIndex)
  266. {
  267. if (array == null)
  268. throw new ArgumentNullException(nameof(array), "Value cannot be null.");
  269. if (arrayIndex < 0)
  270. throw new ArgumentOutOfRangeException(nameof(arrayIndex), "Number was less than the array's lower bound in the first dimension.");
  271. // TODO This may be quite slow because every element access is an internal call.
  272. // It could be moved entirely to an internal call if we find out how to do the cast there.
  273. int count = objectArray.Count;
  274. if (array.Length < (arrayIndex + count))
  275. throw new ArgumentException("Destination array was not long enough. Check destIndex and length, and the array's lower bounds.");
  276. for (int i = 0; i < count; i++)
  277. {
  278. array[arrayIndex] = (T)this[i];
  279. arrayIndex++;
  280. }
  281. }
  282. public bool Remove(T item)
  283. {
  284. return Array.godot_icall_Array_Remove(GetPtr(), item);
  285. }
  286. // IEnumerable<T>
  287. public IEnumerator<T> GetEnumerator()
  288. {
  289. int count = objectArray.Count;
  290. for (int i = 0; i < count; i++)
  291. {
  292. yield return (T)this[i];
  293. }
  294. }
  295. IEnumerator IEnumerable.GetEnumerator()
  296. {
  297. return GetEnumerator();
  298. }
  299. public override string ToString() => objectArray.ToString();
  300. }
  301. }