Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
GenericType.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  /// Base class for all generic types.
13  /// </summary>
14  public abstract class GenericType : TypeBase
15  {
16  #region Constructors and Destructors
17 
18  /// <summary>
19  /// Initializes a new instance of the <see cref="GenericType"/> class.
20  /// </summary>
21  /// <param name="name">
22  /// The name.
23  /// </param>
24  /// <param name="parameterCount">
25  /// The parameter count.
26  /// </param>
27  protected GenericType(string name, int parameterCount)
28  : base(name)
29  {
30  ParameterTypes = new List<Type>();
31  Parameters = new List<Node>();
32  for (int i = 0; i < parameterCount; i++)
33  {
34  Parameters.Add(null);
35  }
36  }
37 
38  #endregion
39 
40  #region Public Properties
41 
42  /// <summary>
43  /// Gets the full name.
44  /// </summary>
45  /// <returns>
46  /// A <see cref="System.String"/> that represents this instance.
47  /// </returns>
48  public override string ToString()
49  {
50  var builder = new StringBuilder();
51  builder.Append(Name).Append("<");
52  for (int i = 0; i < Parameters.Count; i++)
53  {
54  var parameter = Parameters[i];
55  if (i > 0)
56  {
57  builder.Append(",");
58  }
59 
60  builder.Append(parameter is TypeBase ? ((TypeBase)parameter).Name : parameter);
61  }
62 
63  builder.Append(">");
64 
65  return builder.ToString();
66  }
67 
68  /// <summary>
69  /// Gets or sets the parameter types.
70  /// </summary>
71  /// <value>
72  /// The parameter types.
73  /// </value>
74  public List<Type> ParameterTypes { get; set; }
75 
76  /// <summary>
77  /// Gets or sets the parameters.
78  /// </summary>
79  /// <value>
80  /// The parameters.
81  /// </value>
82  public List<Node> Parameters { get; set; }
83 
84  #endregion
85 
86  #region Public Methods
87 
88  /// <summary>
89  /// Equalses the specified other.
90  /// </summary>
91  /// <param name="other">
92  /// The other.
93  /// </param>
94  /// <returns>
95  /// <c>true</c> if the specified <see cref="GenericType"/> is equal to this instance; otherwise, <c>false</c>.
96  /// </returns>
97  public bool Equals(GenericType other)
98  {
99  if (ReferenceEquals(null, other))
100  {
101  return false;
102  }
103 
104  if (ReferenceEquals(this, other))
105  {
106  return true;
107  }
108 
109  //return base.Equals(other) && ParameterTypes.SequenceEqual(other.ParameterTypes) && Parameters.SequenceEqual(other.Parameters);
110  return base.Equals(other) && Parameters.SequenceEqual(other.Parameters);
111  }
112 
113  /// <inheritdoc />
114  public override bool Equals(object obj)
115  {
116  if (ReferenceEquals(null, obj))
117  {
118  return false;
119  }
120 
121  if (ReferenceEquals(this, obj))
122  {
123  return true;
124  }
125 
126  return Equals(obj as GenericType);
127  }
128 
129  /// <summary>
130  /// Gets the child nodes.
131  /// </summary>
132  /// <returns>
133  /// </returns>
134  public override IEnumerable<Node> Childrens()
135  {
136  ChildrenList.Clear();
137  foreach (var parameter in Parameters)
138  {
139  if (parameter is Node)
140  {
141  ChildrenList.Add((Node)parameter);
142  }
143  }
144 
145  return ChildrenList;
146  }
147 
148  /// <inheritdoc />
149  public override int GetHashCode()
150  {
151  var hashCode = base.GetHashCode() * 397;
152  foreach (var parameter in Parameters)
153  {
154  hashCode = (hashCode * 397) ^ (parameter != null ? parameter.GetHashCode() : 0);
155  }
156 
157  return hashCode;
158  }
159 
160  #endregion
161 
162  #region Operators
163 
164  /// <summary>
165  /// Implements the operator ==.
166  /// </summary>
167  /// <param name = "left">The left.</param>
168  /// <param name = "right">The right.</param>
169  /// <returns>
170  /// The result of the operator.
171  /// </returns>
172  public static bool operator ==(GenericType left, GenericType right)
173  {
174  return Equals(left, right);
175  }
176 
177  /// <summary>
178  /// Implements the operator !=.
179  /// </summary>
180  /// <param name = "left">The left.</param>
181  /// <param name = "right">The right.</param>
182  /// <returns>
183  /// The result of the operator.
184  /// </returns>
185  public static bool operator !=(GenericType left, GenericType right)
186  {
187  return !Equals(left, right);
188  }
189 
190  #endregion
191  }
192 
193  /// <summary>
194  /// Generic with one parameter.
195  /// </summary>
196  /// <typeparam name="T1">
197  /// The type of the parameter 1.
198  /// </typeparam>
199  public class GenericType<T1> : GenericType
200  {
201  #region Constants and Fields
202 
203  private static readonly List<Type> ParameterTypeT1 = new List<Type> { typeof(T1) };
204 
205  #endregion
206 
207  #region Constructors and Destructors
208 
209  /// <summary>
210  /// Initializes a new instance of the <see cref="GenericType&lt;T1&gt;"/> class.
211  /// </summary>
212  /// <param name="name">
213  /// The name.
214  /// </param>
215  public GenericType(string name)
216  : this(name, 1)
217  {
218  }
219 
220  /// <summary>
221  /// Initializes a new instance of the <see cref = "GenericType&lt;T1&gt;" /> class.
222  /// </summary>
223  public GenericType()
224  : this(null, 1)
225  {
226  }
227 
228  /// <summary>
229  /// Initializes a new instance of the <see cref="GenericType&lt;T1&gt;"/> class.
230  /// </summary>
231  /// <param name="name">
232  /// The name.
233  /// </param>
234  /// <param name="parameterCount">
235  /// The parameter count.
236  /// </param>
237  protected GenericType(string name, int parameterCount)
238  : base(name, parameterCount)
239  {
240  ParameterTypes = ParameterTypeT1;
241  }
242 
243  #endregion
244  }
245 
246  /// <summary>
247  /// Generic type with two parameters.
248  /// </summary>
249  /// <typeparam name="T1">
250  /// The type of the parameter 1.
251  /// </typeparam>
252  /// <typeparam name="T2">
253  /// The type of the parameter 2.
254  /// </typeparam>
255  public class GenericType<T1, T2> : GenericType<T1>
256  {
257  #region Constants and Fields
258 
259  private static readonly List<Type> ParameterTypeT1T2 = new List<Type> { typeof(T1), typeof(T2) };
260 
261  #endregion
262 
263  #region Constructors and Destructors
264 
265  /// <summary>
266  /// Initializes a new instance of the <see cref="GenericType&lt;T1, T2&gt;"/> class.
267  /// </summary>
268  /// <param name="name">
269  /// The name.
270  /// </param>
271  public GenericType(string name)
272  : this(name, 2)
273  {
274  }
275 
276  /// <summary>
277  /// Initializes a new instance of the <see cref = "GenericType&lt;T1, T2&gt;" /> class.
278  /// </summary>
279  public GenericType()
280  : this(null, 2)
281  {
282  }
283 
284  /// <summary>
285  /// Initializes a new instance of the <see cref="GenericType&lt;T1, T2&gt;"/> class.
286  /// </summary>
287  /// <param name="name">
288  /// The name.
289  /// </param>
290  /// <param name="parameterCount">
291  /// The parameter count.
292  /// </param>
293  protected GenericType(string name, int parameterCount)
294  : base(name, parameterCount)
295  {
296  ParameterTypes = ParameterTypeT1T2;
297  }
298 
299  #endregion
300  }
301 
302  /// <summary>
303  /// Generic type with three parameters.
304  /// </summary>
305  /// <typeparam name="T1">
306  /// The type of the parameter 1.
307  /// </typeparam>
308  /// <typeparam name="T2">
309  /// The type of the parameter 2.
310  /// </typeparam>
311  /// <typeparam name="T3">
312  /// The type of the parameter 3.
313  /// </typeparam>
314  public class GenericType<T1, T2, T3> : GenericType<T1, T2>
315  {
316  #region Constants and Fields
317 
318  private static readonly List<Type> ParameterTypeT1T2T3 = new List<Type> { typeof(T1), typeof(T2), typeof(T3) };
319 
320  #endregion
321 
322  #region Constructors and Destructors
323 
324  /// <summary>
325  /// Initializes a new instance of the <see cref="GenericType&lt;T1, T2, T3&gt;"/> class.
326  /// </summary>
327  /// <param name="name">
328  /// The name.
329  /// </param>
330  public GenericType(string name)
331  : this(name, 3)
332  {
333  ParameterTypes = ParameterTypeT1T2T3;
334  }
335 
336  /// <summary>
337  /// Initializes a new instance of the <see cref = "GenericType&lt;T1, T2, T3&gt;" /> class.
338  /// </summary>
339  public GenericType()
340  : this(null, 3)
341  {
342  ParameterTypes = ParameterTypeT1T2T3;
343  }
344 
345  /// <summary>
346  /// Initializes a new instance of the <see cref="GenericType&lt;T1, T2, T3&gt;"/> class.
347  /// </summary>
348  /// <param name="name">
349  /// The name.
350  /// </param>
351  /// <param name="parameterCount">
352  /// The parameter count.
353  /// </param>
354  protected GenericType(string name, int parameterCount)
355  : base(name, parameterCount)
356  {
357  ParameterTypes = ParameterTypeT1T2T3;
358  }
359 
360  #endregion
361  }
362 }
GenericType()
Initializes a new instance of the GenericType<T1, T2> class.
Definition: GenericType.cs:279
bool Equals(GenericType other)
Equalses the specified other.
Definition: GenericType.cs:97
GenericType(string name, int parameterCount)
Initializes a new instance of the GenericType<T1, T2> class.
Definition: GenericType.cs:293
GenericType(string name)
Initializes a new instance of the GenericType<T1> class.
Definition: GenericType.cs:215
GenericType()
Initializes a new instance of the GenericType<T1> class.
Definition: GenericType.cs:223
GenericType(string name, int parameterCount)
Initializes a new instance of the GenericType class.
Definition: GenericType.cs:27
Abstract node.
Definition: Node.cs:15
override string ToString()
Gets the full name.
Definition: GenericType.cs:48
override IEnumerable< Node > Childrens()
Gets the child nodes.
Definition: GenericType.cs:134
List< Node > Parameters
Gets or sets the parameters.
Definition: GenericType.cs:82
Base type for all types.
Definition: TypeBase.cs:11
override bool Equals(object obj)
Definition: GenericType.cs:114
Base class for all generic types.
Definition: GenericType.cs:14
GenericType()
Initializes a new instance of the GenericType<T1, T2, T3> class.
Definition: GenericType.cs:339
GenericType(string name)
Initializes a new instance of the GenericType<T1, T2, T3> class.
Definition: GenericType.cs:330
GenericType(string name)
Initializes a new instance of the GenericType<T1, T2> class.
Definition: GenericType.cs:271
GenericType(string name, int parameterCount)
Initializes a new instance of the GenericType<T1, T2, T3> class.
Definition: GenericType.cs:354
GenericType(string name, int parameterCount)
Initializes a new instance of the GenericType<T1> class.
Definition: GenericType.cs:237