Mercurial > pub > ImplabNet
annotate Implab/Safe.cs @ 223:27ea7f07e2e4 default
Слияние
author | cin |
---|---|
date | Tue, 22 Aug 2017 09:35:54 +0300 |
parents | a0ff6a0e9c44 |
children | 4d9830a9bbb8 |
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; |
1 | 7 |
8 namespace Implab | |
9 { | |
10 public static class Safe | |
11 { | |
161 | 12 public static void ArgumentAssert(bool condition, string paramName) { |
13 if (!condition) | |
14 throw new ArgumentException("The parameter is invalid", paramName); | |
15 } | |
16 | |
121 | 17 public static void ArgumentMatch(string value, string paramName, Regex rx) { |
51 | 18 if (rx == null) |
19 throw new ArgumentNullException("rx"); | |
121 | 20 if (!rx.IsMatch(value)) |
21 throw new ArgumentException(String.Format("The prameter value must match {0}", rx), paramName); | |
51 | 22 } |
23 | |
121 | 24 public static void ArgumentNotEmpty(string value, string paramName) { |
25 if (String.IsNullOrEmpty(value)) | |
26 throw new ArgumentException("The parameter can't be empty", paramName); | |
90 | 27 } |
28 | |
121 | 29 public static void ArgumentNotEmpty<T>(T[] value, string paramName) { |
30 if (value == null || value.Length == 0) | |
31 throw new ArgumentException("The array must be not emty", paramName); | |
51 | 32 } |
33 | |
121 | 34 public static void ArgumentNotNull(object value, string paramName) { |
35 if (value == null) | |
36 throw new ArgumentNullException(paramName); | |
51 | 37 } |
38 | |
121 | 39 public static void ArgumentInRange(int value, int min, int max, string paramName) { |
40 if (value < min || value > max) | |
41 throw new ArgumentOutOfRangeException(paramName); | |
55 | 42 } |
43 | |
177 | 44 public static void ArgumentOfType(object value, Type type, string paramName) { |
45 if (!type.IsInstanceOfType(value)) | |
46 throw new ArgumentException(String.Format("The parameter must be of type {0}", type), paramName); | |
47 } | |
48 | |
128
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
49 public static void Dispose(params IDisposable[] objects) { |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
50 foreach (var d in objects) |
126 | 51 if (d != null) |
52 d.Dispose(); | |
1 | 53 } |
66 | 54 |
128
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
55 public static void Dispose(params object[] objects) { |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
56 foreach (var obj in objects) { |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
57 var d = obj as IDisposable; |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
58 if (d != null) |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
59 d.Dispose(); |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
60 } |
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 public static void Dispose(object obj) { |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
64 var d = obj as IDisposable; |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
65 if (d != null) |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
66 d.Dispose(); |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
67 } |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
68 |
66 | 69 [DebuggerStepThrough] |
132 | 70 public static IPromise<T> WrapPromise<T>(Func<T> action) { |
66 | 71 ArgumentNotNull(action, "action"); |
72 | |
73 var p = new Promise<T>(); | |
74 try { | |
75 p.Resolve(action()); | |
76 } catch (Exception err) { | |
77 p.Reject(err); | |
78 } | |
79 | |
80 return p; | |
81 } | |
82 | |
83 [DebuggerStepThrough] | |
132 | 84 public static IPromise WrapPromise(Action action) { |
95 | 85 ArgumentNotNull(action, "action"); |
86 | |
119
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
96
diff
changeset
|
87 var p = new Promise(); |
95 | 88 try { |
89 action(); | |
90 p.Resolve(); | |
91 } catch (Exception err) { | |
92 p.Reject(err); | |
93 } | |
94 | |
95 return p; | |
96 } | |
97 | |
98 [DebuggerStepThrough] | |
131 | 99 public static IPromise InvokePromise(Func<IPromise> action) { |
100 ArgumentNotNull(action, "action"); | |
101 | |
102 try { | |
133 | 103 var p = action(); |
104 if (p == null) { | |
105 var d = new Promise(); | |
106 d.Reject(new Exception("The action returned null")); | |
107 p = d; | |
108 } | |
109 return p; | |
131 | 110 } catch (Exception err) { |
133 | 111 var p = new Promise(); |
131 | 112 p.Reject(err); |
133 | 113 return p; |
131 | 114 } |
115 } | |
116 | |
117 [DebuggerStepThrough] | |
94 | 118 public static IPromise<T> InvokePromise<T>(Func<IPromise<T>> action) { |
66 | 119 ArgumentNotNull(action, "action"); |
120 | |
121 try { | |
145 | 122 return action() ?? Promise<T>.FromException(new Exception("The action returned null")); |
66 | 123 } catch (Exception err) { |
145 | 124 return Promise<T>.FromException(err); |
66 | 125 } |
126 } | |
1 | 127 } |
128 } |