2020-09-06 08:07:31 +00:00
|
|
|
|
namespace QSB.Utility
|
2020-07-27 23:13:43 +00:00
|
|
|
|
{
|
2020-12-02 21:23:01 +00:00
|
|
|
|
// Stolen from here : https://stackoverflow.com/questions/3261451/using-a-bitmask-in-c-sharp
|
2020-07-27 23:13:43 +00:00
|
|
|
|
|
2020-12-02 21:23:01 +00:00
|
|
|
|
public static class FlagsHelper
|
|
|
|
|
{
|
|
|
|
|
public static bool IsSet<T>(T flags, T flag) where T : struct
|
|
|
|
|
{
|
|
|
|
|
var flagsValue = (int)(object)flags;
|
|
|
|
|
var flagValue = (int)(object)flag;
|
2020-07-27 23:13:43 +00:00
|
|
|
|
|
2020-12-02 21:23:01 +00:00
|
|
|
|
return (flagsValue & flagValue) != 0;
|
|
|
|
|
}
|
2020-07-27 23:13:43 +00:00
|
|
|
|
|
2020-12-02 21:23:01 +00:00
|
|
|
|
public static void Set<T>(ref T flags, T flag) where T : struct
|
|
|
|
|
{
|
|
|
|
|
var flagsValue = (int)(object)flags;
|
|
|
|
|
var flagValue = (int)(object)flag;
|
2020-07-27 23:13:43 +00:00
|
|
|
|
|
2020-12-02 21:23:01 +00:00
|
|
|
|
flags = (T)(object)(flagsValue | flagValue);
|
|
|
|
|
}
|
2020-07-27 23:13:43 +00:00
|
|
|
|
|
2020-12-02 21:23:01 +00:00
|
|
|
|
public static void Unset<T>(ref T flags, T flag) where T : struct
|
|
|
|
|
{
|
|
|
|
|
var flagsValue = (int)(object)flags;
|
|
|
|
|
var flagValue = (int)(object)flag;
|
2020-07-27 23:13:43 +00:00
|
|
|
|
|
2020-12-02 21:23:01 +00:00
|
|
|
|
flags = (T)(object)(flagsValue & ~flagValue);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-03 08:28:05 +00:00
|
|
|
|
}
|