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