Mercurial > pub > ImplabNet
annotate Implab/Safe.cs @ 273:79110a16cab7 v3
Working on Unity xml configuration: Refactoring in progress
author | cin |
---|---|
date | Thu, 26 Apr 2018 19:35:01 +0300 |
parents | 9d1cca834b05 |
children |
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 | |
272 | 58 public static object CreateDefaultValue(Type type) { |
59 if (type.IsValueType) | |
60 return Activator.CreateInstance(type); | |
61 | |
62 return null; | |
63 } | |
64 | |
228 | 65 [MethodImpl(MethodImplOptions.AggressiveInlining)] |
251 | 66 public static void ArgumentInRange(bool condition, string paramName) { |
67 if (!condition) | |
121 | 68 throw new ArgumentOutOfRangeException(paramName); |
55 | 69 } |
70 | |
228 | 71 [MethodImpl(MethodImplOptions.AggressiveInlining)] |
177 | 72 public static void ArgumentOfType(object value, Type type, string paramName) { |
73 if (!type.IsInstanceOfType(value)) | |
74 throw new ArgumentException(String.Format("The parameter must be of type {0}", type), paramName); | |
75 } | |
76 | |
128
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
77 public static void Dispose(params IDisposable[] objects) { |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
78 foreach (var d in objects) |
126 | 79 if (d != null) |
80 d.Dispose(); | |
1 | 81 } |
66 | 82 |
128
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
83 public static void Dispose(params object[] objects) { |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
84 foreach (var obj in objects) { |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
85 var d = obj as IDisposable; |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
86 if (d != null) |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
87 d.Dispose(); |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
88 } |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
89 } |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
90 |
215
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
213
diff
changeset
|
91 public static void DisposeCollection(IEnumerable<IDisposable> objects) { |
207
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
92 foreach (var d in objects) |
215
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
213
diff
changeset
|
93 Dispose(d); |
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
213
diff
changeset
|
94 } |
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
213
diff
changeset
|
95 |
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
213
diff
changeset
|
96 public static void DisposeCollection(IEnumerable objects) { |
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
213
diff
changeset
|
97 foreach (var d in objects) |
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
213
diff
changeset
|
98 Dispose(d); |
207
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
99 } |
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
100 |
128
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
101 public static void Dispose(object obj) { |
238 | 102 if (obj is IDisposable) |
215
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
213
diff
changeset
|
103 Dispose((IDisposable)obj); |
238 | 104 |
128
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
105 } |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
106 |
215
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
213
diff
changeset
|
107 [DebuggerStepThrough] |
207
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
108 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
|
109 if (handler != null) |
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
110 handler(sender, args); |
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
111 } |
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
112 |
215
fe5101083150
Fixed InteractiveListener to support OLE and clipboard.
cin
parents:
213
diff
changeset
|
113 [DebuggerStepThrough] |
207
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
114 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
|
115 if (handler != null) |
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
116 handler(sender, args); |
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
117 } |
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
118 |
66 | 119 [DebuggerStepThrough] |
203
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
120 public static IPromise<T> Run<T>(Func<T> action) { |
131 | 121 ArgumentNotNull(action, "action"); |
122 | |
123 try { | |
248 | 124 return Promise.Resolve(action()); |
131 | 125 } catch (Exception err) { |
248 | 126 return Promise.Reject<T>(err); |
131 | 127 } |
128 } | |
129 | |
130 [DebuggerStepThrough] | |
203
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
131 public static IPromise Run(Action action) { |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
132 ArgumentNotNull(action, "action"); |
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 try { |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
135 action(); |
248 | 136 return Promise.Resolve(); |
203
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
137 } catch (Exception err) { |
248 | 138 return Promise.Reject(err); |
203
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
139 } |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
140 } |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
141 |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
142 [DebuggerStepThrough] |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
143 public static IPromise Run(Func<IPromise> action) { |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
144 ArgumentNotNull(action, "action"); |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
145 |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
146 try { |
248 | 147 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
|
148 } catch (Exception err) { |
248 | 149 return Promise.Reject(err); |
203
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
150 } |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
151 } |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
152 |
213 | 153 public static void NoWait(IPromise promise) { |
154 } | |
155 | |
251 | 156 public static void NoWait(Task promise) { |
157 } | |
158 | |
159 public static void NoWait<T>(Task<T> promise) { | |
160 } | |
161 | |
259 | 162 public static void Noop() { |
163 } | |
164 | |
165 public static void Noop(CancellationToken ct) { | |
166 ct.ThrowIfCancellationRequested(); | |
167 } | |
168 | |
169 public static Task CreateTask() { | |
170 return new Task(Noop); | |
171 } | |
172 | |
173 public static Task CreateTask(CancellationToken ct) { | |
174 return new Task(Noop, ct); | |
175 } | |
176 | |
203
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
177 [DebuggerStepThrough] |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
178 public static IPromise<T> Run<T>(Func<IPromise<T>> action) { |
66 | 179 ArgumentNotNull(action, "action"); |
180 | |
181 try { | |
248 | 182 return action() ?? Promise.Reject<T>(new Exception("The action returned null")); |
66 | 183 } catch (Exception err) { |
248 | 184 return Promise.Reject<T>(err); |
66 | 185 } |
186 } | |
209 | 187 |
1 | 188 } |
189 } |