annotate Implab/AbstractPromiseT.cs @ 144:8c0b95069066 v2

DRAFT: refactoring
author cin
date Fri, 06 Mar 2015 15:45:26 +0300
parents
children 706fccb85524
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
144
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
1 using System;
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
2 using Implab.Parallels;
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
3
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
4 namespace Implab {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
5 public abstract class AbstractPromise<T> : AbstractEvent<AbstractPromise<T>.HandlerDescriptor>, IPromise<T> {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
6 public struct HandlerDescriptor {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
7 readonly Action m_handler;
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
8 readonly Action<T> m_success;
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
9 readonly Action<Exception> m_error;
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
10 readonly Action<Exception> m_cancel;
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
11 readonly PromiseEventType m_mask;
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
12
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
13 public HandlerDescriptor(Action<T> success, Action<Exception> error, Action<Exception> cancel) {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
14 m_success = success;
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
15 m_error = error;
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
16 m_cancel = cancel;
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
17 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
18
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
19 public HandlerDescriptor(Action success, Action<Exception> error, Action<Exception> cancel) {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
20 m_handler = success;
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
21 m_error = error;
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
22 m_cancel = cancel;
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
23 m_mask = PromiseEventType.Success;
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
24 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
25
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
26 public HandlerDescriptor(Action handler, PromiseEventType mask) {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
27 m_handler = handler;
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
28 m_mask = mask;
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
29 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
30
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
31 public void SignalSuccess(T result) {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
32 if (m_success != null) {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
33 try {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
34 m_success(result);
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
35 } catch(Exception err) {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
36 SignalError(err);
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
37 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
38 } else if (m_mask & PromiseEventType.Success && m_handler != null) {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
39 try {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
40 m_handler();
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
41 } catch(Exception err) {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
42 // avoid calling handler twice in case of error
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
43 if (m_error != null)
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
44 SignalError(err);
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
45 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
46 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
47 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
48
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
49 public void SignalError(Exception err) {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
50 if (m_error != null) {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
51 try {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
52 m_error(err);
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
53 // Analysis disable once EmptyGeneralCatchClause
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
54 } catch {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
55 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
56 } else if (m_mask & PromiseEventType.Error && m_handler != null) {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
57 try {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
58 m_handler();
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
59 // Analysis disable once EmptyGeneralCatchClause
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
60 } catch {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
61 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
62 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
63 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
64
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
65 public void SignalCancel(Exception reason) {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
66 if (m_cancel != null) {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
67 try {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
68 m_cancel(reason);
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
69 } catch (Exception err) {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
70 SignalError(err);
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
71 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
72 } else if (m_mask & PromiseEventType.Cancelled && m_handler != null) {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
73 try {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
74 m_handler();
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
75 // Analysis disable once EmptyGeneralCatchClause
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
76 } catch {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
77 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
78 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
79 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
80 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
81
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
82
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
83
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
84 public Type PromiseType {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
85 get {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
86 return typeof(T);
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
87 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
88 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
89
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
90 public new T Join() {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
91 WaitResult(-1);
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
92 return m_result;
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
93 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
94 public new T Join(int timeout) {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
95 WaitResult(timeout);
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
96 return m_result;
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
97 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
98
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
99 public IPromise<T> On(Action<T> success, Action<Exception> error, Action<Exception> cancel) {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
100 AddHandler(new HandlerDescriptor(success, error, cancel));
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
101 return this;
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
102 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
103
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
104 public IPromise<T> On(Action<T> success, Action<Exception> error) {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
105 AddHandler(new HandlerDescriptor(success, error, null));
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
106 return this;
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
107 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
108
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
109 public IPromise<T> On(Action<T> success) {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
110 AddHandler(new HandlerDescriptor(success, null, null));
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
111 return this;
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
112 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
113
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
114 public IPromise<T> On(Action handler, PromiseEventType events) {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
115 AddHandler(new HandlerDescriptor(handler, events));
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
116 return this;
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
117 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
118
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
119 public IPromise<T> On(Action success, Action<Exception> error, Action<Exception> cancel) {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
120 AddHandler(new HandlerDescriptor(success, error, cancel));
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
121 return this;
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
122 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
123
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
124 public IPromise<T> On(Action success, Action<Exception> error) {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
125 AddHandler(new HandlerDescriptor(success, error, null));
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
126 return this;
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
127 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
128
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
129 public IPromise<T> On(Action success) {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
130 AddHandler(new HandlerDescriptor(success, null, null));
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
131 return this;
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
132 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
133
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
134 IPromise IPromise.On(Action success, Action<Exception> error, Action<Exception> cancel) {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
135 AddHandler(new HandlerDescriptor(success, error, cancel));
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
136 return this;
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
137 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
138
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
139 IPromise IPromise.On(Action success, Action<Exception> error) {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
140 AddHandler(new HandlerDescriptor(success, error, null));
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
141 return this;
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
142 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
143
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
144 IPromise IPromise.On(Action success) {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
145 AddHandler(new HandlerDescriptor(success, null, null));
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
146 return this;
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
147 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
148
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
149 public IPromise<T2> Cast<T2>() {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
150 return (IPromise<T2>)this;
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
151 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
152
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
153 #region implemented abstract members of AbstractPromise
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
154
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
155 protected override Signal GetResolveSignal() {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
156 var signal = new Signal();
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
157 AddHandler(new HandlerDescriptor(signal.Set, PromiseEventType.All));
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
158 return signal;
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
159 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
160
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
161 protected override void SignalSuccess(HandlerDescriptor handler) {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
162 handler.SignalSuccess(m_result);
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
163 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
164
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
165 protected override void SignalError(HandlerDescriptor handler, Exception error) {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
166 handler.SignalError(error);
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
167 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
168
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
169 protected override void SignalCancelled(HandlerDescriptor handler, Exception reason) {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
170 handler.SignalCancel(reason);
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
171 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
172
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
173 #endregion
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
174
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
175 T m_result;
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
176
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
177 protected void SetResult(T value) {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
178 if (BeginSetResult()) {
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
179 m_result = value;
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
180 EndSetResult();
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
181 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
182 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
183 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
184 }
8c0b95069066 DRAFT: refactoring
cin
parents:
diff changeset
185