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