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