##// END OF EJS Templates
Minor code changes
cin -
r213:9ee78a345738 v2
parent child
Show More
@@ -1,105 +1,103
1 using Implab.Diagnostics;
1 using Implab.Diagnostics;
2 using System;
2 using System;
3 using System.Diagnostics.CodeAnalysis;
3 using System.Diagnostics.CodeAnalysis;
4 using System.Threading;
4 using System.Threading;
5
5
6 namespace Implab.Components {
6 namespace Implab.Components {
7 /// <summary>
7 /// <summary>
8 /// Base class the objects which support disposing.
8 /// Base class the objects which support disposing.
9 /// </summary>
9 /// </summary>
10 public class Disposable : IDisposable {
10 public class Disposable : IDisposable {
11
11
12 int m_disposed;
12 int m_disposed;
13
13
14 public event EventHandler Disposed;
14 public event EventHandler Disposed;
15
15
16 public bool IsDisposed {
16 public bool IsDisposed {
17 get {
17 get {
18 Thread.MemoryBarrier();
18 Thread.MemoryBarrier();
19 return m_disposed != 0;
19 return m_disposed != 0;
20 }
20 }
21 }
21 }
22
22
23 /// <summary>
23 /// <summary>
24 /// Asserts the object is not disposed.
24 /// Asserts the object is not disposed.
25 /// </summary>
25 /// </summary>
26 /// <exception cref="ObjectDisposedException">The object is disposed</exception>
26 /// <exception cref="ObjectDisposedException">The object is disposed</exception>
27 /// <remarks>
27 /// <remarks>
28 /// УспСшная ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° Ρ‚ΠΎΠ³ΠΎ, Ρ‡Ρ‚ΠΎ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ Π½Π΅ освобоТдСн Π΅Ρ‰Π΅ Π½Π΅ Π³Π°Ρ€Π°Π½Ρ‚ΠΈΡ€ΡƒΠ΅Ρ‚, Ρ‡Ρ‚ΠΎ ΠΎΠ½ Π½Π΅
28 /// УспСшная ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° Ρ‚ΠΎΠ³ΠΎ, Ρ‡Ρ‚ΠΎ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ Π½Π΅ освобоТдСн Π΅Ρ‰Π΅ Π½Π΅ Π³Π°Ρ€Π°Π½Ρ‚ΠΈΡ€ΡƒΠ΅Ρ‚, Ρ‡Ρ‚ΠΎ ΠΎΠ½ Π½Π΅
29 /// Π±ΡƒΠ΄Π΅Ρ‚ освобоТдСн сразу послС Π½Π΅Π΅, поэтому ΠΌΠ΅Ρ‚ΠΎΠ΄Ρ‹ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΡŽΡ‰ΠΈΠ΅ ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΡƒ Π΄ΠΎΠ»ΠΆΠ½Ρ‹
29 /// Π±ΡƒΠ΄Π΅Ρ‚ освобоТдСн сразу послС Π½Π΅Π΅, поэтому ΠΌΠ΅Ρ‚ΠΎΠ΄Ρ‹ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΡŽΡ‰ΠΈΠ΅ ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΡƒ Π΄ΠΎΠ»ΠΆΠ½Ρ‹
30 /// ΡƒΡ‡ΠΈΡ‚Ρ‹Π²Π°Ρ‚ΡŒ, Ρ‡Ρ‚ΠΎ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ ΠΌΠΎΠΆΠ΅Ρ‚ Π±Ρ‹Ρ‚ΡŒ освобоТдСн ΠΈΠ· ΠΏΠ°Ρ€Π°Π»Π»Π΅Π»ΡŒΠ½ΠΎΠ³ΠΎ ΠΏΠΎΡ‚ΠΎΠΊΠ°.
30 /// ΡƒΡ‡ΠΈΡ‚Ρ‹Π²Π°Ρ‚ΡŒ, Ρ‡Ρ‚ΠΎ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ ΠΌΠΎΠΆΠ΅Ρ‚ Π±Ρ‹Ρ‚ΡŒ освобоТдСн ΠΈΠ· ΠΏΠ°Ρ€Π°Π»Π»Π΅Π»ΡŒΠ½ΠΎΠ³ΠΎ ΠΏΠΎΡ‚ΠΎΠΊΠ°.
31 /// Π”Π°Π½Π½Ρ‹ ΠΌΠ΅Ρ‚ΠΎΠ΄ слуТит для упрощСния ΠΎΡ‚Π»Π°Π΄ΠΊΠΈ ошибок ΠΏΡ€ΠΈ использовании ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π° послС Π΅Π³ΠΎ
31 /// Π”Π°Π½Π½Ρ‹ ΠΌΠ΅Ρ‚ΠΎΠ΄ слуТит для упрощСния ΠΎΡ‚Π»Π°Π΄ΠΊΠΈ ошибок ΠΏΡ€ΠΈ использовании ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π° послС Π΅Π³ΠΎ
32 /// освобоТдСния.
32 /// освобоТдСния.
33 /// </remarks>
33 /// </remarks>
34 /// <example>
34 /// <example>
35 /// // ΠΏΡ€ΠΈΠΌΠ΅Ρ€ синхронизированного освобоТдСния рСсурсов
35 /// // ΠΏΡ€ΠΈΠΌΠ΅Ρ€ синхронизированного освобоТдСния рСсурсов
36 /// class FileStore : Disposable {
36 /// class FileStore : Disposable {
37 /// readonly TextWriter m_file;
37 /// readonly TextWriter m_file;
38 /// readonly obejct m_sync = new object();
38 /// readonly obejct m_sync = new object();
39 ///
39 ///
40 /// public FileStore(string file) {
40 /// public FileStore(string file) {
41 /// m_file = new TextWriter(File.OpenWrite(file));
41 /// m_file = new TextWriter(File.OpenWrite(file));
42 /// }
42 /// }
43 ///
43 ///
44 /// public void Write(string text) {
44 /// public void Write(string text) {
45 /// lock(m_sync) {
45 /// lock(m_sync) {
46 /// AssertNotDisposed();
46 /// AssertNotDisposed();
47 /// m_file.Write(text);
47 /// m_file.Write(text);
48 /// }
48 /// }
49 /// }
49 /// }
50 ///
50 ///
51 /// protected override void Dispose(bool disposing) {
51 /// protected override void Dispose(bool disposing) {
52 /// if (disposing)
52 /// if (disposing)
53 /// lock(m_sync) {
53 /// lock(m_sync) {
54 /// m_file.Dipose();
54 /// m_file.Dipose();
55 /// base.Dispose(true);
55 /// base.Dispose(true);
56 /// }
56 /// }
57 /// else
57 /// else
58 /// base.Dispose(false);
58 /// base.Dispose(false);
59 /// }
59 /// }
60 /// }
60 /// }
61 /// <example>
61 /// <example>
62 protected void AssertNotDisposed() {
62 protected void AssertNotDisposed() {
63 Thread.MemoryBarrier();
63 Thread.MemoryBarrier();
64 if (m_disposed != 0)
64 if (m_disposed != 0)
65 throw new ObjectDisposedException(ToString());
65 throw new ObjectDisposedException(ToString());
66 }
66 }
67 /// <summary>
67 /// <summary>
68 /// Π’Ρ‹Π·Ρ‹Π²Π°Π΅Ρ‚ событиС <see cref="Disposed"/>
68 /// Π’Ρ‹Π·Ρ‹Π²Π°Π΅Ρ‚ событиС <see cref="Disposed"/>
69 /// </summary>
69 /// </summary>
70 /// <param name="disposing">ΠŸΡ€ΠΈΠ·Π½Π°ΠΊ Ρ‚ΠΎΠ³ΠΎ, Ρ‡Ρ‚ΠΎ Π½ΡƒΠΆΠ½ΠΎ ΠΎΡΠ²ΠΎΠ±ΠΎΠ΄ΠΈΡ‚ΡŒ рСсурсы, ΠΈΠ½Π°Ρ‡Π΅ Π΄Π°Π½Π½Ρ‹ΠΉ ΠΌΠ΅Ρ‚ΠΎΠ΄
70 /// <param name="disposing">ΠŸΡ€ΠΈΠ·Π½Π°ΠΊ Ρ‚ΠΎΠ³ΠΎ, Ρ‡Ρ‚ΠΎ Π½ΡƒΠΆΠ½ΠΎ ΠΎΡΠ²ΠΎΠ±ΠΎΠ΄ΠΈΡ‚ΡŒ рСсурсы, ΠΈΠ½Π°Ρ‡Π΅ Π΄Π°Π½Π½Ρ‹ΠΉ ΠΌΠ΅Ρ‚ΠΎΠ΄
71 /// Π²Ρ‹Π·Π²Π°Π½ сборщиком мусора ΠΈ Π½ΡƒΠΆΠ½ΠΎ ΠΎΡΠ²ΠΎΠ±ΠΎΠΆΠ΄Π°Ρ‚ΡŒ Π’ΠžΠ›Π¬ΠšΠž нСуправляСмыС рСсурсы Π’ΠžΠ›Π¬ΠšΠž этого
71 /// Π²Ρ‹Π·Π²Π°Π½ сборщиком мусора ΠΈ Π½ΡƒΠΆΠ½ΠΎ ΠΎΡΠ²ΠΎΠ±ΠΎΠΆΠ΄Π°Ρ‚ΡŒ Π’ΠžΠ›Π¬ΠšΠž нСуправляСмыС рСсурсы Π’ΠžΠ›Π¬ΠšΠž этого
72 /// ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π°.</param>
72 /// ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π°.</param>
73 /// <remarks>
73 /// <remarks>
74 /// Π”Π°Π½Π½Ρ‹ΠΉ ΠΌΠ΅Ρ‚ΠΎΠ΄ вызываСтся Π³Π°Ρ€Π°Π½Ρ‚ΠΈΡ€ΠΎΠ²Π°Π½Π½ΠΎ ΠΎΠ΄ΠΈΠ½ Ρ€Π°Π· Π΄Π°ΠΆΠ΅ ΠΏΡ€ΠΈ ΠΎΠ΄Π½ΠΎΠ²Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΌ Π²Ρ‹Π·ΠΎΠ²Π΅ <see cref="Dispose()"/>
74 /// Π”Π°Π½Π½Ρ‹ΠΉ ΠΌΠ΅Ρ‚ΠΎΠ΄ вызываСтся Π³Π°Ρ€Π°Π½Ρ‚ΠΈΡ€ΠΎΠ²Π°Π½Π½ΠΎ ΠΎΠ΄ΠΈΠ½ Ρ€Π°Π· Π΄Π°ΠΆΠ΅ ΠΏΡ€ΠΈ ΠΎΠ΄Π½ΠΎΠ²Ρ€Π΅ΠΌΠ΅Π½Π½ΠΎΠΌ Π²Ρ‹Π·ΠΎΠ²Π΅ <see cref="Dispose()"/>
75 /// ΠΈΠ· Π½Π΅ΡΠΊΠΎΠ»ΡŒΠΊΠΈΡ… ΠΏΠΎΡ‚ΠΎΠΊΠΎΠ².
75 /// ΠΈΠ· Π½Π΅ΡΠΊΠΎΠ»ΡŒΠΊΠΈΡ… ΠΏΠΎΡ‚ΠΎΠΊΠΎΠ².
76 /// </remarks>
76 /// </remarks>
77 protected virtual void Dispose(bool disposing) {
77 protected virtual void Dispose(bool disposing) {
78 if (disposing) {
78 if (disposing)
79 EventHandler temp = Disposed;
79 Disposed.DispatchEvent(this, EventArgs.Empty);
80 if (temp != null)
80
81 temp(this, EventArgs.Empty);
82 }
83 }
81 }
84
82
85 [SuppressMessage("Microsoft.Design", "CA1063:ImplementIDisposableCorrectly", Justification = "Dipose(bool) and GC.SuppessFinalize are called")]
83 [SuppressMessage("Microsoft.Design", "CA1063:ImplementIDisposableCorrectly", Justification = "Dipose(bool) and GC.SuppessFinalize are called")]
86 public void Dispose() {
84 public void Dispose() {
87 if (Interlocked.Increment(ref m_disposed) == 1) {
85 if (Interlocked.Increment(ref m_disposed) == 1) {
88 Dispose(true);
86 Dispose(true);
89 GC.SuppressFinalize(this);
87 GC.SuppressFinalize(this);
90 }
88 }
91 }
89 }
92
90
93 /// <summary>
91 /// <summary>
94 /// ЗаписываСт сообщСниС ΠΎΠ± ΡƒΡ‚Π΅Ρ‡ΠΊΠ΅ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π°.
92 /// ЗаписываСт сообщСниС ΠΎΠ± ΡƒΡ‚Π΅Ρ‡ΠΊΠ΅ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π°.
95 /// </summary>
93 /// </summary>
96 protected virtual void ReportObjectLeaks() {
94 protected virtual void ReportObjectLeaks() {
97 TraceLog.TraceWarning("The object is marked as disposable but isn't disposed properly: {0}", this);
95 TraceLog.TraceWarning("The object is marked as disposable but isn't disposed properly: {0}", this);
98 }
96 }
99
97
100 ~Disposable() {
98 ~Disposable() {
101 Dispose(false);
99 Dispose(false);
102 ReportObjectLeaks();
100 ReportObjectLeaks();
103 }
101 }
104 }
102 }
105 } No newline at end of file
103 }
@@ -1,132 +1,144
1 using System;
1 using System;
2 using System.Collections.Generic;
2 using System.Collections.Generic;
3 using System.Linq;
3 using System.Linq;
4 using System.Text;
4 using System.Text;
5 using System.Text.RegularExpressions;
5 using System.Text.RegularExpressions;
6 using System.Diagnostics;
6 using System.Diagnostics;
7 using System.Collections;
7 using System.Collections;
8
8
9 #if NET_4_5
10 using System.Threading.Tasks;
11 #endif
12
9 namespace Implab
13 namespace Implab
10 {
14 {
11 public static class Safe
15 public static class Safe
12 {
16 {
13 public static void ArgumentAssert(bool condition, string paramName) {
17 public static void ArgumentAssert(bool condition, string paramName) {
14 if (!condition)
18 if (!condition)
15 throw new ArgumentException("The parameter is invalid", paramName);
19 throw new ArgumentException("The parameter is invalid", paramName);
16 }
20 }
17
21
18 public static void ArgumentMatch(string value, string paramName, Regex rx) {
22 public static void ArgumentMatch(string value, string paramName, Regex rx) {
19 if (rx == null)
23 if (rx == null)
20 throw new ArgumentNullException("rx");
24 throw new ArgumentNullException("rx");
21 if (!rx.IsMatch(value))
25 if (!rx.IsMatch(value))
22 throw new ArgumentException(String.Format("The prameter value must match {0}", rx), paramName);
26 throw new ArgumentException(String.Format("The prameter value must match {0}", rx), paramName);
23 }
27 }
24
28
25 public static void ArgumentNotEmpty(string value, string paramName) {
29 public static void ArgumentNotEmpty(string value, string paramName) {
26 if (String.IsNullOrEmpty(value))
30 if (String.IsNullOrEmpty(value))
27 throw new ArgumentException("The parameter can't be empty", paramName);
31 throw new ArgumentException("The parameter can't be empty", paramName);
28 }
32 }
29
33
30 public static void ArgumentNotEmpty<T>(T[] value, string paramName) {
34 public static void ArgumentNotEmpty<T>(T[] value, string paramName) {
31 if (value == null || value.Length == 0)
35 if (value == null || value.Length == 0)
32 throw new ArgumentException("The array must be not emty", paramName);
36 throw new ArgumentException("The array must be not emty", paramName);
33 }
37 }
34
38
35 public static void ArgumentNotNull(object value, string paramName) {
39 public static void ArgumentNotNull(object value, string paramName) {
36 if (value == null)
40 if (value == null)
37 throw new ArgumentNullException(paramName);
41 throw new ArgumentNullException(paramName);
38 }
42 }
39
43
40 public static void ArgumentInRange(int value, int min, int max, string paramName) {
44 public static void ArgumentInRange(int value, int min, int max, string paramName) {
41 if (value < min || value > max)
45 if (value < min || value > max)
42 throw new ArgumentOutOfRangeException(paramName);
46 throw new ArgumentOutOfRangeException(paramName);
43 }
47 }
44
48
45 public static void ArgumentOfType(object value, Type type, string paramName) {
49 public static void ArgumentOfType(object value, Type type, string paramName) {
46 if (!type.IsInstanceOfType(value))
50 if (!type.IsInstanceOfType(value))
47 throw new ArgumentException(String.Format("The parameter must be of type {0}", type), paramName);
51 throw new ArgumentException(String.Format("The parameter must be of type {0}", type), paramName);
48 }
52 }
49
53
50 public static void Dispose(params IDisposable[] objects) {
54 public static void Dispose(params IDisposable[] objects) {
51 foreach (var d in objects)
55 foreach (var d in objects)
52 if (d != null)
56 if (d != null)
53 d.Dispose();
57 d.Dispose();
54 }
58 }
55
59
56 public static void Dispose(params object[] objects) {
60 public static void Dispose(params object[] objects) {
57 foreach (var obj in objects) {
61 foreach (var obj in objects) {
58 var d = obj as IDisposable;
62 var d = obj as IDisposable;
59 if (d != null)
63 if (d != null)
60 d.Dispose();
64 d.Dispose();
61 }
65 }
62 }
66 }
63
67
64 public static void Dispose(IEnumerable<IDisposable> objects) {
68 public static void Dispose(IEnumerable<IDisposable> objects) {
65 foreach (var d in objects)
69 foreach (var d in objects)
66 if (d != null)
70 if (d != null)
67 d.Dispose();
71 d.Dispose();
68 }
72 }
69
73
70 public static void Dispose(object obj) {
74 public static void Dispose(object obj) {
71 var d = obj as IDisposable;
75 var d = obj as IDisposable;
72 if (d != null)
76 if (d != null)
73 d.Dispose();
77 d.Dispose();
74 }
78 }
75
79
76 public static void DispatchEvent<T>(this EventHandler<T> handler, object sender, T args) {
80 public static void DispatchEvent<T>(this EventHandler<T> handler, object sender, T args) {
77 if (handler != null)
81 if (handler != null)
78 handler(sender, args);
82 handler(sender, args);
79 }
83 }
80
84
81 public static void DispatchEvent(this EventHandler handler, object sender, EventArgs args) {
85 public static void DispatchEvent(this EventHandler handler, object sender, EventArgs args) {
82 if (handler != null)
86 if (handler != null)
83 handler(sender, args);
87 handler(sender, args);
84 }
88 }
85
89
86 [DebuggerStepThrough]
90 [DebuggerStepThrough]
87 public static IPromise<T> Run<T>(Func<T> action) {
91 public static IPromise<T> Run<T>(Func<T> action) {
88 ArgumentNotNull(action, "action");
92 ArgumentNotNull(action, "action");
89
93
90 try {
94 try {
91 return Promise<T>.FromResult(action());
95 return Promise<T>.FromResult(action());
92 } catch (Exception err) {
96 } catch (Exception err) {
93 return Promise<T>.FromException(err);
97 return Promise<T>.FromException(err);
94 }
98 }
95 }
99 }
96
100
97 [DebuggerStepThrough]
101 [DebuggerStepThrough]
98 public static IPromise Run(Action action) {
102 public static IPromise Run(Action action) {
99 ArgumentNotNull(action, "action");
103 ArgumentNotNull(action, "action");
100
104
101 try {
105 try {
102 action();
106 action();
103 return Promise.Success;
107 return Promise.Success;
104 } catch (Exception err) {
108 } catch (Exception err) {
105 return new FailedPromise(err);
109 return new FailedPromise(err);
106 }
110 }
107 }
111 }
108
112
109 [DebuggerStepThrough]
113 [DebuggerStepThrough]
110 public static IPromise Run(Func<IPromise> action) {
114 public static IPromise Run(Func<IPromise> action) {
111 ArgumentNotNull(action, "action");
115 ArgumentNotNull(action, "action");
112
116
113 try {
117 try {
114 return action() ?? new FailedPromise(new Exception("The action returned null"));
118 return action() ?? new FailedPromise(new Exception("The action returned null"));
115 } catch (Exception err) {
119 } catch (Exception err) {
116 return new FailedPromise(err);
120 return new FailedPromise(err);
117 }
121 }
118 }
122 }
119
123
124 public static void NoWait(IPromise promise) {
125 }
126
120 [DebuggerStepThrough]
127 [DebuggerStepThrough]
121 public static IPromise<T> Run<T>(Func<IPromise<T>> action) {
128 public static IPromise<T> Run<T>(Func<IPromise<T>> action) {
122 ArgumentNotNull(action, "action");
129 ArgumentNotNull(action, "action");
123
130
124 try {
131 try {
125 return action() ?? Promise<T>.FromException(new Exception("The action returned null"));
132 return action() ?? Promise<T>.FromException(new Exception("The action returned null"));
126 } catch (Exception err) {
133 } catch (Exception err) {
127 return Promise<T>.FromException(err);
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
Under Review
author

Auto status change to "Under Review"

Approved
author

ok, latest stable version should be in default

You need to be logged in to leave comments. Login now