Array.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. public Error Resize(int newSize)
  131. {
  132. return godot_icall_Array_Resize(GetPtr(), newSize);
  133. }
  134. IEnumerator IEnumerable.GetEnumerator()
  135. {
  136. return GetEnumerator();
  137. }
  138. [MethodImpl(MethodImplOptions.InternalCall)]
  139. internal extern static IntPtr godot_icall_Array_Ctor();
  140. [MethodImpl(MethodImplOptions.InternalCall)]
  141. internal extern static void godot_icall_Array_Dtor(IntPtr ptr);
  142. [MethodImpl(MethodImplOptions.InternalCall)]
  143. internal extern static object godot_icall_Array_At(IntPtr ptr, int index);
  144. [MethodImpl(MethodImplOptions.InternalCall)]
  145. internal extern static object godot_icall_Array_At_Generic(IntPtr ptr, int index, int elemTypeEncoding, IntPtr elemTypeClass);
  146. [MethodImpl(MethodImplOptions.InternalCall)]
  147. internal extern static void godot_icall_Array_SetAt(IntPtr ptr, int index, object value);
  148. [MethodImpl(MethodImplOptions.InternalCall)]
  149. internal extern static int godot_icall_Array_Count(IntPtr ptr);
  150. [MethodImpl(MethodImplOptions.InternalCall)]
  151. internal extern static void godot_icall_Array_Add(IntPtr ptr, object item);
  152. [MethodImpl(MethodImplOptions.InternalCall)]
  153. internal extern static void godot_icall_Array_Clear(IntPtr ptr);
  154. [MethodImpl(MethodImplOptions.InternalCall)]
  155. internal extern static bool godot_icall_Array_Contains(IntPtr ptr, object item);
  156. [MethodImpl(MethodImplOptions.InternalCall)]
  157. internal extern static void godot_icall_Array_CopyTo(IntPtr ptr, object[] array, int arrayIndex);
  158. [MethodImpl(MethodImplOptions.InternalCall)]
  159. internal extern static int godot_icall_Array_IndexOf(IntPtr ptr, object item);
  160. [MethodImpl(MethodImplOptions.InternalCall)]
  161. internal extern static void godot_icall_Array_Insert(IntPtr ptr, int index, object item);
  162. [MethodImpl(MethodImplOptions.InternalCall)]
  163. internal extern static bool godot_icall_Array_Remove(IntPtr ptr, object item);
  164. [MethodImpl(MethodImplOptions.InternalCall)]
  165. internal extern static void godot_icall_Array_RemoveAt(IntPtr ptr, int index);
  166. [MethodImpl(MethodImplOptions.InternalCall)]
  167. internal extern static Error godot_icall_Array_Resize(IntPtr ptr, int newSize);
  168. [MethodImpl(MethodImplOptions.InternalCall)]
  169. internal extern static void godot_icall_Array_Generic_GetElementTypeInfo(Type elemType, out int elemTypeEncoding, out IntPtr elemTypeClass);
  170. }
  171. public class Array<T> : IList<T>, ICollection<T>, IEnumerable<T>
  172. {
  173. Array objectArray;
  174. internal static int elemTypeEncoding;
  175. internal static IntPtr elemTypeClass;
  176. static Array()
  177. {
  178. Array.godot_icall_Array_Generic_GetElementTypeInfo(typeof(T), out elemTypeEncoding, out elemTypeClass);
  179. }
  180. public Array()
  181. {
  182. objectArray = new Array();
  183. }
  184. public Array(Array array)
  185. {
  186. objectArray = array;
  187. }
  188. internal Array(IntPtr handle)
  189. {
  190. objectArray = new Array(handle);
  191. }
  192. internal Array(ArraySafeHandle handle)
  193. {
  194. objectArray = new Array(handle);
  195. }
  196. public static explicit operator Array(Array<T> from)
  197. {
  198. return from.objectArray;
  199. }
  200. public T this[int index]
  201. {
  202. get
  203. {
  204. return (T)Array.godot_icall_Array_At_Generic(GetPtr(), index, elemTypeEncoding, elemTypeClass);
  205. }
  206. set
  207. {
  208. objectArray[index] = value;
  209. }
  210. }
  211. public int Count
  212. {
  213. get
  214. {
  215. return objectArray.Count;
  216. }
  217. }
  218. public bool IsReadOnly
  219. {
  220. get
  221. {
  222. return objectArray.IsReadOnly;
  223. }
  224. }
  225. public void Add(T item)
  226. {
  227. objectArray.Add(item);
  228. }
  229. public void Clear()
  230. {
  231. objectArray.Clear();
  232. }
  233. public bool Contains(T item)
  234. {
  235. return objectArray.Contains(item);
  236. }
  237. public void CopyTo(T[] array, int arrayIndex)
  238. {
  239. if (array == null)
  240. throw new ArgumentNullException(nameof(array), "Value cannot be null.");
  241. if (arrayIndex < 0)
  242. throw new ArgumentOutOfRangeException(nameof(arrayIndex), "Number was less than the array's lower bound in the first dimension.");
  243. // TODO This may be quite slow because every element access is an internal call.
  244. // It could be moved entirely to an internal call if we find out how to do the cast there.
  245. int count = objectArray.Count;
  246. if (array.Length < (arrayIndex + count))
  247. throw new ArgumentException("Destination array was not long enough. Check destIndex and length, and the array's lower bounds.");
  248. for (int i = 0; i < count; i++)
  249. {
  250. array[arrayIndex] = (T)this[i];
  251. arrayIndex++;
  252. }
  253. }
  254. public IEnumerator<T> GetEnumerator()
  255. {
  256. int count = objectArray.Count;
  257. for (int i = 0; i < count; i++)
  258. {
  259. yield return (T)this[i];
  260. }
  261. }
  262. public int IndexOf(T item)
  263. {
  264. return objectArray.IndexOf(item);
  265. }
  266. public void Insert(int index, T item)
  267. {
  268. objectArray.Insert(index, item);
  269. }
  270. public bool Remove(T item)
  271. {
  272. return objectArray.Remove(item);
  273. }
  274. public void RemoveAt(int index)
  275. {
  276. objectArray.RemoveAt(index);
  277. }
  278. public Error Resize(int newSize)
  279. {
  280. return objectArray.Resize(newSize);
  281. }
  282. IEnumerator IEnumerable.GetEnumerator()
  283. {
  284. return GetEnumerator();
  285. }
  286. internal IntPtr GetPtr()
  287. {
  288. return objectArray.GetPtr();
  289. }
  290. }
  291. }