From d79c272df0fe79838f60d4df7618fcf2b9261059 Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Tue, 21 Aug 2018 10:26:24 -0400 Subject: [PATCH] perf: Removing heavy-handed validation from enum utils --- ICD.Common.Utils/EnumUtils.cs | 72 ------------------- ICD.Common.Utils/Extensions/EnumExtensions.cs | 15 ---- 2 files changed, 87 deletions(-) diff --git a/ICD.Common.Utils/EnumUtils.cs b/ICD.Common.Utils/EnumUtils.cs index 6725f52..d94afa2 100644 --- a/ICD.Common.Utils/EnumUtils.cs +++ b/ICD.Common.Utils/EnumUtils.cs @@ -97,9 +97,6 @@ namespace ICD.Common.Utils public static bool IsDefined(T value) where T : struct, IConvertible { - if (!IsEnumType()) - throw new InvalidOperationException(string.Format("{0} is not an enum", typeof(T).Name)); - T all = GetFlagsAllValue(); return HasFlags(all, value); } @@ -228,9 +225,6 @@ namespace ICD.Common.Utils 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; @@ -257,9 +251,6 @@ namespace ICD.Common.Utils 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; @@ -289,9 +280,6 @@ 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); @@ -327,9 +315,6 @@ 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; @@ -345,9 +330,6 @@ namespace ICD.Common.Utils public static IEnumerable GetFlags(T value) where T : struct, IConvertible { - if (!IsEnum(value)) - throw new ArgumentException(string.Format("{0} is not an enum", value.GetType().Name), "value"); - Type type = typeof(T); int valueInt = (int)(object)value; @@ -376,9 +358,6 @@ namespace ICD.Common.Utils public static IEnumerable GetFlagsExceptNone() where T : struct, IConvertible { - if (!IsEnumType()) - throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); - T allValue = GetFlagsAllValue(); return GetFlagsExceptNone(allValue); } @@ -392,9 +371,6 @@ namespace ICD.Common.Utils public static IEnumerable GetFlagsExceptNone(T value) where T : struct, IConvertible { - if (!IsEnum(value)) - throw new ArgumentException(string.Format("{0} is not an enum", value.GetType().Name), "value"); - return GetFlags(value).Except(default(T)); } @@ -410,9 +386,6 @@ namespace ICD.Common.Utils public static IEnumerable GetAllFlagCombinationsExceptNone(T value) where T : struct, IConvertible { - if (!IsEnum(value)) - throw new ArgumentException(string.Format("{0} is not an enum", value.GetType().Name), "value"); - int maxEnumValue = (GetValues().Max(v => (int)(object)v) * 2) -1; return Enumerable.Range(1, maxEnumValue).Select(i => (T)(object)i ).Where(v => HasFlags(value, v)); } @@ -425,9 +398,6 @@ namespace ICD.Common.Utils public static T GetFlagsAllValue() where T : struct, IConvertible { - if (!IsEnumType()) - throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); - int output = GetValues().Aggregate(0, (current, value) => current | (int)(object)value); return (T)Enum.ToObject(typeof(T), output); } @@ -442,9 +412,6 @@ namespace ICD.Common.Utils if (type == null) throw new ArgumentNullException("type"); - if (!IsEnumType(type)) - throw new ArgumentException(string.Format("{0} is not an enum", type.Name)); - return GetValuesUncached(type).Aggregate(0, (current, value) => current | value); } @@ -458,12 +425,6 @@ namespace ICD.Common.Utils public static bool HasFlag(T value, T flag) where T : struct, IConvertible { - if (!IsEnum(value)) - throw new ArgumentException(string.Format("{0} is not an enum", value.GetType().Name), "value"); - - if (!IsEnum(flag)) - throw new ArgumentException(string.Format("{0} is not an enum", flag.GetType().Name), "flag"); - return HasFlags(value, flag); } @@ -488,12 +449,6 @@ namespace ICD.Common.Utils public static bool HasFlags(T value, T flags) where T : struct, IConvertible { - if (!IsEnum(value)) - throw new ArgumentException(string.Format("{0} is not an enum", value.GetType().Name), "value"); - - if (!IsEnum(flags)) - throw new ArgumentException(string.Format("{0} is not an enum", flags.GetType().Name), "flags"); - int a = (int)(object)value; int b = (int)(object)flags; @@ -520,9 +475,6 @@ namespace ICD.Common.Utils public static bool HasSingleFlag(T value) where T : struct, IConvertible { - if (!IsEnum(value)) - throw new ArgumentException(string.Format("{0} is not an enum", value.GetType().Name), "value"); - int numeric = (int)(object)value; return HasAnyFlags(numeric) && !HasMultipleFlags(numeric); @@ -536,9 +488,6 @@ namespace ICD.Common.Utils public static bool HasMultipleFlags(T value) where T : struct, IConvertible { - if (!IsEnum(value)) - throw new ArgumentException(string.Format("{0} is not an enum", value.GetType().Name), "value"); - return HasMultipleFlags((int)(object)value); } @@ -604,9 +553,6 @@ namespace ICD.Common.Utils public static T Parse(string data, bool ignoreCase) where T : struct, IConvertible { - if (!IsEnumType()) - throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); - T output; if (TryParse(data, ignoreCase, out output)) return output; @@ -627,9 +573,6 @@ namespace ICD.Common.Utils if (type == null) throw new ArgumentNullException("type"); - if (!IsEnumType(type)) - throw new ArgumentException(string.Format("{0} is not an enum", type.Name)); - int output; if (TryParse(type, data, ignoreCase, out output)) return output; @@ -649,9 +592,6 @@ namespace ICD.Common.Utils public static bool TryParse(string data, bool ignoreCase, out T result) where T : struct, IConvertible { - if (!IsEnumType()) - throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); - result = default(T); try @@ -678,9 +618,6 @@ namespace ICD.Common.Utils if (type == null) throw new ArgumentNullException("type"); - if (!IsEnumType(type)) - throw new ArgumentException(string.Format("{0} is not an enum", type.Name)); - result = 0; try @@ -705,9 +642,6 @@ namespace ICD.Common.Utils public static T ParseStrict(string data, bool ignoreCase) where T : struct, IConvertible { - if (!IsEnumType()) - throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); - T output; try @@ -739,9 +673,6 @@ namespace ICD.Common.Utils if (type == null) throw new ArgumentNullException("type"); - if (!IsEnumType(type)) - throw new ArgumentException(string.Format("{0} is not an enum", type.Name)); - int output; try @@ -772,9 +703,6 @@ namespace ICD.Common.Utils public static bool TryParseStrict(string data, bool ignoreCase, out T result) where T : struct, IConvertible { - if (!IsEnumType()) - throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); - result = default(T); try diff --git a/ICD.Common.Utils/Extensions/EnumExtensions.cs b/ICD.Common.Utils/Extensions/EnumExtensions.cs index a9e4ce9..41604cc 100644 --- a/ICD.Common.Utils/Extensions/EnumExtensions.cs +++ b/ICD.Common.Utils/Extensions/EnumExtensions.cs @@ -15,12 +15,6 @@ namespace ICD.Common.Utils.Extensions public static bool HasFlag(this T extends, T value) where T : struct, IConvertible { - if (!EnumUtils.IsEnum(extends)) - throw new ArgumentException(string.Format("{0} is not an enum", extends.GetType().Name), "extends"); - - if (!EnumUtils.IsEnum(value)) - throw new ArgumentException(string.Format("{0} is not an enum", value.GetType().Name), "value"); - return EnumUtils.HasFlag(extends, value); } @@ -34,12 +28,6 @@ namespace ICD.Common.Utils.Extensions public static bool HasFlags(this T extends, T values) where T : struct, IConvertible { - if (!EnumUtils.IsEnum(extends)) - throw new ArgumentException(string.Format("{0} is not an enum", extends.GetType().Name), "extends"); - - if (!EnumUtils.IsEnum(values)) - throw new ArgumentException(string.Format("{0} is not an enum", values.GetType().Name), "values"); - return EnumUtils.HasFlags(extends, values); } @@ -53,9 +41,6 @@ namespace ICD.Common.Utils.Extensions public static ushort ToUShort(this T extends) where T : struct, IConvertible { - if (!EnumUtils.IsEnum(extends)) - throw new ArgumentException(string.Format("{0} is not an enum", extends.GetType().Name), "extends"); - return (ushort)(object)extends; } }