annotate Implab/Safe.cs @ 163:419aa51b04fd ref20160224

JSON moved to Formats namespace Working in RegularDFA
author cin
date Wed, 24 Feb 2016 20:12:52 +0300
parents 2a8466f0cb8a
children a0ff6a0e9c44
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
1 using System;
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
2 using System.Collections.Generic;
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
3 using System.Linq;
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
4 using System.Text;
51
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
5 using System.Text.RegularExpressions;
66
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
6 using System.Diagnostics;
1
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
7
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
8 namespace Implab
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
9 {
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
10 public static class Safe
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
11 {
161
2a8466f0cb8a DFA refactoring
cin
parents: 145
diff changeset
12 public static void ArgumentAssert(bool condition, string paramName) {
2a8466f0cb8a DFA refactoring
cin
parents: 145
diff changeset
13 if (!condition)
2a8466f0cb8a DFA refactoring
cin
parents: 145
diff changeset
14 throw new ArgumentException("The parameter is invalid", paramName);
2a8466f0cb8a DFA refactoring
cin
parents: 145
diff changeset
15 }
2a8466f0cb8a DFA refactoring
cin
parents: 145
diff changeset
16
121
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
17 public static void ArgumentMatch(string value, string paramName, Regex rx) {
51
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
18 if (rx == null)
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
19 throw new ArgumentNullException("rx");
121
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
20 if (!rx.IsMatch(value))
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
21 throw new ArgumentException(String.Format("The prameter value must match {0}", rx), paramName);
51
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
22 }
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
23
121
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
24 public static void ArgumentNotEmpty(string value, string paramName) {
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
25 if (String.IsNullOrEmpty(value))
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
26 throw new ArgumentException("The parameter can't be empty", paramName);
90
efcb076407a7 code cleanup
cin
parents: 66
diff changeset
27 }
efcb076407a7 code cleanup
cin
parents: 66
diff changeset
28
121
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
29 public static void ArgumentNotEmpty<T>(T[] value, string paramName) {
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
30 if (value == null || value.Length == 0)
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
31 throw new ArgumentException("The array must be not emty", paramName);
51
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
32 }
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
33
121
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
34 public static void ArgumentNotNull(object value, string paramName) {
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
35 if (value == null)
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
36 throw new ArgumentNullException(paramName);
51
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
37 }
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
38
121
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
39 public static void ArgumentInRange(int value, int min, int max, string paramName) {
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
40 if (value < min || value > max)
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
41 throw new ArgumentOutOfRangeException(paramName);
55
c0bf853aa04f Added initial JSON support
cin
parents: 51
diff changeset
42 }
c0bf853aa04f Added initial JSON support
cin
parents: 51
diff changeset
43
128
6241bff0cd64 Added Signal class a lightweight alternative to ManualResetEvent
cin
parents: 126
diff changeset
44 public static void Dispose(params IDisposable[] objects) {
6241bff0cd64 Added Signal class a lightweight alternative to ManualResetEvent
cin
parents: 126
diff changeset
45 foreach (var d in objects)
126
f7b2b8bfbb8c minor changes
cin
parents: 121
diff changeset
46 if (d != null)
f7b2b8bfbb8c minor changes
cin
parents: 121
diff changeset
47 d.Dispose();
1
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
48 }
66
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
49
128
6241bff0cd64 Added Signal class a lightweight alternative to ManualResetEvent
cin
parents: 126
diff changeset
50 public static void Dispose(params object[] objects) {
6241bff0cd64 Added Signal class a lightweight alternative to ManualResetEvent
cin
parents: 126
diff changeset
51 foreach (var obj in objects) {
6241bff0cd64 Added Signal class a lightweight alternative to ManualResetEvent
cin
parents: 126
diff changeset
52 var d = obj as IDisposable;
6241bff0cd64 Added Signal class a lightweight alternative to ManualResetEvent
cin
parents: 126
diff changeset
53 if (d != null)
6241bff0cd64 Added Signal class a lightweight alternative to ManualResetEvent
cin
parents: 126
diff changeset
54 d.Dispose();
6241bff0cd64 Added Signal class a lightweight alternative to ManualResetEvent
cin
parents: 126
diff changeset
55 }
6241bff0cd64 Added Signal class a lightweight alternative to ManualResetEvent
cin
parents: 126
diff changeset
56 }
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 public static void Dispose(object obj) {
6241bff0cd64 Added Signal class a lightweight alternative to ManualResetEvent
cin
parents: 126
diff changeset
59 var d = obj as IDisposable;
6241bff0cd64 Added Signal class a lightweight alternative to ManualResetEvent
cin
parents: 126
diff changeset
60 if (d != null)
6241bff0cd64 Added Signal class a lightweight alternative to ManualResetEvent
cin
parents: 126
diff changeset
61 d.Dispose();
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
66
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
64 [DebuggerStepThrough]
132
5fb2bbffdece minor fixes
cin
parents: 131
diff changeset
65 public static IPromise<T> WrapPromise<T>(Func<T> action) {
66
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
66 ArgumentNotNull(action, "action");
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
67
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
68 var p = new Promise<T>();
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
69 try {
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
70 p.Resolve(action());
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
71 } catch (Exception err) {
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
72 p.Reject(err);
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
73 }
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
74
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
75 return p;
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
76 }
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
77
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
78 [DebuggerStepThrough]
132
5fb2bbffdece minor fixes
cin
parents: 131
diff changeset
79 public static IPromise WrapPromise(Action action) {
95
0141a165d032 minor changes
cin
parents: 94
diff changeset
80 ArgumentNotNull(action, "action");
0141a165d032 minor changes
cin
parents: 94
diff changeset
81
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 96
diff changeset
82 var p = new Promise();
95
0141a165d032 minor changes
cin
parents: 94
diff changeset
83 try {
0141a165d032 minor changes
cin
parents: 94
diff changeset
84 action();
0141a165d032 minor changes
cin
parents: 94
diff changeset
85 p.Resolve();
0141a165d032 minor changes
cin
parents: 94
diff changeset
86 } catch (Exception err) {
0141a165d032 minor changes
cin
parents: 94
diff changeset
87 p.Reject(err);
0141a165d032 minor changes
cin
parents: 94
diff changeset
88 }
0141a165d032 minor changes
cin
parents: 94
diff changeset
89
0141a165d032 minor changes
cin
parents: 94
diff changeset
90 return p;
0141a165d032 minor changes
cin
parents: 94
diff changeset
91 }
0141a165d032 minor changes
cin
parents: 94
diff changeset
92
0141a165d032 minor changes
cin
parents: 94
diff changeset
93 [DebuggerStepThrough]
131
b5c2d609d71b minor changes
cin
parents: 128
diff changeset
94 public static IPromise InvokePromise(Func<IPromise> action) {
b5c2d609d71b minor changes
cin
parents: 128
diff changeset
95 ArgumentNotNull(action, "action");
b5c2d609d71b minor changes
cin
parents: 128
diff changeset
96
b5c2d609d71b minor changes
cin
parents: 128
diff changeset
97 try {
133
cin
parents: 132
diff changeset
98 var p = action();
cin
parents: 132
diff changeset
99 if (p == null) {
cin
parents: 132
diff changeset
100 var d = new Promise();
cin
parents: 132
diff changeset
101 d.Reject(new Exception("The action returned null"));
cin
parents: 132
diff changeset
102 p = d;
cin
parents: 132
diff changeset
103 }
cin
parents: 132
diff changeset
104 return p;
131
b5c2d609d71b minor changes
cin
parents: 128
diff changeset
105 } catch (Exception err) {
133
cin
parents: 132
diff changeset
106 var p = new Promise();
131
b5c2d609d71b minor changes
cin
parents: 128
diff changeset
107 p.Reject(err);
133
cin
parents: 132
diff changeset
108 return p;
131
b5c2d609d71b minor changes
cin
parents: 128
diff changeset
109 }
b5c2d609d71b minor changes
cin
parents: 128
diff changeset
110 }
b5c2d609d71b minor changes
cin
parents: 128
diff changeset
111
b5c2d609d71b minor changes
cin
parents: 128
diff changeset
112 [DebuggerStepThrough]
94
a43745f81f10 minor fixes
cin
parents: 90
diff changeset
113 public static IPromise<T> InvokePromise<T>(Func<IPromise<T>> action) {
66
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
114 ArgumentNotNull(action, "action");
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
115
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
116 try {
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 133
diff changeset
117 return action() ?? Promise<T>.FromException(new Exception("The action returned null"));
66
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
118 } catch (Exception err) {
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 133
diff changeset
119 return Promise<T>.FromException(err);
66
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
120 }
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
121 }
1
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
122 }
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
123 }