perf: Removing heavy-handed validation from enum utils

This commit is contained in:
Chris Cameron
2018-08-21 10:26:24 -04:00
parent 1c9d311422
commit d79c272df0
2 changed files with 0 additions and 87 deletions

View File

@@ -97,9 +97,6 @@ namespace ICD.Common.Utils
public static bool IsDefined<T>(T value) public static bool IsDefined<T>(T value)
where T : struct, IConvertible where T : struct, IConvertible
{ {
if (!IsEnumType<T>())
throw new InvalidOperationException(string.Format("{0} is not an enum", typeof(T).Name));
T all = GetFlagsAllValue<T>(); T all = GetFlagsAllValue<T>();
return HasFlags(all, value); return HasFlags(all, value);
} }
@@ -228,9 +225,6 @@ namespace ICD.Common.Utils
public static T ExcludeFlags<T>(T a, T b) public static T ExcludeFlags<T>(T a, T b)
where T : struct, IConvertible 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 aInt = (int)(object)a;
int bInt = (int)(object)b; int bInt = (int)(object)b;
@@ -257,9 +251,6 @@ namespace ICD.Common.Utils
public static T IncludeFlags<T>(T a, T b) public static T IncludeFlags<T>(T a, T b)
where T : struct, IConvertible 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 aInt = (int)(object)a;
int bInt = (int)(object)b; int bInt = (int)(object)b;
@@ -289,9 +280,6 @@ namespace ICD.Common.Utils
if (values == null) if (values == null)
throw new ArgumentNullException("values"); throw new ArgumentNullException("values");
if (!IsEnumType<T>())
throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name));
if (values.Length == 0) if (values.Length == 0)
return default(T); return default(T);
@@ -327,9 +315,6 @@ namespace ICD.Common.Utils
public static T GetFlagsIntersection<T>(T a, T b) public static T GetFlagsIntersection<T>(T a, T b)
where T : struct, IConvertible 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 aInt = (int)(object)a;
int bInt = (int)(object)b; int bInt = (int)(object)b;
@@ -345,9 +330,6 @@ namespace ICD.Common.Utils
public static IEnumerable<T> GetFlags<T>(T value) public static IEnumerable<T> GetFlags<T>(T value)
where T : struct, IConvertible 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); Type type = typeof(T);
int valueInt = (int)(object)value; int valueInt = (int)(object)value;
@@ -376,9 +358,6 @@ namespace ICD.Common.Utils
public static IEnumerable<T> GetFlagsExceptNone<T>() public static IEnumerable<T> GetFlagsExceptNone<T>()
where T : struct, IConvertible where T : struct, IConvertible
{ {
if (!IsEnumType<T>())
throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name));
T allValue = GetFlagsAllValue<T>(); T allValue = GetFlagsAllValue<T>();
return GetFlagsExceptNone(allValue); return GetFlagsExceptNone(allValue);
} }
@@ -392,9 +371,6 @@ namespace ICD.Common.Utils
public static IEnumerable<T> GetFlagsExceptNone<T>(T value) public static IEnumerable<T> GetFlagsExceptNone<T>(T value)
where T : struct, IConvertible 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)); return GetFlags(value).Except(default(T));
} }
@@ -410,9 +386,6 @@ namespace ICD.Common.Utils
public static IEnumerable<T> GetAllFlagCombinationsExceptNone<T>(T value) public static IEnumerable<T> GetAllFlagCombinationsExceptNone<T>(T value)
where T : struct, IConvertible where T : struct, IConvertible
{ {
if (!IsEnum(value))
throw new ArgumentException(string.Format("{0} is not an enum", value.GetType().Name), "value");
int maxEnumValue = (GetValues<T>().Max(v => (int)(object)v) * 2) -1; int maxEnumValue = (GetValues<T>().Max(v => (int)(object)v) * 2) -1;
return Enumerable.Range(1, maxEnumValue).Select(i => (T)(object)i ).Where(v => HasFlags(value, v)); 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<T>() public static T GetFlagsAllValue<T>()
where T : struct, IConvertible where T : struct, IConvertible
{ {
if (!IsEnumType<T>())
throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name));
int output = GetValues<T>().Aggregate(0, (current, value) => current | (int)(object)value); int output = GetValues<T>().Aggregate(0, (current, value) => current | (int)(object)value);
return (T)Enum.ToObject(typeof(T), output); return (T)Enum.ToObject(typeof(T), output);
} }
@@ -442,9 +412,6 @@ namespace ICD.Common.Utils
if (type == null) if (type == null)
throw new ArgumentNullException("type"); 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); return GetValuesUncached(type).Aggregate(0, (current, value) => current | value);
} }
@@ -458,12 +425,6 @@ namespace ICD.Common.Utils
public static bool HasFlag<T>(T value, T flag) public static bool HasFlag<T>(T value, T flag)
where T : struct, IConvertible 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); return HasFlags(value, flag);
} }
@@ -488,12 +449,6 @@ namespace ICD.Common.Utils
public static bool HasFlags<T>(T value, T flags) public static bool HasFlags<T>(T value, T flags)
where T : struct, IConvertible 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 a = (int)(object)value;
int b = (int)(object)flags; int b = (int)(object)flags;
@@ -520,9 +475,6 @@ namespace ICD.Common.Utils
public static bool HasSingleFlag<T>(T value) public static bool HasSingleFlag<T>(T value)
where T : struct, IConvertible 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; int numeric = (int)(object)value;
return HasAnyFlags(numeric) && !HasMultipleFlags(numeric); return HasAnyFlags(numeric) && !HasMultipleFlags(numeric);
@@ -536,9 +488,6 @@ namespace ICD.Common.Utils
public static bool HasMultipleFlags<T>(T value) public static bool HasMultipleFlags<T>(T value)
where T : struct, IConvertible 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); return HasMultipleFlags((int)(object)value);
} }
@@ -604,9 +553,6 @@ namespace ICD.Common.Utils
public static T Parse<T>(string data, bool ignoreCase) public static T Parse<T>(string data, bool ignoreCase)
where T : struct, IConvertible where T : struct, IConvertible
{ {
if (!IsEnumType<T>())
throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name));
T output; T output;
if (TryParse(data, ignoreCase, out output)) if (TryParse(data, ignoreCase, out output))
return output; return output;
@@ -627,9 +573,6 @@ namespace ICD.Common.Utils
if (type == null) if (type == null)
throw new ArgumentNullException("type"); throw new ArgumentNullException("type");
if (!IsEnumType(type))
throw new ArgumentException(string.Format("{0} is not an enum", type.Name));
int output; int output;
if (TryParse(type, data, ignoreCase, out output)) if (TryParse(type, data, ignoreCase, out output))
return output; return output;
@@ -649,9 +592,6 @@ namespace ICD.Common.Utils
public static bool TryParse<T>(string data, bool ignoreCase, out T result) public static bool TryParse<T>(string data, bool ignoreCase, out T result)
where T : struct, IConvertible where T : struct, IConvertible
{ {
if (!IsEnumType<T>())
throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name));
result = default(T); result = default(T);
try try
@@ -678,9 +618,6 @@ namespace ICD.Common.Utils
if (type == null) if (type == null)
throw new ArgumentNullException("type"); throw new ArgumentNullException("type");
if (!IsEnumType(type))
throw new ArgumentException(string.Format("{0} is not an enum", type.Name));
result = 0; result = 0;
try try
@@ -705,9 +642,6 @@ namespace ICD.Common.Utils
public static T ParseStrict<T>(string data, bool ignoreCase) public static T ParseStrict<T>(string data, bool ignoreCase)
where T : struct, IConvertible where T : struct, IConvertible
{ {
if (!IsEnumType<T>())
throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name));
T output; T output;
try try
@@ -739,9 +673,6 @@ namespace ICD.Common.Utils
if (type == null) if (type == null)
throw new ArgumentNullException("type"); throw new ArgumentNullException("type");
if (!IsEnumType(type))
throw new ArgumentException(string.Format("{0} is not an enum", type.Name));
int output; int output;
try try
@@ -772,9 +703,6 @@ namespace ICD.Common.Utils
public static bool TryParseStrict<T>(string data, bool ignoreCase, out T result) public static bool TryParseStrict<T>(string data, bool ignoreCase, out T result)
where T : struct, IConvertible where T : struct, IConvertible
{ {
if (!IsEnumType<T>())
throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name));
result = default(T); result = default(T);
try try

View File

@@ -15,12 +15,6 @@ namespace ICD.Common.Utils.Extensions
public static bool HasFlag<T>(this T extends, T value) public static bool HasFlag<T>(this T extends, T value)
where T : struct, IConvertible 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); return EnumUtils.HasFlag(extends, value);
} }
@@ -34,12 +28,6 @@ namespace ICD.Common.Utils.Extensions
public static bool HasFlags<T>(this T extends, T values) public static bool HasFlags<T>(this T extends, T values)
where T : struct, IConvertible 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); return EnumUtils.HasFlags(extends, values);
} }
@@ -53,9 +41,6 @@ namespace ICD.Common.Utils.Extensions
public static ushort ToUShort<T>(this T extends) public static ushort ToUShort<T>(this T extends)
where T : struct, IConvertible 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; return (ushort)(object)extends;
} }
} }