-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
MulticastDelegate.cs
290 lines (242 loc) · 8.25 KB
/
MulticastDelegate.cs
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//
// System.MultiCastDelegate.cs
//
// Authors:
// Miguel de Icaza (miguel@ximian.com)
// Daniel Stodden (stodden@in.tum.de)
//
// (C) Ximian, Inc. http://www.ximian.com
//
//
// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.InteropServices;
namespace System
{
[System.Runtime.InteropServices.ComVisible (true)]
[Serializable]
[StructLayout (LayoutKind.Sequential)]
public abstract class MulticastDelegate : Delegate
{
Delegate[] delegates;
protected MulticastDelegate (object target, string method)
: base (target, method)
{
}
protected MulticastDelegate (Type target, string method)
: base (target, method)
{
}
public override void GetObjectData (SerializationInfo info, StreamingContext context)
{
base.GetObjectData (info, context);
}
protected sealed override object DynamicInvokeImpl (object[] args)
{
if (delegates == null) {
return base.DynamicInvokeImpl (args);
} else {
object r;
int i = 0, len = delegates.Length;
do {
r = delegates [i].DynamicInvoke (args);
} while (++i < len);
return r;
}
}
// Some high-performance applications use this internal property
// to avoid using a slow path to determine if there is more than one handler
// This brings an API that we removed in f410e545e2db0e0dc338673a6b10a5cfd2d3340f
// which some users depeneded on
//
// This is an example of code that used this:
// https://gist.github.com/migueldeicaza/cd99938c2a4372e7e5d5
//
// Do not remove this API
internal bool HasSingleTarget {
get { return delegates == null; }
}
// <remarks>
// Equals: two multicast delegates are equal if their base is equal
// and their invocations list is equal.
// </remarks>
public sealed override bool Equals (object obj)
{
if (!base.Equals (obj))
return false;
MulticastDelegate d = obj as MulticastDelegate;
if (d == null)
return false;
if (delegates == null && d.delegates == null) {
return true;
} else if (delegates == null ^ d.delegates == null) {
return false;
} else {
if (delegates.Length != d.delegates.Length)
return false;
for (int i = 0; i < delegates.Length; ++i) {
if (!delegates [i].Equals (d.delegates [i]))
return false;
}
return true;
}
}
//
// FIXME: This could use some improvements.
//
public sealed override int GetHashCode ()
{
return base.GetHashCode ();
}
protected override MethodInfo GetMethodImpl ()
{
if (delegates != null)
return delegates [delegates.Length - 1].Method;
return base.GetMethodImpl ();
}
// <summary>
// Return, in order of invocation, the invocation list
// of a MulticastDelegate
// </summary>
public sealed override Delegate[] GetInvocationList ()
{
if (delegates != null)
return (Delegate[]) delegates.Clone ();
else
return new Delegate[1] { this };
}
// <summary>
// Combines this MulticastDelegate with the (Multicast)Delegate `follow'.
// This does _not_ combine with Delegates. ECMA states the whole delegate
// thing should have better been a simple System.Delegate class.
// Compiler generated delegates are always MulticastDelegates.
// </summary>
protected sealed override Delegate CombineImpl (Delegate follow)
{
if (follow == null)
return this;
MulticastDelegate other = (MulticastDelegate) follow;
MulticastDelegate ret = AllocDelegateLike_internal (this);
if (delegates == null && other.delegates == null) {
ret.delegates = new Delegate [2] { this, other };
} else if (delegates == null) {
ret.delegates = new Delegate [1 + other.delegates.Length];
ret.delegates [0] = this;
Array.Copy (other.delegates, 0, ret.delegates, 1, other.delegates.Length);
} else if (other.delegates == null) {
ret.delegates = new Delegate [delegates.Length + 1];
Array.Copy (delegates, 0, ret.delegates, 0, delegates.Length);
ret.delegates [ret.delegates.Length - 1] = other;
} else {
ret.delegates = new Delegate [delegates.Length + other.delegates.Length];
Array.Copy (delegates, 0, ret.delegates, 0, delegates.Length);
Array.Copy (other.delegates, 0, ret.delegates, delegates.Length, other.delegates.Length);
}
return ret;
}
/* Based on the Boyer–Moore string search algorithm */
int LastIndexOf (Delegate[] haystack, Delegate[] needle)
{
if (haystack.Length < needle.Length)
return -1;
if (haystack.Length == needle.Length) {
for (int i = 0; i < haystack.Length; ++i)
if (!haystack [i].Equals (needle [i]))
return -1;
return 0;
}
for (int i = haystack.Length - needle.Length, j; i >= 0;) {
for (j = 0; needle [j].Equals (haystack [i]); ++i, ++j) {
if (j == needle.Length - 1)
return i - j;
}
i -= j + 1;
}
return -1;
}
protected sealed override Delegate RemoveImpl (Delegate value)
{
if (value == null)
return this;
MulticastDelegate other = (MulticastDelegate) value;
if (delegates == null && other.delegates == null) {
/* if they are not equal and the current one is not
* a multicastdelegate then we cannot delete it */
return this.Equals (other) ? null : this;
} else if (delegates == null) {
foreach (var d in other.delegates) {
if (this.Equals (d))
return null;
}
return this;
} else if (other.delegates == null) {
int idx = Array.LastIndexOf (delegates, other);
if (idx == -1)
return this;
if (delegates.Length <= 1) {
/* delegates.Length should never be equal or
* lower than 1, it should be 2 or greater */
throw new InvalidOperationException ();
}
if (delegates.Length == 2)
return delegates [idx == 0 ? 1 : 0];
MulticastDelegate ret = AllocDelegateLike_internal (this);
ret.delegates = new Delegate [delegates.Length - 1];
Array.Copy (delegates, ret.delegates, idx);
Array.Copy (delegates, idx + 1, ret.delegates, idx, delegates.Length - idx - 1);
return ret;
} else {
/* wild case : remove MulticastDelegate from MulticastDelegate
* complexity is O(m + n), with n the number of elements in
* this.delegates and m the number of elements in other.delegates */
if (delegates.Equals (other.delegates))
return null;
/* we need to remove elements from the end to the beginning, as
* the addition and removal of delegates behaves like a stack */
int idx = LastIndexOf (delegates, other.delegates);
if (idx == -1)
return this;
MulticastDelegate ret = AllocDelegateLike_internal (this);
ret.delegates = new Delegate [delegates.Length - other.delegates.Length];
Array.Copy (delegates, ret.delegates, idx);
Array.Copy (delegates, idx + other.delegates.Length, ret.delegates, idx, delegates.Length - idx - other.delegates.Length);
return ret;
}
}
public static bool operator == (MulticastDelegate d1, MulticastDelegate d2)
{
if (d1 == null)
return d2 == null;
return d1.Equals (d2);
}
public static bool operator != (MulticastDelegate d1, MulticastDelegate d2)
{
if (d1 == null)
return d2 != null;
return !d1.Equals (d2);
}
}
}