annotate Implab/Safe.cs @ 209:a867536c68fc v2

Bound promise to CancellationToken Added new states to ExecutionSate enum. Added Safe.Guard() method to handle cleanup of the result of the promise
author cin
date Wed, 16 Nov 2016 03:06:08 +0300
parents 558f34b2fb50
children 9ee78a345738
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;
207
558f34b2fb50 added Safe.DispatchEvent() a legacy equivalent for '?.Invoke()'
cin
parents: 205
diff changeset
7 using System.Collections;
1
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
8
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
9 namespace Implab
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
10 {
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
11 public static class Safe
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
12 {
161
2a8466f0cb8a DFA refactoring
cin
parents: 145
diff changeset
13 public static void ArgumentAssert(bool condition, string paramName) {
2a8466f0cb8a DFA refactoring
cin
parents: 145
diff changeset
14 if (!condition)
2a8466f0cb8a DFA refactoring
cin
parents: 145
diff changeset
15 throw new ArgumentException("The parameter is invalid", paramName);
2a8466f0cb8a DFA refactoring
cin
parents: 145
diff changeset
16 }
2a8466f0cb8a DFA refactoring
cin
parents: 145
diff changeset
17
121
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
18 public static void ArgumentMatch(string value, string paramName, Regex rx) {
51
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
19 if (rx == null)
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
20 throw new ArgumentNullException("rx");
121
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
21 if (!rx.IsMatch(value))
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
22 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
23 }
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
24
121
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
25 public static void ArgumentNotEmpty(string value, string paramName) {
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
26 if (String.IsNullOrEmpty(value))
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
27 throw new ArgumentException("The parameter can't be empty", paramName);
90
efcb076407a7 code cleanup
cin
parents: 66
diff changeset
28 }
efcb076407a7 code cleanup
cin
parents: 66
diff changeset
29
121
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
30 public static void ArgumentNotEmpty<T>(T[] value, string paramName) {
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
31 if (value == null || value.Length == 0)
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
32 throw new ArgumentException("The array must be not emty", paramName);
51
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
33 }
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
34
121
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
35 public static void ArgumentNotNull(object value, string paramName) {
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
36 if (value == null)
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
37 throw new ArgumentNullException(paramName);
51
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
38 }
2c332a9c64c0 Added methods for parameter checks
cin
parents: 31
diff changeset
39
121
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
40 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
41 if (value < min || value > max)
62d2f1e98c4e working version of AsyncQueue and batch operations
cin
parents: 119
diff changeset
42 throw new ArgumentOutOfRangeException(paramName);
55
c0bf853aa04f Added initial JSON support
cin
parents: 51
diff changeset
43 }
c0bf853aa04f Added initial JSON support
cin
parents: 51
diff changeset
44
177
a0ff6a0e9c44 refactoring
cin
parents: 161
diff changeset
45 public static void ArgumentOfType(object value, Type type, string paramName) {
a0ff6a0e9c44 refactoring
cin
parents: 161
diff changeset
46 if (!type.IsInstanceOfType(value))
a0ff6a0e9c44 refactoring
cin
parents: 161
diff changeset
47 throw new ArgumentException(String.Format("The parameter must be of type {0}", type), paramName);
a0ff6a0e9c44 refactoring
cin
parents: 161
diff changeset
48 }
a0ff6a0e9c44 refactoring
cin
parents: 161
diff changeset
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
f7b2b8bfbb8c minor changes
cin
parents: 121
diff changeset
52 if (d != null)
f7b2b8bfbb8c minor changes
cin
parents: 121
diff changeset
53 d.Dispose();
1
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
54 }
66
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
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
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
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
b5c2d609d71b minor changes
cin
parents: 128
diff changeset
88 ArgumentNotNull(action, "action");
b5c2d609d71b minor changes
cin
parents: 128
diff changeset
89
b5c2d609d71b minor changes
cin
parents: 128
diff changeset
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
b5c2d609d71b minor changes
cin
parents: 128
diff changeset
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
b5c2d609d71b minor changes
cin
parents: 128
diff changeset
94 }
b5c2d609d71b minor changes
cin
parents: 128
diff changeset
95 }
b5c2d609d71b minor changes
cin
parents: 128
diff changeset
96
b5c2d609d71b minor changes
cin
parents: 128
diff changeset
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
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
122 ArgumentNotNull(action, "action");
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
123
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
124 try {
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 133
diff changeset
125 return action() ?? Promise<T>.FromException(new Exception("The action returned null"));
66
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
126 } catch (Exception err) {
145
706fccb85524 RC: cancellation support for promises + tests
cin
parents: 133
diff changeset
127 return Promise<T>.FromException(err);
66
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
128 }
790e8a997d30 Refactoring
cin
parents: 55
diff changeset
129 }
209
a867536c68fc Bound promise to CancellationToken
cin
parents: 207
diff changeset
130
1
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
131 }
6fb3b01ee971 Added utility class for safe disposing methods.
cin
parents:
diff changeset
132 }