2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / dlr / Runtime / Microsoft.Scripting.Core / Actions / CallInfo.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 #if CODEPLEX_40
21 using System.Dynamic.Utils;
22 using System.Linq.Expressions;
23 #else
24 using Microsoft.Scripting.Utils;
25 using Microsoft.Linq.Expressions;
26 #endif
27
28 #if CODEPLEX_40
29 namespace System.Dynamic {
30 #else
31 namespace Microsoft.Scripting {
32 #endif
33
34     /// <summary>
35     /// Describes arguments in the dynamic binding process.
36     /// </summary>
37     /// <remarks>
38     /// ArgumentCount - all inclusive number of arguments.
39     /// ArgumentNames - names for those arguments that are named.
40     ///
41     /// Argument names match to the argument values in left to right order 
42     /// and last name corresponds to the last argument.
43     /// 
44     /// Example:
45     ///   Foo(arg1, arg2, arg3, name1 = arg4, name2 = arg5, name3 = arg6)
46     ///
47     ///   will correspond to:
48     ///    ArgumentCount: 6
49     ///    ArgumentNames: {"name1", "name2", "name3"}
50     /// </remarks>
51     public sealed class CallInfo {
52         private readonly int _argCount;
53         private readonly ReadOnlyCollection<string> _argNames;
54
55         /// <summary>
56         /// Creates a new PositionalArgumentInfo.
57         /// </summary>
58         /// <param name="argCount">The number of arguments.</param>
59         /// <param name="argNames">The argument names.</param>
60         /// <returns>The new CallInfo</returns>
61         public CallInfo(int argCount, params string[] argNames)
62             : this(argCount, (IEnumerable<string>)argNames) {
63         }
64
65         /// <summary>
66         /// Creates a new CallInfo that represents arguments in the dynamic binding process.
67         /// </summary>
68         /// <param name="argCount">The number of arguments.</param>
69         /// <param name="argNames">The argument names.</param>
70         /// <returns>The new CallInfo</returns>
71         public CallInfo(int argCount, IEnumerable<string> argNames) {
72             ContractUtils.RequiresNotNull(argNames, "argNames");
73
74             var argNameCol = argNames.ToReadOnly();
75
76             ContractUtils.Requires(argCount >= argNameCol.Count, "argCount", Strings.ArgCntMustBeGreaterThanNameCnt);
77             ContractUtils.RequiresNotNullItems(argNameCol, "argNames");
78
79             _argCount = argCount;
80             _argNames = argNameCol;
81         }
82
83         /// <summary>
84         /// The number of arguments.
85         /// </summary>
86         public int ArgumentCount {
87             get { return _argCount; }
88         }
89
90         /// <summary>
91         /// The argument names.
92         /// </summary>
93         public ReadOnlyCollection<string> ArgumentNames {
94             get { return _argNames; }
95         }
96
97         /// <summary>
98         /// Serves as a hash function for the current CallInfo.
99         /// </summary>
100         /// <returns>A hash code for the current CallInfo.</returns>
101         public override int GetHashCode() {
102             return _argCount ^ _argNames.ListHashCode();
103         }
104
105         /// <summary>
106         /// Determines whether the specified CallInfo instance is considered equal to the current.
107         /// </summary>
108         /// <param name="obj">The instance of CallInfo to compare with the current instance.</param>
109         /// <returns>true if the specified instance is equal to the current one otherwise, false.</returns>
110         public override bool Equals(object obj) {
111             var other = obj as CallInfo;
112             return _argCount == other._argCount && _argNames.ListEquals(other._argNames);
113         }
114     }
115 }