Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ArrayType.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections;
5 using System.Collections.Generic;
6 using System.Linq;
7 using System.Text;
8 
9 namespace SiliconStudio.Shaders.Ast
10 {
11  /// <summary>
12  /// Array type.
13  /// </summary>
14  public class ArrayType : TypeBase
15  {
16  #region Constructors and Destructors
17 
18  /// <summary>
19  /// Initializes a new instance of the <see cref = "ArrayType" /> class.
20  /// </summary>
21  public ArrayType() : base("$array")
22  {
23  Dimensions = new List<Expression>();
24  }
25 
26  /// <summary>
27  /// Initializes a new instance of the <see cref="ArrayType"/> class.
28  /// </summary>
29  /// <param name="type">The type.</param>
30  /// <param name="dimensions">The dimensions.</param>
31  public ArrayType(TypeBase type, params Expression[] dimensions) : base("$array")
32  {
33  Type = type;
34  Dimensions = new List<Expression>();
35  Dimensions.AddRange(dimensions);
36  }
37 
38  #endregion
39 
40  #region Public Properties
41 
42  /// <summary>
43  /// Gets or sets the dimensions.
44  /// </summary>
45  /// <value>
46  /// The dimensions.
47  /// </value>
48  public List<Expression> Dimensions { get; set; }
49 
50  /// <inheritdoc />
51  public override string ToString()
52  {
53  var indices = new StringBuilder();
54  foreach (var index in Dimensions)
55  {
56  indices.Append("[").Append(index).Append("]");
57  }
58 
59  return string.Format("{0}{1}", Type, indices);
60  }
61 
62  /// <summary>
63  /// Gets or sets the type.
64  /// </summary>
65  /// <value>
66  /// The type.
67  /// </value>
68  public TypeBase Type { get; set; }
69 
70  /// <summary>
71  /// Gets a value indicating whether this instance is dimension empty.
72  /// </summary>
73  /// <value>
74  /// <c>true</c> if this instance is dimension empty; otherwise, <c>false</c>.
75  /// </value>
76  public bool IsDimensionEmpty
77  {
78  get { return Dimensions.Count == 1 && Dimensions[0] is EmptyExpression; }
79  }
80 
81  #endregion
82 
83  #region Public Methods
84 
85  public bool Equals(ArrayType other)
86  {
87  if (ReferenceEquals(null, other)) return false;
88  if (ReferenceEquals(this, other)) return true;
89 
90  if ( !base.Equals(other) || !Equals(other.Type.ResolveType(), Type.ResolveType()) || other.Dimensions.Count != Dimensions.Count) return false;
91 
92  // Check that dimensions are all equals
93  return !Dimensions.Where((t, i) => t != other.Dimensions[i]).Any();
94  }
95 
96  public override bool Equals(object obj)
97  {
98  if (ReferenceEquals(null, obj)) return false;
99  if (ReferenceEquals(this, obj)) return true;
100  return Equals(obj as ArrayType);
101  }
102 
103  public override int GetHashCode()
104  {
105  unchecked
106  {
107  int result = base.GetHashCode();
108  result = (result * 397) ^ (Dimensions != null ? Dimensions.GetHashCode() : 0);
109  result = (result * 397) ^ (Type != null ? Type.GetHashCode() : 0);
110  return result;
111  }
112  }
113 
114  public static bool operator ==(ArrayType left, ArrayType right)
115  {
116  return Equals(left, right);
117  }
118 
119  public static bool operator !=(ArrayType left, ArrayType right)
120  {
121  return !Equals(left, right);
122  }
123 
124  /// <inheritdoc />
125  public override IEnumerable<Node> Childrens()
126  {
127  return Dimensions;
128  }
129 
130  // TODO Implements equals for array types
131 
132  #endregion
133  }
134 }
ArrayType(TypeBase type, params Expression[] dimensions)
Initializes a new instance of the ArrayType class.
Definition: ArrayType.cs:31
virtual TypeBase ResolveType()
Resolves the type.
Definition: TypeBase.cs:101
bool Equals(ArrayType other)
Definition: ArrayType.cs:85
ArrayType()
Initializes a new instance of the ArrayType class.
Definition: ArrayType.cs:21
override IEnumerable< Node > Childrens()
Gets the child nodes. An enumeration of child nodes
Definition: ArrayType.cs:125
override bool Equals(object obj)
Definition: ArrayType.cs:96
List< Expression > Dimensions
Gets or sets the dimensions.
Definition: ArrayType.cs:48
Base type for all types.
Definition: TypeBase.cs:11
TypeBase Type
Gets or sets the type.
Definition: ArrayType.cs:68