Mercurial > pub > ImplabNet
annotate Implab/Safe.cs @ 136:e9e7940c7d98 v2
shared locks + tests
author | cin |
---|---|
date | Mon, 16 Feb 2015 01:14:09 +0300 |
parents | 6c49d02a9a05 |
children | 706fccb85524 |
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] |
132 | 60 public static IPromise<T> WrapPromise<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] | |
132 | 74 public static IPromise WrapPromise(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] | |
131 | 89 public static IPromise InvokePromise(Func<IPromise> action) { |
90 ArgumentNotNull(action, "action"); | |
91 | |
92 try { | |
133 | 93 var p = action(); |
94 if (p == null) { | |
95 var d = new Promise(); | |
96 d.Reject(new Exception("The action returned null")); | |
97 p = d; | |
98 } | |
99 return p; | |
131 | 100 } catch (Exception err) { |
133 | 101 var p = new Promise(); |
131 | 102 p.Reject(err); |
133 | 103 return p; |
131 | 104 } |
105 } | |
106 | |
107 [DebuggerStepThrough] | |
94 | 108 public static IPromise<T> InvokePromise<T>(Func<IPromise<T>> action) { |
66 | 109 ArgumentNotNull(action, "action"); |
110 | |
111 try { | |
94 | 112 return action() ?? Promise<T>.ExceptionToPromise(new Exception("The action returned null")); |
66 | 113 } catch (Exception err) { |
114 return Promise<T>.ExceptionToPromise(err); | |
115 } | |
116 } | |
1 | 117 } |
118 } |