using System; using System.Collections.Generic; using UnityEngine.XR.ARSubsystems; namespace UnityEngine.XR.ARFoundation { /// /// A collection for s. /// This collection implements an IEnumerable-like interface which can be used /// in a foreach statement. /// /// The concrete . public struct TrackableCollection : IEquatable> { /// /// Creates an Enumerator for this collection. /// /// An Enumerator. public Enumerator GetEnumerator() { return new Enumerator(m_Trackables); } /// /// Constructs a . /// /// A Dictionary of TTrackables. public TrackableCollection(Dictionary trackables) { if (trackables == null) throw new ArgumentNullException("trackables"); m_Trackables = trackables; } /// /// Returns the number of trackables in this collection. /// public int count { get { if (m_Trackables == null) throw new InvalidOperationException("This collection has not been initialized."); return m_Trackables.Count; } } /// /// Retrieves a TTrackables by TrackableId. /// /// The trackable id associated with the trackable to retrieve. /// The TTrackables if present. Throws KeyNotFoundException if the is not found. public TTrackable this[TrackableId trackableId] { get { if (m_Trackables == null) throw new InvalidOperationException("This collection has not been initialized."); try { return m_Trackables[trackableId]; } catch (KeyNotFoundException e) { throw new KeyNotFoundException( string.Format("Trackable with id {0} does not exist in this collection.", trackableId), e); } } } /// /// Retrieves the hashcode of the . /// /// The hashcode of if the is instantiated and 0 if it is null. public override int GetHashCode() { unchecked { return m_Trackables == null ? 0 : m_Trackables.GetHashCode(); } } /// /// Checks the equality of this object against this . /// /// The object that this collection should be checked against for equivalency. /// true if the object is a and is considered equivalent to this and false otherwise. public override bool Equals(object obj) { if (!(obj is TrackableCollection)) return false; return Equals((TrackableCollection) obj); } /// /// Checks the equality of this against another of the same TTrackable generic type. /// /// The that this collection should be checked against for equivalency. /// true if the two s are considered equivalent and false otherwise. public bool Equals(TrackableCollection other) { return ReferenceEquals(m_Trackables, other.m_Trackables); } /// /// Overloads the == operator to use the equals method for equality checking. /// /// The on the left hand side of the operator. /// The on the right hand side of the operator. /// true if the two s are considered equivalent and false otherwise. /// public static bool operator ==(TrackableCollection lhs, TrackableCollection rhs) { return lhs.Equals(rhs); } /// /// Overloads the != operator to use the equals method for equality checking. /// /// The on the left hand side of the operator. /// The on the right hand side of the operator. /// true if the two s are not considered equivalent and false otherwise. /// public static bool operator !=(TrackableCollection lhs, TrackableCollection rhs) { return !lhs.Equals(rhs); } /// /// Attempts to retrieve a trackable by TrackableId. /// /// The trackable id associated with the trackable to retrieve. /// Set to the trackable with , if present in the collection. /// true if the trackable with exists. false otherwise. public bool TryGetTrackable(TrackableId trackableId, out TTrackable trackable) { if (m_Trackables == null) throw new InvalidOperationException("This collection has not been initialized."); return m_Trackables.TryGetValue(trackableId, out trackable); } /// /// An Enumerator for TTrackables. /// public struct Enumerator { /// /// Constructs an Enumerator for use with TTrackables. /// /// A Dictionary of TTrackables. public Enumerator(Dictionary trackables) { if (trackables == null) throw new ArgumentNullException("trackables"); m_Enumerator = trackables.GetEnumerator(); } /// /// Moves to the next trackable in the collection. /// /// true if the next trackable is valid, or false if it is the end of the collection. public bool MoveNext() { return m_Enumerator.MoveNext(); } /// /// The current value in the collection. /// public TTrackable Current { get { return m_Enumerator.Current.Value; } } /// /// Releases all resources used by this Enumerator. /// public void Dispose() { m_Enumerator.Dispose(); } Dictionary.Enumerator m_Enumerator; } Dictionary m_Trackables; } }