FlagUtility.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. namespace SharpCompress.Common
  3. {
  4. internal static class FlagUtility
  5. {
  6. /// <summary>
  7. /// Returns true if the flag is set on the specified bit field.
  8. /// Currently only works with 32-bit bitfields.
  9. /// </summary>
  10. /// <typeparam name="T">Enumeration with Flags attribute</typeparam>
  11. /// <param name="bitField">Flagged variable</param>
  12. /// <param name="flag">Flag to test</param>
  13. /// <returns></returns>
  14. public static bool HasFlag<T>(long bitField, T flag)
  15. where T : struct
  16. {
  17. return HasFlag(bitField, flag);
  18. }
  19. /// <summary>
  20. /// Returns true if the flag is set on the specified bit field.
  21. /// Currently only works with 32-bit bitfields.
  22. /// </summary>
  23. /// <typeparam name="T">Enumeration with Flags attribute</typeparam>
  24. /// <param name="bitField">Flagged variable</param>
  25. /// <param name="flag">Flag to test</param>
  26. /// <returns></returns>
  27. public static bool HasFlag<T>(ulong bitField, T flag)
  28. where T : struct
  29. {
  30. return HasFlag(bitField, flag);
  31. }
  32. /// <summary>
  33. /// Returns true if the flag is set on the specified bit field.
  34. /// Currently only works with 32-bit bitfields.
  35. /// </summary>
  36. /// <param name="bitField">Flagged variable</param>
  37. /// <param name="flag">Flag to test</param>
  38. /// <returns></returns>
  39. public static bool HasFlag(ulong bitField, ulong flag)
  40. {
  41. return ((bitField & flag) == flag);
  42. }
  43. public static bool HasFlag(short bitField, short flag)
  44. {
  45. return ((bitField & flag) == flag);
  46. }
  47. /// <summary>
  48. /// Returns true if the flag is set on the specified bit field.
  49. /// Currently only works with 32-bit bitfields.
  50. /// </summary>
  51. /// <typeparam name="T">Enumeration with Flags attribute</typeparam>
  52. /// <param name="bitField">Flagged variable</param>
  53. /// <param name="flag">Flag to test</param>
  54. /// <returns></returns>
  55. public static bool HasFlag<T>(T bitField, T flag)
  56. where T : struct
  57. {
  58. return HasFlag(Convert.ToInt64(bitField), Convert.ToInt64(flag));
  59. }
  60. /// <summary>
  61. /// Returns true if the flag is set on the specified bit field.
  62. /// Currently only works with 32-bit bitfields.
  63. /// </summary>
  64. /// <param name="bitField">Flagged variable</param>
  65. /// <param name="flag">Flag to test</param>
  66. /// <returns></returns>
  67. public static bool HasFlag(long bitField, long flag)
  68. {
  69. return ((bitField & flag) == flag);
  70. }
  71. /// <summary>
  72. /// Sets a bit-field to either on or off for the specified flag.
  73. /// </summary>
  74. /// <param name="bitField">Flagged variable</param>
  75. /// <param name="flag">Flag to change</param>
  76. /// <param name="on">bool</param>
  77. /// <returns>The flagged variable with the flag changed</returns>
  78. public static long SetFlag(long bitField, long flag, bool on)
  79. {
  80. if (on)
  81. {
  82. return bitField | flag;
  83. }
  84. return bitField & (~flag);
  85. }
  86. /// <summary>
  87. /// Sets a bit-field to either on or off for the specified flag.
  88. /// </summary>
  89. /// <typeparam name="T">Enumeration with Flags attribute</typeparam>
  90. /// <param name="bitField">Flagged variable</param>
  91. /// <param name="flag">Flag to change</param>
  92. /// <param name="on">bool</param>
  93. /// <returns>The flagged variable with the flag changed</returns>
  94. public static long SetFlag<T>(T bitField, T flag, bool on)
  95. where T : struct
  96. {
  97. return SetFlag(Convert.ToInt64(bitField), Convert.ToInt64(flag), on);
  98. }
  99. }
  100. }