BindingFlags.Public needed here as Exception.HResult is now public in .NET 4.5. This...
[mono.git] / mcs / class / System / System.ComponentModel / EventHandlerList.cs
1 //
2 // System.ComponentModel.EventHandlerList.cs
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //   Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //
8 // (C) Ximian, Inc.  http://www.ximian.com
9 //
10
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System;
33 using System.Collections;
34
35 #if NET_2_0
36 using System.Collections.Generic;
37 #endif
38
39 namespace System.ComponentModel {
40
41         internal class ListEntry {
42                 public object key;
43                 public Delegate value;
44                 public ListEntry next;
45         }
46
47         // <summary>
48         //   List of Event delegates.
49         // </summary>
50         //
51         // <remarks>
52         //   Longer description
53         // </remarks>
54         public sealed class EventHandlerList : IDisposable
55         {
56                 ListEntry entries;
57
58                 Delegate null_entry;
59
60                 public EventHandlerList ()
61                 {
62                 }
63
64                 public Delegate this [object key] {
65                         get {
66                                 if (key == null)
67                                         return null_entry;
68                                 ListEntry entry = FindEntry (key);
69                                 if (entry != null)
70                                         return entry.value;
71                                 else
72                                         return null;
73                         }
74
75                         set {
76                                 AddHandler (key, value);
77                         }
78                 }
79
80                 public void AddHandler (object key, Delegate value)
81                 {
82                         if (key == null) {
83                                 null_entry = Delegate.Combine (null_entry, value);
84                                 return;
85                         }
86
87                         ListEntry entry = FindEntry (key);
88                         if (entry == null) {
89                                 entry = new ListEntry ();
90                                 entry.key = key;
91                                 entry.value = null;
92                                 entry.next = entries;
93                                 entries = entry;
94                         }
95
96                         entry.value = Delegate.Combine (entry.value, value);
97                 }
98
99 #if NET_2_0
100                 public void AddHandlers (EventHandlerList listToAddFrom)
101                 {
102                         if (listToAddFrom == null)
103                                 return;
104                         
105                         ListEntry entry = listToAddFrom.entries;
106                         while (entry != null) {
107                                 AddHandler (entry.key, entry.value);
108                                 entry = entry.next;
109                         }
110                 }
111 #endif
112
113                 public void RemoveHandler (object key, Delegate value)
114                 {
115                         if (key == null) {
116                                 null_entry = Delegate.Remove (null_entry, value);
117                                 return;
118                         }
119
120                         ListEntry entry = FindEntry (key);
121                         if (entry == null)
122                                 return;
123
124                         entry.value = Delegate.Remove (entry.value, value);
125                 }
126
127                 public void Dispose ()
128                 {
129                         entries = null;
130                 }
131                 
132                 private ListEntry FindEntry (object key)
133                 {
134                         ListEntry entry = entries;
135                         while (entry != null) {
136                                 if (entry.key == key)
137                                         return entry;
138                                 entry = entry.next;
139                         }
140
141                         return null;
142                 }
143         }
144 }
145