Vectors.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Reflection;
  5. using System.Runtime.InteropServices;
  6. namespace Urho
  7. {
  8. internal static class Vectors {
  9. [DllImport (Consts.NativeImport, CallingConvention=CallingConvention.Cdecl)]
  10. internal extern static int VectorSharedPtr_Count (IntPtr h);
  11. [DllImport (Consts.NativeImport, CallingConvention=CallingConvention.Cdecl)]
  12. internal extern static IntPtr VectorSharedPtr_GetIdx (IntPtr h, int idx);
  13. [DllImport (Consts.NativeImport, CallingConvention=CallingConvention.Cdecl)]
  14. internal extern static void VectorSharedPtr_SetIdx (IntPtr h, int idx, IntPtr v);
  15. internal class ProxyUrhoObject<T> : ProxyRefCounted<T>, IReadOnlyList<T> where T : UrhoObject
  16. {
  17. public ProxyUrhoObject(IntPtr handle) : base(handle) { }
  18. public override T this[int idx] => Runtime.LookupObject<T>(VectorSharedPtr_GetIdx(handle, idx));
  19. IEnumerator IEnumerable.GetEnumerator() => new ProxyUrhoObjectEnumerator<UrhoObject>(handle);
  20. public override IEnumerator<T> GetEnumerator() => new ProxyUrhoObjectEnumerator<T>(handle);
  21. class ProxyUrhoObjectEnumerator<U> : IEnumerator, IEnumerator<U> where U : UrhoObject
  22. {
  23. readonly IntPtr handle;
  24. int index;
  25. U current;
  26. public ProxyUrhoObjectEnumerator(IntPtr handle)
  27. {
  28. this.handle = handle;
  29. }
  30. public bool MoveNext()
  31. {
  32. var count = VectorSharedPtr_Count(handle);
  33. if (count < 1 || count <= index)
  34. return false;
  35. current = Runtime.LookupObject<U>(VectorSharedPtr_GetIdx(handle, index));
  36. index++;
  37. return true;
  38. }
  39. public void Reset()
  40. {
  41. index = 0;
  42. current = null;
  43. }
  44. U IEnumerator<U>.Current => current;
  45. public object Current => current;
  46. public void Dispose()
  47. {
  48. Reset();
  49. }
  50. }
  51. }
  52. internal class ProxyRefCounted<T> : IReadOnlyList<T> where T : RefCounted
  53. {
  54. protected IntPtr handle;
  55. public ProxyRefCounted(IntPtr handle)
  56. {
  57. this.handle = handle;
  58. }
  59. public virtual T this [int idx] => Runtime.LookupRefCounted<T> (VectorSharedPtr_GetIdx (handle, idx));
  60. public int Count => VectorSharedPtr_Count (handle);
  61. IEnumerator IEnumerable.GetEnumerator () => new ProxyRefCountedEnumerator<RefCounted>(handle);
  62. public virtual IEnumerator<T> GetEnumerator () => new ProxyRefCountedEnumerator<T>(handle);
  63. class ProxyRefCountedEnumerator<U> : IEnumerator, IEnumerator<U> where U : RefCounted
  64. {
  65. readonly IntPtr handle;
  66. int index;
  67. U current;
  68. public ProxyRefCountedEnumerator(IntPtr handle)
  69. {
  70. this.handle = handle;
  71. }
  72. public bool MoveNext()
  73. {
  74. var count = VectorSharedPtr_Count(handle);
  75. if (count < 1 || count <= index)
  76. return false;
  77. current = Runtime.LookupRefCounted<U>(VectorSharedPtr_GetIdx(handle, index));
  78. index++;
  79. return true;
  80. }
  81. public void Reset()
  82. {
  83. index = 0;
  84. current = null;
  85. }
  86. U IEnumerator<U>.Current => current;
  87. public object Current => current;
  88. public void Dispose()
  89. {
  90. Reset();
  91. }
  92. }
  93. }
  94. }
  95. }