SW 중심대학 OSS GIT 서버 박건태, 이승준, 고기완, 이준호 새로운 배포
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 lines
8.4 KiB

4 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine.XR.ARSubsystems;
  4. namespace UnityEngine.XR.ARFoundation
  5. {
  6. /// <summary>
  7. /// A collection for <see cref="ARTrackable{TSessionRelativeData, TTrackable}"/>s.
  8. /// This collection implements an IEnumerable-like interface which can be used
  9. /// in a <c>foreach</c> statement.
  10. /// </summary>
  11. /// <typeparam name="TTrackable">The concrete <see cref="ARTrackable{TSessionRelativeData, TTrackable}"/>.</typeparam>
  12. public struct TrackableCollection<TTrackable> : IEquatable<TrackableCollection<TTrackable>>
  13. {
  14. /// <summary>
  15. /// Creates an <c>Enumerator</c> for this collection.
  16. /// </summary>
  17. /// <returns></returns>
  18. public Enumerator GetEnumerator()
  19. {
  20. return new Enumerator(m_Trackables);
  21. }
  22. /// <summary>
  23. /// Constructs a <see cref="TrackableCollection{TSessionRelativeData, TTrackable}"/>.
  24. /// </summary>
  25. /// <param name="trackables"></param>
  26. public TrackableCollection(Dictionary<TrackableId, TTrackable> trackables)
  27. {
  28. if (trackables == null)
  29. throw new ArgumentNullException("trackables");
  30. m_Trackables = trackables;
  31. }
  32. /// <summary>
  33. /// Returns the number of trackables in this collection.
  34. /// </summary>
  35. public int count
  36. {
  37. get
  38. {
  39. if (m_Trackables == null)
  40. throw new InvalidOperationException("This collection has not been initialized.");
  41. return m_Trackables.Count;
  42. }
  43. }
  44. /// <summary>
  45. /// Retrieves a <c>TTrackable</c>s by <c>TrackableId</c>.
  46. /// </summary>
  47. /// <param name="trackableId">The trackable id associated with the trackable to retrieve.</param>
  48. /// <returns>The <c>TTrackable</c>s if present. Throws <c>KeyNotFoundException</c> if the <paramref name="trackableId"/> is not found.</returns>
  49. public TTrackable this[TrackableId trackableId]
  50. {
  51. get
  52. {
  53. if (m_Trackables == null)
  54. throw new InvalidOperationException("This collection has not been initialized.");
  55. try
  56. {
  57. return m_Trackables[trackableId];
  58. }
  59. catch (KeyNotFoundException e)
  60. {
  61. throw new KeyNotFoundException(
  62. string.Format("Trackable with id {0} does not exist in this collection.", trackableId),
  63. e);
  64. }
  65. }
  66. }
  67. /// <summary>
  68. /// Retrieves the hashcode of the <see cref="TrackableCollection{TTrackable}"/>.
  69. /// </summary>
  70. /// <returns>The hashcode of if the <see cref="TrackableCollection{TTrackable}"/> is instantiated and <c>0</c> if it is <c>null</c>.</returns>
  71. public override int GetHashCode()
  72. {
  73. unchecked
  74. {
  75. return m_Trackables == null ? 0 : m_Trackables.GetHashCode();
  76. }
  77. }
  78. /// <summary>
  79. /// Checks the equality of this object against this <see cref="TrackableCollection{TTrackable}"/>.
  80. /// </summary>
  81. /// <param name="obj">The object that this collection should be checked against for equivalency.</param>
  82. /// <returns><c>true</c> if the object is a <see cref="TrackableCollection{TTrackable}"/> and is considered equivalent to this and <c>false</c> otherwise.</returns>
  83. public override bool Equals(object obj)
  84. {
  85. if (!(obj is TrackableCollection<TTrackable>))
  86. return false;
  87. return Equals((TrackableCollection<TTrackable>) obj);
  88. }
  89. /// <summary>
  90. /// Checks the equality of this <see cref="TrackableCollection{TTrackable}"/> against another <see cref="TrackableCollection{TTrackable}"/> of the same <c>TTrackable</c> generic type.
  91. /// </summary>
  92. /// <param name="other">The <see cref="TrackableCollection{TTrackable}"/> that this collection should be checked against for equivalency.</param>
  93. /// <returns><c>true</c> if the two <see cref="TrackableCollection{TTrackable}"/>s are considered equivalent and <c>false</c> otherwise.</returns>
  94. public bool Equals(TrackableCollection<TTrackable> other)
  95. {
  96. return ReferenceEquals(m_Trackables, other.m_Trackables);
  97. }
  98. /// <summary>
  99. /// Overloads the <c>==</c> operator to utilize the equals method for equality checking.
  100. /// </summary>
  101. /// <param name="lhs">The <see cref="TrackableCollection{TTrackable}"/> on the left hand side of the operator.</param>
  102. /// <param name="rhs">The <see cref="TrackableCollection{TTrackable}"/> on the right hand side of the operator.</param>
  103. /// <returns><c>true</c> if the two <see cref="TrackableCollection{TTrackable}"/>s are considered equivalent and <c>false</c> otherwise.</returns>
  104. /// <seealso cref="TrackableCollection.Equals(TrackableCollection{TTrackable})"/>
  105. public static bool operator ==(TrackableCollection<TTrackable> lhs, TrackableCollection<TTrackable> rhs)
  106. {
  107. return lhs.Equals(rhs);
  108. }
  109. /// <summary>
  110. /// Overloads the <c>!=</c> operator to utilize the equals method for equality checking.
  111. /// </summary>
  112. /// <param name="lhs">The <see cref="TrackableCollection{TTrackable}"/> on the left hand side of the operator.</param>
  113. /// <param name="rhs">The <see cref="TrackableCollection{TTrackable}"/> on the right hand side of the operator.</param>
  114. /// <returns><c>true</c> if the two <see cref="TrackableCollection{TTrackable}"/>s are not considered equivalent and <c>false</c> otherwise.</returns>
  115. /// <seealso cref="TrackableCollection.Equals(TrackableCollection{TTrackable})"/>
  116. public static bool operator !=(TrackableCollection<TTrackable> lhs, TrackableCollection<TTrackable> rhs)
  117. {
  118. return !lhs.Equals(rhs);
  119. }
  120. /// <summary>
  121. /// Attempts to retrieve a trackable by <c>TrackableId</c>.
  122. /// </summary>
  123. /// <param name="trackableId">The trackable id associated with the trackable to retrieve.</param>
  124. /// <param name="trackable">Set to the trackable with <paramref name="trackableId"/>, if present in the collection.</param>
  125. /// <returns><c>true</c> if the trackable with <paramref name="trackableId"/> exists. <c>false</c> otherwise.</returns>
  126. public bool TryGetTrackable(TrackableId trackableId, out TTrackable trackable)
  127. {
  128. if (m_Trackables == null)
  129. throw new InvalidOperationException("This collection has not been initialized.");
  130. return m_Trackables.TryGetValue(trackableId, out trackable);
  131. }
  132. /// <summary>
  133. /// An <c>Enumerator</c> for <c>TTrackable</c>s.
  134. /// </summary>
  135. public struct Enumerator
  136. {
  137. /// <summary>
  138. /// Constructs an <c>Enumerator</c> for use with <c>TTrackable</c>s.
  139. /// </summary>
  140. /// <param name="trackables">A <c>Dictionary</c> of <c>TTrackable</c>s.</param>
  141. public Enumerator(Dictionary<TrackableId, TTrackable> trackables)
  142. {
  143. if (trackables == null)
  144. throw new ArgumentNullException("trackables");
  145. m_Enumerator = trackables.GetEnumerator();
  146. }
  147. /// <summary>
  148. /// Moves to the next trackable in the collection.
  149. /// </summary>
  150. /// <returns><c>true</c> if the next trackable is valid, or <c>false</c> if it is the end of the collection.</returns>
  151. public bool MoveNext()
  152. {
  153. return m_Enumerator.MoveNext();
  154. }
  155. /// <summary>
  156. /// The current value in the collection.
  157. /// </summary>
  158. public TTrackable Current
  159. {
  160. get
  161. {
  162. return m_Enumerator.Current.Value;
  163. }
  164. }
  165. /// <summary>
  166. /// Releases all resources used by this <c>Enumerator</c>.
  167. /// </summary>
  168. public void Dispose()
  169. {
  170. m_Enumerator.Dispose();
  171. }
  172. Dictionary<TrackableId, TTrackable>.Enumerator m_Enumerator;
  173. }
  174. Dictionary<TrackableId, TTrackable> m_Trackables;
  175. }
  176. }