diff Implab/FuncTaskBase.cs @ 144:8c0b95069066 v2

DRAFT: refactoring
author cin
date Fri, 06 Mar 2015 15:45:26 +0300
parents
children eb793fbbe4ea
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Implab/FuncTaskBase.cs	Fri Mar 06 15:45:26 2015 +0300
@@ -0,0 +1,53 @@
+using System;
+using System.Threading;
+
+namespace Implab {
+    public class FuncTaskBase<TResult> : AbstractPromise<TResult> {
+        readonly Func<Exception, TResult> m_cancel;
+        readonly Func<Exception, TResult> m_error;
+
+        int m_cancelationLock;
+
+        protected FuncTaskBase( Func<Exception, TResult> error, Func<Exception, TResult> cancel) {
+            m_error = error;
+            m_cancel = cancel;
+        }
+
+        public void Reject(Exception error) {
+            Safe.ArgumentNotNull(error, "error");
+            if (LockCancelation())
+                HandleErrorInternal(error);
+        }
+
+        protected void HandleErrorInternal(Exception error) {
+            if (m_error != null) {
+                try {
+                    SetResult(m_error(error));
+                } catch(Exception err) {
+                    SetError(err);
+                }
+            } else {
+                SetError(error);
+            }
+        }
+
+        public override void CancelOperation(Exception reason) {
+            if (LockCancelation()) {
+                if (m_cancel != null) {
+                    try {
+                        SetResult(m_cancel(reason));
+                    } catch (Exception err) {
+                        HandleErrorInternal(err);
+                    }
+                } else {
+                    SetCancelled(reason);
+                }
+            }
+        }
+
+        protected bool LockCancelation() {
+            return 0 == Interlocked.CompareExchange(ref m_cancelationLock, 1, 0);
+        }
+    }
+}
+