Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
Semantic.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 
7 namespace SiliconStudio.Shaders.Ast.Hlsl
8 {
9  using System.Text.RegularExpressions;
10 
11  /// <summary>
12  /// Describes a semantic.
13  /// </summary>
14  public class Semantic : Qualifier
15  {
16  #region Constructors and Destructors
17 
18  /// <summary>
19  /// Initializes a new instance of the <see cref = "Semantic" /> class.
20  /// </summary>
21  public Semantic()
22  : base("semantic")
23  {
24  IsPost = true;
25  }
26 
27  /// <summary>
28  /// Initializes a new instance of the <see cref="Semantic"/> class.
29  /// </summary>
30  /// <param name="name">
31  /// The name.
32  /// </param>
33  public Semantic(string name)
34  : base("semantic")
35  {
36  Name = new Identifier(name);
37  IsPost = true;
38  }
39 
40  #endregion
41 
42  #region Public Properties
43 
44  /// <summary>
45  /// Gets or sets the name.
46  /// </summary>
47  /// <value>
48  /// The name.
49  /// </value>
50  public Identifier Name { get; set; }
51 
52  /// <summary>
53  /// Gets the base name of a semantic (COLOR1 -> COLOR)
54  /// </summary>
55  /// <value>
56  /// The base name of sematnic.
57  /// </value>
58  public string BaseName
59  {
60  get
61  {
62  var match = MatchSemanticName.Match(Name.Text);
63  return match.Groups[1].Value;
64  }
65  }
66 
67  /// <summary>
68  /// Parses the specified semantic.
69  /// </summary>
70  /// <param name="text">The semantic.</param>
71  /// <returns>The base name and index. COLOR1 -> {COLOR, 1}</returns>
72  public static KeyValuePair<string, int> Parse(string text)
73  {
74  var match = MatchSemanticName.Match(text);
75  if (!match.Success)
76  return new KeyValuePair<string, int>(text, 0);
77 
78  string baseName = match.Groups[1].Value;
79  int value = 0;
80  if (!string.IsNullOrEmpty(match.Groups[2].Value))
81  {
82  value = int.Parse(match.Groups[2].Value);
83  }
84 
85  return new KeyValuePair<string, int>(baseName, value);
86  }
87 
88  #endregion
89 
90  #region Public Methods
91 
92  /// <summary>
93  /// Determines whether the specified <see cref = "Semantic" /> is equal to this instance.
94  /// </summary>
95  /// <param name = "other">The <see cref = "Semantic" /> to compare with this instance.</param>
96  /// <returns>
97  /// <c>true</c> if the specified <see cref = "Semantic" /> is equal to this instance; otherwise, <c>false</c>.
98  /// </returns>
99  /// <inheritdoc />
100  public bool Equals(Semantic other)
101  {
102  if (ReferenceEquals(null, other))
103  {
104  return false;
105  }
106 
107  if (ReferenceEquals(this, other))
108  {
109  return true;
110  }
111 
112  return base.Equals(other) && Equals(other.Name, Name);
113  }
114 
115  /// <inheritdoc />
116  public override bool Equals(object obj)
117  {
118  if (ReferenceEquals(null, obj))
119  {
120  return false;
121  }
122 
123  if (ReferenceEquals(this, obj))
124  {
125  return true;
126  }
127 
128  return Equals(obj as Semantic);
129  }
130 
131  /// <inheritdoc />
132  public override IEnumerable<Node> Childrens()
133  {
134  ChildrenList.Clear();
135  ChildrenList.Add(Name);
136  return ChildrenList;
137  }
138 
139  /// <inheritdoc />
140  public override int GetHashCode()
141  {
142  unchecked
143  {
144  return (base.GetHashCode() * 397) ^ (Name != null ? Name.GetHashCode() : 0);
145  }
146  }
147 
148  /// <inheritdoc />
149  public override string DisplayName
150  {
151  get
152  {
153  return string.Format(": {0}", Name);
154  }
155  }
156 
157  #endregion
158 
159  #region Operators
160 
161  /// <summary>
162  /// Implements the operator ==.
163  /// </summary>
164  /// <param name = "left">The left.</param>
165  /// <param name = "right">The right.</param>
166  /// <returns>
167  /// The result of the operator.
168  /// </returns>
169  public static bool operator ==(Semantic left, Semantic right)
170  {
171  return Equals(left, right);
172  }
173 
174  /// <summary>
175  /// Implements the operator !=.
176  /// </summary>
177  /// <param name = "left">The left.</param>
178  /// <param name = "right">The right.</param>
179  /// <returns>
180  /// The result of the operator.
181  /// </returns>
182  public static bool operator !=(Semantic left, Semantic right)
183  {
184  return !Equals(left, right);
185  }
186 
187  private readonly static Regex MatchSemanticName = new Regex(@"([A-Za-z_]+)(\d*)");
188 
189  #endregion
190  }
191 }
override IEnumerable< Node > Childrens()
Gets the child nodes. An enumeration of child nodes
Definition: Semantic.cs:132
override bool Equals(object obj)
Definition: Semantic.cs:116
Identifier Name
Gets or sets the name.
Definition: Semantic.cs:50
Semantic(string name)
Initializes a new instance of the Semantic class.
Definition: Semantic.cs:33
static KeyValuePair< string, int > Parse(string text)
Parses the specified semantic.
Definition: Semantic.cs:72
Semantic()
Initializes a new instance of the Semantic class.
Definition: Semantic.cs:21
bool Equals(Semantic other)
Determines whether the specified Semantic is equal to this instance.
Definition: Semantic.cs:100