From 425d651eba9bd54fdfa26a620cecd35dc12c92c2 Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Fri, 27 Jul 2018 11:14:59 -0400 Subject: [PATCH] feat: Additional EnumUtils methods for excluding/including flags --- ICD.Common.Utils/EnumUtils.cs | 64 +++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/ICD.Common.Utils/EnumUtils.cs b/ICD.Common.Utils/EnumUtils.cs index be5ff95..6725f52 100644 --- a/ICD.Common.Utils/EnumUtils.cs +++ b/ICD.Common.Utils/EnumUtils.cs @@ -219,6 +219,64 @@ namespace ICD.Common.Utils #region Flags + /// + /// Excludes b from a. + /// + /// + /// + /// + public static T ExcludeFlags(T a, T b) + where T : struct, IConvertible + { + if (!IsEnumType()) + 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); + } + + /// + /// Excludes b from a. + /// + /// + /// + /// + public static int ExcludeFlags(int a, int b) + { + return a & ~b; + } + + /// + /// Includes a and b. + /// + /// + /// + /// + public static T IncludeFlags(T a, T b) + where T : struct, IConvertible + { + if (!IsEnumType()) + 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); + } + + /// + /// Includes a and b. + /// + /// + /// + /// + public static int IncludeFlags(int a, int b) + { + return a | b; + } + /// /// Gets the overlapping values of the given enum flags. /// @@ -231,6 +289,9 @@ namespace ICD.Common.Utils if (values == null) throw new ArgumentNullException("values"); + if (!IsEnumType()) + 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 a, T b) where T : struct, IConvertible { + if (!IsEnumType()) + throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); + int aInt = (int)(object)a; int bInt = (int)(object)b;