PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms 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
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  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. using System; using Microsoft;
  16. using System.Runtime.CompilerServices;
  17. using Microsoft.Runtime.CompilerServices;
  18. namespace IronPython.Runtime.Operations {
  19. public static class EnumOps {
  20. [SpecialName]
  21. public static object BitwiseOr(object self, object other) {
  22. if (self is Enum && other is Enum) {
  23. Type selfType = self.GetType();
  24. Type otherType = other.GetType();
  25. if (selfType == otherType) {
  26. Type underType = Enum.GetUnderlyingType(selfType);
  27. if (underType == typeof(int)) {
  28. return Enum.ToObject(selfType, (int)self | (int)other);
  29. } else if (underType == typeof(long)) {
  30. return Enum.ToObject(selfType, (long)self | (long)other);
  31. } else if (underType == typeof(short)) {
  32. return Enum.ToObject(selfType, (short)self | (short)other);
  33. } else if (underType == typeof(byte)) {
  34. return Enum.ToObject(selfType, (byte)self | (byte)other);
  35. } else if (underType == typeof(sbyte)) {
  36. return Enum.ToObject(selfType, (sbyte)self | (sbyte)other);
  37. } else if (underType == typeof(uint)) {
  38. return Enum.ToObject(selfType, (uint)self | (uint)other);
  39. } else if (underType == typeof(ulong)) {
  40. return Enum.ToObject(selfType, (ulong)self | (ulong)other);
  41. } else if (underType == typeof(ushort)) {
  42. return Enum.ToObject(selfType, (ushort)self | (ushort)other);
  43. }
  44. }
  45. }
  46. throw PythonOps.ValueError("bitwise or cannot be applied to {0} and {1}", self.GetType(), other.GetType());
  47. }
  48. [SpecialName]
  49. public static object BitwiseAnd(object self, object other) {
  50. if (self is Enum && other is Enum) {
  51. Type selfType = self.GetType();
  52. Type otherType = other.GetType();
  53. if (selfType == otherType) {
  54. Type underType = Enum.GetUnderlyingType(selfType);
  55. if (underType == typeof(int)) {
  56. return Enum.ToObject(selfType, (int)self & (int)other);
  57. } else if (underType == typeof(long)) {
  58. return Enum.ToObject(selfType, (long)self & (long)other);
  59. } else if (underType == typeof(short)) {
  60. return Enum.ToObject(selfType, (short)self & (short)other);
  61. } else if (underType == typeof(byte)) {
  62. return Enum.ToObject(selfType, (byte)self & (byte)other);
  63. } else if (underType == typeof(sbyte)) {
  64. return Enum.ToObject(selfType, (sbyte)self & (sbyte)other);
  65. } else if (underType == typeof(uint)) {
  66. return Enum.ToObject(selfType, (uint)self & (uint)other);
  67. } else if (underType == typeof(ulong)) {
  68. return Enum.ToObject(selfType, (ulong)self & (ulong)other);
  69. } else if (underType == typeof(ushort)) {
  70. return Enum.ToObject(selfType, (ushort)self & (ushort)other);
  71. }
  72. }
  73. }
  74. throw PythonOps.ValueError("bitwise and cannot be applied to {0} and {1}", self.GetType(), other.GetType());
  75. }
  76. [SpecialName]
  77. public static object ExclusiveOr(object self, object other) {
  78. if (self is Enum && other is Enum) {
  79. Type selfType = self.GetType();
  80. Type otherType = other.GetType();
  81. if (selfType == otherType) {
  82. Type underType = Enum.GetUnderlyingType(selfType);
  83. if (underType == typeof(int)) {
  84. return Enum.ToObject(selfType, (int)self ^ (int)other);
  85. } else if (underType == typeof(long)) {
  86. return Enum.ToObject(selfType, (long)self ^ (long)other);
  87. } else if (underType == typeof(short)) {
  88. return Enum.ToObject(selfType, (short)self ^ (short)other);
  89. } else if (underType == typeof(byte)) {
  90. return Enum.ToObject(selfType, (byte)self ^ (byte)other);
  91. } else if (underType == typeof(sbyte)) {
  92. return Enum.ToObject(selfType, (sbyte)self ^ (sbyte)other);
  93. } else if (underType == typeof(uint)) {
  94. return Enum.ToObject(selfType, (uint)self ^ (uint)other);
  95. } else if (underType == typeof(ulong)) {
  96. return Enum.ToObject(selfType, (ulong)self ^ (ulong)other);
  97. } else if (underType == typeof(ushort)) {
  98. return Enum.ToObject(selfType, (ushort)self ^ (ushort)other);
  99. }
  100. }
  101. }
  102. throw PythonOps.ValueError("bitwise xor cannot be applied to {0} and {1}", self.GetType(), other.GetType());
  103. }
  104. [SpecialName]
  105. public static object OnesComplement(object self) {
  106. if (self is Enum) {
  107. Type selfType = self.GetType();
  108. Type underType = Enum.GetUnderlyingType(selfType);
  109. if (underType == typeof(int)) {
  110. return Enum.ToObject(selfType, ~(int)self);
  111. } else if (underType == typeof(long)) {
  112. return Enum.ToObject(selfType, ~(long)self);
  113. } else if (underType == typeof(short)) {
  114. return Enum.ToObject(selfType, ~(short)self);
  115. } else if (underType == typeof(byte)) {
  116. return Enum.ToObject(selfType, ~(byte)self);
  117. } else if (underType == typeof(sbyte)) {
  118. return Enum.ToObject(selfType, ~(sbyte)self);
  119. } else if (underType == typeof(uint)) {
  120. return Enum.ToObject(selfType, ~(uint)self);
  121. } else if (underType == typeof(ulong)) {
  122. return Enum.ToObject(selfType, ~(ulong)self);
  123. } else if (underType == typeof(ushort)) {
  124. return Enum.ToObject(selfType, ~(ushort)self);
  125. }
  126. }
  127. throw PythonOps.ValueError("one's complement cannot be applied to {0}", self.GetType());
  128. }
  129. public static bool __nonzero__(object self) {
  130. if (self is Enum) {
  131. Type selfType = self.GetType();
  132. Type underType = Enum.GetUnderlyingType(selfType);
  133. switch(Type.GetTypeCode(underType)) {
  134. case TypeCode.Int16: return (short)self != 0;
  135. case TypeCode.Int32: return (int)self != 0;
  136. case TypeCode.Int64: return (long)self != 0;
  137. case TypeCode.UInt16: return (ushort)self != 0;
  138. case TypeCode.UInt32: return (uint)self != 0;
  139. case TypeCode.UInt64: return ~(ulong)self != 0;
  140. case TypeCode.Byte: return (byte)self != 0;
  141. case TypeCode.SByte: return (sbyte)self != 0;
  142. }
  143. }
  144. throw PythonOps.ValueError("__nonzero__ cannot be applied to {0}", self.GetType());
  145. }
  146. }
  147. }