**** Merged from MCS ****
[mono-project.git] / mcs / class / corlib / System / MulticastDelegate.cs
blob7e790a46900f89719e0e6b472556c343a6da275f
1 //
2 // System.MultiCastDelegate.cs
3 //
4 // Authors:
5 // Miguel de Icaza (miguel@ximian.com)
6 // Daniel Stodden (stodden@in.tum.de)
7 //
8 // (C) Ximian, Inc. http://www.ximian.com
9 //
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:
21 //
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 //
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.Runtime.Serialization;
37 namespace System
39 public abstract class MulticastDelegate : Delegate
41 private MulticastDelegate prev;
42 private MulticastDelegate kpm_next;
44 protected MulticastDelegate (object target, string method)
45 : base (target, method)
47 prev = null;
50 protected MulticastDelegate (Type target_type, string method)
51 : base (target_type, method)
53 prev = null;
56 public override void GetObjectData (SerializationInfo info, StreamingContext context)
58 base.GetObjectData (info, context);
62 protected override object DynamicInvokeImpl (object[] args)
64 if (prev != null)
65 prev.DynamicInvokeImpl (args);
67 return base.DynamicInvokeImpl (args);
70 // <remarks>
71 // Equals: two multicast delegates are equal if their base is equal
72 // and their invocations list is equal.
73 // </remarks>
74 public override bool Equals (object o)
76 if (!base.Equals (o))
77 return false;
79 MulticastDelegate d = (MulticastDelegate) o;
81 if (this.prev == null) {
82 if (d.prev == null)
83 return true;
84 else
85 return false;
88 return this.prev.Equals (d.prev);
92 // FIXME: This could use some improvements.
94 public override int GetHashCode ()
96 return base.GetHashCode ();
99 // <summary>
100 // Return, in order of invocation, the invocation list
101 // of a MulticastDelegate
102 // </summary>
103 public override Delegate[] GetInvocationList ()
105 MulticastDelegate d;
106 for (d = (MulticastDelegate) this.Clone (); d.prev != null; d = d.prev)
107 d.prev.kpm_next = d;
109 if (d.kpm_next == null) {
110 MulticastDelegate other = (MulticastDelegate) d.Clone ();
111 other.prev = null;
112 other.kpm_next = null;
113 return new Delegate [1] { other };
116 ArrayList list = new ArrayList ();
117 for (; d != null; d = d.kpm_next) {
118 MulticastDelegate other = (MulticastDelegate) d.Clone ();
119 other.prev = null;
120 other.kpm_next = null;
121 list.Add (other);
124 return (Delegate []) list.ToArray (typeof (Delegate));
127 // <summary>
128 // Combines this MulticastDelegate with the (Multicast)Delegate `follow'.
129 // This does _not_ combine with Delegates. ECMA states the whole delegate
130 // thing should have better been a simple System.Delegate class.
131 // Compiler generated delegates are always MulticastDelegates.
132 // </summary>
133 protected override Delegate CombineImpl (Delegate follow)
135 MulticastDelegate combined, orig, clone;
137 if (this.GetType() != follow.GetType ())
138 throw new ArgumentException (Locale.GetText ("Incompatible Delegate Types."));
140 combined = (MulticastDelegate)follow.Clone ();
142 for (clone = combined, orig = ((MulticastDelegate)follow).prev; orig != null; orig = orig.prev) {
144 clone.prev = (MulticastDelegate)orig.Clone ();
145 clone = clone.prev;
148 clone.prev = (MulticastDelegate)this.Clone ();
150 for (clone = clone.prev, orig = this.prev; orig != null; orig = orig.prev) {
152 clone.prev = (MulticastDelegate)orig.Clone ();
153 clone = clone.prev;
156 return combined;
159 private bool BaseEquals (MulticastDelegate value)
161 return base.Equals (value);
165 * Perform a slightly crippled version of
166 * Knuth-Pratt-Morris over MulticastDelegate chains.
167 * Border values are set as pointers in kpm_next;
168 * Generally, KPM border arrays are length n+1 for
169 * strings of n. This one works with length n at the
170 * expense of a few additional comparisions.
172 private static MulticastDelegate KPM (MulticastDelegate needle, MulticastDelegate haystack,
173 out MulticastDelegate tail)
175 MulticastDelegate nx, hx;
177 // preprocess
178 hx = needle;
179 nx = needle.kpm_next = null;
180 do {
181 while ((nx != null) && (!nx.BaseEquals (hx)))
182 nx = nx.kpm_next;
184 hx = hx.prev;
185 if (hx == null)
186 break;
188 nx = nx == null ? needle : nx.prev;
189 if (hx.BaseEquals (nx))
190 hx.kpm_next = nx.kpm_next;
191 else
192 hx.kpm_next = nx;
194 } while (true);
196 // match
197 MulticastDelegate match = haystack;
198 nx = needle;
199 hx = haystack;
200 do {
201 while (nx != null && !nx.BaseEquals (hx)) {
202 nx = nx.kpm_next;
203 match = match.prev;
206 nx = nx == null ? needle : nx.prev;
207 if (nx == null) {
208 // bingo
209 tail = hx.prev;
210 return match;
213 hx = hx.prev;
214 } while (hx != null);
216 tail = null;
217 return null;
220 protected override Delegate RemoveImpl (Delegate value)
222 if (value == null)
223 return this;
225 // match this with value
226 MulticastDelegate head, tail;
227 head = KPM ((MulticastDelegate)value, this, out tail);
228 if (head == null)
229 return this;
231 // duplicate chain without head..tail
232 MulticastDelegate prev = null, retval = null, orig;
233 for (orig = this; (object)orig != (object)head; orig = orig.prev) {
234 MulticastDelegate clone = (MulticastDelegate)orig.Clone ();
235 if (prev != null)
236 prev.prev = clone;
237 else
238 retval = clone;
239 prev = clone;
241 for (orig = tail; (object)orig != null; orig = orig.prev) {
242 MulticastDelegate clone = (MulticastDelegate)orig.Clone ();
243 if (prev != null)
244 prev.prev = clone;
245 else
246 retval = clone;
247 prev = clone;
249 if (prev != null)
250 prev.prev = null;
252 return retval;
255 public static bool operator == (MulticastDelegate a, MulticastDelegate b)
257 if ((object)a == null) {
258 if ((object)b == null)
259 return true;
260 return false;
262 return a.Equals (b);
265 public static bool operator != (MulticastDelegate a, MulticastDelegate b)
267 return !(a == b);