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.

172 lines
8.9 KiB

4 years ago
  1. using System;
  2. using Unity.Collections;
  3. using Unity.Collections.LowLevel.Unsafe;
  4. namespace UnityEngine.XR.ARKit
  5. {
  6. /// <summary>
  7. /// Use this to construct an <see cref="ARCollaborationData"/> incrementally from serialized bytes.
  8. /// </summary>
  9. /// <remarks>
  10. /// This struct may be useful if you receive data through a stream. If you already have all
  11. /// the bytes, use a <see cref="ARCollaborationData"/> constructor instead.
  12. /// This struct represents a native resource and must be explicitly disposed when no longer needed.
  13. /// While this struct is not thread safe, you may construct, Dispose, and Append from any thread.
  14. /// </remarks>
  15. public struct ARCollaborationDataBuilder : IDisposable, IEquatable<ARCollaborationDataBuilder>
  16. {
  17. /// <summary>
  18. /// Whether the <see cref="ARCollaborationDataBuilder"/> has allocated any data. If <c>true</c>,
  19. /// this struct must be disposed to avoid leaking native resources. If <c>false</c>, this struct
  20. /// either never allocated memory
  21. /// (with <see cref="Append(byte[], int)"/> or <see cref="Append(NativeArray{byte}, int)"/>)
  22. /// or it has already been <see cref="Dispose"/>d.
  23. /// </summary>
  24. public bool hasData => m_NSMutableData.created;
  25. /// <summary>
  26. /// The number of bytes owned by this struct.
  27. /// </summary>
  28. /// <seealso cref="Append(byte[])"/>
  29. /// <seealso cref="Append(byte[], int)"/>
  30. /// <seealso cref="Append(NativeSlice{byte}, int)"/>.
  31. public int length => m_NSMutableData.created ? m_NSMutableData.length : 0;
  32. /// <summary>
  33. /// Converts the bytes accumulated through calls to Append to an <see cref="ARCollaborationData"/>.
  34. /// </summary>
  35. /// <exception cref="System.NotSupportedException">Thrown if <see cref="ARCollaborationData"/> is not supported.
  36. /// Check for support with <see cref="ARKitSessionSubsystem.supportsCollaboration"/>.</exception>
  37. /// <exception cref="System.InvalidOperationException">Thrown if <see cref="hasData"/> is false.</exception>
  38. public ARCollaborationData ToCollaborationData()
  39. {
  40. if (!ARKitSessionSubsystem.supportsCollaboration)
  41. throw new NotSupportedException("ARCollaborationData is not supported by this version of iOS.");
  42. if (!hasData)
  43. throw new InvalidOperationException("No data to convert to ARCollaborationData.");
  44. return new ARCollaborationData(m_NSMutableData.ToNSData());
  45. }
  46. /// <summary>
  47. /// Appends <paramref name="size"/> <c>byte</c>s of the array <paramref name="buffer"/> to an existing array of bytes.
  48. /// </summary>
  49. /// <param name="buffer">A buffer containing <c>byte</c>s to append.</param>
  50. /// <param name="offset">The offset within <paramref name="buffer"/> to start appending bytes to the internal array.</param>
  51. /// <param name="size">The number of bytes from <paramref name="buffer"/> to append. Must be less than <c><paramref name="bytes"/>.Length + <paramref name="offset"/>></c>.</param>
  52. /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="buffer"/> is <c>null</c>.</exception>
  53. /// <exception cref="System.ArgumentOutOfRangeException">Thrown if <paramref name="size"/> is less than zero.</exception>
  54. /// <exception cref="System.ArgumentOutOfRangeException">Thrown if <paramref name="offset"/> is less than zero.</exception>
  55. /// <exception cref="System.InvalidOperationException">Thrown if <paramref name="size"/> is less than zero or greater than the length of <paramref name="buffer"/>.</exception>
  56. public unsafe void Append(byte[] buffer, int offset, int size)
  57. {
  58. if (buffer == null)
  59. throw new ArgumentNullException(nameof(buffer));
  60. if (size < 0)
  61. throw new ArgumentOutOfRangeException(nameof(size), size, $"'{nameof(size)}' must be greater than or equal to zero.");
  62. if (offset < 0)
  63. throw new ArgumentOutOfRangeException(nameof(offset), offset, $"'{nameof(offset)}' must be greater than or equal to zero.");
  64. if (buffer.Length < size + offset)
  65. throw new InvalidOperationException($"Reading {size} bytes starting at offset {offset} would write past the end of '{nameof(buffer)}' (buffer length = {buffer.Length}).");
  66. fixed (byte* ptr = &buffer[offset])
  67. {
  68. AppendUnchecked(ptr, size);
  69. }
  70. }
  71. /// <summary>
  72. /// Appends all <c>byte</c>s of the array <paramref name="buffer"/> to an existing array of bytes.
  73. /// </summary>
  74. /// <param name="buffer">A buffer containing <c>byte</c>s to append.</param>
  75. /// <exception cref="System.ArgumentNullException">Thrown if <paramref name="buffer"/> is <c>null</c>.</exception>
  76. public unsafe void Append(byte[] buffer)
  77. {
  78. if (buffer == null)
  79. throw new ArgumentNullException(nameof(buffer));
  80. fixed (byte* ptr = buffer)
  81. {
  82. AppendUnchecked(ptr, buffer.Length);
  83. }
  84. }
  85. /// <summary>
  86. /// Appends <paramref name="bytes"/> to an existing array of bytes.
  87. /// </summary>
  88. /// <param name="bytes">An array of bytes to append to the existing data.</param>
  89. /// <exception cref="System.ArgumentException">Thrown if <paramref name="bytes"/> does not reference valid data.</exception>
  90. public unsafe void Append(NativeSlice<byte> bytes)
  91. {
  92. void* ptr = bytes.GetUnsafePtr();
  93. if (ptr == null)
  94. throw new ArgumentException("Invalid NativeSlice", nameof(bytes));
  95. AppendUnchecked(ptr, bytes.Length);
  96. }
  97. /// <summary>
  98. /// Releases the native resource.
  99. /// </summary>
  100. public void Dispose() => m_NSMutableData.Dispose();
  101. /// <summary>
  102. /// Appends data to the underlying NSMutableData array.
  103. /// </summary>
  104. /// <param name="bytes">A pointer to an array of bytes to append to the existing data.</param>
  105. /// <param name="size">The number of bytes pointed to by <paramref name="bytes"/>.</param>
  106. unsafe void AppendUnchecked(void* bytes, int size)
  107. {
  108. if (m_NSMutableData.created)
  109. {
  110. m_NSMutableData.Append(bytes, size);
  111. }
  112. else
  113. {
  114. m_NSMutableData = new NSMutableData(bytes, size);
  115. }
  116. }
  117. /// <summary>
  118. /// Computes a hash code suitable for use in a <c>Dictionary</c> or <c>HashSet</c>.
  119. /// </summary>
  120. /// <returns>A hash code suitable for use in a <c>Dictionary</c> or <c>HashSet</c>.</returns>
  121. public override int GetHashCode() => m_NSMutableData.GetHashCode();
  122. /// <summary>
  123. /// IEquatable interface. Compares for equality.
  124. /// </summary>
  125. /// <param name="obj">The object to compare for equality.</param>
  126. /// <returns><c>true</c> if <paramref name="obj"/> is of type <see cref="ARCollaborationDataBuilder"/> and compares equal with <see cref="Equals(ARCollaborationDataBuilder)"/>.</returns>
  127. public override bool Equals(object obj) => (obj is ARCollaborationDataBuilder) && Equals((ARCollaborationDataBuilder)obj);
  128. /// <summary>
  129. /// IEquatable interface. Compares for equality.
  130. /// </summary>
  131. /// <param name="other">The <see cref="ARCollaborationDataBuilder"/> to compare against.</param>
  132. /// <returns><c>true</c> if all fields of this <see cref="ARCollaborationDataBuilder"/> compare equal to <paramref name="other"/>.</returns>
  133. public bool Equals(ARCollaborationDataBuilder other) => m_NSMutableData.Equals(other.m_NSMutableData);
  134. /// <summary>
  135. /// Compares for equality. Same as <see cref="Equals(ARCollaborationDataBuilder)"/>.
  136. /// </summary>
  137. /// <param name="lhs">The left-hand side of the comparison.</param>
  138. /// <param name="rhs">The right-hand side of the comparison.</param>
  139. /// <returns><c>true</c> if all fields of this <see cref="ARCollaborationDataBuilder"/> compare equal to <paramref name="other"/>.</returns>
  140. public static bool operator ==(ARCollaborationDataBuilder lhs, ARCollaborationDataBuilder rhs) => lhs.Equals(rhs);
  141. /// <summary>
  142. /// Compares for inequality. Same as <c>!</c><see cref="Equals(ARCollaborationDataBuilder)"/>.
  143. /// </summary>
  144. /// <param name="lhs">The left-hand side of the comparison.</param>
  145. /// <param name="rhs">The right-hand side of the comparison.</param>
  146. /// <returns><c>true</c> if any of the fields of this <see cref="ARCollaborationDataBuilder"/> are not equal to <paramref name="other"/>.</returns>
  147. public static bool operator !=(ARCollaborationDataBuilder lhs, ARCollaborationDataBuilder rhs) => !lhs.Equals(rhs);
  148. internal NSMutableData m_NSMutableData;
  149. }
  150. }