#if UNITY_XR_MAGICLEAP_PROVIDER // This class also exists inside Unity's Magic Leap XR Plugin so we want to avoid naming conflicts. #else using System; using Unity.Collections; namespace UnityEngine.XR.MagicLeap { /// /// Has List-like semantics (Capacity and Length) using a NativeArray as the backing store. /// The NativeArray is never resized. This is useful for times you don't know how big the /// array will be, but there is a definite upper bound. /// This list supports duck-typed foreach Enumerator semantics. /// internal struct NativeFixedList : IEquatable>, IDisposable where T : struct { /// /// Allocates a new NativeFixedList with . /// Caller must Dispose the array when no longer needed. /// /// /// public NativeFixedList(int Capacity, Allocator allocator) { m_NativeArray = new NativeArray(Capacity, allocator); Length = 0; } /// /// Create a NativeFixedList from an existing NativeArray and a length. /// This NativeFixedList now owns the memory and should be Disposed when finished. /// /// The array to take ownership of /// The number of elements actually used by the NativeArray public NativeFixedList(NativeArray other, int length) { m_NativeArray = other; Length = length; } public bool IsCreated { get { return m_NativeArray.IsCreated; } } public int Capacity { get { return m_NativeArray.Length; } } public int Length { get; private set; } public T this[int index] { get { return m_NativeArray[index]; } set { m_NativeArray[index] = value; } } public void Clear() { Length = 0; } public void Add(T item) { if (Length == Capacity) throw new InvalidOperationException($"Cannot Add when Length ({Length}) is already at ({Capacity})"); m_NativeArray[Length++] = item; } /// /// Copies the contents of this list to another NativeArray. /// must have the same Length as this list. /// /// The destination array public void CopyTo(NativeArray destination) { NativeArray.Copy(m_NativeArray, destination, Length); } public void Dispose() { m_NativeArray.Dispose(); } public override int GetHashCode() { unchecked { var hash = m_NativeArray.GetHashCode(); hash = hash * 486187739 + Length.GetHashCode(); return hash; } } public override bool Equals(object obj) { return ((obj is NativeFixedList) && Equals((NativeFixedList)obj)); } public bool Equals(NativeFixedList other) { return m_NativeArray.Equals(other.m_NativeArray) && (Length == other.Length); } public static bool operator ==(NativeFixedList lhs, NativeFixedList rhs) { return lhs.Equals(rhs); } public static bool operator !=(NativeFixedList lhs, NativeFixedList rhs) { return !lhs.Equals(rhs); } public Enumerator GetEnumerator() { return new Enumerator(this); } public struct Enumerator { internal Enumerator(NativeFixedList list) { m_Index = -1; m_NativeFixedList = list; } public bool MoveNext() { return ++m_Index < m_NativeFixedList.Length; } public void Reset() { m_Index = -1; } public T Current { get { return m_NativeFixedList[m_Index]; } } public void Dispose() { m_NativeFixedList = default(NativeFixedList); m_Index = 0; } int m_Index; NativeFixedList m_NativeFixedList; } NativeArray m_NativeArray; } } #endif