mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-09 17:55:23 +00:00
feat: Additional EnumUtils methods for excluding/including flags
This commit is contained in:
@@ -219,6 +219,64 @@ namespace ICD.Common.Utils
|
||||
|
||||
#region Flags
|
||||
|
||||
/// <summary>
|
||||
/// Excludes b from a.
|
||||
/// </summary>
|
||||
/// <param name="a"></param>
|
||||
/// <param name="b"></param>
|
||||
/// <returns></returns>
|
||||
public static T ExcludeFlags<T>(T a, T b)
|
||||
where T : struct, IConvertible
|
||||
{
|
||||
if (!IsEnumType<T>())
|
||||
throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name));
|
||||
|
||||
int aInt = (int)(object)a;
|
||||
int bInt = (int)(object)b;
|
||||
|
||||
return (T)(object)ExcludeFlags(aInt, bInt);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Excludes b from a.
|
||||
/// </summary>
|
||||
/// <param name="a"></param>
|
||||
/// <param name="b"></param>
|
||||
/// <returns></returns>
|
||||
public static int ExcludeFlags(int a, int b)
|
||||
{
|
||||
return a & ~b;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Includes a and b.
|
||||
/// </summary>
|
||||
/// <param name="a"></param>
|
||||
/// <param name="b"></param>
|
||||
/// <returns></returns>
|
||||
public static T IncludeFlags<T>(T a, T b)
|
||||
where T : struct, IConvertible
|
||||
{
|
||||
if (!IsEnumType<T>())
|
||||
throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name));
|
||||
|
||||
int aInt = (int)(object)a;
|
||||
int bInt = (int)(object)b;
|
||||
|
||||
return (T)(object)IncludeFlags(aInt, bInt);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Includes a and b.
|
||||
/// </summary>
|
||||
/// <param name="a"></param>
|
||||
/// <param name="b"></param>
|
||||
/// <returns></returns>
|
||||
public static int IncludeFlags(int a, int b)
|
||||
{
|
||||
return a | b;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the overlapping values of the given enum flags.
|
||||
/// </summary>
|
||||
@@ -231,6 +289,9 @@ namespace ICD.Common.Utils
|
||||
if (values == null)
|
||||
throw new ArgumentNullException("values");
|
||||
|
||||
if (!IsEnumType<T>())
|
||||
throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name));
|
||||
|
||||
if (values.Length == 0)
|
||||
return default(T);
|
||||
|
||||
@@ -266,6 +327,9 @@ namespace ICD.Common.Utils
|
||||
public static T GetFlagsIntersection<T>(T a, T b)
|
||||
where T : struct, IConvertible
|
||||
{
|
||||
if (!IsEnumType<T>())
|
||||
throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name));
|
||||
|
||||
int aInt = (int)(object)a;
|
||||
int bInt = (int)(object)b;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user