Array.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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
  17. {
  18. return handle == IntPtr.Zero;
  19. }
  20. }
  21. protected override bool ReleaseHandle()
  22. {
  23. Array.godot_icall_Array_Dtor(handle);
  24. return true;
  25. }
  26. }
  27. public class Array : IList<object>, ICollection<object>, IEnumerable<object>, IDisposable
  28. {
  29. ArraySafeHandle safeHandle;
  30. bool disposed = false;
  31. public Array()
  32. {
  33. safeHandle = new ArraySafeHandle(godot_icall_Array_Ctor());
  34. }
  35. internal Array(ArraySafeHandle handle)
  36. {
  37. safeHandle = handle;
  38. }
  39. internal Array(IntPtr handle)
  40. {
  41. safeHandle = new ArraySafeHandle(handle);
  42. }
  43. internal IntPtr GetPtr()
  44. {
  45. return safeHandle.DangerousGetHandle();
  46. }
  47. public void Dispose()
  48. {
  49. if (disposed)
  50. return;
  51. if (safeHandle != null)
  52. {
  53. safeHandle.Dispose();
  54. safeHandle = null;
  55. }
  56. disposed = true;
  57. }
  58. public object this[int index]
  59. {
  60. get
  61. {
  62. return godot_icall_Array_At(GetPtr(), index);
  63. }
  64. set
  65. {
  66. godot_icall_Array_SetAt(GetPtr(), index, value);
  67. }
  68. }
  69. public int Count
  70. {
  71. get
  72. {
  73. return godot_icall_Array_Count(GetPtr());
  74. }
  75. }
  76. public bool IsReadOnly
  77. {
  78. get
  79. {
  80. return false;
  81. }
  82. }
  83. public void Add(object item)
  84. {
  85. godot_icall_Array_Add(GetPtr(), item);
  86. }
  87. public void Clear()
  88. {
  89. godot_icall_Array_Clear(GetPtr());
  90. }
  91. public bool Contains(object item)
  92. {
  93. return godot_icall_Array_Contains(GetPtr(), item);
  94. }
  95. public void CopyTo(object[] array, int arrayIndex)
  96. {
  97. if (array == null)
  98. throw new ArgumentNullException(nameof(array), "Value cannot be null.");
  99. if (arrayIndex < 0)
  100. throw new ArgumentOutOfRangeException(nameof(arrayIndex), "Number was less than the array's lower bound in the first dimension.");
  101. // Internal call may throw ArgumentException
  102. godot_icall_Array_CopyTo(GetPtr(), array, arrayIndex);
  103. }
  104. public IEnumerator<object> GetEnumerator()
  105. {
  106. int count = Count;
  107. for (int i = 0; i < count; i++)
  108. {
  109. yield return godot_icall_Array_At(GetPtr(), i);
  110. }
  111. }
  112. public int IndexOf(object item)
  113. {
  114. return godot_icall_Array_IndexOf(GetPtr(), item);
  115. }
  116. public void Insert(int index, object item)
  117. {
  118. godot_icall_Array_Insert(GetPtr(), index, item);
  119. }
  120. public bool Remove(object item)
  121. {
  122. return godot_icall_Array_Remove(GetPtr(), item);
  123. }
  124. public void RemoveAt(int index)
  125. {
  126. godot_icall_Array_RemoveAt(GetPtr(), index);
  127. }
  128. IEnumerator IEnumerable.GetEnumerator()
  129. {
  130. return GetEnumerator();
  131. }
  132. [MethodImpl(MethodImplOptions.InternalCall)]
  133. internal extern static IntPtr godot_icall_Array_Ctor();
  134. [MethodImpl(MethodImplOptions.InternalCall)]
  135. internal extern static void godot_icall_Array_Dtor(IntPtr ptr);
  136. [MethodImpl(MethodImplOptions.InternalCall)]
  137. internal extern static object godot_icall_Array_At(IntPtr ptr, int index);
  138. [MethodImpl(MethodImplOptions.InternalCall)]
  139. internal extern static void godot_icall_Array_SetAt(IntPtr ptr, int index, object value);
  140. [MethodImpl(MethodImplOptions.InternalCall)]
  141. internal extern static int godot_icall_Array_Count(IntPtr ptr);
  142. [MethodImpl(MethodImplOptions.InternalCall)]
  143. internal extern static void godot_icall_Array_Add(IntPtr ptr, object item);
  144. [MethodImpl(MethodImplOptions.InternalCall)]
  145. internal extern static void godot_icall_Array_Clear(IntPtr ptr);
  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, object[] array, int arrayIndex);
  150. [MethodImpl(MethodImplOptions.InternalCall)]
  151. internal extern static int godot_icall_Array_IndexOf(IntPtr ptr, object item);
  152. [MethodImpl(MethodImplOptions.InternalCall)]
  153. internal extern static void godot_icall_Array_Insert(IntPtr ptr, int index, object item);
  154. [MethodImpl(MethodImplOptions.InternalCall)]
  155. internal extern static bool godot_icall_Array_Remove(IntPtr ptr, object item);
  156. [MethodImpl(MethodImplOptions.InternalCall)]
  157. internal extern static void godot_icall_Array_RemoveAt(IntPtr ptr, int index);
  158. }
  159. public class Array<T> : IList<T>, ICollection<T>, IEnumerable<T>
  160. {
  161. Array objectArray;
  162. public Array()
  163. {
  164. objectArray = new Array();
  165. }
  166. public Array(Array array)
  167. {
  168. objectArray = array;
  169. }
  170. internal Array(IntPtr handle)
  171. {
  172. objectArray = new Array(handle);
  173. }
  174. internal Array(ArraySafeHandle handle)
  175. {
  176. objectArray = new Array(handle);
  177. }
  178. public static explicit operator Array(Array<T> from)
  179. {
  180. return from.objectArray;
  181. }
  182. public T this[int index]
  183. {
  184. get
  185. {
  186. return (T)objectArray[index];
  187. }
  188. set
  189. {
  190. objectArray[index] = value;
  191. }
  192. }
  193. public int Count
  194. {
  195. get
  196. {
  197. return objectArray.Count;
  198. }
  199. }
  200. public bool IsReadOnly
  201. {
  202. get
  203. {
  204. return objectArray.IsReadOnly;
  205. }
  206. }
  207. public void Add(T item)
  208. {
  209. objectArray.Add(item);
  210. }
  211. public void Clear()
  212. {
  213. objectArray.Clear();
  214. }
  215. public bool Contains(T item)
  216. {
  217. return objectArray.Contains(item);
  218. }
  219. public void CopyTo(T[] array, int arrayIndex)
  220. {
  221. if (array == null)
  222. throw new ArgumentNullException(nameof(array), "Value cannot be null.");
  223. if (arrayIndex < 0)
  224. throw new ArgumentOutOfRangeException(nameof(arrayIndex), "Number was less than the array's lower bound in the first dimension.");
  225. // TODO This may be quite slow because every element access is an internal call.
  226. // It could be moved entirely to an internal call if we find out how to do the cast there.
  227. int count = objectArray.Count;
  228. if (array.Length < (arrayIndex + count))
  229. throw new ArgumentException("Destination array was not long enough. Check destIndex and length, and the array's lower bounds.");
  230. for (int i = 0; i < count; i++)
  231. {
  232. array[arrayIndex] = (T)objectArray[i];
  233. arrayIndex++;
  234. }
  235. }
  236. public IEnumerator<T> GetEnumerator()
  237. {
  238. int count = objectArray.Count;
  239. for (int i = 0; i < count; i++)
  240. {
  241. yield return (T)objectArray[i];
  242. }
  243. }
  244. public int IndexOf(T item)
  245. {
  246. return objectArray.IndexOf(item);
  247. }
  248. public void Insert(int index, T item)
  249. {
  250. objectArray.Insert(index, item);
  251. }
  252. public bool Remove(T item)
  253. {
  254. return objectArray.Remove(item);
  255. }
  256. public void RemoveAt(int index)
  257. {
  258. objectArray.RemoveAt(index);
  259. }
  260. IEnumerator IEnumerable.GetEnumerator()
  261. {
  262. return GetEnumerator();
  263. }
  264. internal IntPtr GetPtr()
  265. {
  266. return objectArray.GetPtr();
  267. }
  268. }
  269. }