EnumHelper - generic add/remove_flags methods

This commit is contained in:
John Colvin committed 2019-11-25 12:55:13 -06:00
1 parent 03940b291b
commit d42f6b6fff
1 file changed
+112 -2
+112 -2
View File
@@ -24,6 +24,18 @@ namespace mbts.ui.utility {
return Enum.GetValues(t);
}
public static object add_flags(object value, object flags) =>
EnumDescriptorStore.descriptor(value.GetType()).add_flags(value, flags);
public static object remove_flags(object value, object flags) =>
EnumDescriptorStore.descriptor(value.GetType()).add_flags(value, flags);
public static T add_flags<T>(T value, T flags) where T : struct =>
(T)EnumDescriptorStore<T>.descriptor.add_flags(value, flags);
public static T remove_flags<T>(T value, T flags) where T : struct =>
(T)EnumDescriptorStore<T>.descriptor.remove_flags(value, flags);
public static T parse<T>(string name) where T : struct /*(Enum, more specifically)*/ {
if (!typeof(T).IsEnum) throw new InvalidOperationException($"{typeof(T)} is not an enum type.");
if (!Enum.TryParse(name, true, out T result)) throw new ArgumentException(nameof(name), "Does not match any of the values");
@@ -103,9 +115,16 @@ namespace mbts.ui.utility {
struct EnumDescriptor {
public readonly Func<object, object, bool> has_flags;
public readonly Func<object, object, object> add_flags, remove_flags;
public EnumDescriptor(Func<object, object, bool> has_flags) =>
public EnumDescriptor(
Func<object, object, bool> has_flags,
Func<object, object, object> add_flags,
Func<object, object, object> remove_flags) {
this.has_flags = has_flags;
this.add_flags = add_flags;
this.remove_flags = remove_flags;
}
}
static class EnumDescriptorStore {
@@ -129,83 +148,174 @@ namespace mbts.ui.utility {
static EnumDescriptorStore() {
Func<object, object, bool> has_flags;
Func<object, object, object> add_flags, remove_flags;
if (!typeof(T).IsEnum) {
// N/A
has_flags = Impl.as_none_has_flags;
add_flags = Impl.as_none_add_flags;
remove_flags = Impl.as_none_remove_flags;
}
var numeric_type = Enum.GetUnderlyingType(typeof(T));
if (numeric_type == typeof(byte)) {
// byte
has_flags = Impl.as_byte_has_flags;
add_flags = Impl.as_byte_add_flags;
remove_flags = Impl.as_byte_remove_flags;
} else if (numeric_type == typeof(short)) {
// short
has_flags = Impl.as_int16_has_flags;
add_flags = Impl.as_int16_add_flags;
remove_flags = Impl.as_int16_remove_flags;
} else if (numeric_type == typeof(int)) {
// int
has_flags = Impl.as_int32_has_flags;
add_flags = Impl.as_int32_add_flags;
remove_flags = Impl.as_int32_remove_flags;
} else if (numeric_type == typeof(long)) {
// long
has_flags = Impl.as_int64_has_flags;
add_flags = Impl.as_int64_add_flags;
remove_flags = Impl.as_int64_remove_flags;
} else if (numeric_type == typeof(ushort)) {
// ushort
has_flags = Impl.as_uint16_has_flags;
add_flags = Impl.as_uint16_add_flags;
remove_flags = Impl.as_uint16_remove_flags;
} else if (numeric_type == typeof(uint)) {
// uint
has_flags = Impl.as_uint32_has_flags;
add_flags = Impl.as_uint32_add_flags;
remove_flags = Impl.as_uint32_remove_flags;
} else if (numeric_type == typeof(ulong)) {
// ulong
has_flags = Impl.as_uint64_has_flags;
add_flags = Impl.as_uint64_add_flags;
remove_flags = Impl.as_uint64_remove_flags;
} else {
// ?
has_flags = Impl.as_none_has_flags;
add_flags = Impl.as_none_add_flags;
remove_flags = Impl.as_none_remove_flags;
}
descriptor = new EnumDescriptor(has_flags);
descriptor = new EnumDescriptor(has_flags, add_flags, remove_flags);
}
static class Impl {
public static bool as_none_has_flags(object value, object flags) => false;
public static object as_none_add_flags(object value, object flags) => value;
public static object as_none_remove_flags(object value, object flags) => value;
// byte
public static bool as_byte_has_flags(object value, object flags) {
byte value_as_byte = (byte)Convert.ChangeType(value, typeof(byte));
byte flags_as_byte = (byte)Convert.ChangeType(flags, typeof(byte));
return (value_as_byte & flags_as_byte) == flags_as_byte;
}
public static object as_byte_add_flags(object value, object flags) {
byte value_as_byte = (byte)Convert.ChangeType(value, typeof(byte));
byte flags_as_byte = (byte)Convert.ChangeType(flags, typeof(byte));
return value_as_byte | flags_as_byte;
}
public static object as_byte_remove_flags(object value, object flags) {
byte value_as_byte = (byte)Convert.ChangeType(value, typeof(byte));
byte flags_as_byte = (byte)Convert.ChangeType(flags, typeof(byte));
return (value_as_byte | flags_as_byte) ^ flags_as_byte;
}
// short
public static bool as_int16_has_flags(object value, object flags) {
short value_as_short = (short)Convert.ChangeType(value, typeof(short));
short flags_as_short = (short)Convert.ChangeType(flags, typeof(short));
return (value_as_short & flags_as_short) == flags_as_short;
}
public static object as_int16_add_flags(object value, object flags) {
short value_as_short = (short)Convert.ChangeType(value, typeof(short));
short flags_as_short = (short)Convert.ChangeType(flags, typeof(short));
return value_as_short | flags_as_short;
}
public static object as_int16_remove_flags(object value, object flags) {
short value_as_short = (short)Convert.ChangeType(value, typeof(short));
short flags_as_short = (short)Convert.ChangeType(flags, typeof(short));
return (value_as_short | flags_as_short) ^ flags_as_short;
}
// int
public static bool as_int32_has_flags(object value, object flags) {
int value_as_byte = (int)Convert.ChangeType(value, typeof(int));
int flags_as_byte = (int)Convert.ChangeType(flags, typeof(int));
return (value_as_byte & flags_as_byte) == flags_as_byte;
}
public static object as_int32_add_flags(object value, object flags) {
int value_as_int = (int)Convert.ChangeType(value, typeof(int));
int flags_as_int = (int)Convert.ChangeType(flags, typeof(int));
return value_as_int | flags_as_int;
}
public static object as_int32_remove_flags(object value, object flags) {
int value_as_int = (int)Convert.ChangeType(value, typeof(int));
int flags_as_int = (int)Convert.ChangeType(flags, typeof(int));
return (value_as_int | flags_as_int) ^ flags_as_int;
}
// long
public static bool as_int64_has_flags(object value, object flags) {
long value_as_long = (long)Convert.ChangeType(value, typeof(long));
long flags_as_long = (long)Convert.ChangeType(flags, typeof(long));
return (value_as_long & flags_as_long) == flags_as_long;
}
public static object as_int64_add_flags(object value, object flags) {
long value_as_long = (long)Convert.ChangeType(value, typeof(long));
long flags_as_long = (long)Convert.ChangeType(flags, typeof(long));
return value_as_long | flags_as_long;
}
public static object as_int64_remove_flags(object value, object flags) {
long value_as_long = (long)Convert.ChangeType(value, typeof(long));
long flags_as_long = (long)Convert.ChangeType(flags, typeof(long));
return (value_as_long | flags_as_long) ^ flags_as_long;
}
// ushort
public static bool as_uint16_has_flags(object value, object flags) {
ushort value_as_ushort = (ushort)Convert.ChangeType(value, typeof(ushort));
ushort flags_as_ushort = (ushort)Convert.ChangeType(flags, typeof(ushort));
return (value_as_ushort & flags_as_ushort) == flags_as_ushort;
}
public static object as_uint16_add_flags(object value, object flags) {
ushort value_as_ushort = (ushort)Convert.ChangeType(value, typeof(ushort));
ushort flags_as_ushort = (ushort)Convert.ChangeType(flags, typeof(ushort));
return value_as_ushort | flags_as_ushort;
}
public static object as_uint16_remove_flags(object value, object flags) {
ushort value_as_ushort = (ushort)Convert.ChangeType(value, typeof(ushort));
ushort flags_as_ushort = (ushort)Convert.ChangeType(flags, typeof(ushort));
return (value_as_ushort | flags_as_ushort) ^ flags_as_ushort;
}
// uint
public static bool as_uint32_has_flags(object value, object flags) {
uint value_as_uint = (uint)Convert.ChangeType(value, typeof(uint));
uint flags_as_uint = (uint)Convert.ChangeType(flags, typeof(uint));
return (value_as_uint & flags_as_uint) == flags_as_uint;
}
public static object as_uint32_add_flags(object value, object flags) {
uint value_as_uint = (uint)Convert.ChangeType(value, typeof(uint));
uint flags_as_uint = (uint)Convert.ChangeType(flags, typeof(uint));
return value_as_uint | flags_as_uint;
}
public static object as_uint32_remove_flags(object value, object flags) {
uint value_as_uint = (uint)Convert.ChangeType(value, typeof(uint));
uint flags_as_uint = (uint)Convert.ChangeType(flags, typeof(uint));
return (value_as_uint | flags_as_uint) ^ flags_as_uint;
}
// ulong
public static bool as_uint64_has_flags(object value, object flags) {
ulong value_as_ulong = (ulong)Convert.ChangeType(value, typeof(ulong));
ulong flags_as_ulong = (ulong)Convert.ChangeType(flags, typeof(ulong));
return (value_as_ulong & flags_as_ulong) == flags_as_ulong;
}
public static object as_uint64_add_flags(object value, object flags) {
ulong value_as_ulong = (ulong)Convert.ChangeType(value, typeof(ulong));
ulong flags_as_ulong = (ulong)Convert.ChangeType(flags, typeof(ulong));
return value_as_ulong | flags_as_ulong;
}
public static object as_uint64_remove_flags(object value, object flags) {
ulong value_as_ulong = (ulong)Convert.ChangeType(value, typeof(ulong));
ulong flags_as_ulong = (ulong)Convert.ChangeType(flags, typeof(ulong));
return (value_as_ulong | flags_as_ulong) ^ flags_as_ulong;
}
}
}
}