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