xbuild: use the AdditionalReferencePath items to locate assemblies
[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.prev = (MulticastDelegate)this.Clone ();
166
167                         for (clone = clone.prev, orig = this.prev; orig != null; orig = orig.prev) {
168
169                                 clone.prev = (MulticastDelegate)orig.Clone ();
170                                 clone = clone.prev;
171                         }
172
173                         return combined;
174                 }
175
176                 private bool BaseEquals (MulticastDelegate value)
177                 {
178                         return base.Equals (value);
179                 }
180
181                 /* 
182                  * Perform a slightly crippled version of
183                  * Knuth-Pratt-Morris over MulticastDelegate chains.
184                  * Border values are set as pointers in kpm_next;
185                  * Generally, KPM border arrays are length n+1 for
186                  * strings of n. This one works with length n at the
187                  * expense of a few additional comparisions.
188                  */
189                 private static MulticastDelegate KPM (MulticastDelegate needle, MulticastDelegate haystack,
190                                                       out MulticastDelegate tail)
191                 {
192                         MulticastDelegate nx, hx;
193
194                         // preprocess
195                         hx = needle;
196                         nx = needle.kpm_next = null;
197                         do {
198                                 while ((nx != null) && (!nx.BaseEquals (hx)))
199                                         nx = nx.kpm_next;
200
201                                 hx = hx.prev;
202                                 if (hx == null)
203                                         break;
204                                         
205                                 nx = nx == null ? needle : nx.prev;
206                                 if (hx.BaseEquals (nx))
207                                         hx.kpm_next = nx.kpm_next;
208                                 else
209                                         hx.kpm_next = nx;
210
211                         } while (true);
212
213                         // match
214                         MulticastDelegate match = haystack;
215                         nx = needle;
216                         hx = haystack;
217                         do {
218                                 while (nx != null && !nx.BaseEquals (hx)) {
219                                         nx = nx.kpm_next;
220                                         match = match.prev;
221                                 }
222
223                                 nx = nx == null ? needle : nx.prev;
224                                 if (nx == null) {
225                                         // bingo
226                                         tail = hx.prev;
227                                         return match;
228                                 }
229
230                                 hx = hx.prev;
231                         } while (hx != null);
232
233                         tail = null;
234                         return null;
235                 }
236
237                 protected sealed override Delegate RemoveImpl (Delegate value)
238                 {
239                         if (value == null)
240                                 return this;
241
242                         // match this with value
243                         MulticastDelegate head, tail;
244                         head = KPM ((MulticastDelegate)value, this, out tail);
245                         if (head == null)
246                                 return this;
247
248                         // duplicate chain without head..tail
249                         MulticastDelegate prev = null, retval = null, orig;
250                         for (orig = this; (object)orig != (object)head; 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                         for (orig = tail; (object)orig != null; orig = orig.prev) {
259                                 MulticastDelegate clone = (MulticastDelegate)orig.Clone ();
260                                 if (prev != null)
261                                         prev.prev = clone;
262                                 else
263                                         retval = clone;
264                                 prev = clone;
265                         }
266                         if (prev != null)
267                                 prev.prev = null;
268
269                         return retval;
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                 public static bool operator != (MulticastDelegate d1, MulticastDelegate d2)
281                 {
282                         if (d1 == null)
283                                 return d2 != null;
284                         
285                         return !d1.Equals (d2);
286                 }
287         }
288 }