Mercurial > pub > ImplabNet
annotate Implab/Safe.cs @ 130:671f60cd0250 v2
fixed Resove method bug when calling it on already cancelled promise
author | cin |
---|---|
date | Fri, 30 Jan 2015 17:07:17 +0300 |
parents | 6241bff0cd64 |
children | b5c2d609d71b |
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 { | |
121 | 12 public static void ArgumentMatch(string value, string paramName, Regex rx) { |
51 | 13 if (rx == null) |
14 throw new ArgumentNullException("rx"); | |
121 | 15 if (!rx.IsMatch(value)) |
16 throw new ArgumentException(String.Format("The prameter value must match {0}", rx), paramName); | |
51 | 17 } |
18 | |
121 | 19 public static void ArgumentNotEmpty(string value, string paramName) { |
20 if (String.IsNullOrEmpty(value)) | |
21 throw new ArgumentException("The parameter can't be empty", paramName); | |
90 | 22 } |
23 | |
121 | 24 public static void ArgumentNotEmpty<T>(T[] value, string paramName) { |
25 if (value == null || value.Length == 0) | |
26 throw new ArgumentException("The array must be not emty", paramName); | |
51 | 27 } |
28 | |
121 | 29 public static void ArgumentNotNull(object value, string paramName) { |
30 if (value == null) | |
31 throw new ArgumentNullException(paramName); | |
51 | 32 } |
33 | |
121 | 34 public static void ArgumentInRange(int value, int min, int max, string paramName) { |
35 if (value < min || value > max) | |
36 throw new ArgumentOutOfRangeException(paramName); | |
55 | 37 } |
38 | |
128
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
39 public static void Dispose(params IDisposable[] objects) { |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
40 foreach (var d in objects) |
126 | 41 if (d != null) |
42 d.Dispose(); | |
1 | 43 } |
66 | 44 |
128
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
45 public static void Dispose(params object[] objects) { |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
46 foreach (var obj in objects) { |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
47 var d = obj as IDisposable; |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
48 if (d != null) |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
49 d.Dispose(); |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
50 } |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
51 } |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
52 |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
53 public static void Dispose(object obj) { |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
54 var d = obj as IDisposable; |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
55 if (d != null) |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
56 d.Dispose(); |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
57 } |
6241bff0cd64
Added Signal class a lightweight alternative to ManualResetEvent
cin
parents:
126
diff
changeset
|
58 |
66 | 59 [DebuggerStepThrough] |
94 | 60 public static IPromise<T> InvokePromise<T>(Func<T> action) { |
66 | 61 ArgumentNotNull(action, "action"); |
62 | |
63 var p = new Promise<T>(); | |
64 try { | |
65 p.Resolve(action()); | |
66 } catch (Exception err) { | |
67 p.Reject(err); | |
68 } | |
69 | |
70 return p; | |
71 } | |
72 | |
73 [DebuggerStepThrough] | |
96 | 74 public static IPromise InvokePromise(Action action) { |
95 | 75 ArgumentNotNull(action, "action"); |
76 | |
119
2573b562e328
Promises rewritten, added improved version of AsyncQueue
cin
parents:
96
diff
changeset
|
77 var p = new Promise(); |
95 | 78 try { |
79 action(); | |
80 p.Resolve(); | |
81 } catch (Exception err) { | |
82 p.Reject(err); | |
83 } | |
84 | |
85 return p; | |
86 } | |
87 | |
88 [DebuggerStepThrough] | |
94 | 89 public static IPromise<T> InvokePromise<T>(Func<IPromise<T>> action) { |
66 | 90 ArgumentNotNull(action, "action"); |
91 | |
92 try { | |
94 | 93 return action() ?? Promise<T>.ExceptionToPromise(new Exception("The action returned null")); |
66 | 94 } catch (Exception err) { |
95 return Promise<T>.ExceptionToPromise(err); | |
96 } | |
97 } | |
1 | 98 } |
99 } |