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