PageRenderTime 52ms CodeModel.GetById 13ms app.highlight 33ms RepoModel.GetById 1ms app.codeStats 0ms

/IronPython_2_0/Src/IronPython/Runtime/Operations/EnumOps.cs

#
C# | 163 lines | 133 code | 16 blank | 14 comment | 114 complexity | 9cc4c22b02c15f8dca97307a005d3547 MD5 | raw file
  1/* ****************************************************************************
  2 *
  3 * Copyright (c) Microsoft Corporation. 
  4 *
  5 * This source code is subject to terms and conditions of the Microsoft Public License. A 
  6 * copy of the license can be found in the License.html file at the root of this distribution. If 
  7 * you cannot locate the  Microsoft Public License, please send an email to 
  8 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
  9 * by the terms of the Microsoft Public License.
 10 *
 11 * You must not remove this notice, or any other, from this software.
 12 *
 13 *
 14 * ***************************************************************************/
 15
 16using System; using Microsoft;
 17using System.Runtime.CompilerServices;
 18using Microsoft.Runtime.CompilerServices;
 19
 20
 21namespace IronPython.Runtime.Operations {
 22    public static class EnumOps {        
 23        [SpecialName]
 24        public static object BitwiseOr(object self, object other) {
 25            if (self is Enum && other is Enum) {
 26                Type selfType = self.GetType();
 27                Type otherType = other.GetType();
 28
 29                if (selfType == otherType) {
 30                    Type underType = Enum.GetUnderlyingType(selfType);
 31                    if (underType == typeof(int)) {
 32                        return Enum.ToObject(selfType, (int)self | (int)other);
 33                    } else if (underType == typeof(long)) {
 34                        return Enum.ToObject(selfType, (long)self | (long)other);
 35                    } else if (underType == typeof(short)) {
 36                        return Enum.ToObject(selfType, (short)self | (short)other);
 37                    } else if (underType == typeof(byte)) {
 38                        return Enum.ToObject(selfType, (byte)self | (byte)other);
 39                    } else if (underType == typeof(sbyte)) {
 40                        return Enum.ToObject(selfType, (sbyte)self | (sbyte)other);
 41                    } else if (underType == typeof(uint)) {
 42                        return Enum.ToObject(selfType, (uint)self | (uint)other);
 43                    } else if (underType == typeof(ulong)) {
 44                        return Enum.ToObject(selfType, (ulong)self | (ulong)other);
 45                    } else if (underType == typeof(ushort)) {
 46                        return Enum.ToObject(selfType, (ushort)self | (ushort)other);
 47                    }
 48                }
 49            }
 50
 51            throw PythonOps.ValueError("bitwise or cannot be applied to {0} and {1}", self.GetType(), other.GetType());
 52        }
 53
 54        [SpecialName]
 55        public static object BitwiseAnd(object self, object other) {
 56            if (self is Enum && other is Enum) {
 57                Type selfType = self.GetType();
 58                Type otherType = other.GetType();
 59
 60                if (selfType == otherType) {
 61                    Type underType = Enum.GetUnderlyingType(selfType);
 62                    if (underType == typeof(int)) {
 63                        return Enum.ToObject(selfType, (int)self & (int)other);
 64                    } else if (underType == typeof(long)) {
 65                        return Enum.ToObject(selfType, (long)self & (long)other);
 66                    } else if (underType == typeof(short)) {
 67                        return Enum.ToObject(selfType, (short)self & (short)other);
 68                    } else if (underType == typeof(byte)) {
 69                        return Enum.ToObject(selfType, (byte)self & (byte)other);
 70                    } else if (underType == typeof(sbyte)) {
 71                        return Enum.ToObject(selfType, (sbyte)self & (sbyte)other);
 72                    } else if (underType == typeof(uint)) {
 73                        return Enum.ToObject(selfType, (uint)self & (uint)other);
 74                    } else if (underType == typeof(ulong)) {
 75                        return Enum.ToObject(selfType, (ulong)self & (ulong)other);
 76                    } else if (underType == typeof(ushort)) {
 77                        return Enum.ToObject(selfType, (ushort)self & (ushort)other);
 78                    }
 79                }
 80            }
 81
 82            throw PythonOps.ValueError("bitwise and cannot be applied to {0} and {1}", self.GetType(), other.GetType());
 83        }
 84
 85        [SpecialName]
 86        public static object ExclusiveOr(object self, object other) {
 87            if (self is Enum && other is Enum) {
 88                Type selfType = self.GetType();
 89                Type otherType = other.GetType();
 90
 91                if (selfType == otherType) {
 92                    Type underType = Enum.GetUnderlyingType(selfType);
 93                    if (underType == typeof(int)) {
 94                        return Enum.ToObject(selfType, (int)self ^ (int)other);
 95                    } else if (underType == typeof(long)) {
 96                        return Enum.ToObject(selfType, (long)self ^ (long)other);
 97                    } else if (underType == typeof(short)) {
 98                        return Enum.ToObject(selfType, (short)self ^ (short)other);
 99                    } else if (underType == typeof(byte)) {
100                        return Enum.ToObject(selfType, (byte)self ^ (byte)other);
101                    } else if (underType == typeof(sbyte)) {
102                        return Enum.ToObject(selfType, (sbyte)self ^ (sbyte)other);
103                    } else if (underType == typeof(uint)) {
104                        return Enum.ToObject(selfType, (uint)self ^ (uint)other);
105                    } else if (underType == typeof(ulong)) {
106                        return Enum.ToObject(selfType, (ulong)self ^ (ulong)other);
107                    } else if (underType == typeof(ushort)) {
108                        return Enum.ToObject(selfType, (ushort)self ^ (ushort)other);
109                    }
110                }
111            }
112
113            throw PythonOps.ValueError("bitwise xor cannot be applied to {0} and {1}", self.GetType(), other.GetType());
114        }
115
116        [SpecialName]
117        public static object OnesComplement(object self) {
118            if (self is Enum) {
119                Type selfType = self.GetType();
120                Type underType = Enum.GetUnderlyingType(selfType);
121                if (underType == typeof(int)) {
122                    return Enum.ToObject(selfType, ~(int)self);
123                } else if (underType == typeof(long)) {
124                    return Enum.ToObject(selfType, ~(long)self);
125                } else if (underType == typeof(short)) {
126                    return Enum.ToObject(selfType, ~(short)self);
127                } else if (underType == typeof(byte)) {
128                    return Enum.ToObject(selfType, ~(byte)self);
129                } else if (underType == typeof(sbyte)) {
130                    return Enum.ToObject(selfType, ~(sbyte)self);
131                } else if (underType == typeof(uint)) {
132                    return Enum.ToObject(selfType, ~(uint)self);
133                } else if (underType == typeof(ulong)) {
134                    return Enum.ToObject(selfType, ~(ulong)self);
135                } else if (underType == typeof(ushort)) {
136                    return Enum.ToObject(selfType, ~(ushort)self);
137                }
138            }
139
140            throw PythonOps.ValueError("one's complement cannot be applied to {0}", self.GetType());
141        }
142
143        public static bool __nonzero__(object self) {
144            if (self is Enum) {
145                Type selfType = self.GetType();
146                Type underType = Enum.GetUnderlyingType(selfType);
147
148                switch(Type.GetTypeCode(underType)) {
149                    case TypeCode.Int16: return (short)self != 0;
150                    case TypeCode.Int32: return (int)self != 0;
151                    case TypeCode.Int64: return (long)self != 0;
152                    case TypeCode.UInt16: return (ushort)self != 0;
153                    case TypeCode.UInt32: return (uint)self != 0;
154                    case TypeCode.UInt64: return ~(ulong)self != 0;
155                    case TypeCode.Byte: return (byte)self != 0;
156                    case TypeCode.SByte: return (sbyte)self != 0;
157                }
158            }
159
160            throw PythonOps.ValueError("__nonzero__ cannot be applied to {0}", self.GetType());
161        }
162    }
163}