xoreos  0.0.5
variable.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
28 
29 namespace Aurora {
30 
31 namespace ActionScript {
32 
34  Variable v;
35  v._type = kTypeNull;
36  return v;
37 }
38 
40 }
41 
43  _value.string = value;
44 }
45 
46 Variable::Variable(const char *value) : _type(kTypeString) {
47  _value.string = value;
48 }
49 
51  _value.object = ObjectPtr(value);
52 }
53 
54 Variable::Variable(double value) : _type(kTypeNumber) {
55  _value.number = value;
56 }
57 
58 Variable::Variable(unsigned int value) : _type(kTypeNumber) {
59  _value.number = value;
60 }
61 
62 Variable::Variable(unsigned long value) : _type(kTypeNumber) {
63  _value.number = value;
64 }
65 
66 Variable::Variable(bool value) : _type(kTypeBoolean) {
67  _value.boolean = value;
68 }
69 
71  _value.object = value;
72 }
73 
74 Variable::Variable(const Variable &variable) {
75  _type = variable._type;
76 
77  switch (variable._type) {
78  case kTypeObject:
79  _value.object = variable._value.object;
80  break;
81  case kTypeString:
82  _value.string = variable._value.string;
83  break;
84  case kTypeBoolean:
85  _value.boolean = variable._value.boolean;
86  break;
87  case kTypeNumber:
88  _value.number = variable._value.number;
89  break;
90  default:
91  _type = variable._type;
92  }
93 }
94 
96 }
97 
98 bool Variable::isUndefined() const {
99  return _type == kTypeUndefined;
100 }
101 
102 bool Variable::isObject() const {
103  return _type == kTypeObject;
104 }
105 
106 bool Variable::isString() const {
107  return _type == kTypeString;
108 }
109 
110 bool Variable::isNumber() const {
111  return _type == kTypeNumber;
112 }
113 
114 bool Variable::isFunction() const {
115  return _type == kTypeObject && dynamic_cast<Function *>(_value.object.get());
116 }
117 
118 double Variable::asNumber() const {
119  switch (_type) {
120  case kTypeNumber:
121  return _value.number;
122  case kTypeBoolean:
123  return _value.boolean ? 1 : 0;
124  default:
125  return 0;
126  }
127 }
128 
130  if (_type == kTypeUndefined) {
131  _type = kTypeObject;
132  _value.object = ObjectPtr(new Object());
133  }
134  return _value.object;
135 }
136 
138  return _value.string;
139 }
140 
141 bool Variable::asBoolean() const {
142  if (_type == kTypeNumber)
143  return _value.number != 0;
144  else if (_type == kTypeBoolean)
145  return _value.boolean;
146  else if (_type == kTypeObject)
147  return static_cast<bool>(_value.object.get());
148  else
149  return false;
150 }
151 
153  _type = v._type;
154  switch (_type) {
155  case kTypeString:
156  _value.string = v._value.string;
157  break;
158  case kTypeObject:
159  _value.object = v._value.object;
160  break;
161  case kTypeNumber:
162  _value.number = v._value.number;
163  break;
164  case kTypeBoolean:
165  _value.boolean = v._value.boolean;
166  break;
167  default:
168  _type = v._type;
169  }
170 }
171 
173  switch (_type) {
174  case kTypeNumber:
175  return !_value.number;
176  case kTypeBoolean:
177  return !_value.boolean;
178  default:
179  return true;
180  }
181 }
182 
184  if (v._type == kTypeNumber && _type == kTypeNumber)
185  return v.asNumber() && asNumber();
186  else
187  return false;
188 }
189 
191  if (v._type == kTypeNumber && _type == kTypeNumber)
192  return v.asNumber() || asNumber();
193  else
194  return false;
195 }
196 
198  if (_type != v._type)
199  return false;
200 
201  switch (_type) {
202  case kTypeNull:
203  return v._type == kTypeNull;
204  case kTypeNumber:
205  return v._value.number == _value.number;
206  default:
207  return false;
208  }
209 }
210 
212  if (v._type == kTypeNumber && _type == kTypeNumber)
213  return asNumber() < v.asNumber();
214  else
215  return false;
216 }
217 
219  if (v._type == kTypeNumber && _type == kTypeNumber)
220  return asNumber() - v.asNumber();
221  else
222  return 0.0;
223 }
224 
226  if (v._type == kTypeNumber && _type == kTypeNumber)
227  return asNumber() + v.asNumber();
228  else
229  return 0.0;
230 }
231 
233  return asNumber() * v.asNumber();
234 }
235 
237  return asNumber() / v.asNumber();
238 }
239 
241  return asNumber() + 1;
242 }
243 
244 } // End of namespace ActionScript
245 
246 } // End of namespace Aurora
Variable operator+(Variable v)
Definition: variable.cpp:225
void operator=(Variable v)
Definition: variable.cpp:152
A class holding an UTF-8 string.
Definition: ustring.h:48
Variable operator/(Variable v)
Definition: variable.cpp:236
A variable used in the execution context.
Variable operator||(Variable v)
Definition: variable.cpp:190
Variable operator &&(Variable v)
const Common::UString & asString() const
Definition: variable.cpp:137
Function objects for ActionScript.
static Variable Null()
Definition: variable.cpp:33
Variable operator*(Variable v)
Definition: variable.cpp:232
Variable operator-(Variable v)
Definition: variable.cpp:218
Abstract object which is inherited by every other class.
Variable operator==(Variable v)
Definition: variable.cpp:197
An action script variable.
Definition: variable.h:44
struct Aurora::ActionScript::Variable::@0 _value
boost::shared_ptr< Object > ObjectPtr
Definition: object.h:43
Variable operator<(Variable v)
Definition: variable.cpp:211