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 MulticastDelegate prev;
47 MulticastDelegate kpm_next;
49 protected MulticastDelegate (object target, string method)
50 : base (target, method)
54 protected MulticastDelegate (Type target, string method)
55 : base (target, method)
59 public override void GetObjectData (SerializationInfo info, StreamingContext context)
61 base.GetObjectData (info, context);
65 protected sealed override object DynamicInvokeImpl (object[] args)
68 prev.DynamicInvokeImpl (args);
70 return base.DynamicInvokeImpl (args);
74 // Equals: two multicast delegates are equal if their base is equal
75 // and their invocations list is equal.
77 public sealed override bool Equals (object obj)
79 if (!base.Equals (obj))
82 MulticastDelegate d = obj as MulticastDelegate;
86 MulticastDelegate this_prev = this.prev;
87 MulticastDelegate obj_prev = d.prev;
90 if (this_prev == null)
91 return obj_prev == null;
93 if (!this_prev.Compare (obj_prev))
96 this_prev = this_prev.prev;
97 obj_prev = obj_prev.prev;
102 // FIXME: This could use some improvements.
104 public sealed override int GetHashCode ()
106 return base.GetHashCode ();
110 // Return, in order of invocation, the invocation list
111 // of a MulticastDelegate
113 public sealed override Delegate[] GetInvocationList ()
116 d = (MulticastDelegate) this.Clone ();
117 for (d.kpm_next = null; d.prev != null; d = d.prev)
120 if (d.kpm_next == null) {
121 MulticastDelegate other = (MulticastDelegate) d.Clone ();
123 other.kpm_next = null;
124 return new Delegate [1] { other };
127 var list = new List<Delegate> ();
128 for (; d != null; d = d.kpm_next) {
129 MulticastDelegate other = (MulticastDelegate) d.Clone ();
131 other.kpm_next = null;
135 return list.ToArray ();
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)
146 MulticastDelegate combined, orig, clone;
148 if (this.GetType() != follow.GetType ())
149 throw new ArgumentException (Locale.GetText ("Incompatible Delegate Types. First is {0} second is {1}.", this.GetType ().FullName, follow.GetType ().FullName));
151 combined = (MulticastDelegate)follow.Clone ();
152 combined.SetMulticastInvoke ();
154 for (clone = combined, orig = ((MulticastDelegate)follow).prev; orig != null; orig = orig.prev) {
156 clone.prev = (MulticastDelegate)orig.Clone ();
160 clone.SetMulticastInvoke ();
161 clone.prev = (MulticastDelegate)this.Clone ();
163 for (clone = clone.prev, orig = this.prev; orig != null; orig = orig.prev) {
165 clone.prev = (MulticastDelegate)orig.Clone ();
172 private bool BaseEquals (MulticastDelegate value)
174 return base.Equals (value);
178 * Perform a slightly crippled version of
179 * Knuth-Pratt-Morris over MulticastDelegate chains.
180 * Border values are set as pointers in kpm_next;
181 * Generally, KPM border arrays are length n+1 for
182 * strings of n. This one works with length n at the
183 * expense of a few additional comparisions.
185 private static MulticastDelegate KPM (MulticastDelegate needle, MulticastDelegate haystack,
186 out MulticastDelegate tail)
188 MulticastDelegate nx, hx;
192 nx = needle.kpm_next = null;
194 while ((nx != null) && (!nx.BaseEquals (hx)))
201 nx = nx == null ? needle : nx.prev;
202 if (hx.BaseEquals (nx))
203 hx.kpm_next = nx.kpm_next;
210 MulticastDelegate match = haystack;
214 while (nx != null && !nx.BaseEquals (hx)) {
219 nx = nx == null ? needle : nx.prev;
227 } while (hx != null);
233 protected sealed override Delegate RemoveImpl (Delegate value)
238 // match this with value
239 MulticastDelegate head, tail;
240 head = KPM ((MulticastDelegate)value, this, out tail);
244 // duplicate chain without head..tail
245 MulticastDelegate prev = null, retval = null, orig;
246 for (orig = this; (object)orig != (object)head; orig = orig.prev) {
247 MulticastDelegate clone = (MulticastDelegate)orig.Clone ();
254 for (orig = tail; (object)orig != null; orig = orig.prev) {
255 MulticastDelegate clone = (MulticastDelegate)orig.Clone ();
268 public static bool operator == (MulticastDelegate d1, MulticastDelegate d2)
273 return d1.Equals (d2);
276 public static bool operator != (MulticastDelegate d1, MulticastDelegate d2)
281 return !d1.Equals (d2);