Array.cs 8.7 KB

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