forked from QuantConnect/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpsHelper.cs
More file actions
158 lines (134 loc) · 4.84 KB
/
OpsHelper.cs
File metadata and controls
158 lines (134 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using System;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using static Python.Runtime.OpsHelper;
namespace Python.Runtime
{
static class OpsHelper
{
public static BindingFlags BindingFlags => BindingFlags.Public | BindingFlags.Static;
public static Func<T, T, T> Binary<T>(Func<Expression, Expression, Expression> func)
{
var a = Expression.Parameter(typeof(T), "a");
var b = Expression.Parameter(typeof(T), "b");
var body = func(a, b);
var lambda = Expression.Lambda<Func<T, T, T>>(body, a, b);
return lambda.Compile();
}
public static Func<T, T> Unary<T>(Func<Expression, Expression> func)
{
var value = Expression.Parameter(typeof(T), "value");
var body = func(value);
var lambda = Expression.Lambda<Func<T, T>>(body, value);
return lambda.Compile();
}
public static bool IsOpsHelper(this MethodBase method)
=> method.DeclaringType.GetCustomAttribute<OpsAttribute>() is not null;
public static Expression EnumUnderlyingValue(Expression enumValue)
=> Expression.Convert(enumValue, enumValue.Type.GetEnumUnderlyingType());
}
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
internal class OpsAttribute : Attribute { }
[Ops]
internal static class FlagEnumOps<T> where T : Enum
{
static readonly Func<T, T, T> and = BinaryOp(Expression.And);
static readonly Func<T, T, T> or = BinaryOp(Expression.Or);
static readonly Func<T, T, T> xor = BinaryOp(Expression.ExclusiveOr);
static readonly Func<T, T> invert = UnaryOp(Expression.OnesComplement);
public static T op_BitwiseAnd(T a, T b) => and(a, b);
public static T op_BitwiseOr(T a, T b) => or(a, b);
public static T op_ExclusiveOr(T a, T b) => xor(a, b);
public static T op_OnesComplement(T value) => invert(value);
static Expression FromNumber(Expression number)
=> Expression.Convert(number, typeof(T));
static Func<T, T, T> BinaryOp(Func<Expression, Expression, BinaryExpression> op)
{
return Binary<T>((a, b) =>
{
var numericA = EnumUnderlyingValue(a);
var numericB = EnumUnderlyingValue(b);
var numericResult = op(numericA, numericB);
return FromNumber(numericResult);
});
}
static Func<T, T> UnaryOp(Func<Expression, UnaryExpression> op)
{
return Unary<T>(value =>
{
var numeric = EnumUnderlyingValue(value);
var numericResult = op(numeric);
return FromNumber(numericResult);
});
}
}
[Ops]
internal static class EnumOps<T> where T : Enum
{
private static bool IsUnsigned = typeof(T).GetEnumUnderlyingType() == typeof(UInt64);
[ForbidPythonThreads]
#pragma warning disable IDE1006 // Naming Styles - must match Python
public static object __int__(T value)
#pragma warning restore IDE1006 // Naming Styles
=> IsUnsigned ? Convert.ToUInt64(value) : Convert.ToInt64(value);
#region Arithmetic operators
public static double op_Addition(T a, double b)
{
if (IsUnsigned)
{
return Convert.ToUInt64(a) + b;
}
return Convert.ToInt64(a) + b;
}
public static double op_Addition(double a, T b)
{
return op_Addition(b, a);
}
public static double op_Subtraction(T a, double b)
{
if (IsUnsigned)
{
return Convert.ToUInt64(a) - b;
}
return Convert.ToInt64(a) - b;
}
public static double op_Subtraction(double a, T b)
{
if (IsUnsigned)
{
return a - Convert.ToUInt64(b);
}
return a - Convert.ToInt64(b);
}
public static double op_Multiply(T a, double b)
{
if (IsUnsigned)
{
return Convert.ToUInt64(a) * b;
}
return Convert.ToInt64(a) * b;
}
public static double op_Multiply(double a, T b)
{
return op_Multiply(b, a);
}
public static double op_Division(T a, double b)
{
if (IsUnsigned)
{
return Convert.ToUInt64(a) / b;
}
return Convert.ToInt64(a) / b;
}
public static double op_Division(double a, T b)
{
if (IsUnsigned)
{
return a / Convert.ToUInt64(b);
}
return a / Convert.ToInt64(b);
}
#endregion
}
}