KeyBindingScopeExtensions.cs 3.9 KB

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