New test.
[mono.git] / mcs / class / corlib / System / MulticastDelegate.cs
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 //
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
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.
32 //
33
34 using System.Collections;
35 using System.Runtime.Serialization;
36
37 namespace System
38 {
39         [System.Runtime.InteropServices.ComVisible (true)]
40         [Serializable]
41         public abstract class MulticastDelegate : Delegate
42         {
43                 private MulticastDelegate prev;
44                 private MulticastDelegate kpm_next;
45
46                 protected MulticastDelegate (object target, string method)
47                         : base (target, method)
48                 {
49                         prev = null;
50                 }
51
52                 protected MulticastDelegate (Type target, string method)
53                         : base (target, method)
54                 {
55                         prev = null;
56                 }
57                 
58                 public override void GetObjectData (SerializationInfo info, StreamingContext context)
59                 {
60                         base.GetObjectData  (info, context);
61                 }
62
63
64                 protected sealed override object DynamicInvokeImpl (object[] args)
65                 {
66                         if (prev != null)
67                                 prev.DynamicInvokeImpl (args);
68
69                         return base.DynamicInvokeImpl (args);
70                 }
71
72                 internal bool HasSingleTarget {
73                         get { return prev == null; }
74                 }
75                 // <remarks>
76                 //   Equals: two multicast delegates are equal if their base is equal
77                 //   and their invocations list is equal.
78                 // </remarks>
79                 public sealed override bool Equals (object obj)
80                 {
81                         if (!base.Equals (obj))
82                                 return false;
83
84                         MulticastDelegate d = obj as MulticastDelegate;
85                         if (d == null)
86                                 return false;
87                         
88                         if (this.prev == null) {
89                                 if (d.prev == null)
90                                         return true;
91                                 else
92                                         return false;
93                         }
94
95                         return this.prev.Equals (d.prev);
96                 }
97
98                 //
99                 // FIXME: This could use some improvements.
100                 //
101                 public sealed override int GetHashCode ()
102                 {
103                         return base.GetHashCode ();
104                 }
105
106                 // <summary>
107                 //   Return, in order of invocation, the invocation list
108                 //   of a MulticastDelegate
109                 // </summary>
110                 public sealed override Delegate[] GetInvocationList ()
111                 {
112                         MulticastDelegate d;
113                         d = (MulticastDelegate) this.Clone ();
114                         for (d.kpm_next = null; d.prev != null; d = d.prev)
115                                 d.prev.kpm_next = d;
116
117                         if (d.kpm_next == null) {
118                                 MulticastDelegate other = (MulticastDelegate) d.Clone ();
119                                 other.prev = null;
120                                 other.kpm_next = null;                          
121                                 return new Delegate [1] { other };
122                         }
123
124                         ArrayList list = new ArrayList ();
125                         for (; d != null; d = d.kpm_next) {
126                                 MulticastDelegate other = (MulticastDelegate) d.Clone ();
127                                 other.prev = null;
128                                 other.kpm_next = null;
129                                 list.Add (other);
130                         }
131
132                         return (Delegate []) list.ToArray (typeof (Delegate));
133                 }
134
135                 // <summary>
136                 //   Combines this MulticastDelegate with the (Multicast)Delegate `follow'.
137                 //   This does _not_ combine with Delegates. ECMA states the whole delegate
138                 //   thing should have better been a simple System.Delegate class.
139                 //   Compiler generated delegates are always MulticastDelegates.
140                 // </summary>
141                 protected sealed override Delegate CombineImpl (Delegate follow)
142                 {
143                         MulticastDelegate combined, orig, clone;
144
145                         if (this.GetType() != follow.GetType ())
146                                 throw new ArgumentException (Locale.GetText ("Incompatible Delegate Types."));
147
148                         combined = (MulticastDelegate)follow.Clone ();
149                         combined.SetMulticastInvoke ();
150
151                         for (clone = combined, orig = ((MulticastDelegate)follow).prev; orig != null; orig = orig.prev) {
152                                 
153                                 clone.prev = (MulticastDelegate)orig.Clone ();
154                                 clone = clone.prev;
155                         }
156
157                         clone.prev = (MulticastDelegate)this.Clone ();
158
159                         for (clone = clone.prev, orig = this.prev; orig != null; orig = orig.prev) {
160
161                                 clone.prev = (MulticastDelegate)orig.Clone ();
162                                 clone = clone.prev;
163                         }
164
165                         return combined;
166                 }
167
168                 private bool BaseEquals (MulticastDelegate value)
169                 {
170                         return base.Equals (value);
171                 }
172
173                 /* 
174                  * Perform a slightly crippled version of
175                  * Knuth-Pratt-Morris over MulticastDelegate chains.
176                  * Border values are set as pointers in kpm_next;
177                  * Generally, KPM border arrays are length n+1 for
178                  * strings of n. This one works with length n at the
179                  * expense of a few additional comparisions.
180                  */
181                 private static MulticastDelegate KPM (MulticastDelegate needle, MulticastDelegate haystack,
182                                                       out MulticastDelegate tail)
183                 {
184                         MulticastDelegate nx, hx;
185
186                         // preprocess
187                         hx = needle;
188                         nx = needle.kpm_next = null;
189                         do {
190                                 while ((nx != null) && (!nx.BaseEquals (hx)))
191                                         nx = nx.kpm_next;
192
193                                 hx = hx.prev;
194                                 if (hx == null)
195                                         break;
196                                         
197                                 nx = nx == null ? needle : nx.prev;
198                                 if (hx.BaseEquals (nx))
199                                         hx.kpm_next = nx.kpm_next;
200                                 else
201                                         hx.kpm_next = nx;
202
203                         } while (true);
204
205                         // match
206                         MulticastDelegate match = haystack;
207                         nx = needle;
208                         hx = haystack;
209                         do {
210                                 while (nx != null && !nx.BaseEquals (hx)) {
211                                         nx = nx.kpm_next;
212                                         match = match.prev;
213                                 }
214
215                                 nx = nx == null ? needle : nx.prev;
216                                 if (nx == null) {
217                                         // bingo
218                                         tail = hx.prev;
219                                         return match;
220                                 }
221
222                                 hx = hx.prev;
223                         } while (hx != null);
224
225                         tail = null;
226                         return null;
227                 }
228
229                 protected sealed override Delegate RemoveImpl (Delegate value)
230                 {
231                         if (value == null)
232                                 return this;
233
234                         // match this with value
235                         MulticastDelegate head, tail;
236                         head = KPM ((MulticastDelegate)value, this, out tail);
237                         if (head == null)
238                                 return this;
239
240                         // duplicate chain without head..tail
241                         MulticastDelegate prev = null, retval = null, orig;
242                         for (orig = this; (object)orig != (object)head; orig = orig.prev) {
243                                 MulticastDelegate clone = (MulticastDelegate)orig.Clone ();
244                                 if (prev != null)
245                                         prev.prev = clone;
246                                 else
247                                         retval = clone;
248                                 prev = clone;
249                         }
250                         for (orig = tail; (object)orig != null; orig = orig.prev) {
251                                 MulticastDelegate clone = (MulticastDelegate)orig.Clone ();
252                                 if (prev != null)
253                                         prev.prev = clone;
254                                 else
255                                         retval = clone;
256                                 prev = clone;
257                         }
258                         if (prev != null)
259                                 prev.prev = null;
260
261                         return retval;
262                 }
263
264                 public static bool operator == (MulticastDelegate d1, MulticastDelegate d2)
265                 {
266                         if (d1 == null)
267                                 return d2 == null;
268                                 
269                         return d1.Equals (d2);
270                 }
271                 
272                 public static bool operator != (MulticastDelegate d1, MulticastDelegate d2)
273                 {
274                         if (d1 == null)
275                                 return d2 != null;
276                         
277                         return !d1.Equals (d2);
278                 }
279         }
280 }