Array.cs 9.3 KB

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