2
0

DictionaryDisposable.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. using System;
  2. using System.Collections.Generic;
  3. namespace UniRx
  4. {
  5. public sealed class DictionaryDisposable<TKey, TValue> : IDisposable, IDictionary<TKey, TValue>
  6. where TValue : IDisposable
  7. {
  8. bool isDisposed = false;
  9. readonly Dictionary<TKey, TValue> inner;
  10. public DictionaryDisposable()
  11. {
  12. inner = new Dictionary<TKey, TValue>();
  13. }
  14. public DictionaryDisposable(IEqualityComparer<TKey> comparer)
  15. {
  16. inner = new Dictionary<TKey, TValue>(comparer);
  17. }
  18. public TValue this[TKey key]
  19. {
  20. get
  21. {
  22. lock (inner)
  23. {
  24. return inner[key];
  25. }
  26. }
  27. set
  28. {
  29. lock (inner)
  30. {
  31. if (isDisposed) value.Dispose();
  32. TValue oldValue;
  33. if (TryGetValue(key, out oldValue))
  34. {
  35. oldValue.Dispose();
  36. inner[key] = value;
  37. }
  38. else
  39. {
  40. inner[key] = value;
  41. }
  42. }
  43. }
  44. }
  45. public int Count
  46. {
  47. get
  48. {
  49. lock (inner)
  50. {
  51. return inner.Count;
  52. }
  53. }
  54. }
  55. public Dictionary<TKey, TValue>.KeyCollection Keys
  56. {
  57. get
  58. {
  59. throw new NotSupportedException("please use .Select(x => x.Key).ToArray()");
  60. }
  61. }
  62. public Dictionary<TKey, TValue>.ValueCollection Values
  63. {
  64. get
  65. {
  66. throw new NotSupportedException("please use .Select(x => x.Value).ToArray()");
  67. }
  68. }
  69. public void Add(TKey key, TValue value)
  70. {
  71. lock (inner)
  72. {
  73. if (isDisposed)
  74. {
  75. value.Dispose();
  76. return;
  77. }
  78. inner.Add(key, value);
  79. }
  80. }
  81. public void Clear()
  82. {
  83. lock (inner)
  84. {
  85. foreach (var item in inner)
  86. {
  87. item.Value.Dispose();
  88. }
  89. inner.Clear();
  90. }
  91. }
  92. public bool Remove(TKey key)
  93. {
  94. lock (inner)
  95. {
  96. TValue oldValue;
  97. if (inner.TryGetValue(key, out oldValue))
  98. {
  99. var isSuccessRemove = inner.Remove(key);
  100. if (isSuccessRemove)
  101. {
  102. oldValue.Dispose();
  103. }
  104. return isSuccessRemove;
  105. }
  106. else
  107. {
  108. return false;
  109. }
  110. }
  111. }
  112. public bool ContainsKey(TKey key)
  113. {
  114. lock (inner)
  115. {
  116. return inner.ContainsKey(key);
  117. }
  118. }
  119. public bool TryGetValue(TKey key, out TValue value)
  120. {
  121. lock (inner)
  122. {
  123. return inner.TryGetValue(key, out value);
  124. }
  125. }
  126. public Dictionary<TKey, TValue>.Enumerator GetEnumerator()
  127. {
  128. lock (inner)
  129. {
  130. return new Dictionary<TKey, TValue>(inner).GetEnumerator();
  131. }
  132. }
  133. bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly
  134. {
  135. get
  136. {
  137. return ((ICollection<KeyValuePair<TKey, TValue>>)inner).IsReadOnly;
  138. }
  139. }
  140. ICollection<TKey> IDictionary<TKey, TValue>.Keys
  141. {
  142. get
  143. {
  144. lock (inner)
  145. {
  146. return new List<TKey>(inner.Keys);
  147. }
  148. }
  149. }
  150. ICollection<TValue> IDictionary<TKey, TValue>.Values
  151. {
  152. get
  153. {
  154. lock (inner)
  155. {
  156. return new List<TValue>(inner.Values);
  157. }
  158. }
  159. }
  160. #if !UNITY_METRO
  161. public void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
  162. {
  163. lock (inner)
  164. {
  165. ((System.Runtime.Serialization.ISerializable)inner).GetObjectData(info, context);
  166. }
  167. }
  168. public void OnDeserialization(object sender)
  169. {
  170. lock (inner)
  171. {
  172. ((System.Runtime.Serialization.IDeserializationCallback)inner).OnDeserialization(sender);
  173. }
  174. }
  175. #endif
  176. void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item)
  177. {
  178. Add((TKey)item.Key, (TValue)item.Value);
  179. }
  180. bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item)
  181. {
  182. lock (inner)
  183. {
  184. return ((ICollection<KeyValuePair<TKey, TValue>>)inner).Contains(item);
  185. }
  186. }
  187. void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
  188. {
  189. lock (inner)
  190. {
  191. ((ICollection<KeyValuePair<TKey, TValue>>)inner).CopyTo(array, arrayIndex);
  192. }
  193. }
  194. IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator()
  195. {
  196. lock (inner)
  197. {
  198. return new List<KeyValuePair<TKey, TValue>>((ICollection<KeyValuePair<TKey, TValue>>)inner).GetEnumerator();
  199. }
  200. }
  201. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  202. {
  203. return GetEnumerator();
  204. }
  205. bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item)
  206. {
  207. throw new NotSupportedException();
  208. }
  209. public void Dispose()
  210. {
  211. lock (inner)
  212. {
  213. if (isDisposed) return;
  214. isDisposed = true;
  215. foreach (var item in inner)
  216. {
  217. item.Value.Dispose();
  218. }
  219. inner.Clear();
  220. }
  221. }
  222. }
  223. }