|
1 |
| -using Box2D.Core; |
2 |
| -using System; |
3 |
| -using System.Runtime.CompilerServices; |
4 |
| -using System.Runtime.InteropServices; |
5 |
| - |
6 |
| -namespace Box2D.Collections; |
7 |
| - |
8 |
| -/// <summary> |
9 |
| -/// Represents a managed reference to an unmanaged Box2D array. |
10 |
| -/// </summary> |
11 |
| -/// <typeparam name="T">The array item type.</typeparam> |
12 |
| -public readonly ref struct ArrayRef<T> where T : struct |
13 |
| -{ |
14 |
| - private static readonly int _elementSize = Marshal.SizeOf<T>(); |
15 |
| - |
16 |
| - private readonly IntPtr _native; |
17 |
| - |
18 |
| - /// <summary> |
19 |
| - /// Gets the length of the array. |
20 |
| - /// </summary> |
21 |
| - public int Length { get; } |
22 |
| - |
23 |
| - /// <summary> |
24 |
| - /// Gets whether the <see cref="ArrayRef{T}"/> points to a null |
25 |
| - /// unmanaged array. |
26 |
| - /// </summary> |
27 |
| - public bool IsNull => _native == IntPtr.Zero; |
28 |
| - |
29 |
| - /// <summary> |
30 |
| - /// Gets or sets the value of an item in the array. |
31 |
| - /// </summary> |
32 |
| - /// <param name="index">The index of the item in the array.</param> |
33 |
| - public T this[int index] |
34 |
| - { |
35 |
| - get |
36 |
| - { |
37 |
| - ThrowIfInvalidIndex(index); |
38 |
| - return Marshal.PtrToStructure<T>(_native + _elementSize * index); |
39 |
| - } |
40 |
| - set |
41 |
| - { |
42 |
| - ThrowIfInvalidIndex(index); |
43 |
| - Marshal.StructureToPtr(value, _native + _elementSize * index, false); |
44 |
| - } |
45 |
| - } |
46 |
| - |
47 |
| - internal ArrayRef(IntPtr native, int length) |
48 |
| - { |
49 |
| - _native = native; |
50 |
| - Length = length; |
51 |
| - } |
52 |
| - |
53 |
| - [MethodImpl(MethodImplOptions.AggressiveInlining)] |
54 |
| - private void ThrowIfInvalidIndex(int index) |
55 |
| - { |
56 |
| - Errors.ThrowIfNull(_native, nameof(ArrayRef<T>)); |
57 |
| - |
58 |
| - if (index < 0 || index >= Length) |
59 |
| - { |
60 |
| - throw new IndexOutOfRangeException(); |
61 |
| - } |
62 |
| - } |
63 |
| - |
64 |
| - /// <summary> |
65 |
| - /// Gets an <see cref="Enumerator"/> for the current <see cref="ArrayRef{T}"/> instance. |
66 |
| - /// </summary> |
67 |
| - public Enumerator GetEnumerator() |
68 |
| - => new(in this); |
69 |
| - |
70 |
| - /// <summary> |
71 |
| - /// An enumerator for <see cref="ArrayRef{T}"/> instances. |
72 |
| - /// </summary> |
73 |
| - public ref struct Enumerator |
74 |
| - { |
75 |
| - private readonly ArrayRef<T> _source; |
76 |
| - private int _index = -1; |
77 |
| - |
78 |
| - /// <summary> |
79 |
| - /// Gets the current element. |
80 |
| - /// </summary> |
81 |
| - public T Current => _source[_index]; |
82 |
| - |
83 |
| - /// <summary> |
84 |
| - /// Constructs a new <see cref="Enumerator"/> instance. |
85 |
| - /// </summary> |
86 |
| - public Enumerator(in ArrayRef<T> source) |
87 |
| - { |
88 |
| - _source = source; |
89 |
| - } |
90 |
| - |
91 |
| - /// <summary> |
92 |
| - /// Moves to the next element. |
93 |
| - /// </summary> |
94 |
| - public bool MoveNext() |
95 |
| - => ++_index < _source.Length; |
96 |
| - } |
97 |
| -} |
| 1 | +using Box2D.Core; |
| 2 | +using System; |
| 3 | +using System.Runtime.CompilerServices; |
| 4 | +using System.Runtime.InteropServices; |
| 5 | + |
| 6 | +namespace Box2D.Collections; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// Represents a managed reference to an unmanaged Box2D array. |
| 10 | +/// </summary> |
| 11 | +/// <typeparam name="T">The array item type.</typeparam> |
| 12 | +public readonly ref struct ArrayRef<T> where T : struct |
| 13 | +{ |
| 14 | + private static readonly int _elementSize = Marshal.SizeOf<T>(); |
| 15 | + |
| 16 | + private readonly IntPtr _native; |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// Gets the length of the array. |
| 20 | + /// </summary> |
| 21 | + public int Length { get; } |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Gets whether the <see cref="ArrayRef{T}"/> points to a null |
| 25 | + /// unmanaged array. |
| 26 | + /// </summary> |
| 27 | + public bool IsNull => _native == IntPtr.Zero; |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Gets or sets the value of an item in the array. |
| 31 | + /// </summary> |
| 32 | + /// <param name="index">The index of the item in the array.</param> |
| 33 | + public T this[int index] |
| 34 | + { |
| 35 | + get |
| 36 | + { |
| 37 | + ThrowIfInvalidIndex(index); |
| 38 | + return Marshal.PtrToStructure<T>(_native + _elementSize * index); |
| 39 | + } |
| 40 | + set |
| 41 | + { |
| 42 | + ThrowIfInvalidIndex(index); |
| 43 | + Marshal.StructureToPtr(value, _native + _elementSize * index, false); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + internal ArrayRef(IntPtr native, int length) |
| 48 | + { |
| 49 | + _native = native; |
| 50 | + Length = length; |
| 51 | + } |
| 52 | + |
| 53 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 54 | + private void ThrowIfInvalidIndex(int index) |
| 55 | + { |
| 56 | + Errors.ThrowIfNull(_native, nameof(ArrayRef<T>)); |
| 57 | + |
| 58 | + if (index < 0 || index >= Length) |
| 59 | + { |
| 60 | + throw new IndexOutOfRangeException(); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Gets an <see cref="Enumerator"/> for the current <see cref="ArrayRef{T}"/> instance. |
| 66 | + /// </summary> |
| 67 | + public Enumerator GetEnumerator() |
| 68 | + => new(in this); |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// An enumerator for <see cref="ArrayRef{T}"/> instances. |
| 72 | + /// </summary> |
| 73 | + public ref struct Enumerator |
| 74 | + { |
| 75 | + private readonly ArrayRef<T> _source; |
| 76 | + private int _index = -1; |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Gets the current element. |
| 80 | + /// </summary> |
| 81 | + public T Current => _source[_index]; |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// Constructs a new <see cref="Enumerator"/> instance. |
| 85 | + /// </summary> |
| 86 | + public Enumerator(scoped in ArrayRef<T> source) |
| 87 | + { |
| 88 | + _source = source; |
| 89 | + } |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Moves to the next element. |
| 93 | + /// </summary> |
| 94 | + public bool MoveNext() |
| 95 | + => ++_index < _source.Length; |
| 96 | + } |
| 97 | +} |
0 commit comments