Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.Core/Reactive/Disposables/CompositeDisposable.cs

raw
1 // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
2
3 using System.Collections.Generic;
4 using System.Linq;
5
6 namespace System.Reactive.Disposables
7 {
8 /// <summary>
9 /// Represents a group of disposable resources that are disposed together.
10 /// </summary>
11 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix", Justification = "Backward compat + ideally want to get rid of the ICollection nature of the type.")]
12 public sealed class CompositeDisposable : ICollection<IDisposable>, ICancelable
13 {
14 private readonly object _gate = new object();
15
16 private bool _disposed;
17 private List<IDisposable> _disposables;
18 private int _count;
19 private const int SHRINK_THRESHOLD = 64;
20
21 /// <summary>
22 /// Initializes a new instance of the <see cref="T:System.Reactive.Disposables.CompositeDisposable"/> class with no disposables contained by it initially.
23 /// </summary>
24 public CompositeDisposable()
25 {
26 _disposables = new List<IDisposable>();
27 }
28
29 /// <summary>
30 /// Initializes a new instance of the <see cref="T:System.Reactive.Disposables.CompositeDisposable"/> class with the specified number of disposables.
31 /// </summary>
32 /// <param name="capacity">The number of disposables that the new CompositeDisposable can initially store.</param>
33 /// <exception cref="ArgumentOutOfRangeException"><paramref name="capacity"/> is less than zero.</exception>
34 public CompositeDisposable(int capacity)
35 {
36 if (capacity < 0)
37 throw new ArgumentOutOfRangeException("capacity");
38
39 _disposables = new List<IDisposable>(capacity);
40 }
41
42 /// <summary>
43 /// Initializes a new instance of the <see cref="T:System.Reactive.Disposables.CompositeDisposable"/> class from a group of disposables.
44 /// </summary>
45 /// <param name="disposables">Disposables that will be disposed together.</param>
46 /// <exception cref="ArgumentNullException"><paramref name="disposables"/> is null.</exception>
47 public CompositeDisposable(params IDisposable[] disposables)
48 {
49 if (disposables == null)
50 throw new ArgumentNullException("disposables");
51
52 _disposables = new List<IDisposable>(disposables);
53 _count = _disposables.Count;
54 }
55
56 /// <summary>
57 /// Initializes a new instance of the <see cref="T:System.Reactive.Disposables.CompositeDisposable"/> class from a group of disposables.
58 /// </summary>
59 /// <param name="disposables">Disposables that will be disposed together.</param>
60 /// <exception cref="ArgumentNullException"><paramref name="disposables"/> is null.</exception>
61 public CompositeDisposable(IEnumerable<IDisposable> disposables)
62 {
63 if (disposables == null)
64 throw new ArgumentNullException("disposables");
65
66 _disposables = new List<IDisposable>(disposables);
67 _count = _disposables.Count;
68 }
69
70 /// <summary>
71 /// Gets the number of disposables contained in the CompositeDisposable.
72 /// </summary>
73 public int Count
74 {
75 get
76 {
77 return _count;
78 }
79 }
80
81 /// <summary>
82 /// Adds a disposable to the CompositeDisposable or disposes the disposable if the CompositeDisposable is disposed.
83 /// </summary>
84 /// <param name="item">Disposable to add.</param>
85 /// <exception cref="ArgumentNullException"><paramref name="item"/> is null.</exception>
86 public void Add(IDisposable item)
87 {
88 if (item == null)
89 throw new ArgumentNullException("item");
90
91 var shouldDispose = false;
92 lock (_gate)
93 {
94 shouldDispose = _disposed;
95 if (!_disposed)
96 {
97 _disposables.Add(item);
98 _count++;
99 }
100 }
101 if (shouldDispose)
102 item.Dispose();
103 }
104
105 /// <summary>
106 /// Removes and disposes the first occurrence of a disposable from the CompositeDisposable.
107 /// </summary>
108 /// <param name="item">Disposable to remove.</param>
109 /// <returns>true if found; false otherwise.</returns>
110 /// <exception cref="ArgumentNullException"><paramref name="item"/> is null.</exception>
111 public bool Remove(IDisposable item)
112 {
113 if (item == null)
114 throw new ArgumentNullException("item");
115
116 var shouldDispose = false;
117
118 lock (_gate)
119 {
120 if (!_disposed)
121 {
122 //
123 // List<T> doesn't shrink the size of the underlying array but does collapse the array
124 // by copying the tail one position to the left of the removal index. We don't need
125 // index-based lookup but only ordering for sequential disposal. So, instead of spending
126 // cycles on the Array.Copy imposed by Remove, we use a null sentinel value. We also
127 // do manual Swiss cheese detection to shrink the list if there's a lot of holes in it.
128 //
129 var i = _disposables.IndexOf(item);
130 if (i >= 0)
131 {
132 shouldDispose = true;
133 _disposables[i] = null;
134 _count--;
135
136 if (_disposables.Capacity > SHRINK_THRESHOLD && _count < _disposables.Capacity / 2)
137 {
138 var old = _disposables;
139 _disposables = new List<IDisposable>(_disposables.Capacity / 2);
140
141 foreach (var d in old)
142 if (d != null)
143 _disposables.Add(d);
144 }
145 }
146 }
147 }
148
149 if (shouldDispose)
150 item.Dispose();
151
152 return shouldDispose;
153 }
154
155 /// <summary>
156 /// Disposes all disposables in the group and removes them from the group.
157 /// </summary>
158 public void Dispose()
159 {
160 var currentDisposables = default(IDisposable[]);
161 lock (_gate)
162 {
163 if (!_disposed)
164 {
165 _disposed = true;
166 currentDisposables = _disposables.ToArray();
167 _disposables.Clear();
168 _count = 0;
169 }
170 }
171
172 if (currentDisposables != null)
173 {
174 foreach (var d in currentDisposables)
175 if (d != null)
176 d.Dispose();
177 }
178 }
179
180 /// <summary>
181 /// Removes and disposes all disposables from the CompositeDisposable, but does not dispose the CompositeDisposable.
182 /// </summary>
183 public void Clear()
184 {
185 var currentDisposables = default(IDisposable[]);
186 lock (_gate)
187 {
188 currentDisposables = _disposables.ToArray();
189 _disposables.Clear();
190 _count = 0;
191 }
192
193 foreach (var d in currentDisposables)
194 if (d != null)
195 d.Dispose();
196 }
197
198 /// <summary>
199 /// Determines whether the CompositeDisposable contains a specific disposable.
200 /// </summary>
201 /// <param name="item">Disposable to search for.</param>
202 /// <returns>true if the disposable was found; otherwise, false.</returns>
203 /// <exception cref="ArgumentNullException"><paramref name="item"/> is null.</exception>
204 public bool Contains(IDisposable item)
205 {
206 if (item == null)
207 throw new ArgumentNullException("item");
208
209 lock (_gate)
210 {
211 return _disposables.Contains(item);
212 }
213 }
214
215 /// <summary>
216 /// Copies the disposables contained in the CompositeDisposable to an array, starting at a particular array index.
217 /// </summary>
218 /// <param name="array">Array to copy the contained disposables to.</param>
219 /// <param name="arrayIndex">Target index at which to copy the first disposable of the group.</param>
220 /// <exception cref="ArgumentNullException"><paramref name="array"/> is null.</exception>
221 /// <exception cref="ArgumentOutOfRangeException"><paramref name="arrayIndex"/> is less than zero. -or - <paramref name="arrayIndex"/> is larger than or equal to the array length.</exception>
222 public void CopyTo(IDisposable[] array, int arrayIndex)
223 {
224 if (array == null)
225 throw new ArgumentNullException("array");
226 if (arrayIndex < 0 || arrayIndex >= array.Length)
227 throw new ArgumentOutOfRangeException("arrayIndex");
228
229 lock (_gate)
230 {
231 Array.Copy(_disposables.Where(d => d != null).ToArray(), 0, array, arrayIndex, array.Length - arrayIndex);
232 }
233 }
234
235 /// <summary>
236 /// Always returns false.
237 /// </summary>
238 public bool IsReadOnly
239 {
240 get { return false; }
241 }
242
243 /// <summary>
244 /// Returns an enumerator that iterates through the CompositeDisposable.
245 /// </summary>
246 /// <returns>An enumerator to iterate over the disposables.</returns>
247 public IEnumerator<IDisposable> GetEnumerator()
248 {
249 var res = default(IEnumerable<IDisposable>);
250
251 lock (_gate)
252 {
253 res = _disposables.Where(d => d != null).ToList();
254 }
255
256 return res.GetEnumerator();
257 }
258
259 /// <summary>
260 /// Returns an enumerator that iterates through the CompositeDisposable.
261 /// </summary>
262 /// <returns>An enumerator to iterate over the disposables.</returns>
263 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
264 {
265 return GetEnumerator();
266 }
267
268 /// <summary>
269 /// Gets a value that indicates whether the object is disposed.
270 /// </summary>
271 public bool IsDisposed
272 {
273 get { return _disposed; }
274 }
275 }
276 }