using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace RomCheater.Logging { public class EnumNameValuePair where TValue : IConvertible { #region implicit operators //public static implicit operator EnumNameValuePair(TValue o) { return new EnumNameValuePair("", o); } public static implicit operator TValue(EnumNameValuePair o) { return o.Value; } public static implicit operator string(EnumNameValuePair o) { return o.Name; } public static TValue operator |(EnumNameValuePair x, EnumNameValuePair y) { return bitwise_or(x, y); } public static TValue operator &(EnumNameValuePair x, EnumNameValuePair y) { return bitwise_and(x, y); } public static bool operator ==(EnumNameValuePair x, EnumNameValuePair y) { return x == y; } public static bool operator !=(EnumNameValuePair x, EnumNameValuePair y) { return x != y; } public override bool Equals(object obj) { EnumNameValuePair t = (obj as EnumNameValuePair); if (t == null) return false; return this.Equals(t); } public override int GetHashCode() { return base.GetHashCode(); } #endregion public EnumNameValuePair() : this("") { } public EnumNameValuePair(string name) : this(name, default(TValue)) { } public EnumNameValuePair(string name, TValue value) { this.Name = name; this.Value = value; } public string Name { get; protected set; } public TValue Value { get; protected set; } public override string ToString() { return string.Format("[{0}: 0x{1:x8}]", Name.ToString(), Value); } private static TValue bitwise_or(EnumNameValuePair x, EnumNameValuePair y) { #region bitwise code string type = (typeof(TValue).Name); if (type.ToLower() == "byte") { byte x1 = Convert.ToByte(x.Value); byte y1 = Convert.ToByte(y.Value); return (TValue)Convert.ChangeType(x1 | y1, typeof(TValue)); } else if (type.ToLower() == "sbyte") { sbyte x1 = Convert.ToSByte(x.Value); sbyte y1 = Convert.ToSByte(y.Value); return (TValue)Convert.ChangeType(x1 | y1, typeof(TValue)); } else if (type.ToLower() == "int16") { short x1 = Convert.ToInt16(x.Value); short y1 = Convert.ToInt16(y.Value); return (TValue)Convert.ChangeType(x1 | y1, typeof(TValue)); } else if (type.ToLower() == "uint16") { ushort x1 = Convert.ToUInt16(x.Value); ushort y1 = Convert.ToUInt16(y.Value); return (TValue)Convert.ChangeType(x1 | y1, typeof(TValue)); } else if (type.ToLower() == "int32") { int x1 = Convert.ToInt32(x.Value); int y1 = Convert.ToInt32(y.Value); return (TValue)Convert.ChangeType(x1 | y1, typeof(TValue)); } else if (type.ToLower() == "uint32") { uint x1 = Convert.ToUInt32(x.Value); uint y1 = Convert.ToUInt32(y.Value); return (TValue)Convert.ChangeType(x1 | y1, typeof(TValue)); } else if (type.ToLower() == "int64") { long x1 = Convert.ToInt64(x.Value); long y1 = Convert.ToInt64(y.Value); return (TValue)Convert.ChangeType(x1 | y1, typeof(TValue)); } else if (type.ToLower() == "uint64") { ulong x1 = Convert.ToUInt64(x.Value); ulong y1 = Convert.ToUInt64(y.Value); return (TValue)Convert.ChangeType(x1 | y1, typeof(TValue)); } else { throw new InvalidCastException(string.Format("Unable to cast values to type: {0}", type), new NotImplementedException(string.Format("Type: {0} has not been implemented", type))); } #endregion } private static TValue bitwise_and(EnumNameValuePair x, EnumNameValuePair y) { #region bitwise code string type = (typeof(TValue).Name); if (type.ToLower() == "byte") { byte x1 = Convert.ToByte(x.Value); byte y1 = Convert.ToByte(y.Value); return (TValue)Convert.ChangeType(x1 & y1, typeof(TValue)); } else if (type.ToLower() == "sbyte") { sbyte x1 = Convert.ToSByte(x.Value); sbyte y1 = Convert.ToSByte(y.Value); return (TValue)Convert.ChangeType(x1 & y1, typeof(TValue)); } else if (type.ToLower() == "int16") { short x1 = Convert.ToInt16(x.Value); short y1 = Convert.ToInt16(y.Value); return (TValue)Convert.ChangeType(x1 & y1, typeof(TValue)); } else if (type.ToLower() == "uint16") { ushort x1 = Convert.ToUInt16(x.Value); ushort y1 = Convert.ToUInt16(y.Value); return (TValue)Convert.ChangeType(x1 & y1, typeof(TValue)); } else if (type.ToLower() == "int32") { int x1 = Convert.ToInt32(x.Value); int y1 = Convert.ToInt32(y.Value); return (TValue)Convert.ChangeType(x1 & y1, typeof(TValue)); } else if (type.ToLower() == "uint32") { uint x1 = Convert.ToUInt32(x.Value); uint y1 = Convert.ToUInt32(y.Value); return (TValue)Convert.ChangeType(x1 & y1, typeof(TValue)); } else if (type.ToLower() == "int64") { long x1 = Convert.ToInt64(x.Value); long y1 = Convert.ToInt64(y.Value); return (TValue)Convert.ChangeType(x1 & y1, typeof(TValue)); } else if (type.ToLower() == "uint64") { ulong x1 = Convert.ToUInt64(x.Value); ulong y1 = Convert.ToUInt64(y.Value); return (TValue)Convert.ChangeType(x1 & y1, typeof(TValue)); } else { throw new InvalidCastException(string.Format("Unable to cast values to type: {0}", type), new NotImplementedException(string.Format("Type: {0} has not been implemented", type))); } #endregion } internal static TValue bitwise_or(TValue x, TValue y) { return bitwise_or(new EnumNameValuePair("",x), new EnumNameValuePair("",y)); } internal static TValue bitwise_or(params EnumNameValuePair[] values) { if (!(values.Length > 1 )) { throw new InvalidOperationException("bitwise_or must have at least two operands"); } TValue value = default(TValue); foreach (EnumNameValuePair v in values) { value = bitwise_or(value, v.Value); } return value; } internal static TValue bitwise_and(TValue x, TValue y) { return bitwise_and(new EnumNameValuePair("", x), new EnumNameValuePair("", y)); } internal static TValue bitwise_and(params EnumNameValuePair[] values) { if (!(values.Length > 1)) { throw new InvalidOperationException("bitwise_and must have at least two operands"); } TValue value = default(TValue); foreach (EnumNameValuePair v in values) { value = bitwise_and(value, v.Value); } return value; } } }