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.

85 lines
3.2 KiB

4 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. namespace UnityEngine.XR.ARSubsystems
  4. {
  5. /// <summary>
  6. /// A utility to validate data from certain types of <c>Subsystem</c>s.
  7. /// </summary>
  8. /// <typeparam name="T">The <see cref="ITrackable"/> managed by the subsystem.</typeparam>
  9. /// <seealso cref="XRDepthSubsystem"/>
  10. /// <seealso cref="XRPlaneSubsystem"/>
  11. /// <seealso cref="XRAnchorSubsystem"/>
  12. public class ValidationUtility<T>
  13. where T : struct, ITrackable
  14. {
  15. /// <summary>
  16. /// Performs validation checks that ensure a trackable does not exist in multiple lists
  17. /// simultaneously, e.g., added and removed. Also ensures that a trackable cannot be
  18. /// removed before being added.
  19. /// </summary>
  20. /// <param name="changes">A set of trackable changes (added, updated & removed)</param>
  21. public void ValidateAndThrow(TrackableChanges<T> changes)
  22. {
  23. s_IdSet.Clear();
  24. foreach (var trackable in changes.added)
  25. {
  26. AddToSetAndThrowIfDuplicate(trackable.trackableId, false, k_AddedAction);
  27. m_Trackables.Add(trackable.trackableId);
  28. }
  29. foreach (var trackable in changes.updated)
  30. AddToSetAndThrowIfDuplicate(trackable.trackableId, true, k_UpdatedAction);
  31. foreach (var trackableId in changes.removed)
  32. {
  33. AddToSetAndThrowIfDuplicate(trackableId, true, k_RemovedAction);
  34. m_Trackables.Remove(trackableId);
  35. }
  36. }
  37. /// <summary>
  38. /// Same as <see cref="ValidateAndThrow(TrackableChanges{T})"/> but also disposes the <paramref name="changes"/>.
  39. /// </summary>
  40. /// <param name="changes">A set of trackable changes (added, updated & removed)</param>
  41. public void ValidateAndDisposeIfThrown(TrackableChanges<T> changes)
  42. {
  43. try
  44. {
  45. ValidateAndThrow(changes);
  46. }
  47. catch
  48. {
  49. changes.Dispose();
  50. throw;
  51. }
  52. }
  53. void AddToSetAndThrowIfDuplicate(
  54. TrackableId trackableId,
  55. bool shouldBeInDictionary,
  56. string action)
  57. {
  58. if (!s_IdSet.Add(trackableId))
  59. throw new InvalidOperationException(
  60. string.Format(
  61. "Trackable {0} being {1} this frame has at least one other action associated with it.",
  62. trackableId, action));
  63. if (m_Trackables.Contains(trackableId) != shouldBeInDictionary)
  64. throw new InvalidOperationException(string.Format(
  65. "Trackable {0} is being {1} but is {2} in the list of trackables.",
  66. trackableId, action, shouldBeInDictionary ? "not" : "already"));
  67. }
  68. static HashSet<TrackableId> s_IdSet = new HashSet<TrackableId>();
  69. static readonly string k_AddedAction = "added";
  70. static readonly string k_UpdatedAction = "updated";
  71. static readonly string k_RemovedAction = "removed";
  72. HashSet<TrackableId> m_Trackables = new HashSet<TrackableId>();
  73. }
  74. }