Mercurial > pub > ImplabNet
annotate Implab/Safe.cs @ 210:5dc21f6a3222 v2
Code review for RunnableComponent
Added StaApartment class based on System.Windows.Forms.Application message loop
author | cin |
---|---|
date | Mon, 20 Mar 2017 17:44:18 +0300 |
parents | a867536c68fc |
children | 9ee78a345738 |
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 |
9 namespace Implab | |
10 { | |
11 public static class Safe | |
12 { | |
161 | 13 public static void ArgumentAssert(bool condition, string paramName) { |
14 if (!condition) | |
15 throw new ArgumentException("The parameter is invalid", paramName); | |
16 } | |
17 | |
121 | 18 public static void ArgumentMatch(string value, string paramName, Regex rx) { |
51 | 19 if (rx == null) |
20 throw new ArgumentNullException("rx"); | |
121 | 21 if (!rx.IsMatch(value)) |
22 throw new ArgumentException(String.Format("The prameter value must match {0}", rx), paramName); | |
51 | 23 } |
24 | |
121 | 25 public static void ArgumentNotEmpty(string value, string paramName) { |
26 if (String.IsNullOrEmpty(value)) | |
27 throw new ArgumentException("The parameter can't be empty", paramName); | |
90 | 28 } |
29 | |
121 | 30 public static void ArgumentNotEmpty<T>(T[] value, string paramName) { |
31 if (value == null || value.Length == 0) | |
32 throw new ArgumentException("The array must be not emty", paramName); | |
51 | 33 } |
34 | |
121 | 35 public static void ArgumentNotNull(object value, string paramName) { |
36 if (value == null) | |
37 throw new ArgumentNullException(paramName); | |
51 | 38 } |
39 | |
121 | 40 public static void ArgumentInRange(int value, int min, int max, string paramName) { |
41 if (value < min || value > max) | |
42 throw new ArgumentOutOfRangeException(paramName); | |
55 | 43 } |
44 | |
177 | 45 public static void ArgumentOfType(object value, Type type, string paramName) { |
46 if (!type.IsInstanceOfType(value)) | |
47 throw new ArgumentException(String.Format("The parameter must be of type {0}", type), paramName); | |
48 } | |
49 | |
128
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
50 public static void Dispose(params IDisposable[] objects) { |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
51 foreach (var d in objects) |
126 | 52 if (d != null) |
53 d.Dispose(); | |
1 | 54 } |
66 | 55 |
128
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
56 public static void Dispose(params object[] objects) { |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
57 foreach (var obj in objects) { |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
58 var d = obj as IDisposable; |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
59 if (d != null) |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
60 d.Dispose(); |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
61 } |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
62 } |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
63 |
207
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
64 public static void Dispose(IEnumerable<IDisposable> objects) { |
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
65 foreach (var d in objects) |
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
66 if (d != null) |
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
67 d.Dispose(); |
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
68 } |
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
69 |
128
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
70 public static void Dispose(object obj) { |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
71 var d = obj as IDisposable; |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
72 if (d != null) |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
73 d.Dispose(); |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
74 } |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
75 |
207
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
76 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
|
77 if (handler != null) |
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
78 handler(sender, args); |
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
79 } |
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
80 |
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
81 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
|
82 if (handler != null) |
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
83 handler(sender, args); |
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
84 } |
558f34b2fb50
added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents:
205
diff
changeset
|
85 |
66 | 86 [DebuggerStepThrough] |
203
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
87 public static IPromise<T> Run<T>(Func<T> action) { |
131 | 88 ArgumentNotNull(action, "action"); |
89 | |
90 try { | |
203
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
91 return Promise<T>.FromResult(action()); |
131 | 92 } catch (Exception err) { |
203
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
93 return Promise<T>.FromException(err); |
131 | 94 } |
95 } | |
96 | |
97 [DebuggerStepThrough] | |
203
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
98 public static IPromise Run(Action action) { |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
99 ArgumentNotNull(action, "action"); |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
100 |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
101 try { |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
102 action(); |
205
8200ab154c8a
Added ResetState to RunnableComponent to reset in case of failure
cin
parents:
203
diff
changeset
|
103 return Promise.Success; |
203
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
104 } catch (Exception err) { |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
105 return new FailedPromise(err); |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
106 } |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
107 } |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
108 |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
109 [DebuggerStepThrough] |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
110 public static IPromise Run(Func<IPromise> 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 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
|
115 } catch (Exception err) { |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
116 return new FailedPromise(err); |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
117 } |
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 [DebuggerStepThrough] |
4d9830a9bbb8
Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents:
177
diff
changeset
|
121 public static IPromise<T> Run<T>(Func<IPromise<T>> action) { |
66 | 122 ArgumentNotNull(action, "action"); |
123 | |
124 try { | |
145 | 125 return action() ?? Promise<T>.FromException(new Exception("The action returned null")); |
66 | 126 } catch (Exception err) { |
145 | 127 return Promise<T>.FromException(err); |
66 | 128 } |
129 } | |
209 | 130 |
1 | 131 } |
132 } |