##// END OF EJS Templates
Minor code changes
cin -
r213:9ee78a345738 v2
parent child
Show More
@@ -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
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