Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
Literal.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.Globalization;
6 using System.Text;
7 
8 namespace SiliconStudio.Shaders.Ast
9 {
10  /// <summary>
11  /// A field of a struct.
12  /// </summary>
13  public sealed class Literal : Node
14  {
15  private object value;
16 
17 
18  #region Constructors and Destructors
19 
20  /// <summary>
21  /// Initializes a new instance of the <see cref = "Literal" /> class.
22  /// </summary>
23  public Literal()
24  {
25  }
26 
27  /// <summary>
28  /// Initializes a new instance of the <see cref="Literal"/> class.
29  /// </summary>
30  /// <param name="value">
31  /// The value.
32  /// </param>
33  public Literal(object value)
34  {
35  Value = value;
36  }
37 
38  #endregion
39 
40  #region Public Properties
41 
42  /// <summary>
43  /// Gets or sets the text.
44  /// </summary>
45  /// <value>
46  /// The text.
47  /// </value>
48  public string Text { get; set; }
49 
50  /// <summary>
51  /// Gets or sets the value.
52  /// </summary>
53  /// <value>
54  /// The value.
55  /// </value>
56  public object Value
57  {
58  get
59  {
60  return value;
61  }
62 
63  set
64  {
65  this.value = value;
66  Text = ConvertValueToString(value);
67  }
68  }
69 
70  /// <summary>
71  /// Gets or sets the sub literals.
72  /// </summary>
73  /// <value>
74  /// The sub literals.
75  /// </value>
76  /// <remarks>
77  /// This value can be null.
78  /// </remarks>
79  public List<Literal> SubLiterals { get; set; }
80 
81  #endregion
82 
83  #region Public Methods
84 
85  /// <inheritdoc />
86  public override IEnumerable<Node> Childrens()
87  {
88  ChildrenList.Clear();
89  if (SubLiterals != null) ChildrenList.AddRange(SubLiterals);
90  return ChildrenList;
91  }
92 
93  /// <inheritdoc />
94  public override string ToString()
95  {
96  var str = Text;
97  if (SubLiterals != null) str = string.Join(" ", SubLiterals);
98  return string.Format("{0}", str);
99  }
100 
101  private static string ConvertValueToString(object value)
102  {
103  if (value is float)
104  {
105  string defaultString = ((float)value).ToString("g", CultureInfo.InvariantCulture);
106  if (!defaultString.Contains(".") && !defaultString.Contains("e"))
107  defaultString += ".0";
108  return defaultString;
109  }
110  if (value is double)
111  {
112  string defaultString = ((double)value).ToString("g", CultureInfo.InvariantCulture);
113  if (!defaultString.Contains(".") && !defaultString.Contains("e"))
114  defaultString += ".0";
115  return defaultString;
116  }
117  if (value is int)
118  return ((int)value).ToString(CultureInfo.InvariantCulture);
119  if (value is uint)
120  return ((uint)value).ToString(CultureInfo.InvariantCulture);
121 
122  return value.ToString();
123  }
124 
125  /// <inheritdoc/>
126  public bool Equals(Literal other)
127  {
128  if (ReferenceEquals(null, other))
129  {
130  return false;
131  }
132  if (ReferenceEquals(this, other))
133  {
134  return true;
135  }
136  return Equals(other.value, value);
137  }
138 
139  /// <inheritdoc/>
140  public override bool Equals(object obj)
141  {
142  if (ReferenceEquals(null, obj))
143  {
144  return false;
145  }
146  if (ReferenceEquals(this, obj))
147  {
148  return true;
149  }
150  if (obj.GetType() != typeof(Literal))
151  {
152  return false;
153  }
154  return Equals((Literal)obj);
155  }
156 
157  /// <inheritdoc/>
158  public override int GetHashCode()
159  {
160  return (value != null ? value.GetHashCode() : 0);
161  }
162 
163  /// <summary>
164  /// Implements the operator ==.
165  /// </summary>
166  /// <param name="left">The left.</param>
167  /// <param name="right">The right.</param>
168  /// <returns>
169  /// The result of the operator.
170  /// </returns>
171  public static bool operator ==(Literal left, Literal right)
172  {
173  return Equals(left, right);
174  }
175 
176  /// <summary>
177  /// Implements the operator !=.
178  /// </summary>
179  /// <param name="left">The left.</param>
180  /// <param name="right">The right.</param>
181  /// <returns>
182  /// The result of the operator.
183  /// </returns>
184  public static bool operator !=(Literal left, Literal right)
185  {
186  return !Equals(left, right);
187  }
188 
189  #endregion
190  }
191 }
override IEnumerable< Node > Childrens()
Gets the child nodes. An enumeration of child nodes
Definition: Literal.cs:86
override bool Equals(object obj)
Definition: Literal.cs:140
Abstract node.
Definition: Node.cs:15
override string ToString()
Definition: Literal.cs:94
Literal()
Initializes a new instance of the Literal class.
Definition: Literal.cs:23
bool Equals(Literal other)
Definition: Literal.cs:126
Literal(object value)
Initializes a new instance of the Literal class.
Definition: Literal.cs:33
A field of a struct.
Definition: Literal.cs:13