2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / dlr / Runtime / Microsoft.Scripting.Core / Ast / SwitchCase.cs
1 /* ****************************************************************************
2  *
3  * Copyright (c) Microsoft Corporation. 
4  *
5  * This source code is subject to terms and conditions of the Microsoft Public License. A 
6  * copy of the license can be found in the License.html file at the root of this distribution. If 
7  * you cannot locate the  Microsoft Public License, please send an email to 
8  * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
9  * by the terms of the Microsoft Public License.
10  *
11  * You must not remove this notice, or any other, from this software.
12  *
13  *
14  * ***************************************************************************/
15 using System; using Microsoft;
16
17
18 using System.Collections.Generic;
19 using System.Collections.ObjectModel;
20 using System.Diagnostics;
21 #if CODEPLEX_40
22 using System.Dynamic.Utils;
23 #else
24 using Microsoft.Scripting.Utils;
25 #endif
26
27 #if CODEPLEX_40
28 namespace System.Linq.Expressions {
29 #else
30 namespace Microsoft.Linq.Expressions {
31 #endif
32     /// <summary>
33     /// Represents one case of a <see cref="SwitchExpression"/>.
34     /// </summary>
35 #if !SILVERLIGHT
36     [DebuggerTypeProxy(typeof(Expression.SwitchCaseProxy))]
37 #endif
38     public sealed class SwitchCase {
39         private readonly ReadOnlyCollection<Expression> _testValues;
40         private readonly Expression _body;
41
42         internal SwitchCase(Expression body, ReadOnlyCollection<Expression> testValues) {
43             _body = body;
44             _testValues = testValues;
45         }
46
47         /// <summary>
48         /// Gets the values of this case. This case is selected for execution when the <see cref="SwitchExpression.SwitchValue"/> matches any of these values.
49         /// </summary>
50         public ReadOnlyCollection<Expression> TestValues {
51             get { return _testValues; }
52         }
53
54         /// <summary>
55         /// Gets the body of this case.
56         /// </summary>
57         public Expression Body {
58             get { return _body; }
59         }
60
61         /// <summary>
62         /// Returns a <see cref="String"/> that represents the current <see cref="Object"/>. 
63         /// </summary>
64         /// <returns>A <see cref="String"/> that represents the current <see cref="Object"/>. </returns>
65         public override string ToString() {
66             return ExpressionStringBuilder.SwitchCaseToString(this);
67         }
68     }
69
70     public partial class Expression {
71         /// <summary>
72         /// Creates a <see cref="Microsoft.Linq.Expressions.SwitchCase">SwitchCase</see> for use in a <see cref="SwitchExpression"/>.
73         /// </summary>
74         /// <param name="body">The body of the case.</param>
75         /// <param name="testValues">The test values of the case.</param>
76         /// <returns>The created <see cref="Microsoft.Linq.Expressions.SwitchCase">SwitchCase</see>.</returns>
77         public static SwitchCase SwitchCase(Expression body, params Expression[] testValues) {
78             return SwitchCase(body, (IEnumerable<Expression>)testValues);
79         }
80
81         /// <summary>
82         /// Creates a <see cref="Microsoft.Linq.Expressions.SwitchCase">SwitchCase</see> for use in a <see cref="SwitchExpression"/>.
83         /// </summary>
84         /// <param name="body">The body of the case.</param>
85         /// <param name="testValues">The test values of the case.</param>
86         /// <returns>The created <see cref="Microsoft.Linq.Expressions.SwitchCase">SwitchCase</see>.</returns>
87         public static SwitchCase SwitchCase(Expression body, IEnumerable<Expression> testValues) {
88             RequiresCanRead(body, "body");
89             
90             var values = testValues.ToReadOnly();
91             RequiresCanRead(values, "testValues");
92             ContractUtils.RequiresNotEmpty(values, "testValues");
93
94             return new SwitchCase(body, values);
95         }
96     }
97 }