ViewDiagnosticFlagsExtensions.cs 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System.CodeDom.Compiler;
  2. using System.Diagnostics;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Runtime.CompilerServices;
  5. namespace Terminal.Gui.ViewBase;
  6. /// <summary>Extension methods for the <see cref="ViewDiagnosticFlags"/> <see langword="enum"/> type.</summary>
  7. [GeneratedCode ("Terminal.Gui.Analyzers.Internal", "1.0")]
  8. [CompilerGenerated]
  9. [DebuggerNonUserCode]
  10. [ExcludeFromCodeCoverage (Justification = "Generated code is already tested.")]
  11. [PublicAPI]
  12. public static class ViewDiagnosticFlagsExtensions
  13. {
  14. /// <summary>
  15. /// Directly converts this <see cref="ViewDiagnosticFlags"/> value to an <see langword="int"/> value with
  16. /// the same binary representation.
  17. /// </summary>
  18. /// <remarks>NO VALIDATION IS PERFORMED!</remarks>
  19. [MethodImpl (MethodImplOptions.AggressiveInlining)]
  20. public static int AsInt32 (this ViewDiagnosticFlags e) => Unsafe.As<ViewDiagnosticFlags, int> (ref e);
  21. /// <summary>
  22. /// Directly converts this <see cref="ViewDiagnosticFlags"/> value to a <see langword="uint"/> value with
  23. /// the same binary representation.
  24. /// </summary>
  25. /// <remarks>NO VALIDATION IS PERFORMED!</remarks>
  26. [MethodImpl (MethodImplOptions.AggressiveInlining)]
  27. public static uint AsUInt32 (this ViewDiagnosticFlags e) => Unsafe.As<ViewDiagnosticFlags, uint> (ref e);
  28. /// <summary>
  29. /// Determines if the specified flags are set in the current value of this
  30. /// <see cref="ViewDiagnosticFlags"/>.
  31. /// </summary>
  32. /// <remarks>NO VALIDATION IS PERFORMED!</remarks>
  33. /// <returns>
  34. /// True, if all flags present in <paramref name="checkFlags"/> are also present in the current value of the
  35. /// <see cref="ViewDiagnosticFlags"/>.<br/>Otherwise false.
  36. /// </returns>
  37. [MethodImpl (MethodImplOptions.AggressiveInlining)]
  38. public static bool FastHasFlags (this ViewDiagnosticFlags e, ViewDiagnosticFlags checkFlags)
  39. {
  40. ref uint enumCurrentValueRef = ref Unsafe.As<ViewDiagnosticFlags, uint> (ref e);
  41. ref uint checkFlagsValueRef = ref Unsafe.As<ViewDiagnosticFlags, uint> (ref checkFlags);
  42. return (enumCurrentValueRef & checkFlagsValueRef) == checkFlagsValueRef;
  43. }
  44. /// <summary>
  45. /// Determines if the specified mask bits are set in the current value of this
  46. /// <see cref="ViewDiagnosticFlags"/>.
  47. /// </summary>
  48. /// <param name="e">
  49. /// The <see cref="ViewDiagnosticFlags"/> value to check against the <paramref name="mask"/>
  50. /// value.
  51. /// </param>
  52. /// <param name="mask">A mask to apply to the current value.</param>
  53. /// <returns>
  54. /// True, if all bits set to 1 in the mask are also set to 1 in the current value of the
  55. /// <see cref="ViewDiagnosticFlags"/>.<br/>Otherwise false.
  56. /// </returns>
  57. /// <remarks>NO VALIDATION IS PERFORMED!</remarks>
  58. [MethodImpl (MethodImplOptions.AggressiveInlining)]
  59. public static bool FastHasFlags (this ViewDiagnosticFlags e, uint mask)
  60. {
  61. ref uint enumCurrentValueRef = ref Unsafe.As<ViewDiagnosticFlags, uint> (ref e);
  62. return (enumCurrentValueRef & mask) == mask;
  63. }
  64. /// <summary>
  65. /// Determines if the specified <see langword="uint"/> value is explicitly defined as a named value of the
  66. /// <see cref="ViewDiagnosticFlags"/> <see langword="enum"/> type.
  67. /// </summary>
  68. /// <remarks>
  69. /// Only explicitly named values return true, as with IsDefined. Combined valid flag values of flags enums which are
  70. /// not explicitly named will return false.
  71. /// </remarks>
  72. public static bool FastIsDefined (this ViewDiagnosticFlags _, uint value)
  73. {
  74. return value switch
  75. {
  76. 0 => true,
  77. 1 => true,
  78. 2 => true,
  79. 4 => true,
  80. _ => false
  81. };
  82. }
  83. }