暫無描述
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.

FlagEnumUtility.cs 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using UnityEngine;
  3. namespace UnityEditor.TestTools.TestRunner.GUI.Controls
  4. {
  5. /// <summary>
  6. /// Provides methods for dealing with common enumerator operations.
  7. /// </summary>
  8. internal static class FlagEnumUtility
  9. {
  10. /// <summary>
  11. /// Checks for the presence of a flag in a flag enum value.
  12. /// </summary>
  13. /// <param name="value">The value to check for the presence of the flag.</param>
  14. /// <param name="flag">The flag whose presence is to be checked.</param>
  15. /// <typeparam name="T">The flag enum type.</typeparam>
  16. /// <returns></returns>
  17. internal static bool HasFlag<T>(T value, T flag) where T : Enum
  18. {
  19. ValidateUnderlyingType<T>();
  20. var intValue = (int)(object)value;
  21. var intFlag = (int)(object)flag;
  22. return (intValue & intFlag) == intFlag;
  23. }
  24. /// <summary>
  25. /// Sets a flag in a flag enum value.
  26. /// </summary>
  27. /// <param name="value">The value where the flag should be set.</param>
  28. /// <param name="flag">The flag to be set.</param>
  29. /// <typeparam name="T">The flag enum type.</typeparam>
  30. /// <returns>The input value with the flag set.</returns>
  31. internal static T SetFlag<T>(T value, T flag) where T : Enum
  32. {
  33. ValidateUnderlyingType<T>();
  34. var intValue = (int)(object)value;
  35. var intFlag = (int)(object)flag;
  36. var result = intValue | intFlag;
  37. return (T)Enum.ToObject(typeof(T), result);
  38. }
  39. /// <summary>
  40. /// Removes a flag in a flag enum value.
  41. /// </summary>
  42. /// <param name="value">The value where the flag should be removed.</param>
  43. /// <param name="flag">The flag to be removed.</param>
  44. /// <typeparam name="T">The flag enum type.</typeparam>
  45. /// <returns>The input value with the flag removed.</returns>
  46. internal static T RemoveFlag<T>(T value, T flag) where T : Enum
  47. {
  48. ValidateUnderlyingType<T>();
  49. var intValue = (int)(object)value;
  50. var intFlag = (int)(object)flag;
  51. var result = intValue & ~intFlag;
  52. return (T)Enum.ToObject(typeof(T), result);
  53. }
  54. /// <summary>
  55. /// Validates that the underlying type of an enum is integer.
  56. /// </summary>
  57. /// <typeparam name="T">The enum type.</typeparam>
  58. /// <exception cref="ArgumentException">Thrown if the underlying type of the enum type parameter is not integer.</exception>
  59. private static void ValidateUnderlyingType<T>() where T : Enum
  60. {
  61. if (Enum.GetUnderlyingType(typeof(T)) != typeof(int))
  62. {
  63. throw new ArgumentException("Argument underlying type must be integer.");
  64. }
  65. }
  66. }
  67. }