|
| 1 | +using System; |
| 2 | +using System.Runtime.CompilerServices; |
| 3 | + |
| 4 | +namespace Python.Runtime |
| 5 | +{ |
| 6 | + /// <summary> |
| 7 | + /// Managed class that provides the implementation for reflected enum types. |
| 8 | + /// </summary> |
| 9 | + [Serializable] |
| 10 | + internal class EnumObject : ClassBase |
| 11 | + { |
| 12 | + internal EnumObject(Type type) : base(type) |
| 13 | + { |
| 14 | + } |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// Standard comparison implementation for instances of enum types. |
| 18 | + /// </summary> |
| 19 | + public static NewReference tp_richcompare(BorrowedReference ob, BorrowedReference other, int op) |
| 20 | + { |
| 21 | + object rightInstance; |
| 22 | + CLRObject leftClrObject; |
| 23 | + int comparisonResult; |
| 24 | + |
| 25 | + switch (op) |
| 26 | + { |
| 27 | + case Runtime.Py_EQ: |
| 28 | + case Runtime.Py_NE: |
| 29 | + var pytrue = Runtime.PyTrue; |
| 30 | + var pyfalse = Runtime.PyFalse; |
| 31 | + |
| 32 | + // swap true and false for NE |
| 33 | + if (op != Runtime.Py_EQ) |
| 34 | + { |
| 35 | + pytrue = Runtime.PyFalse; |
| 36 | + pyfalse = Runtime.PyTrue; |
| 37 | + } |
| 38 | + |
| 39 | + if (ob == other) |
| 40 | + { |
| 41 | + return new NewReference(pytrue); |
| 42 | + } |
| 43 | + |
| 44 | + if (!TryGetSecondCompareOperandInstance(ob, other, out leftClrObject, out rightInstance)) |
| 45 | + { |
| 46 | + return new NewReference(pyfalse); |
| 47 | + } |
| 48 | + |
| 49 | + if (rightInstance != null && |
| 50 | + TryCompare(leftClrObject.inst as Enum, rightInstance, out comparisonResult) && |
| 51 | + comparisonResult == 0) |
| 52 | + { |
| 53 | + return new NewReference(pytrue); |
| 54 | + } |
| 55 | + else |
| 56 | + { |
| 57 | + return new NewReference(pyfalse); |
| 58 | + } |
| 59 | + |
| 60 | + case Runtime.Py_LT: |
| 61 | + case Runtime.Py_LE: |
| 62 | + case Runtime.Py_GT: |
| 63 | + case Runtime.Py_GE: |
| 64 | + if (!TryGetSecondCompareOperandInstance(ob, other, out leftClrObject, out rightInstance)) |
| 65 | + { |
| 66 | + return Exceptions.RaiseTypeError("Cannot get managed object"); |
| 67 | + } |
| 68 | + |
| 69 | + if (rightInstance == null) |
| 70 | + { |
| 71 | + return Exceptions.RaiseTypeError($"Cannot compare {leftClrObject.inst.GetType()} to None"); |
| 72 | + } |
| 73 | + |
| 74 | + try |
| 75 | + { |
| 76 | + if (!TryCompare(leftClrObject.inst as Enum, rightInstance, out comparisonResult)) |
| 77 | + { |
| 78 | + return Exceptions.RaiseTypeError($"Cannot compare {leftClrObject.inst.GetType()} with {rightInstance.GetType()}"); |
| 79 | + } |
| 80 | + |
| 81 | + return new NewReference(GetComparisonResult(op, comparisonResult)); |
| 82 | + } |
| 83 | + catch (ArgumentException e) |
| 84 | + { |
| 85 | + return Exceptions.RaiseTypeError(e.Message); |
| 86 | + } |
| 87 | + |
| 88 | + default: |
| 89 | + return new NewReference(Runtime.PyNotImplemented); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /// <summary> |
| 94 | + /// Tries comparing the give enum to the right operand by converting it to the appropriate type if possible |
| 95 | + /// </summary> |
| 96 | + /// <returns>True if the right operand was converted to a supported type and the comparison was performed successfully</returns> |
| 97 | + private static bool TryCompare(Enum left, object right, out int result) |
| 98 | + { |
| 99 | + result = int.MinValue; |
| 100 | + var conversionSuccessful = true; |
| 101 | + var leftType = left.GetType(); |
| 102 | + var rightType = right.GetType(); |
| 103 | + |
| 104 | + // Same enum comparison: |
| 105 | + if (leftType == rightType) |
| 106 | + { |
| 107 | + result = left.CompareTo(right); |
| 108 | + } |
| 109 | + // Comparison with other enum types |
| 110 | + else if (rightType.IsEnum) |
| 111 | + { |
| 112 | + var leftIsUnsigned = leftType.GetEnumUnderlyingType() == typeof(UInt64); |
| 113 | + result = Compare(left, right as Enum, leftIsUnsigned); |
| 114 | + } |
| 115 | + else if (right is string rightString) |
| 116 | + { |
| 117 | + result = left.ToString().CompareTo(rightString); |
| 118 | + } |
| 119 | + else |
| 120 | + { |
| 121 | + var leftIsUnsigned = leftType.GetEnumUnderlyingType() == typeof(UInt64); |
| 122 | + switch (right) |
| 123 | + { |
| 124 | + case double rightDouble: |
| 125 | + result = Compare(left, rightDouble, leftIsUnsigned); |
| 126 | + break; |
| 127 | + case long rightLong: |
| 128 | + result = Compare(left, rightLong, leftIsUnsigned); |
| 129 | + break; |
| 130 | + case ulong rightULong: |
| 131 | + result = Compare(left, rightULong, leftIsUnsigned); |
| 132 | + break; |
| 133 | + case int rightInt: |
| 134 | + result = Compare(left, (long)rightInt, leftIsUnsigned); |
| 135 | + break; |
| 136 | + case uint rightUInt: |
| 137 | + result = Compare(left, (ulong)rightUInt, leftIsUnsigned); |
| 138 | + break; |
| 139 | + default: |
| 140 | + conversionSuccessful = false; |
| 141 | + break; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + return conversionSuccessful; |
| 146 | + } |
| 147 | + |
| 148 | + #region Comparison against integers |
| 149 | + |
| 150 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 151 | + private static int Compare(long a, ulong b) |
| 152 | + { |
| 153 | + if (a < 0) return -1; |
| 154 | + return ((ulong)a).CompareTo(b); |
| 155 | + } |
| 156 | + |
| 157 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 158 | + private static int Compare(Enum a, long b, bool isUnsigned) |
| 159 | + { |
| 160 | + |
| 161 | + if (isUnsigned) |
| 162 | + { |
| 163 | + return -Compare(b, Convert.ToUInt64(a)); |
| 164 | + } |
| 165 | + return Convert.ToInt64(a).CompareTo(b); |
| 166 | + } |
| 167 | + |
| 168 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 169 | + private static int Compare(Enum a, ulong b, bool inUnsigned) |
| 170 | + { |
| 171 | + if (inUnsigned) |
| 172 | + { |
| 173 | + return Convert.ToUInt64(a).CompareTo(b); |
| 174 | + } |
| 175 | + return Compare(Convert.ToInt64(a), b); |
| 176 | + } |
| 177 | + |
| 178 | + #endregion |
| 179 | + |
| 180 | + #region Comparison against doubles |
| 181 | + |
| 182 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 183 | + private static int Compare(Enum a, double b, bool isUnsigned) |
| 184 | + { |
| 185 | + if (isUnsigned) |
| 186 | + { |
| 187 | + var uIntA = Convert.ToUInt64(a); |
| 188 | + if (uIntA < b) return -1; |
| 189 | + if (uIntA > b) return 1; |
| 190 | + return 0; |
| 191 | + } |
| 192 | + |
| 193 | + var intA = Convert.ToInt64(a); |
| 194 | + if (intA < b) return -1; |
| 195 | + if (intA > b) return 1; |
| 196 | + return 0; |
| 197 | + } |
| 198 | + |
| 199 | + #endregion |
| 200 | + |
| 201 | + #region Comparison against other enum types |
| 202 | + |
| 203 | + /// <summary> |
| 204 | + /// We support comparing enums of different types by comparing their underlying values. |
| 205 | + /// </summary> |
| 206 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 207 | + private static int Compare(Enum a, Enum b, bool isUnsigned) |
| 208 | + { |
| 209 | + if (b.GetType().GetEnumUnderlyingType() == typeof(UInt64)) |
| 210 | + { |
| 211 | + return Compare(a, Convert.ToUInt64(b), isUnsigned); |
| 212 | + } |
| 213 | + return Compare(a, Convert.ToInt64(b), isUnsigned); |
| 214 | + } |
| 215 | + |
| 216 | + #endregion |
| 217 | + } |
| 218 | +} |
0 commit comments