2022-12-20 15:20:49 +00:00
|
|
|
|
using System;
|
2015-12-30 11:19:42 +00:00
|
|
|
|
|
2022-12-20 15:06:44 +00:00
|
|
|
|
namespace SharpCompress.Common;
|
|
|
|
|
|
|
|
|
|
|
|
internal static class FlagUtility
|
2015-12-30 11:19:42 +00:00
|
|
|
|
{
|
2022-12-20 15:06:44 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Returns true if the flag is set on the specified bit field.
|
|
|
|
|
|
/// Currently only works with 32-bit bitfields.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">Enumeration with Flags attribute</typeparam>
|
|
|
|
|
|
/// <param name="bitField">Flagged variable</param>
|
|
|
|
|
|
/// <param name="flag">Flag to test</param>
|
|
|
|
|
|
/// <returns></returns>
|
2023-03-21 13:14:08 +00:00
|
|
|
|
public static bool HasFlag<T>(long bitField, T flag)
|
|
|
|
|
|
where T : struct => HasFlag(bitField, flag);
|
2015-12-30 11:19:42 +00:00
|
|
|
|
|
2022-12-20 15:06:44 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Returns true if the flag is set on the specified bit field.
|
|
|
|
|
|
/// Currently only works with 32-bit bitfields.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">Enumeration with Flags attribute</typeparam>
|
|
|
|
|
|
/// <param name="bitField">Flagged variable</param>
|
|
|
|
|
|
/// <param name="flag">Flag to test</param>
|
|
|
|
|
|
/// <returns></returns>
|
2023-03-21 13:14:08 +00:00
|
|
|
|
public static bool HasFlag<T>(ulong bitField, T flag)
|
|
|
|
|
|
where T : struct => HasFlag(bitField, flag);
|
2015-12-30 11:19:42 +00:00
|
|
|
|
|
2022-12-20 15:06:44 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Returns true if the flag is set on the specified bit field.
|
|
|
|
|
|
/// Currently only works with 32-bit bitfields.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="bitField">Flagged variable</param>
|
|
|
|
|
|
/// <param name="flag">Flag to test</param>
|
|
|
|
|
|
/// <returns></returns>
|
2022-12-20 15:20:49 +00:00
|
|
|
|
public static bool HasFlag(ulong bitField, ulong flag) => ((bitField & flag) == flag);
|
2015-12-30 11:19:42 +00:00
|
|
|
|
|
2022-12-20 15:20:49 +00:00
|
|
|
|
public static bool HasFlag(short bitField, short flag) => ((bitField & flag) == flag);
|
2016-09-26 10:55:52 +01:00
|
|
|
|
|
2022-12-20 15:06:44 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Returns true if the flag is set on the specified bit field.
|
|
|
|
|
|
/// Currently only works with 32-bit bitfields.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">Enumeration with Flags attribute</typeparam>
|
|
|
|
|
|
/// <param name="bitField">Flagged variable</param>
|
|
|
|
|
|
/// <param name="flag">Flag to test</param>
|
|
|
|
|
|
/// <returns></returns>
|
2023-03-21 13:14:08 +00:00
|
|
|
|
public static bool HasFlag<T>(T bitField, T flag)
|
|
|
|
|
|
where T : struct => HasFlag(Convert.ToInt64(bitField), Convert.ToInt64(flag));
|
2015-12-30 11:19:42 +00:00
|
|
|
|
|
2022-12-20 15:06:44 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Returns true if the flag is set on the specified bit field.
|
|
|
|
|
|
/// Currently only works with 32-bit bitfields.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="bitField">Flagged variable</param>
|
|
|
|
|
|
/// <param name="flag">Flag to test</param>
|
|
|
|
|
|
/// <returns></returns>
|
2022-12-20 15:20:49 +00:00
|
|
|
|
public static bool HasFlag(long bitField, long flag) => ((bitField & flag) == flag);
|
2015-12-30 11:19:42 +00:00
|
|
|
|
|
2022-12-20 15:06:44 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Sets a bit-field to either on or off for the specified flag.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="bitField">Flagged variable</param>
|
|
|
|
|
|
/// <param name="flag">Flag to change</param>
|
|
|
|
|
|
/// <param name="on">bool</param>
|
|
|
|
|
|
/// <returns>The flagged variable with the flag changed</returns>
|
|
|
|
|
|
public static long SetFlag(long bitField, long flag, bool on)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (on)
|
2015-12-30 11:19:42 +00:00
|
|
|
|
{
|
2022-12-20 15:06:44 +00:00
|
|
|
|
return bitField | flag;
|
2015-12-30 11:19:42 +00:00
|
|
|
|
}
|
2022-12-20 15:06:44 +00:00
|
|
|
|
return bitField & (~flag);
|
|
|
|
|
|
}
|
2015-12-30 11:19:42 +00:00
|
|
|
|
|
2022-12-20 15:06:44 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Sets a bit-field to either on or off for the specified flag.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">Enumeration with Flags attribute</typeparam>
|
|
|
|
|
|
/// <param name="bitField">Flagged variable</param>
|
|
|
|
|
|
/// <param name="flag">Flag to change</param>
|
|
|
|
|
|
/// <param name="on">bool</param>
|
|
|
|
|
|
/// <returns>The flagged variable with the flag changed</returns>
|
2023-03-21 13:14:08 +00:00
|
|
|
|
public static long SetFlag<T>(T bitField, T flag, bool on)
|
|
|
|
|
|
where T : struct => SetFlag(Convert.ToInt64(bitField), Convert.ToInt64(flag), on);
|
2022-12-20 13:45:47 +00:00
|
|
|
|
}
|