Merge pull request #799 from kebby/master
[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                 MulticastDelegate prev;
47                 MulticastDelegate kpm_next;
48
49                 protected MulticastDelegate (object target, string method)
50                         : base (target, method)
51                 {
52                 }
53
54                 protected MulticastDelegate (Type target, string method)
55                         : base (target, method)
56                 {
57                 }
58                 
59                 public override void GetObjectData (SerializationInfo info, StreamingContext context)
60                 {
61                         base.GetObjectData  (info, context);
62                 }
63
64
65                 protected sealed override object DynamicInvokeImpl (object[] args)
66                 {
67                         if (prev != null)
68                                 prev.DynamicInvokeImpl (args);
69
70                         return base.DynamicInvokeImpl (args);
71                 }
72
73                 // <remarks>
74                 //   Equals: two multicast delegates are equal if their base is equal
75                 //   and their invocations list is equal.
76                 // </remarks>
77                 public sealed override bool Equals (object obj)
78                 {
79                         if (!base.Equals (obj))
80                                 return false;
81
82                         MulticastDelegate d = obj as MulticastDelegate;
83                         if (d == null)
84                                 return false;
85
86                         MulticastDelegate this_prev = this.prev;
87                         MulticastDelegate obj_prev = d.prev;
88
89                         do {
90                                 if (this_prev == null)
91                                         return obj_prev == null;
92
93                                 if (!this_prev.Compare (obj_prev))
94                                         return false;
95                                 
96                                 this_prev = this_prev.prev;
97                                 obj_prev = obj_prev.prev;
98                         } while (true);
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.SetMulticastInvoke ();
161                         clone.prev = (MulticastDelegate)this.Clone ();
162
163                         for (clone = clone.prev, orig = this.prev; orig != null; orig = orig.prev) {
164
165                                 clone.prev = (MulticastDelegate)orig.Clone ();
166                                 clone = clone.prev;
167                         }
168
169                         return combined;
170                 }
171
172                 private bool BaseEquals (MulticastDelegate value)
173                 {
174                         return base.Equals (value);
175                 }
176
177                 /* 
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.
184                  */
185                 private static MulticastDelegate KPM (MulticastDelegate needle, MulticastDelegate haystack,
186                                                       out MulticastDelegate tail)
187                 {
188                         MulticastDelegate nx, hx;
189
190                         // preprocess
191                         hx = needle;
192                         nx = needle.kpm_next = null;
193                         do {
194                                 while ((nx != null) && (!nx.BaseEquals (hx)))
195                                         nx = nx.kpm_next;
196
197                                 hx = hx.prev;
198                                 if (hx == null)
199                                         break;
200                                         
201                                 nx = nx == null ? needle : nx.prev;
202                                 if (hx.BaseEquals (nx))
203                                         hx.kpm_next = nx.kpm_next;
204                                 else
205                                         hx.kpm_next = nx;
206
207                         } while (true);
208
209                         // match
210                         MulticastDelegate match = haystack;
211                         nx = needle;
212                         hx = haystack;
213                         do {
214                                 while (nx != null && !nx.BaseEquals (hx)) {
215                                         nx = nx.kpm_next;
216                                         match = match.prev;
217                                 }
218
219                                 nx = nx == null ? needle : nx.prev;
220                                 if (nx == null) {
221                                         // bingo
222                                         tail = hx.prev;
223                                         return match;
224                                 }
225
226                                 hx = hx.prev;
227                         } while (hx != null);
228
229                         tail = null;
230                         return null;
231                 }
232
233                 protected sealed override Delegate RemoveImpl (Delegate value)
234                 {
235                         if (value == null)
236                                 return this;
237
238                         // match this with value
239                         MulticastDelegate head, tail;
240                         head = KPM ((MulticastDelegate)value, this, out tail);
241                         if (head == null)
242                                 return this;
243
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 ();
248                                 if (prev != null)
249                                         prev.prev = clone;
250                                 else
251                                         retval = clone;
252                                 prev = clone;
253                         }
254                         for (orig = tail; (object)orig != null; orig = orig.prev) {
255                                 MulticastDelegate clone = (MulticastDelegate)orig.Clone ();
256                                 if (prev != null)
257                                         prev.prev = clone;
258                                 else
259                                         retval = clone;
260                                 prev = clone;
261                         }
262                         if (prev != null)
263                                 prev.prev = null;
264
265                         return retval;
266                 }
267
268                 public static bool operator == (MulticastDelegate d1, MulticastDelegate d2)
269                 {
270                         if (d1 == null)
271                                 return d2 == null;
272                                 
273                         return d1.Equals (d2);
274                 }
275                 
276                 public static bool operator != (MulticastDelegate d1, MulticastDelegate d2)
277                 {
278                         if (d1 == null)
279                                 return d2 != null;
280                         
281                         return !d1.Equals (d2);
282                 }
283         }
284 }