Merge pull request #649 from DavidS/feature/implement-additional-reference-path
[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                         MulticastDelegate this_prev = this.prev;
92                         MulticastDelegate obj_prev = d.prev;
93
94                         do {
95                                 if (this_prev == null)
96                                         return obj_prev == null;
97
98                                 if (!this_prev.Compare (obj_prev))
99                                         return false;
100                                 
101                                 this_prev = this_prev.prev;
102                                 obj_prev = obj_prev.prev;
103                         } while (true);
104                 }
105
106                 //
107                 // FIXME: This could use some improvements.
108                 //
109                 public sealed override int GetHashCode ()
110                 {
111                         return base.GetHashCode ();
112                 }
113
114                 // <summary>
115                 //   Return, in order of invocation, the invocation list
116                 //   of a MulticastDelegate
117                 // </summary>
118                 public sealed override Delegate[] GetInvocationList ()
119                 {
120                         MulticastDelegate d;
121                         d = (MulticastDelegate) this.Clone ();
122                         for (d.kpm_next = null; d.prev != null; d = d.prev)
123                                 d.prev.kpm_next = d;
124
125                         if (d.kpm_next == null) {
126                                 MulticastDelegate other = (MulticastDelegate) d.Clone ();
127                                 other.prev = null;
128                                 other.kpm_next = null;                          
129                                 return new Delegate [1] { other };
130                         }
131
132                         var list = new List<Delegate> ();
133                         for (; d != null; d = d.kpm_next) {
134                                 MulticastDelegate other = (MulticastDelegate) d.Clone ();
135                                 other.prev = null;
136                                 other.kpm_next = null;
137                                 list.Add (other);
138                         }
139
140                         return list.ToArray ();
141                 }
142
143                 // <summary>
144                 //   Combines this MulticastDelegate with the (Multicast)Delegate `follow'.
145                 //   This does _not_ combine with Delegates. ECMA states the whole delegate
146                 //   thing should have better been a simple System.Delegate class.
147                 //   Compiler generated delegates are always MulticastDelegates.
148                 // </summary>
149                 protected sealed override Delegate CombineImpl (Delegate follow)
150                 {
151                         MulticastDelegate combined, orig, clone;
152
153                         if (this.GetType() != follow.GetType ())
154                                 throw new ArgumentException (Locale.GetText ("Incompatible Delegate Types. First is {0} second is {1}.", this.GetType ().FullName, follow.GetType ().FullName));
155
156                         combined = (MulticastDelegate)follow.Clone ();
157                         combined.SetMulticastInvoke ();
158
159                         for (clone = combined, orig = ((MulticastDelegate)follow).prev; orig != null; orig = orig.prev) {
160                                 
161                                 clone.prev = (MulticastDelegate)orig.Clone ();
162                                 clone = clone.prev;
163                         }
164
165                         clone.SetMulticastInvoke ();
166                         clone.prev = (MulticastDelegate)this.Clone ();
167
168                         for (clone = clone.prev, orig = this.prev; orig != null; orig = orig.prev) {
169
170                                 clone.prev = (MulticastDelegate)orig.Clone ();
171                                 clone = clone.prev;
172                         }
173
174                         return combined;
175                 }
176
177                 private bool BaseEquals (MulticastDelegate value)
178                 {
179                         return base.Equals (value);
180                 }
181
182                 /* 
183                  * Perform a slightly crippled version of
184                  * Knuth-Pratt-Morris over MulticastDelegate chains.
185                  * Border values are set as pointers in kpm_next;
186                  * Generally, KPM border arrays are length n+1 for
187                  * strings of n. This one works with length n at the
188                  * expense of a few additional comparisions.
189                  */
190                 private static MulticastDelegate KPM (MulticastDelegate needle, MulticastDelegate haystack,
191                                                       out MulticastDelegate tail)
192                 {
193                         MulticastDelegate nx, hx;
194
195                         // preprocess
196                         hx = needle;
197                         nx = needle.kpm_next = null;
198                         do {
199                                 while ((nx != null) && (!nx.BaseEquals (hx)))
200                                         nx = nx.kpm_next;
201
202                                 hx = hx.prev;
203                                 if (hx == null)
204                                         break;
205                                         
206                                 nx = nx == null ? needle : nx.prev;
207                                 if (hx.BaseEquals (nx))
208                                         hx.kpm_next = nx.kpm_next;
209                                 else
210                                         hx.kpm_next = nx;
211
212                         } while (true);
213
214                         // match
215                         MulticastDelegate match = haystack;
216                         nx = needle;
217                         hx = haystack;
218                         do {
219                                 while (nx != null && !nx.BaseEquals (hx)) {
220                                         nx = nx.kpm_next;
221                                         match = match.prev;
222                                 }
223
224                                 nx = nx == null ? needle : nx.prev;
225                                 if (nx == null) {
226                                         // bingo
227                                         tail = hx.prev;
228                                         return match;
229                                 }
230
231                                 hx = hx.prev;
232                         } while (hx != null);
233
234                         tail = null;
235                         return null;
236                 }
237
238                 protected sealed override Delegate RemoveImpl (Delegate value)
239                 {
240                         if (value == null)
241                                 return this;
242
243                         // match this with value
244                         MulticastDelegate head, tail;
245                         head = KPM ((MulticastDelegate)value, this, out tail);
246                         if (head == null)
247                                 return this;
248
249                         // duplicate chain without head..tail
250                         MulticastDelegate prev = null, retval = null, orig;
251                         for (orig = this; (object)orig != (object)head; orig = orig.prev) {
252                                 MulticastDelegate clone = (MulticastDelegate)orig.Clone ();
253                                 if (prev != null)
254                                         prev.prev = clone;
255                                 else
256                                         retval = clone;
257                                 prev = clone;
258                         }
259                         for (orig = tail; (object)orig != null; orig = orig.prev) {
260                                 MulticastDelegate clone = (MulticastDelegate)orig.Clone ();
261                                 if (prev != null)
262                                         prev.prev = clone;
263                                 else
264                                         retval = clone;
265                                 prev = clone;
266                         }
267                         if (prev != null)
268                                 prev.prev = null;
269
270                         return retval;
271                 }
272
273                 public static bool operator == (MulticastDelegate d1, MulticastDelegate d2)
274                 {
275                         if (d1 == null)
276                                 return d2 == null;
277                                 
278                         return d1.Equals (d2);
279                 }
280                 
281                 public static bool operator != (MulticastDelegate d1, MulticastDelegate d2)
282                 {
283                         if (d1 == null)
284                                 return d2 != null;
285                         
286                         return !d1.Equals (d2);
287                 }
288         }
289 }