-
Notifications
You must be signed in to change notification settings - Fork 774
Expand file tree
/
Copy pathEventBinding.cs
More file actions
110 lines (90 loc) · 3.2 KB
/
EventBinding.cs
File metadata and controls
110 lines (90 loc) · 3.2 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
using System;
using System.Diagnostics;
using System.Reflection;
namespace Python.Runtime
{
/// <summary>
/// Implements a Python event binding type, similar to a method binding.
/// </summary>
[Serializable]
internal class EventBinding : ExtensionType
{
private readonly string name;
private readonly EventHandlerCollection e;
private readonly PyObject? target;
public EventBinding(string name, EventHandlerCollection e, PyObject? target)
{
this.name = name;
this.target = target;
this.e = e;
}
public EventBinding(EventInfo @event) : this(@event.Name, new EventHandlerCollection(@event), target: null)
{
Debug.Assert(@event.AddMethod.IsStatic);
}
/// <summary>
/// EventBinding += operator implementation.
/// </summary>
public static NewReference nb_inplace_add(BorrowedReference ob, BorrowedReference arg)
{
var self = (EventBinding)GetManagedObject(ob)!;
if (Runtime.PyCallable_Check(arg) < 1)
{
Exceptions.SetError(Exceptions.TypeError, "event handlers must be callable");
return default;
}
if (!self.e.AddEventHandler(self.target.BorrowNullable(), new PyObject(arg)))
{
return default;
}
return new NewReference(ob);
}
/// <summary>
/// EventBinding -= operator implementation.
/// </summary>
public static NewReference nb_inplace_subtract(BorrowedReference ob, BorrowedReference arg)
{
var self = (EventBinding)GetManagedObject(ob)!;
if (Runtime.PyCallable_Check(arg) < 1)
{
Exceptions.SetError(Exceptions.TypeError, "invalid event handler");
return default;
}
if (!self.e.RemoveEventHandler(self.target.BorrowNullable(), arg))
{
return default;
}
return new NewReference(ob);
}
public static int tp_descr_set(BorrowedReference ds, BorrowedReference ob, BorrowedReference val)
=> EventObject.tp_descr_set(ds, ob, val);
/// <summary>
/// EventBinding __hash__ implementation.
/// </summary>
public static nint tp_hash(BorrowedReference ob)
{
var self = (EventBinding)GetManagedObject(ob)!;
nint x = 0;
if (self.target != null)
{
x = Runtime.PyObject_Hash(self.target);
if (x == -1)
{
return x;
}
}
nint y = self.e.GetHashCode();
return x ^ y;
}
/// <summary>
/// EventBinding __repr__ implementation.
/// </summary>
public static NewReference tp_repr(BorrowedReference ob)
{
var self = (EventBinding)GetManagedObject(ob)!;
string type = self.target == null ? "unbound" : "bound";
string s = string.Format("<{0} event '{1}'>", type, self.name);
return Runtime.PyString_FromString(s);
}
}
}