Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
Identifier.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.Generic;
5 using System.Text;
6 
7 namespace SiliconStudio.Shaders.Ast
8 {
9  /// <summary>
10  /// An identifier.
11  /// </summary>
12  public class Identifier : Node
13  {
14  #region Constructors and Destructors
15 
16  /// <summary>
17  /// Initializes a new instance of the <see cref = "Identifier" /> class.
18  /// </summary>
19  public Identifier()
20  {
21  }
22 
23  /// <summary>
24  /// Initializes a new instance of the <see cref="Identifier"/> class.
25  /// </summary>
26  /// <param name="name">
27  /// The name.
28  /// </param>
29  public Identifier(string name)
30  {
31  Text = name;
32  }
33 
34  #endregion
35 
36  #region Public Properties
37 
38  /// <summary>
39  /// Gets a value indicating whether this instance has indices.
40  /// </summary>
41  /// <value>
42  /// <c>true</c> if this instance has indices; otherwise, <c>false</c>.
43  /// </value>
44  public bool HasIndices
45  {
46  get
47  {
48  return Indices != null && Indices.Count > 0;
49  }
50  }
51 
52  /// <summary>
53  /// Gets or sets the indices.
54  /// </summary>
55  /// <value>
56  /// The indices.
57  /// </value>
58  /// <remarks>
59  /// This property can be null.
60  /// </remarks>
61  public List<Expression> Indices { get; set; }
62 
63  /// <summary>
64  /// Gets or sets a value indicating whether this instance is a special reference using &lt; &gt;
65  /// </summary>
66  /// <value>
67  /// <c>true</c> if this instance is special reference; otherwise, <c>false</c>.
68  /// </value>
69  public bool IsSpecialReference { get; set; }
70 
71  /// <summary>
72  /// Gets or sets the name.
73  /// </summary>
74  /// <value>
75  /// The name.
76  /// </value>
77  public string Text { get; set; }
78 
79  #endregion
80 
81  #region Public Methods
82 
83  /// <summary>
84  /// Equalses the specified other.
85  /// </summary>
86  /// <param name="other">
87  /// The other.
88  /// </param>
89  /// <returns>
90  /// true if equals to other.
91  /// </returns>
92  public bool Equals(Identifier other)
93  {
94  if (ReferenceEquals(null, other))
95  {
96  return false;
97  }
98  if (ReferenceEquals(this, other))
99  {
100  return true;
101  }
102  return Equals(other.Text, this.Text) && other.IsSpecialReference.Equals(this.IsSpecialReference);
103  }
104 
105  /// <inheritdoc />
106  public override bool Equals(object obj)
107  {
108  if (ReferenceEquals(null, obj))
109  {
110  return false;
111  }
112  if (ReferenceEquals(this, obj))
113  {
114  return true;
115  }
116  var other = obj as Identifier;
117  if (other == null)
118  return false;
119 
120  return Equals(other.Text, Text) && other.IsSpecialReference == IsSpecialReference;
121  }
122 
123  /// <inheritdoc />
124  public override int GetHashCode()
125  {
126  unchecked
127  {
128  int result = this.Text != null ? this.Text.GetHashCode() : 0;
129  result = (result * 397) ^ this.IsSpecialReference.GetHashCode();
130  return result;
131  }
132  }
133 
134  /// <summary>
135  /// Returns a <see cref = "System.String" /> that represents this instance.
136  /// </summary>
137  /// <returns>
138  /// A <see cref = "System.String" /> that represents this instance.
139  /// </returns>
140  /// <inheritdoc />
141  public override string ToString()
142  {
143  var ranks = new StringBuilder();
144  if (Indices != null)
145  {
146  foreach (var expression in Indices)
147  {
148  ranks.Append("[").Append(expression).Append("]");
149  }
150  }
151 
152  return string.Format(IsSpecialReference ? "<{0}{1}>" : "{0}{1}", this.Text, ranks);
153  }
154 
155  #endregion
156 
157  #region Operators
158 
159  /// <summary>
160  /// Implements the operator ==.
161  /// </summary>
162  /// <param name = "left">The left.</param>
163  /// <param name = "right">The right.</param>
164  /// <returns>
165  /// The result of the operator.
166  /// </returns>
167  public static bool operator ==(Identifier left, Identifier right)
168  {
169  return Equals(left, right);
170  }
171 
172  /// <summary>
173  /// Performs an implicit conversion from <see cref = "Identifier" /> to <see cref = "System.String" />.
174  /// </summary>
175  /// <param name = "identifier">The identifier.</param>
176  /// <returns>
177  /// The result of the conversion.
178  /// </returns>
179  public static implicit operator string(Identifier identifier)
180  {
181  return identifier.ToString();
182  }
183 
184  /// <summary>
185  /// Performs an implicit conversion from <see cref="System.String"/> to <see cref="Identifier"/>.
186  /// </summary>
187  /// <param name="identifierName">Name of the identifier.</param>
188  /// <returns>
189  /// The result of the conversion.
190  /// </returns>
191  public static implicit operator Identifier(string identifierName)
192  {
193  return new Identifier(identifierName);
194  }
195 
196  /// <summary>
197  /// Implements the operator !=.
198  /// </summary>
199  /// <param name = "left">The left.</param>
200  /// <param name = "right">The right.</param>
201  /// <returns>
202  /// The result of the operator.
203  /// </returns>
204  public static bool operator !=(Identifier left, Identifier right)
205  {
206  return !Equals(left, right);
207  }
208 
209  #endregion
210  }
211 }
Identifier(string name)
Initializes a new instance of the Identifier class.
Definition: Identifier.cs:29
string Text
Gets or sets the name.
Definition: Identifier.cs:77
Abstract node.
Definition: Node.cs:15
Identifier()
Initializes a new instance of the Identifier class.
Definition: Identifier.cs:19
bool Equals(Identifier other)
Equalses the specified other.
Definition: Identifier.cs:92
override string ToString()
Returns a System.String that represents this instance.
Definition: Identifier.cs:141
override bool Equals(object obj)
Definition: Identifier.cs:106