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