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
46 private MulticastDelegate prev;
47 private MulticastDelegate kpm_next;
49 protected MulticastDelegate (object target, string method)
50 : base (target, method)
55 protected MulticastDelegate (Type target, string method)
56 : base (target, method)
61 public override void GetObjectData (SerializationInfo info, StreamingContext context)
63 base.GetObjectData (info, context);
67 protected sealed override object DynamicInvokeImpl (object[] args)
70 prev.DynamicInvokeImpl (args);
72 return base.DynamicInvokeImpl (args);
75 internal bool HasSingleTarget {
76 get { return prev == null; }
79 // Equals: two multicast delegates are equal if their base is equal
80 // and their invocations list is equal.
82 public sealed override bool Equals (object obj)
84 if (!base.Equals (obj))
87 MulticastDelegate d = obj as MulticastDelegate;
91 MulticastDelegate this_prev = this.prev;
92 MulticastDelegate obj_prev = d.prev;
95 if (this_prev == null)
96 return obj_prev == null;
98 if (!this_prev.Compare (obj_prev))
101 this_prev = this_prev.prev;
102 obj_prev = obj_prev.prev;
107 // FIXME: This could use some improvements.
109 public sealed override int GetHashCode ()
111 return base.GetHashCode ();
115 // Return, in order of invocation, the invocation list
116 // of a MulticastDelegate
118 public sealed override Delegate[] GetInvocationList ()
121 d = (MulticastDelegate) this.Clone ();
122 for (d.kpm_next = null; d.prev != null; d = d.prev)
125 if (d.kpm_next == null) {
126 MulticastDelegate other = (MulticastDelegate) d.Clone ();
128 other.kpm_next = null;
129 return new Delegate [1] { other };
132 var list = new List<Delegate> ();
133 for (; d != null; d = d.kpm_next) {
134 MulticastDelegate other = (MulticastDelegate) d.Clone ();
136 other.kpm_next = null;
140 return list.ToArray ();
144 // Combines this MulticastDelegate with the (Multicast)Delegate `follow'.
145 // This does _not_ combine with Delegates. ECMA states the whole delegate
146 // thing should have better been a simple System.Delegate class.
147 // Compiler generated delegates are always MulticastDelegates.
149 protected sealed override Delegate CombineImpl (Delegate follow)
151 MulticastDelegate combined, orig, clone;
153 if (this.GetType() != follow.GetType ())
154 throw new ArgumentException (Locale.GetText ("Incompatible Delegate Types. First is {0} second is {1}.", this.GetType ().FullName, follow.GetType ().FullName));
156 combined = (MulticastDelegate)follow.Clone ();
157 combined.SetMulticastInvoke ();
159 for (clone = combined, orig = ((MulticastDelegate)follow).prev; orig != null; orig = orig.prev) {
161 clone.prev = (MulticastDelegate)orig.Clone ();
165 clone.SetMulticastInvoke ();
166 clone.prev = (MulticastDelegate)this.Clone ();
168 for (clone = clone.prev, orig = this.prev; orig != null; orig = orig.prev) {
170 clone.prev = (MulticastDelegate)orig.Clone ();
177 private bool BaseEquals (MulticastDelegate value)
179 return base.Equals (value);
183 * Perform a slightly crippled version of
184 * Knuth-Pratt-Morris over MulticastDelegate chains.
185 * Border values are set as pointers in kpm_next;
186 * Generally, KPM border arrays are length n+1 for
187 * strings of n. This one works with length n at the
188 * expense of a few additional comparisions.
190 private static MulticastDelegate KPM (MulticastDelegate needle, MulticastDelegate haystack,
191 out MulticastDelegate tail)
193 MulticastDelegate nx, hx;
197 nx = needle.kpm_next = null;
199 while ((nx != null) && (!nx.BaseEquals (hx)))
206 nx = nx == null ? needle : nx.prev;
207 if (hx.BaseEquals (nx))
208 hx.kpm_next = nx.kpm_next;
215 MulticastDelegate match = haystack;
219 while (nx != null && !nx.BaseEquals (hx)) {
224 nx = nx == null ? needle : nx.prev;
232 } while (hx != null);
238 protected sealed override Delegate RemoveImpl (Delegate value)
243 // match this with value
244 MulticastDelegate head, tail;
245 head = KPM ((MulticastDelegate)value, this, out tail);
249 // duplicate chain without head..tail
250 MulticastDelegate prev = null, retval = null, orig;
251 for (orig = this; (object)orig != (object)head; orig = orig.prev) {
252 MulticastDelegate clone = (MulticastDelegate)orig.Clone ();
259 for (orig = tail; (object)orig != null; orig = orig.prev) {
260 MulticastDelegate clone = (MulticastDelegate)orig.Clone ();
273 public static bool operator == (MulticastDelegate d1, MulticastDelegate d2)
278 return d1.Equals (d2);
281 public static bool operator != (MulticastDelegate d1, MulticastDelegate d2)
286 return !d1.Equals (d2);