Dictionary.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 DictionarySafeHandle : SafeHandle
  9. {
  10. public DictionarySafeHandle(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. Dictionary.godot_icall_Dictionary_Dtor(handle);
  24. return true;
  25. }
  26. }
  27. public class Dictionary :
  28. IDictionary<object, object>,
  29. ICollection<KeyValuePair<object, object>>,
  30. IEnumerable<KeyValuePair<object, object>>,
  31. IDisposable
  32. {
  33. [MethodImpl(MethodImplOptions.InternalCall)]
  34. internal extern static IntPtr godot_icall_Dictionary_Ctor();
  35. [MethodImpl(MethodImplOptions.InternalCall)]
  36. internal extern static void godot_icall_Dictionary_Dtor(IntPtr ptr);
  37. [MethodImpl(MethodImplOptions.InternalCall)]
  38. internal extern static object godot_icall_Dictionary_GetValue(IntPtr ptr, object key);
  39. [MethodImpl(MethodImplOptions.InternalCall)]
  40. internal extern static void godot_icall_Dictionary_SetValue(IntPtr ptr, object key, object value);
  41. [MethodImpl(MethodImplOptions.InternalCall)]
  42. internal extern static IntPtr godot_icall_Dictionary_Keys(IntPtr ptr);
  43. [MethodImpl(MethodImplOptions.InternalCall)]
  44. internal extern static IntPtr godot_icall_Dictionary_Values(IntPtr ptr);
  45. [MethodImpl(MethodImplOptions.InternalCall)]
  46. internal extern static int godot_icall_Dictionary_Count(IntPtr ptr);
  47. [MethodImpl(MethodImplOptions.InternalCall)]
  48. internal extern static void godot_icall_Dictionary_Add(IntPtr ptr, object key, object value);
  49. [MethodImpl(MethodImplOptions.InternalCall)]
  50. internal extern static void godot_icall_Dictionary_Clear(IntPtr ptr);
  51. [MethodImpl(MethodImplOptions.InternalCall)]
  52. internal extern static bool godot_icall_Dictionary_Contains(IntPtr ptr, object key, object value);
  53. [MethodImpl(MethodImplOptions.InternalCall)]
  54. internal extern static bool godot_icall_Dictionary_ContainsKey(IntPtr ptr, object key);
  55. [MethodImpl(MethodImplOptions.InternalCall)]
  56. internal extern static bool godot_icall_Dictionary_RemoveKey(IntPtr ptr, object key);
  57. [MethodImpl(MethodImplOptions.InternalCall)]
  58. internal extern static bool godot_icall_Dictionary_Remove(IntPtr ptr, object key, object value);
  59. [MethodImpl(MethodImplOptions.InternalCall)]
  60. internal extern static bool godot_icall_Dictionary_TryGetValue(IntPtr ptr, object key, out object value);
  61. DictionarySafeHandle safeHandle;
  62. bool disposed = false;
  63. public Dictionary()
  64. {
  65. safeHandle = new DictionarySafeHandle(godot_icall_Dictionary_Ctor());
  66. }
  67. internal Dictionary(DictionarySafeHandle handle)
  68. {
  69. safeHandle = handle;
  70. }
  71. internal Dictionary(IntPtr handle)
  72. {
  73. safeHandle = new DictionarySafeHandle(handle);
  74. }
  75. internal IntPtr GetPtr()
  76. {
  77. return safeHandle.DangerousGetHandle();
  78. }
  79. public void Dispose()
  80. {
  81. Dispose(true);
  82. }
  83. protected virtual void Dispose(bool disposing)
  84. {
  85. if (disposed)
  86. return;
  87. if (safeHandle != null)
  88. {
  89. safeHandle.Dispose();
  90. safeHandle = null;
  91. }
  92. disposed = true;
  93. }
  94. public object this[object key]
  95. {
  96. get
  97. {
  98. return godot_icall_Dictionary_GetValue(GetPtr(), key);
  99. }
  100. set
  101. {
  102. godot_icall_Dictionary_SetValue(GetPtr(), key, value);
  103. }
  104. }
  105. public ICollection<object> Keys
  106. {
  107. get
  108. {
  109. IntPtr handle = godot_icall_Dictionary_Keys(GetPtr());
  110. return new Array(new ArraySafeHandle(handle));
  111. }
  112. }
  113. public ICollection<object> Values
  114. {
  115. get
  116. {
  117. IntPtr handle = godot_icall_Dictionary_Values(GetPtr());
  118. return new Array(new ArraySafeHandle(handle));
  119. }
  120. }
  121. public int Count
  122. {
  123. get
  124. {
  125. return godot_icall_Dictionary_Count(GetPtr());
  126. }
  127. }
  128. public bool IsReadOnly
  129. {
  130. get
  131. {
  132. return false;
  133. }
  134. }
  135. public void Add(object key, object value)
  136. {
  137. godot_icall_Dictionary_Add(GetPtr(), key, value);
  138. }
  139. public void Add(KeyValuePair<object, object> item)
  140. {
  141. Add(item.Key, item.Value);
  142. }
  143. public void Clear()
  144. {
  145. godot_icall_Dictionary_Clear(GetPtr());
  146. }
  147. public bool Contains(KeyValuePair<object, object> item)
  148. {
  149. return godot_icall_Dictionary_Contains(GetPtr(), item.Key, item.Value);
  150. }
  151. public bool ContainsKey(object key)
  152. {
  153. return godot_icall_Dictionary_ContainsKey(GetPtr(), key);
  154. }
  155. public void CopyTo(KeyValuePair<object, object>[] array, int arrayIndex)
  156. {
  157. // TODO 3 internal calls, can reduce to 1
  158. Array keys = (Array)Keys;
  159. Array values = (Array)Values;
  160. int count = Count;
  161. for (int i = 0; i < count; i++)
  162. {
  163. // TODO 2 internal calls, can reduce to 1
  164. array[arrayIndex] = new KeyValuePair<object, object>(keys[i], values[i]);
  165. arrayIndex++;
  166. }
  167. }
  168. public IEnumerator<KeyValuePair<object, object>> GetEnumerator()
  169. {
  170. // TODO 3 internal calls, can reduce to 1
  171. Array keys = (Array)Keys;
  172. Array values = (Array)Values;
  173. int count = Count;
  174. for (int i = 0; i < count; i++)
  175. {
  176. // TODO 2 internal calls, can reduce to 1
  177. yield return new KeyValuePair<object, object>(keys[i], values[i]);
  178. }
  179. }
  180. public bool Remove(object key)
  181. {
  182. return godot_icall_Dictionary_RemoveKey(GetPtr(), key);
  183. }
  184. public bool Remove(KeyValuePair<object, object> item)
  185. {
  186. return godot_icall_Dictionary_Remove(GetPtr(), item.Key, item.Value);
  187. }
  188. public bool TryGetValue(object key, out object value)
  189. {
  190. object retValue;
  191. bool found = godot_icall_Dictionary_TryGetValue(GetPtr(), key, out retValue);
  192. value = found ? retValue : default(object);
  193. return found;
  194. }
  195. IEnumerator IEnumerable.GetEnumerator()
  196. {
  197. return GetEnumerator();
  198. }
  199. }
  200. public class Dictionary<TKey, TValue> :
  201. IDictionary<TKey, TValue>,
  202. ICollection<KeyValuePair<TKey, TValue>>,
  203. IEnumerable<KeyValuePair<TKey, TValue>>
  204. {
  205. Dictionary objectDict;
  206. public Dictionary()
  207. {
  208. objectDict = new Dictionary();
  209. }
  210. public Dictionary(Dictionary dictionary)
  211. {
  212. objectDict = dictionary;
  213. }
  214. internal Dictionary(IntPtr handle)
  215. {
  216. objectDict = new Dictionary(handle);
  217. }
  218. internal Dictionary(DictionarySafeHandle handle)
  219. {
  220. objectDict = new Dictionary(handle);
  221. }
  222. public static explicit operator Dictionary(Dictionary<TKey, TValue> from)
  223. {
  224. return from.objectDict;
  225. }
  226. public TValue this[TKey key]
  227. {
  228. get
  229. {
  230. return (TValue)objectDict[key];
  231. }
  232. set
  233. {
  234. objectDict[key] = value;
  235. }
  236. }
  237. public ICollection<TKey> Keys
  238. {
  239. get
  240. {
  241. IntPtr handle = Dictionary.godot_icall_Dictionary_Keys(objectDict.GetPtr());
  242. return new Array<TKey>(new ArraySafeHandle(handle));
  243. }
  244. }
  245. public ICollection<TValue> Values
  246. {
  247. get
  248. {
  249. IntPtr handle = Dictionary.godot_icall_Dictionary_Values(objectDict.GetPtr());
  250. return new Array<TValue>(new ArraySafeHandle(handle));
  251. }
  252. }
  253. public int Count
  254. {
  255. get
  256. {
  257. return objectDict.Count;
  258. }
  259. }
  260. public bool IsReadOnly
  261. {
  262. get
  263. {
  264. return objectDict.IsReadOnly;
  265. }
  266. }
  267. public void Add(TKey key, TValue value)
  268. {
  269. objectDict.Add(key, value);
  270. }
  271. public void Add(KeyValuePair<TKey, TValue> item)
  272. {
  273. objectDict.Add(item.Key, item.Value);
  274. }
  275. public void Clear()
  276. {
  277. objectDict.Clear();
  278. }
  279. public bool Contains(KeyValuePair<TKey, TValue> item)
  280. {
  281. return objectDict.Contains(new KeyValuePair<object, object>(item.Key, item.Value));
  282. }
  283. public bool ContainsKey(TKey key)
  284. {
  285. return objectDict.ContainsKey(key);
  286. }
  287. public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
  288. {
  289. // TODO 3 internal calls, can reduce to 1
  290. Array<TKey> keys = (Array<TKey>)Keys;
  291. Array<TValue> values = (Array<TValue>)Values;
  292. int count = Count;
  293. for (int i = 0; i < count; i++)
  294. {
  295. // TODO 2 internal calls, can reduce to 1
  296. array[arrayIndex] = new KeyValuePair<TKey, TValue>(keys[i], values[i]);
  297. arrayIndex++;
  298. }
  299. }
  300. public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
  301. {
  302. // TODO 3 internal calls, can reduce to 1
  303. Array<TKey> keys = (Array<TKey>)Keys;
  304. Array<TValue> values = (Array<TValue>)Values;
  305. int count = Count;
  306. for (int i = 0; i < count; i++)
  307. {
  308. // TODO 2 internal calls, can reduce to 1
  309. yield return new KeyValuePair<TKey, TValue>(keys[i], values[i]);
  310. }
  311. }
  312. public bool Remove(TKey key)
  313. {
  314. return objectDict.Remove(key);
  315. }
  316. public bool Remove(KeyValuePair<TKey, TValue> item)
  317. {
  318. return objectDict.Remove(new KeyValuePair<object, object>(item.Key, item.Value));
  319. }
  320. public bool TryGetValue(TKey key, out TValue value)
  321. {
  322. object retValue;
  323. bool found = objectDict.TryGetValue(key, out retValue);
  324. value = found ? (TValue)retValue : default(TValue);
  325. return found;
  326. }
  327. IEnumerator IEnumerable.GetEnumerator()
  328. {
  329. return GetEnumerator();
  330. }
  331. }
  332. }