Split internal types in their own files
[mono.git] / mcs / class / corlib / System.Threading.Tasks / Task_T.cs
1 //
2 // Task_T.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 #if NET_4_0 || MOBILE
27 using System;
28
29 namespace System.Threading.Tasks
30 {
31         [System.Diagnostics.DebuggerDisplay ("Id = {Id}, Status = {Status}, Method = {DebuggerDisplayMethodDescription}, Result = {DebuggerDisplayResultDescription}")]
32         [System.Diagnostics.DebuggerTypeProxy ("System.Threading.Tasks.SystemThreadingTasks_FutureDebugView`1")]
33         public class Task<TResult>: Task
34         {
35                 TResult value;
36                 static TaskFactory<TResult> factory = new TaskFactory<TResult> ();
37                 static readonly Action<object> emptyAction = delegate {};
38                 
39                 Func<object, TResult> function;
40                 object state;
41                 
42                 [System.Diagnostics.DebuggerBrowsable (System.Diagnostics.DebuggerBrowsableState.Never)]
43                 public TResult Result {
44                         get {
45                                 if (function != null)
46                                         Wait ();
47                                 else if (Exception != null)
48                                         throw Exception;
49                                 return value;
50                         }
51                         internal set {
52                                 this.value = value;
53                         }
54                 }
55                 
56                 public static new TaskFactory<TResult> Factory {
57                         get {
58                                 return factory;
59                         }
60                 }
61                 
62                 public Task (Func<TResult> function) : this (function, TaskCreationOptions.None)
63                 {
64                         
65                 }
66                 
67                 public Task (Func<TResult> function, CancellationToken cancellationToken)
68                         : this (function == null ? (Func<object, TResult>)null : (o) => function(), null, cancellationToken, TaskCreationOptions.None)
69                 {
70                         
71                 }
72                 
73                 public Task (Func<TResult> function, TaskCreationOptions creationOptions)
74                         : this (function == null ? (Func<object, TResult>)null : (o) => function(), null, CancellationToken.None, creationOptions)
75                 {
76                         
77                 }
78                 
79                 public Task (Func<TResult> function, CancellationToken cancellationToken, TaskCreationOptions creationOptions)
80                         : this (function == null ? (Func<object, TResult>)null : (o) => function(), null, cancellationToken, creationOptions)
81                 {
82                         
83                 }
84                 
85                 public Task (Func<object, TResult> function, object state) : this (function, state, TaskCreationOptions.None)
86                 {
87                         
88                 }
89                 
90                 public Task (Func<object, TResult> function, object state, CancellationToken cancellationToken)
91                         : this (function, state, cancellationToken, TaskCreationOptions.None)
92                 {
93                         
94                 }
95                 
96                 public Task (Func<object, TResult> function, object state, TaskCreationOptions creationOptions)
97                         : this (function, state, CancellationToken.None, creationOptions)
98                 {
99                         
100                 }
101
102                 public Task (Func<object, TResult> function, object state, CancellationToken cancellationToken, TaskCreationOptions creationOptions)
103                         : base (emptyAction, state, cancellationToken, creationOptions)
104                 {
105                         this.function = function;
106                         this.state = state;
107                 }
108
109                 internal Task (Func<object, TResult> function,
110                                object state,
111                                CancellationToken cancellationToken,
112                                TaskCreationOptions creationOptions,
113                                Task parent)
114                 : base (null, state, cancellationToken, creationOptions, parent)
115                 {
116                         this.function = function;
117                         this.state = state;
118                 }
119                 
120                 internal override void InnerInvoke ()
121                 {
122                         if (function != null)
123                                 value = function (state);
124                         
125                         function = null;
126                         state = null;
127                 }
128                 
129                 public Task ContinueWith (Action<Task<TResult>> continuationAction)
130                 {
131                         return ContinueWith (continuationAction, TaskContinuationOptions.None);
132                 }
133                 
134                 public Task ContinueWith (Action<Task<TResult>> continuationAction, TaskContinuationOptions continuationOptions)
135                 {
136                         return ContinueWith (continuationAction, CancellationToken.None, continuationOptions, TaskScheduler.Current);
137                 }
138                 
139                 public Task ContinueWith (Action<Task<TResult>> continuationAction, CancellationToken cancellationToken)
140                 {
141                         return ContinueWith (continuationAction, cancellationToken, TaskContinuationOptions.None, TaskScheduler.Current);
142                 }
143                 
144                 public Task ContinueWith (Action<Task<TResult>> continuationAction, TaskScheduler scheduler)
145                 {
146                         return ContinueWith (continuationAction, CancellationToken.None, TaskContinuationOptions.None, scheduler);
147                 }
148                 
149                 public Task ContinueWith (Action<Task<TResult>> continuationAction, CancellationToken cancellationToken,
150                                           TaskContinuationOptions continuationOptions, TaskScheduler scheduler)
151                 {
152                         if (continuationAction == null)
153                                 throw new ArgumentNullException ("continuationFunction");
154                         if (scheduler == null)
155                                 throw new ArgumentNullException ("scheduler");
156
157                         Task t = new Task ((o) => continuationAction ((Task<TResult>)o),
158                                            this,
159                                            cancellationToken,
160                                            GetCreationOptions (continuationOptions),
161                                            this);
162                         ContinueWithCore (t, continuationOptions, scheduler);
163                         
164                         return t;
165                 }
166                 
167                 public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> continuationFunction)
168                 {
169                         return ContinueWith<TNewResult> (continuationFunction, TaskContinuationOptions.None);
170                 }
171                 
172                 public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> continuationFunction, CancellationToken cancellationToken)
173                 {
174                         return ContinueWith<TNewResult> (continuationFunction, cancellationToken, TaskContinuationOptions.None, TaskScheduler.Current);
175                 }
176                 
177                 public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> continuationFunction, TaskContinuationOptions continuationOptions)
178                 {
179                         return ContinueWith<TNewResult> (continuationFunction, CancellationToken.None, continuationOptions, TaskScheduler.Current);
180                 }
181                 
182                 public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> continuationFunction, TaskScheduler scheduler)
183                 {
184                         return ContinueWith<TNewResult> (continuationFunction, CancellationToken.None, TaskContinuationOptions.None, scheduler);
185                 }
186                 
187                 public Task<TNewResult> ContinueWith<TNewResult> (Func<Task<TResult>, TNewResult> continuationFunction,
188                                                                   CancellationToken cancellationToken,
189                                                                   TaskContinuationOptions continuationOptions,
190                                                                   TaskScheduler scheduler)
191                 {
192                         Task<TNewResult> t = new Task<TNewResult> ((o) => continuationFunction ((Task<TResult>)o),
193                                                                    this,
194                                                                    cancellationToken,
195                                                                    GetCreationOptions (continuationOptions),
196                                                                    this);
197                         ContinueWithCore (t, continuationOptions, scheduler);
198                         
199                         return t;
200                 }
201         }
202 }
203 #endif