FourCC.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System;
  2. namespace UnityEngine.InputSystem.Utilities
  3. {
  4. /// <summary>
  5. /// A four-character code.
  6. /// </summary>
  7. /// <remarks>
  8. /// A four-character code is a struct containing four byte characters totalling a single <c>int</c>.
  9. /// FourCCs are frequently used in the input system to identify the format of data sent to or from
  10. /// the native backend representing events, input device state or commands sent to input devices.
  11. /// </remarks>
  12. public struct FourCC : IEquatable<FourCC>
  13. {
  14. private int m_Code;
  15. /// <summary>
  16. /// Create a FourCC from the given integer.
  17. /// </summary>
  18. /// <param name="code">FourCC code represented as an <c>int</c>. Character order is
  19. /// little endian. "ABCD" is stored with A in the highest order 8 bits and D in the
  20. /// lowest order 8 bits.</param>
  21. /// <remarks>
  22. /// This method does not actually verify whether the four characters in the code
  23. /// are printable.
  24. /// </remarks>
  25. public FourCC(int code)
  26. {
  27. m_Code = code;
  28. }
  29. /// <summary>
  30. /// Create a FourCC from the given four characters.
  31. /// </summary>
  32. /// <param name="a">First character.</param>
  33. /// <param name="b">Second character.</param>
  34. /// <param name="c">Third character.</param>
  35. /// <param name="d">Fourth character.</param>
  36. public FourCC(char a, char b = ' ', char c = ' ', char d = ' ')
  37. {
  38. m_Code = (a << 24) | (b << 16) | (c << 8) | d;
  39. }
  40. /// <summary>
  41. /// Create a FourCC from the given string.
  42. /// </summary>
  43. /// <param name="str">A string with four characters or less but with at least one character.</param>
  44. /// <exception cref="ArgumentException"><paramref name="str"/> is empty or has more than four characters.</exception>
  45. /// <exception cref="ArgumentNullException"><paramref name="str"/> is <c>null</c>.</exception>
  46. public FourCC(string str)
  47. : this()
  48. {
  49. if (str == null)
  50. throw new ArgumentNullException(nameof(str));
  51. var length = str.Length;
  52. if (length < 1 || length > 4)
  53. throw new ArgumentException("FourCC string must be one to four characters long!", nameof(str));
  54. var a = str[0];
  55. var b = length > 1 ? str[1] : ' ';
  56. var c = length > 2 ? str[2] : ' ';
  57. var d = length > 3 ? str[3] : ' ';
  58. m_Code = (a << 24) | (b << 16) | (c << 8) | d;
  59. }
  60. /// <summary>
  61. /// Convert the given FourCC into an <c>int</c>.
  62. /// </summary>
  63. /// <param name="fourCC">A FourCC.</param>
  64. /// <returns>The four characters of the code packed into one <c>int</c>. Character order is
  65. /// little endian. "ABCD" is stored with A in the highest order 8 bits and D in the
  66. /// lowest order 8 bits.</returns>
  67. public static implicit operator int(FourCC fourCC)
  68. {
  69. return fourCC.m_Code;
  70. }
  71. /// <summary>
  72. /// Convert the given <c>int</c> into a FourCC.
  73. /// </summary>
  74. /// <param name="i">FourCC code represented as an <c>int</c>. Character order is
  75. /// little endian. "ABCD" is stored with A in the highest order 8 bits and D in the
  76. /// lowest order 8 bits.</param>
  77. /// <returns>The FourCC converted from <paramref name="i"/>.</returns>
  78. public static implicit operator FourCC(int i)
  79. {
  80. return new FourCC(i);
  81. }
  82. /// <summary>
  83. /// Convert the FourCC into a string in the form of "ABCD".
  84. /// </summary>
  85. /// <returns>String representation of the FourCC.</returns>
  86. public override string ToString()
  87. {
  88. return
  89. $"{(char) (m_Code >> 24)}{(char) ((m_Code & 0xff0000) >> 16)}{(char) ((m_Code & 0xff00) >> 8)}{(char) (m_Code & 0xff)}";
  90. }
  91. /// <summary>
  92. /// Compare two FourCCs for equality.
  93. /// </summary>
  94. /// <param name="other">Another FourCC.</param>
  95. /// <returns>True if the two FourCCs are equal, i.e. have the same exact
  96. /// character codes.</returns>
  97. public bool Equals(FourCC other)
  98. {
  99. return m_Code == other.m_Code;
  100. }
  101. /// <summary>
  102. /// Compare the FourCC to the given object.
  103. /// </summary>
  104. /// <param name="obj">An object. Can be null.</param>
  105. /// <returns>True if <paramref name="obj"/> is a FourCC that has the same
  106. /// character code sequence.</returns>
  107. public override bool Equals(object obj)
  108. {
  109. if (ReferenceEquals(null, obj))
  110. return false;
  111. return obj is FourCC cc && Equals(cc);
  112. }
  113. /// <summary>
  114. /// Compute a hash code for the FourCC.
  115. /// </summary>
  116. /// <returns>Simply returns the FourCC converted to an <c>int</c>.</returns>
  117. public override int GetHashCode()
  118. {
  119. return m_Code;
  120. }
  121. /// <summary>
  122. /// Compare two FourCCs for equality.
  123. /// </summary>
  124. /// <param name="left">First FourCC.</param>
  125. /// <param name="right">Second FourCC.</param>
  126. /// <returns>True if the two FourCCs are equal, i.e. have the same exact
  127. /// character codes.</returns>
  128. public static bool operator==(FourCC left, FourCC right)
  129. {
  130. return left.m_Code == right.m_Code;
  131. }
  132. /// <summary>
  133. /// Compare two FourCCs for inequality.
  134. /// </summary>
  135. /// <param name="left">First FourCC.</param>
  136. /// <param name="right">Second FourCC.</param>
  137. /// <returns>True if the two FourCCs are not equal, i.e. do not have the same exact
  138. /// character codes.</returns>
  139. public static bool operator!=(FourCC left, FourCC right)
  140. {
  141. return left.m_Code != right.m_Code;
  142. }
  143. // Make annoying Microsoft code analyzer happy.
  144. public static FourCC FromInt32(int i)
  145. {
  146. return i;
  147. }
  148. public static int ToInt32(FourCC fourCC)
  149. {
  150. return fourCC.m_Code;
  151. }
  152. }
  153. }