Mercurial > pub > ImplabNet
annotate Implab/Safe.cs @ 260:547a2fc0d93e v3 v3.0.6
minor fixes
author | cin |
---|---|
date | Fri, 13 Apr 2018 19:14:59 +0300 |
parents | 7d52dc684bbd |
children | 9d1cca834b05 |
rev | line source |
---|---|
1 | 1 using System; |
2 using System.Collections.Generic; | |
3 using System.Linq; | |
4 using System.Text; | |
51 | 5 using System.Text.RegularExpressions; |
66 | 6 using System.Diagnostics; |
207
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
7 using System.Collections; |
228 | 8 using System.Runtime.CompilerServices; |
251 | 9 using System.Threading.Tasks; |
259 | 10 using System.Threading; |
1 | 11 |
213 | 12 #if NET_4_5 |
13 using System.Threading.Tasks; | |
14 #endif | |
15 | |
1 | 16 namespace Implab |
17 { | |
18 public static class Safe | |
19 { | |
228 | 20 [MethodImpl(MethodImplOptions.AggressiveInlining)] |
161 | 21 public static void ArgumentAssert(bool condition, string paramName) { |
22 if (!condition) | |
23 throw new ArgumentException("The parameter is invalid", paramName); | |
24 } | |
25 | |
228 | 26 [MethodImpl(MethodImplOptions.AggressiveInlining)] |
121 | 27 public static void ArgumentMatch(string value, string paramName, Regex rx) { |
51 | 28 if (rx == null) |
29 throw new ArgumentNullException("rx"); | |
121 | 30 if (!rx.IsMatch(value)) |
31 throw new ArgumentException(String.Format("The prameter value must match {0}", rx), paramName); | |
51 | 32 } |
33 | |
228 | 34 [MethodImpl(MethodImplOptions.AggressiveInlining)] |
121 | 35 public static void ArgumentNotEmpty(string value, string paramName) { |
36 if (String.IsNullOrEmpty(value)) | |
37 throw new ArgumentException("The parameter can't be empty", paramName); | |
90 | 38 } |
39 | |
228 | 40 [MethodImpl(MethodImplOptions.AggressiveInlining)] |
121 | 41 public static void ArgumentNotEmpty<T>(T[] value, string paramName) { |
42 if (value == null || value.Length == 0) | |
43 throw new ArgumentException("The array must be not emty", paramName); | |
51 | 44 } |
45 | |
228 | 46 [MethodImpl(MethodImplOptions.AggressiveInlining)] |
121 | 47 public static void ArgumentNotNull(object value, string paramName) { |
48 if (value == null) | |
49 throw new ArgumentNullException(paramName); | |
51 | 50 } |
51 | |
228 | 52 [MethodImpl(MethodImplOptions.AggressiveInlining)] |
251 | 53 internal static void ArgumentGreaterEqThan(int value, int min, string paramName) { |
228 | 54 if (value < min) |
55 throw new ArgumentOutOfRangeException(paramName); | |
56 } | |
57 | |
58 [MethodImpl(MethodImplOptions.AggressiveInlining)] | |
251 | 59 public static void ArgumentInRange(bool condition, string paramName) { |
60 if (!condition) | |
121 | 61 throw new ArgumentOutOfRangeException(paramName); |
55 | 62 } |
63 | |
228 | 64 [MethodImpl(MethodImplOptions.AggressiveInlining)] |
177 | 65 public static void ArgumentOfType(object value, Type type, string paramName) { |
66 if (!type.IsInstanceOfType(value)) | |
67 throw new ArgumentException(String.Format("The parameter must be of type {0}", type), paramName); | |
68 } | |
69 | |
128
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
70 public static void Dispose(params IDisposable[] objects) { |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
71 foreach (var d in objects) |
126 | 72 if (d != null) |
73 d.Dispose(); | |
1 | 74 } |
66 | 75 |
128
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
76 public static void Dispose(params object[] objects) { |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
77 foreach (var obj in objects) { |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
78 var d = obj as IDisposable; |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
79 if (d != null) |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
80 d.Dispose(); |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
81 } |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
82 } |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
83 |
215
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
213
diff
changeset
|
84 public static void DisposeCollection(IEnumerable<IDisposable> objects) { |
207
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
85 foreach (var d in objects) |
215
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
213
diff
changeset
|
86 Dispose(d); |
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
213
diff
changeset
|
87 } |
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
213
diff
changeset
|
88 |
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
213
diff
changeset
|
89 public static void DisposeCollection(IEnumerable objects) { |
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
213
diff
changeset
|
90 foreach (var d in objects) |
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
213
diff
changeset
|
91 Dispose(d); |
207
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
92 } |
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
93 |
128
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
94 public static void Dispose(object obj) { |
238 | 95 if (obj is IDisposable) |
215
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
213
diff
changeset
|
96 Dispose((IDisposable)obj); |
238 | 97 |
128
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
98 } |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
99 |
215
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
213
diff
changeset
|
100 [DebuggerStepThrough] |
207
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
101 public static void DispatchEvent<T>(this EventHandler<T> handler, object sender, T args) { |
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
102 if (handler != null) |
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
103 handler(sender, args); |
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
104 } |
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
105 |
215
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
213
diff
changeset
|
106 [DebuggerStepThrough] |
207
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
107 public static void DispatchEvent(this EventHandler handler, object sender, EventArgs args) { |
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
108 if (handler != null) |
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
109 handler(sender, args); |
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
110 } |
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
111 |
66 | 112 [DebuggerStepThrough] |
203
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
113 public static IPromise<T> Run<T>(Func<T> action) { |
131 | 114 ArgumentNotNull(action, "action"); |
115 | |
116 try { | |
248 | 117 return Promise.Resolve(action()); |
131 | 118 } catch (Exception err) { |
248 | 119 return Promise.Reject<T>(err); |
131 | 120 } |
121 } | |
122 | |
123 [DebuggerStepThrough] | |
203
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
124 public static IPromise Run(Action action) { |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
125 ArgumentNotNull(action, "action"); |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
126 |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
127 try { |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
128 action(); |
248 | 129 return Promise.Resolve(); |
203
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
130 } catch (Exception err) { |
248 | 131 return Promise.Reject(err); |
203
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
132 } |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
133 } |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
134 |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
135 [DebuggerStepThrough] |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
136 public static IPromise Run(Func<IPromise> action) { |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
137 ArgumentNotNull(action, "action"); |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
138 |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
139 try { |
248 | 140 return action() ?? Promise.Reject(new Exception("The action returned null")); |
203
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
141 } catch (Exception err) { |
248 | 142 return Promise.Reject(err); |
203
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
143 } |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
144 } |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
145 |
213 | 146 public static void NoWait(IPromise promise) { |
147 } | |
148 | |
251 | 149 public static void NoWait(Task promise) { |
150 } | |
151 | |
152 public static void NoWait<T>(Task<T> promise) { | |
153 } | |
154 | |
259 | 155 public static void Noop() { |
156 } | |
157 | |
158 public static void Noop(CancellationToken ct) { | |
159 ct.ThrowIfCancellationRequested(); | |
160 } | |
161 | |
162 public static Task CreateTask() { | |
163 return new Task(Noop); | |
164 } | |
165 | |
166 public static Task CreateTask(CancellationToken ct) { | |
167 return new Task(Noop, ct); | |
168 } | |
169 | |
203
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
170 [DebuggerStepThrough] |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
171 public static IPromise<T> Run<T>(Func<IPromise<T>> action) { |
66 | 172 ArgumentNotNull(action, "action"); |
173 | |
174 try { | |
248 | 175 return action() ?? Promise.Reject<T>(new Exception("The action returned null")); |
66 | 176 } catch (Exception err) { |
248 | 177 return Promise.Reject<T>(err); |
66 | 178 } |
179 } | |
209 | 180 |
1 | 181 } |
182 } |