2010-04-15 Jérémie Laval <jeremie.laval@gmail.com>
[mono.git] / mcs / class / System.Core / System.Linq / Internal / QueryNodes / QueryOptionNode.cs
1 #if NET_4_0
2 //
3 // QueryOptionNode.cs
4 //
5 // Author:
6 //       Jérémie "Garuma" Laval <jeremie.laval@gmail.com>
7 //
8 // Copyright (c) 2010 Jérémie "Garuma" Laval
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining a copy
11 // of this software and associated documentation files (the "Software"), to deal
12 // in the Software without restriction, including without limitation the rights
13 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 // copies of the Software, and to permit persons to whom the Software is
15 // furnished to do so, subject to the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be included in
18 // all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 // THE SOFTWARE.
27
28 using System;
29 using System.Threading;
30 using System.Collections.Generic;
31
32 namespace System.Linq
33 {
34         // The first four elements correspond to the public operator With*
35         // Last CancellationToken parameter is used internally for ImplementerToken
36         using OptionsList = Tuple<ParallelMergeOptions?, ParallelExecutionMode?, CancellationToken?, int, CancellationTokenSource>;
37
38         internal class QueryOptionNode<T> : QueryChildNode<T, T>
39         {
40
41                 public QueryOptionNode (QueryBaseNode<T> parent)
42                         : base (parent)
43                 {
44
45                 }
46
47                 internal virtual OptionsList GetOptions ()
48                 {
49                         return new OptionsList (null, null, null, -1, null);
50                 }
51
52                 internal override IList<IEnumerable<T>> GetEnumerables (QueryOptions options)
53                 {
54                         return Parent.GetEnumerables (options);
55                 }
56
57                 internal override IList<IEnumerable<KeyValuePair<long, T>>> GetOrderedEnumerables (QueryOptions options)
58                 {
59                         return Parent.GetOrderedEnumerables (options);
60                 }
61
62                 internal override IEnumerable<T> GetSequential ()
63                 {
64                         return Parent.GetSequential ();
65                 }
66
67                 public override void Visit (INodeVisitor visitor)
68                 {
69                         visitor.Visit<T> (this);
70                 }
71         }
72
73         internal class ParallelExecutionModeNode<T> : QueryOptionNode<T>
74         {
75                 ParallelExecutionMode mode;
76
77                 internal ParallelExecutionModeNode (ParallelExecutionMode mode, QueryBaseNode<T> parent)
78                         : base (parent)
79                 {
80                         this.mode = mode;
81                 }
82
83                 internal override OptionsList GetOptions ()
84                 {
85                         return new OptionsList (null, mode, null, -1, null);
86                 }
87         }
88
89
90         internal class ParallelMergeOptionsNode<T> : QueryOptionNode<T>
91         {
92                 ParallelMergeOptions opts;
93
94                 internal ParallelMergeOptionsNode (ParallelMergeOptions opts, QueryBaseNode<T> parent)
95                         : base (parent)
96                 {
97                         this.opts = opts;
98                 }
99
100                 internal override OptionsList GetOptions ()
101                 {
102                         return new OptionsList (opts, null, null, -1, null);
103                 }
104         }
105
106
107         internal class CancellationTokenNode<T> : QueryOptionNode<T>
108         {
109                 CancellationToken token;
110
111                 internal CancellationTokenNode (CancellationToken token, QueryBaseNode<T> parent)
112                         : base (parent)
113                 {
114                         this.token = token;
115                 }
116
117                 internal override OptionsList GetOptions ()
118                 {
119                         return new OptionsList (null, null, token, -1, null);
120                 }
121         }
122
123         internal class DegreeOfParallelismNode<T> : QueryOptionNode<T>
124         {
125                 int degreeParallelism;
126
127                 internal DegreeOfParallelismNode (int degreeParallelism, QueryBaseNode<T> parent)
128                         : base (parent)
129                 {
130                         this.degreeParallelism = degreeParallelism;
131                 }
132
133                 internal override OptionsList GetOptions ()
134                 {
135                         return new OptionsList (null, null, null, degreeParallelism, null);
136                 }
137         }
138
139         internal class ImplementerTokenNode<T> : QueryOptionNode<T>
140         {
141                 CancellationTokenSource source;
142
143                 internal ImplementerTokenNode (CancellationTokenSource token, QueryBaseNode<T> parent)
144                         : base (parent)
145                 {
146                         this.source = token;
147                 }
148
149                 internal override OptionsList GetOptions ()
150                 {
151                         return new OptionsList (null, null, null, -1, source);
152                 }
153         }
154 }
155 #endif