New tests.
[mono.git] / mcs / class / corlib / System.Threading.Tasks / Future.cs
1 #if NET_4_0 || BOOTSTRAP_NET_4_0
2 // Future.cs
3 //
4 // Copyright (c) 2008 Jérémie "Garuma" Laval
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 // THE SOFTWARE.
23 //
24 //
25
26 using System;
27
28 namespace System.Threading.Tasks
29 {
30         
31         public class Task<TResult>: Task
32         {
33                 TResult value;
34                 static TaskFactory<TResult> factory = new TaskFactory<TResult> ();
35                 
36                 Func<object, TResult> function;
37                 object state;
38                 
39                 public TResult Result {
40                         get {
41                                 if (function != null)
42                                         Wait ();
43                                 return value;
44                         }
45                         internal set {
46                                 this.value = value;
47                         }
48                 }
49                 
50                 public static new TaskFactory<TResult> Factory {
51                         get {
52                                 return factory;
53                         }
54                 }
55                 
56                 public Task (Func<TResult> function) : this (function, TaskCreationOptions.None)
57                 {
58                         
59                 }
60                 
61                 public Task (Func<TResult> function, CancellationToken token)
62                         : this ((o) => function (), null, token, TaskCreationOptions.None)
63                 {
64                         
65                 }
66                 
67                 public Task (Func<TResult> function, TaskCreationOptions options)
68                         : this ((o) => function (), null, CancellationToken.None, options)
69                 {
70                         
71                 }
72                 
73                 public Task (Func<TResult> function, CancellationToken token, TaskCreationOptions options)
74                         : this ((o) => function (), null, token, options)
75                 {
76                         
77                 }
78                 
79                 public Task (Func<object, TResult> function, object state) : this (function, state, TaskCreationOptions.None)
80                 {
81                         
82                 }
83                 
84                 public Task (Func<object, TResult> function, object state, CancellationToken token)
85                         : this (function, state, token, TaskCreationOptions.None)
86                 {
87                         
88                 }
89                 
90                 public Task (Func<object, TResult> function, object state, TaskCreationOptions options)
91                         : this (function, state, CancellationToken.None, options)
92                 {
93                         
94                 }
95                 
96                 public Task (Func<object, TResult> function, object state, CancellationToken token, TaskCreationOptions options)
97                         : base (null, state, token, options)
98                 {
99                         this.function = function;
100                         this.state = state;
101                 }
102                 
103                 internal override void InnerInvoke ()
104                 {
105                         if (function != null)
106                                 value = function (state);
107                         
108                         function = null;
109                         state = null;
110                 }
111                 
112                 public Task ContinueWith (Action<Task<TResult>> a)
113                 {
114                         return ContinueWith (a, TaskContinuationOptions.None);
115                 }
116                 
117                 public Task ContinueWith (Action<Task<TResult>> a, TaskContinuationOptions options)
118                 {
119                         return ContinueWith (a, CancellationToken.None, options, TaskScheduler.Current);
120                 }
121                 
122                 public Task ContinueWith (Action<Task<TResult>> a, CancellationToken token)
123                 {
124                         return ContinueWith (a, token, TaskContinuationOptions.None, TaskScheduler.Current);
125                 }
126                 
127                 public Task ContinueWith (Action<Task<TResult>> a, TaskScheduler scheduler)
128                 {
129                         return ContinueWith (a, CancellationToken.None, TaskContinuationOptions.None, scheduler);
130                 }
131                 
132                 public Task ContinueWith (Action<Task<TResult>> a, CancellationToken token,
133                                           TaskContinuationOptions options, TaskScheduler scheduler)
134                 {
135                         Task t = new Task ((o) => a ((Task<TResult>)o), this, token, GetCreationOptions (options));
136                         ContinueWithCore (t, options, scheduler);
137                         
138                         return t;
139                 }
140                 
141                 public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> a)
142                 {
143                         return ContinueWith<TNewResult> (a, TaskContinuationOptions.None);
144                 }
145                 
146                 public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> a, CancellationToken token)
147                 {
148                         return ContinueWith<TNewResult> (a, token, TaskContinuationOptions.None, TaskScheduler.Current);
149                 }
150                 
151                 public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> a, TaskContinuationOptions options)
152                 {
153                         return ContinueWith<TNewResult> (a, CancellationToken.None, options, TaskScheduler.Current);
154                 }
155                 
156                 public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> a, TaskScheduler scheduler)
157                 {
158                         return ContinueWith<TNewResult> (a, CancellationToken.None, TaskContinuationOptions.None, scheduler);
159                 }
160                 
161                 public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> a, CancellationToken token,
162                                                                   TaskContinuationOptions options,
163                                                                   TaskScheduler scheduler)
164                 {
165                         Task<TNewResult> t = new Task<TNewResult> ((o) => a ((Task<TResult>)o), this, token, GetCreationOptions (options));
166                         ContinueWithCore (t, options, scheduler);
167                         
168                         return t;
169                 }
170         }
171 }
172 #endif