ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/RomCheater/trunk/RomCheater.Logging/EnumNameValuePair.cs
(Generate patch)

Comparing trunk/RomCheater.Logging/EnumNameValuePair.cs (file contents):
Revision 111 by william, Thu May 10 13:55:44 2012 UTC vs.
Revision 115 by william, Thu May 10 15:30:14 2012 UTC

# Line 5 | Line 5 | using System.Text;
5  
6   namespace RomCheater.Logging
7   {
8 <    public class EnumNameValuePair<TName,TValue>
8 >    public class EnumNameValuePair<TValue>
9          where TValue : IConvertible
10      {
11          #region implicit operators
12 <        public static implicit operator TValue(EnumNameValuePair<TName, TValue> o) { return o.Value; }
13 <        public static implicit operator TName(EnumNameValuePair<TName, TValue> o) { return o.Name; }
12 >        //public static implicit operator EnumNameValuePair<TValue>(TValue o) { return new EnumNameValuePair<TValue>("", o); }
13 >        public static implicit operator TValue(EnumNameValuePair<TValue> o) { return o.Value; }
14 >        public static implicit operator string(EnumNameValuePair<TValue> o) { return o.Name; }
15 >        public static TValue operator |(EnumNameValuePair<TValue> x, EnumNameValuePair<TValue> y) { return bitwise_or(x, y); }
16 >        public static TValue operator &(EnumNameValuePair<TValue> x, EnumNameValuePair<TValue> y) { return bitwise_and(x, y); }
17 >        public static bool operator ==(EnumNameValuePair<TValue> x, EnumNameValuePair<TValue> y) { return x == y; }
18 >        public static bool operator !=(EnumNameValuePair<TValue> x, EnumNameValuePair<TValue> y) { return x != y; }
19 >        public override bool Equals(object obj)
20 >        {
21 >            EnumNameValuePair<TValue> t = (obj as EnumNameValuePair<TValue>);
22 >            if (t == null) return false;
23 >            return this.Equals(t);
24 >        }
25 >        public override int GetHashCode()
26 >        {
27 >            return base.GetHashCode();
28 >        }
29          #endregion
30 <        public EnumNameValuePair() : this(default(TName)) { }
31 <        public EnumNameValuePair(TName name) : this(name, default(TValue)) { }
32 <        public EnumNameValuePair(TName name, TValue value)
30 >        public EnumNameValuePair() : this("") { }
31 >        public EnumNameValuePair(string name) : this(name, default(TValue)) { }
32 >        public EnumNameValuePair(string name, TValue value)
33          {
34              this.Name = name;
35              this.Value = value;
36          }
37 <        public TName Name { get; protected set; }
37 >        public string Name { get; protected set; }
38          public TValue Value { get; protected set; }
39          public override string ToString()
40          {
41              return string.Format("[{0}: 0x{1:x8}]", Name.ToString(), Value);
42          }
43 +        private static TValue bitwise_or(EnumNameValuePair<TValue> x, EnumNameValuePair<TValue> y)
44 +        {
45 +            #region bitwise code
46 +            string type = (typeof(TValue).Name);
47 +            if (type.ToLower() == "byte")
48 +            {
49 +                byte x1 = Convert.ToByte(x.Value);
50 +                byte y1 = Convert.ToByte(y.Value);
51 +                return (TValue)Convert.ChangeType(x1 | y1, typeof(TValue));
52 +            }
53 +            else if (type.ToLower() == "sbyte")
54 +            {
55 +                sbyte x1 = Convert.ToSByte(x.Value);
56 +                sbyte y1 = Convert.ToSByte(y.Value);
57 +                return (TValue)Convert.ChangeType(x1 | y1, typeof(TValue));
58 +            }
59 +            else if (type.ToLower() == "int16")
60 +            {
61 +                short x1 = Convert.ToInt16(x.Value);
62 +                short y1 = Convert.ToInt16(y.Value);
63 +                return (TValue)Convert.ChangeType(x1 | y1, typeof(TValue));
64 +            }
65 +            else if (type.ToLower() == "uint16")
66 +            {
67 +                ushort x1 = Convert.ToUInt16(x.Value);
68 +                ushort y1 = Convert.ToUInt16(y.Value);
69 +                return (TValue)Convert.ChangeType(x1 | y1, typeof(TValue));
70 +            }
71 +            else if (type.ToLower() == "int32")
72 +            {
73 +                int x1 = Convert.ToInt32(x.Value);
74 +                int y1 = Convert.ToInt32(y.Value);
75 +                return (TValue)Convert.ChangeType(x1 | y1, typeof(TValue));
76 +            }
77 +            else if (type.ToLower() == "uint32")
78 +            {
79 +                uint x1 = Convert.ToUInt32(x.Value);
80 +                uint y1 = Convert.ToUInt32(y.Value);
81 +                return (TValue)Convert.ChangeType(x1 | y1, typeof(TValue));
82 +            }
83 +            else if (type.ToLower() == "int64")
84 +            {
85 +                long x1 = Convert.ToInt64(x.Value);
86 +                long y1 = Convert.ToInt64(y.Value);
87 +                return (TValue)Convert.ChangeType(x1 | y1, typeof(TValue));
88 +            }
89 +            else if (type.ToLower() == "uint64")
90 +            {
91 +                ulong x1 = Convert.ToUInt64(x.Value);
92 +                ulong y1 = Convert.ToUInt64(y.Value);
93 +                return (TValue)Convert.ChangeType(x1 | y1, typeof(TValue));
94 +            }
95 +            else
96 +            {
97 +                throw new InvalidCastException(string.Format("Unable to cast values to type: {0}", type), new NotImplementedException(string.Format("Type: {0} has not been implemented", type)));
98 +            }
99 +            #endregion
100 +        }
101 +        private static TValue bitwise_and(EnumNameValuePair<TValue> x, EnumNameValuePair<TValue> y)
102 +        {
103 +            #region bitwise code
104 +            string type = (typeof(TValue).Name);
105 +            if (type.ToLower() == "byte")
106 +            {
107 +                byte x1 = Convert.ToByte(x.Value);
108 +                byte y1 = Convert.ToByte(y.Value);
109 +                return (TValue)Convert.ChangeType(x1 & y1, typeof(TValue));
110 +            }
111 +            else if (type.ToLower() == "sbyte")
112 +            {
113 +                sbyte x1 = Convert.ToSByte(x.Value);
114 +                sbyte y1 = Convert.ToSByte(y.Value);
115 +                return (TValue)Convert.ChangeType(x1 & y1, typeof(TValue));
116 +            }
117 +            else if (type.ToLower() == "int16")
118 +            {
119 +                short x1 = Convert.ToInt16(x.Value);
120 +                short y1 = Convert.ToInt16(y.Value);
121 +                return (TValue)Convert.ChangeType(x1 & y1, typeof(TValue));
122 +            }
123 +            else if (type.ToLower() == "uint16")
124 +            {
125 +                ushort x1 = Convert.ToUInt16(x.Value);
126 +                ushort y1 = Convert.ToUInt16(y.Value);
127 +                return (TValue)Convert.ChangeType(x1 & y1, typeof(TValue));
128 +            }
129 +            else if (type.ToLower() == "int32")
130 +            {
131 +                int x1 = Convert.ToInt32(x.Value);
132 +                int y1 = Convert.ToInt32(y.Value);
133 +                return (TValue)Convert.ChangeType(x1 & y1, typeof(TValue));
134 +            }
135 +            else if (type.ToLower() == "uint32")
136 +            {
137 +                uint x1 = Convert.ToUInt32(x.Value);
138 +                uint y1 = Convert.ToUInt32(y.Value);
139 +                return (TValue)Convert.ChangeType(x1 & y1, typeof(TValue));
140 +            }
141 +            else if (type.ToLower() == "int64")
142 +            {
143 +                long x1 = Convert.ToInt64(x.Value);
144 +                long y1 = Convert.ToInt64(y.Value);
145 +                return (TValue)Convert.ChangeType(x1 & y1, typeof(TValue));
146 +            }
147 +            else if (type.ToLower() == "uint64")
148 +            {
149 +                ulong x1 = Convert.ToUInt64(x.Value);
150 +                ulong y1 = Convert.ToUInt64(y.Value);
151 +                return (TValue)Convert.ChangeType(x1 & y1, typeof(TValue));
152 +            }
153 +            else
154 +            {
155 +                throw new InvalidCastException(string.Format("Unable to cast values to type: {0}", type), new NotImplementedException(string.Format("Type: {0} has not been implemented", type)));
156 +            }
157 +            #endregion
158 +        }
159 +
160 +        internal static TValue bitwise_or(TValue x, TValue y)
161 +        {
162 +            return bitwise_or(new EnumNameValuePair<TValue>("",x), new EnumNameValuePair<TValue>("",y));
163 +        }
164 +        internal static TValue bitwise_or(params EnumNameValuePair<TValue>[] values)
165 +        {
166 +            if (!(values.Length > 1 )) { throw new InvalidOperationException("bitwise_or must have at least two operands"); }
167 +            TValue value = default(TValue);
168 +            foreach (EnumNameValuePair<TValue> v in values) { value = bitwise_or(value, v.Value); }
169 +            return value;
170 +        }
171 +
172 +        internal static TValue bitwise_and(TValue x, TValue y)
173 +        {
174 +            return bitwise_and(new EnumNameValuePair<TValue>("", x), new EnumNameValuePair<TValue>("", y));
175 +        }
176 +        internal static TValue bitwise_and(params EnumNameValuePair<TValue>[] values)
177 +        {
178 +            if (!(values.Length > 1)) { throw new InvalidOperationException("bitwise_and must have at least two operands"); }
179 +            TValue value = default(TValue);
180 +            foreach (EnumNameValuePair<TValue> v in values) { value = bitwise_and(value, v.Value); }
181 +            return value;
182 +        }
183      }
184   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines