2 // System.MultiCastDelegate.cs
5 // Miguel de Icaza (miguel@ximian.com)
6 // Daniel Stodden (stodden@in.tum.de)
8 // (C) Ximian, Inc. http://www.ximian.com
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Collections;
35 using System.Collections.Generic;
36 using System.Runtime.Serialization;
37 using System.Runtime.InteropServices;
41 [System.Runtime.InteropServices.ComVisible (true)]
43 [StructLayout (LayoutKind.Sequential)]
44 public abstract class MulticastDelegate : Delegate
48 protected MulticastDelegate (object target, string method)
49 : base (target, method)
53 protected MulticastDelegate (Type target, string method)
54 : base (target, method)
58 public override void GetObjectData (SerializationInfo info, StreamingContext context)
60 base.GetObjectData (info, context);
63 protected sealed override object DynamicInvokeImpl (object[] args)
65 if (delegates == null) {
66 return base.DynamicInvokeImpl (args);
69 int i = 0, len = delegates.Length;
71 r = delegates [i].DynamicInvoke (args);
77 // Some high-performance applications use this internal property
78 // to avoid using a slow path to determine if there is more than one handler
79 // This brings an API that we removed in f410e545e2db0e0dc338673a6b10a5cfd2d3340f
80 // which some users depeneded on
82 // This is an example of code that used this:
83 // https://gist.github.com/migueldeicaza/cd99938c2a4372e7e5d5
85 // Do not remove this API
86 internal bool HasSingleTarget => delegates == null;
89 // Equals: two multicast delegates are equal if their base is equal
90 // and their invocations list is equal.
92 public sealed override bool Equals (object obj)
94 if (!base.Equals (obj))
97 MulticastDelegate d = obj as MulticastDelegate;
101 if (delegates == null && d.delegates == null) {
103 } else if (delegates == null ^ d.delegates == null) {
106 if (delegates.Length != d.delegates.Length)
109 for (int i = 0; i < delegates.Length; ++i) {
110 if (!delegates [i].Equals (d.delegates [i]))
119 // FIXME: This could use some improvements.
121 public sealed override int GetHashCode ()
123 return base.GetHashCode ();
127 // Return, in order of invocation, the invocation list
128 // of a MulticastDelegate
130 public sealed override Delegate[] GetInvocationList ()
132 if (delegates != null)
133 return (Delegate[]) delegates.Clone ();
135 return new Delegate[1] { this };
139 // Combines this MulticastDelegate with the (Multicast)Delegate `follow'.
140 // This does _not_ combine with Delegates. ECMA states the whole delegate
141 // thing should have better been a simple System.Delegate class.
142 // Compiler generated delegates are always MulticastDelegates.
144 protected sealed override Delegate CombineImpl (Delegate follow)
149 MulticastDelegate other = (MulticastDelegate) follow;
151 MulticastDelegate ret = AllocDelegateLike_internal (this);
153 if (delegates == null && other.delegates == null) {
154 ret.delegates = new Delegate [2] { this, other };
155 } else if (delegates == null) {
156 ret.delegates = new Delegate [1 + other.delegates.Length];
158 ret.delegates [0] = this;
159 Array.Copy (other.delegates, 0, ret.delegates, 1, other.delegates.Length);
160 } else if (other.delegates == null) {
161 ret.delegates = new Delegate [delegates.Length + 1];
163 Array.Copy (delegates, 0, ret.delegates, 0, delegates.Length);
164 ret.delegates [ret.delegates.Length - 1] = other;
166 ret.delegates = new Delegate [delegates.Length + other.delegates.Length];
168 Array.Copy (delegates, 0, ret.delegates, 0, delegates.Length);
169 Array.Copy (other.delegates, 0, ret.delegates, delegates.Length, other.delegates.Length);
175 protected sealed override Delegate RemoveImpl (Delegate value)
180 MulticastDelegate other = (MulticastDelegate) value;
182 if (delegates == null && other.delegates == null) {
183 /* if they are not equal and the current one is not
184 * a multicastdelegate then we cannot delete it */
185 return this.Equals (other) ? null : this;
186 } else if (delegates == null) {
187 foreach (var d in other.delegates) {
192 } else if (other.delegates == null) {
193 int idx = Array.LastIndexOf (delegates, other);
197 if (delegates.Length <= 1) {
198 /* delegates.Length should never be equal or
199 * lower than 1, it should be 2 or greater */
200 throw new InvalidOperationException ();
203 if (delegates.Length == 2)
204 return delegates [idx == 0 ? 1 : 0];
206 MulticastDelegate ret = AllocDelegateLike_internal (this);
207 ret.delegates = new Delegate [delegates.Length - 1];
209 Array.Copy (delegates, ret.delegates, idx);
210 Array.Copy (delegates, idx + 1, ret.delegates, idx, delegates.Length - idx - 1);
214 /* wild case : remove MulticastDelegate from MulticastDelegate
215 * complexity is O(m * n), with n the number of elements in
216 * this.delegates and m the number of elements in other.delegates */
217 MulticastDelegate ret = AllocDelegateLike_internal (this);
218 ret.delegates = new Delegate [delegates.Length];
220 /* we should use a set with O(1) lookup complexity
221 * but HashSet is implemented in System.Core.dll */
222 List<Delegate> other_delegates = new List<Delegate> ();
223 for (int i = 0; i < other.delegates.Length; ++i)
224 other_delegates.Add (other.delegates [i]);
226 int idx = delegates.Length;
228 /* we need to remove elements from the end to the beginning, as
229 * the addition and removal of delegates behaves like a stack */
230 for (int i = delegates.Length - 1; i >= 0; --i) {
231 /* if delegates[i] is not in other_delegates,
232 * then we can safely add it to ret.delegates
233 * otherwise we remove it from other_delegates */
234 if (!other_delegates.Remove (delegates [i]))
235 ret.delegates [--idx] = delegates [i];
238 /* the elements are at the end of the array, we
239 * need to move them back to the beginning of it */
240 int count = delegates.Length - idx;
241 Array.Copy (ret.delegates, idx, ret.delegates, 0, count);
247 return ret.delegates [0];
249 if (count != delegates.Length)
250 Array.Resize (ref ret.delegates, count);
256 public static bool operator == (MulticastDelegate d1, MulticastDelegate d2)
261 return d1.Equals (d2);
264 public static bool operator != (MulticastDelegate d1, MulticastDelegate d2)
269 return !d1.Equals (d2);