Auto status change to "Under Review"
@@ -1,105 +1,103 | |||
|
1 | 1 | using Implab.Diagnostics; |
|
2 | 2 | using System; |
|
3 | 3 | using System.Diagnostics.CodeAnalysis; |
|
4 | 4 | using System.Threading; |
|
5 | 5 | |
|
6 | 6 | namespace Implab.Components { |
|
7 | 7 | /// <summary> |
|
8 | 8 | /// Base class the objects which support disposing. |
|
9 | 9 | /// </summary> |
|
10 | 10 | public class Disposable : IDisposable { |
|
11 | ||
|
11 | ||
|
12 | 12 | int m_disposed; |
|
13 | 13 | |
|
14 | 14 | public event EventHandler Disposed; |
|
15 | 15 | |
|
16 | 16 | public bool IsDisposed { |
|
17 | 17 | get { |
|
18 | 18 | Thread.MemoryBarrier(); |
|
19 | 19 | return m_disposed != 0; |
|
20 | 20 | } |
|
21 | 21 | } |
|
22 | 22 | |
|
23 | 23 | /// <summary> |
|
24 | 24 | /// Asserts the object is not disposed. |
|
25 | 25 | /// </summary> |
|
26 | 26 | /// <exception cref="ObjectDisposedException">The object is disposed</exception> |
|
27 | 27 | /// <remarks> |
|
28 | 28 | /// Π£ΡΠΏΠ΅ΡΠ½Π°Ρ ΠΏΡΠΎΠ²Π΅ΡΠΊΠ° ΡΠΎΠ³ΠΎ, ΡΡΠΎ ΠΎΠ±ΡΠ΅ΠΊΡ Π½Π΅ ΠΎΡΠ²ΠΎΠ±ΠΎΠΆΠ΄Π΅Π½ Π΅ΡΠ΅ Π½Π΅ Π³Π°ΡΠ°Π½ΡΠΈΡΡΠ΅Ρ, ΡΡΠΎ ΠΎΠ½ Π½Π΅ |
|
29 | 29 | /// Π±ΡΠ΄Π΅Ρ ΠΎΡΠ²ΠΎΠ±ΠΎΠΆΠ΄Π΅Π½ ΡΡΠ°Π·Ρ ΠΏΠΎΡΠ»Π΅ Π½Π΅Π΅, ΠΏΠΎΡΡΠΎΠΌΡ ΠΌΠ΅ΡΠΎΠ΄Ρ ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΡΡΠΈΠ΅ ΠΏΡΠΎΠ²Π΅ΡΠΊΡ Π΄ΠΎΠ»ΠΆΠ½Ρ |
|
30 | 30 | /// ΡΡΠΈΡΡΠ²Π°ΡΡ, ΡΡΠΎ ΠΎΠ±ΡΠ΅ΠΊΡ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ ΠΎΡΠ²ΠΎΠ±ΠΎΠΆΠ΄Π΅Π½ ΠΈΠ· ΠΏΠ°ΡΠ°Π»Π»Π΅Π»ΡΠ½ΠΎΠ³ΠΎ ΠΏΠΎΡΠΎΠΊΠ°. |
|
31 | 31 | /// ΠΠ°Π½Π½Ρ ΠΌΠ΅ΡΠΎΠ΄ ΡΠ»ΡΠΆΠΈΡ Π΄Π»Ρ ΡΠΏΡΠΎΡΠ΅Π½ΠΈΡ ΠΎΡΠ»Π°Π΄ΠΊΠΈ ΠΎΡΠΈΠ±ΠΎΠΊ ΠΏΡΠΈ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΠΈ ΠΎΠ±ΡΠ΅ΠΊΡΠ° ΠΏΠΎΡΠ»Π΅ Π΅Π³ΠΎ |
|
32 | 32 | /// ΠΎΡΠ²ΠΎΠ±ΠΎΠΆΠ΄Π΅Π½ΠΈΡ. |
|
33 | 33 | /// </remarks> |
|
34 | 34 | /// <example> |
|
35 | 35 | /// // ΠΏΡΠΈΠΌΠ΅Ρ ΡΠΈΠ½Ρ ΡΠΎΠ½ΠΈΠ·ΠΈΡΠΎΠ²Π°Π½Π½ΠΎΠ³ΠΎ ΠΎΡΠ²ΠΎΠ±ΠΎΠΆΠ΄Π΅Π½ΠΈΡ ΡΠ΅ΡΡΡΡΠΎΠ² |
|
36 | 36 | /// class FileStore : Disposable { |
|
37 | 37 | /// readonly TextWriter m_file; |
|
38 | 38 | /// readonly obejct m_sync = new object(); |
|
39 | 39 | /// |
|
40 | 40 | /// public FileStore(string file) { |
|
41 | 41 | /// m_file = new TextWriter(File.OpenWrite(file)); |
|
42 | 42 | /// } |
|
43 | 43 | /// |
|
44 | 44 | /// public void Write(string text) { |
|
45 | 45 | /// lock(m_sync) { |
|
46 | 46 | /// AssertNotDisposed(); |
|
47 | 47 | /// m_file.Write(text); |
|
48 | 48 | /// } |
|
49 | 49 | /// } |
|
50 | 50 | /// |
|
51 | 51 | /// protected override void Dispose(bool disposing) { |
|
52 | 52 | /// if (disposing) |
|
53 | 53 | /// lock(m_sync) { |
|
54 | 54 | /// m_file.Dipose(); |
|
55 | 55 | /// base.Dispose(true); |
|
56 | 56 | /// } |
|
57 | 57 | /// else |
|
58 | 58 | /// base.Dispose(false); |
|
59 | 59 | /// } |
|
60 | 60 | /// } |
|
61 | 61 | /// <example> |
|
62 | 62 | protected void AssertNotDisposed() { |
|
63 | 63 | Thread.MemoryBarrier(); |
|
64 | 64 | if (m_disposed != 0) |
|
65 | 65 | throw new ObjectDisposedException(ToString()); |
|
66 | 66 | } |
|
67 | 67 | /// <summary> |
|
68 | 68 | /// ΠΡΠ·ΡΠ²Π°Π΅Ρ ΡΠΎΠ±ΡΡΠΈΠ΅ <see cref="Disposed"/> |
|
69 | 69 | /// </summary> |
|
70 | 70 | /// <param name="disposing">ΠΡΠΈΠ·Π½Π°ΠΊ ΡΠΎΠ³ΠΎ, ΡΡΠΎ Π½ΡΠΆΠ½ΠΎ ΠΎΡΠ²ΠΎΠ±ΠΎΠ΄ΠΈΡΡ ΡΠ΅ΡΡΡΡΡ, ΠΈΠ½Π°ΡΠ΅ Π΄Π°Π½Π½ΡΠΉ ΠΌΠ΅ΡΠΎΠ΄ |
|
71 | 71 | /// Π²ΡΠ·Π²Π°Π½ ΡΠ±ΠΎΡΡΠΈΠΊΠΎΠΌ ΠΌΡΡΠΎΡΠ° ΠΈ Π½ΡΠΆΠ½ΠΎ ΠΎΡΠ²ΠΎΠ±ΠΎΠΆΠ΄Π°ΡΡ Π’ΠΠΠ¬ΠΠ Π½Π΅ΡΠΏΡΠ°Π²Π»ΡΠ΅ΠΌΡΠ΅ ΡΠ΅ΡΡΡΡΡ Π’ΠΠΠ¬ΠΠ ΡΡΠΎΠ³ΠΎ |
|
72 | 72 | /// ΠΎΠ±ΡΠ΅ΠΊΡΠ°.</param> |
|
73 | 73 | /// <remarks> |
|
74 | 74 | /// ΠΠ°Π½Π½ΡΠΉ ΠΌΠ΅ΡΠΎΠ΄ Π²ΡΠ·ΡΠ²Π°Π΅ΡΡΡ Π³Π°ΡΠ°Π½ΡΠΈΡΠΎΠ²Π°Π½Π½ΠΎ ΠΎΠ΄ΠΈΠ½ ΡΠ°Π· Π΄Π°ΠΆΠ΅ ΠΏΡΠΈ ΠΎΠ΄Π½ΠΎΠ²ΡΠ΅ΠΌΠ΅Π½Π½ΠΎΠΌ Π²ΡΠ·ΠΎΠ²Π΅ <see cref="Dispose()"/> |
|
75 | 75 | /// ΠΈΠ· Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΈΡ ΠΏΠΎΡΠΎΠΊΠΎΠ². |
|
76 | 76 | /// </remarks> |
|
77 | 77 | protected virtual void Dispose(bool disposing) { |
|
78 |
if (disposing) |
|
|
79 | EventHandler temp = Disposed; | |
|
80 | if (temp != null) | |
|
81 | temp(this, EventArgs.Empty); | |
|
82 | } | |
|
78 | if (disposing) | |
|
79 | Disposed.DispatchEvent(this, EventArgs.Empty); | |
|
80 | ||
|
83 | 81 | } |
|
84 | 82 | |
|
85 | 83 | [SuppressMessage("Microsoft.Design", "CA1063:ImplementIDisposableCorrectly", Justification = "Dipose(bool) and GC.SuppessFinalize are called")] |
|
86 | 84 | public void Dispose() { |
|
87 | 85 | if (Interlocked.Increment(ref m_disposed) == 1) { |
|
88 | 86 | Dispose(true); |
|
89 | 87 | GC.SuppressFinalize(this); |
|
90 | 88 | } |
|
91 | 89 | } |
|
92 | 90 | |
|
93 | 91 | /// <summary> |
|
94 | 92 | /// ΠΠ°ΠΏΠΈΡΡΠ²Π°Π΅Ρ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅ ΠΎΠ± ΡΡΠ΅ΡΠΊΠ΅ ΠΎΠ±ΡΠ΅ΠΊΡΠ°. |
|
95 | 93 | /// </summary> |
|
96 | 94 | protected virtual void ReportObjectLeaks() { |
|
97 | 95 | TraceLog.TraceWarning("The object is marked as disposable but isn't disposed properly: {0}", this); |
|
98 | 96 | } |
|
99 | 97 | |
|
100 | 98 | ~Disposable() { |
|
101 | 99 | Dispose(false); |
|
102 | 100 | ReportObjectLeaks(); |
|
103 | 101 | } |
|
104 | 102 | } |
|
105 | 103 | } No newline at end of file |
@@ -1,132 +1,144 | |||
|
1 | 1 | using System; |
|
2 | 2 | using System.Collections.Generic; |
|
3 | 3 | using System.Linq; |
|
4 | 4 | using System.Text; |
|
5 | 5 | using System.Text.RegularExpressions; |
|
6 | 6 | using System.Diagnostics; |
|
7 | 7 | using System.Collections; |
|
8 | 8 | |
|
9 | #if NET_4_5 | |
|
10 | using System.Threading.Tasks; | |
|
11 | #endif | |
|
12 | ||
|
9 | 13 | namespace Implab |
|
10 | 14 | { |
|
11 | 15 | public static class Safe |
|
12 | 16 | { |
|
13 | 17 | public static void ArgumentAssert(bool condition, string paramName) { |
|
14 | 18 | if (!condition) |
|
15 | 19 | throw new ArgumentException("The parameter is invalid", paramName); |
|
16 | 20 | } |
|
17 | 21 | |
|
18 | 22 | public static void ArgumentMatch(string value, string paramName, Regex rx) { |
|
19 | 23 | if (rx == null) |
|
20 | 24 | throw new ArgumentNullException("rx"); |
|
21 | 25 | if (!rx.IsMatch(value)) |
|
22 | 26 | throw new ArgumentException(String.Format("The prameter value must match {0}", rx), paramName); |
|
23 | 27 | } |
|
24 | 28 | |
|
25 | 29 | public static void ArgumentNotEmpty(string value, string paramName) { |
|
26 | 30 | if (String.IsNullOrEmpty(value)) |
|
27 | 31 | throw new ArgumentException("The parameter can't be empty", paramName); |
|
28 | 32 | } |
|
29 | 33 | |
|
30 | 34 | public static void ArgumentNotEmpty<T>(T[] value, string paramName) { |
|
31 | 35 | if (value == null || value.Length == 0) |
|
32 | 36 | throw new ArgumentException("The array must be not emty", paramName); |
|
33 | 37 | } |
|
34 | 38 | |
|
35 | 39 | public static void ArgumentNotNull(object value, string paramName) { |
|
36 | 40 | if (value == null) |
|
37 | 41 | throw new ArgumentNullException(paramName); |
|
38 | 42 | } |
|
39 | 43 | |
|
40 | 44 | public static void ArgumentInRange(int value, int min, int max, string paramName) { |
|
41 | 45 | if (value < min || value > max) |
|
42 | 46 | throw new ArgumentOutOfRangeException(paramName); |
|
43 | 47 | } |
|
44 | 48 | |
|
45 | 49 | public static void ArgumentOfType(object value, Type type, string paramName) { |
|
46 | 50 | if (!type.IsInstanceOfType(value)) |
|
47 | 51 | throw new ArgumentException(String.Format("The parameter must be of type {0}", type), paramName); |
|
48 | 52 | } |
|
49 | 53 | |
|
50 | 54 | public static void Dispose(params IDisposable[] objects) { |
|
51 | 55 | foreach (var d in objects) |
|
52 | 56 | if (d != null) |
|
53 | 57 | d.Dispose(); |
|
54 | 58 | } |
|
55 | 59 | |
|
56 | 60 | public static void Dispose(params object[] objects) { |
|
57 | 61 | foreach (var obj in objects) { |
|
58 | 62 | var d = obj as IDisposable; |
|
59 | 63 | if (d != null) |
|
60 | 64 | d.Dispose(); |
|
61 | 65 | } |
|
62 | 66 | } |
|
63 | 67 | |
|
64 | 68 | public static void Dispose(IEnumerable<IDisposable> objects) { |
|
65 | 69 | foreach (var d in objects) |
|
66 | 70 | if (d != null) |
|
67 | 71 | d.Dispose(); |
|
68 | 72 | } |
|
69 | 73 | |
|
70 | 74 | public static void Dispose(object obj) { |
|
71 | 75 | var d = obj as IDisposable; |
|
72 | 76 | if (d != null) |
|
73 | 77 | d.Dispose(); |
|
74 | 78 | } |
|
75 | 79 | |
|
76 | 80 | public static void DispatchEvent<T>(this EventHandler<T> handler, object sender, T args) { |
|
77 | 81 | if (handler != null) |
|
78 | 82 | handler(sender, args); |
|
79 | 83 | } |
|
80 | 84 | |
|
81 | 85 | public static void DispatchEvent(this EventHandler handler, object sender, EventArgs args) { |
|
82 | 86 | if (handler != null) |
|
83 | 87 | handler(sender, args); |
|
84 | 88 | } |
|
85 | 89 | |
|
86 | 90 | [DebuggerStepThrough] |
|
87 | 91 | public static IPromise<T> Run<T>(Func<T> action) { |
|
88 | 92 | ArgumentNotNull(action, "action"); |
|
89 | 93 | |
|
90 | 94 | try { |
|
91 | 95 | return Promise<T>.FromResult(action()); |
|
92 | 96 | } catch (Exception err) { |
|
93 | 97 | return Promise<T>.FromException(err); |
|
94 | 98 | } |
|
95 | 99 | } |
|
96 | 100 | |
|
97 | 101 | [DebuggerStepThrough] |
|
98 | 102 | public static IPromise Run(Action action) { |
|
99 | 103 | ArgumentNotNull(action, "action"); |
|
100 | 104 | |
|
101 | 105 | try { |
|
102 | 106 | action(); |
|
103 | 107 | return Promise.Success; |
|
104 | 108 | } catch (Exception err) { |
|
105 | 109 | return new FailedPromise(err); |
|
106 | 110 | } |
|
107 | 111 | } |
|
108 | 112 | |
|
109 | 113 | [DebuggerStepThrough] |
|
110 | 114 | public static IPromise Run(Func<IPromise> action) { |
|
111 | 115 | ArgumentNotNull(action, "action"); |
|
112 | 116 | |
|
113 | 117 | try { |
|
114 | 118 | return action() ?? new FailedPromise(new Exception("The action returned null")); |
|
115 | 119 | } catch (Exception err) { |
|
116 | 120 | return new FailedPromise(err); |
|
117 | 121 | } |
|
118 | 122 | } |
|
119 | 123 | |
|
124 | public static void NoWait(IPromise promise) { | |
|
125 | } | |
|
126 | ||
|
120 | 127 | [DebuggerStepThrough] |
|
121 | 128 | public static IPromise<T> Run<T>(Func<IPromise<T>> action) { |
|
122 | 129 | ArgumentNotNull(action, "action"); |
|
123 | 130 | |
|
124 | 131 | try { |
|
125 | 132 | return action() ?? Promise<T>.FromException(new Exception("The action returned null")); |
|
126 | 133 | } catch (Exception err) { |
|
127 | 134 | return Promise<T>.FromException(err); |
|
128 | 135 | } |
|
129 | 136 | } |
|
130 | 137 | |
|
138 | #if NET_4_5 | |
|
139 | public static void NoWait(Task t) { | |
|
140 | } | |
|
141 | #endif | |
|
142 | ||
|
131 | 143 | } |
|
132 | 144 | } |
General Comments 3
ok, latest stable version should be in default
You need to be logged in to leave comments.
Login now