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 
25 #include "src/common/error.h"
26 
28 #include "src/aurora/lua/table.h"
30 
31 namespace Aurora {
32 
33 namespace Lua {
34 
35 Variable::Variable(Type type, const Common::UString &exactType) : _type(kTypeNone) {
36  setType(type, exactType);
37 }
38 
39 Variable::Variable(bool value) : _type(kTypeNone) {
41  *this = value;
42 }
43 
46  *this = value;
47 }
48 
49 Variable::Variable(float value) : _type(kTypeNone) {
51  *this = value;
52 }
53 
54 Variable::Variable(const char *value) : _type(kTypeNone) {
56  *this = value;
57 }
58 
61  *this = value;
62 }
63 
64 Variable::Variable(const TableRef &value) : _type(kTypeNone) {
66  *this = value;
67 }
68 
69 Variable::Variable(const FunctionRef &value) : _type(kTypeNone) {
71  *this = value;
72 }
73 
74 Variable::Variable(void *value, const Common::UString &exactType) : _type(kTypeNone) {
75  assert(!exactType.empty());
76 
77  setType(kTypeUserType, exactType);
78  *this = value;
79 }
80 
81 Variable::Variable(const Variable &var) : _type(kTypeNone) {
82  *this = var;
83 }
84 
87 }
88 
89 void Variable::setType(Type type, const Common::UString &exactType) {
90  switch (_type) {
91  case kTypeString:
92  delete _value._string;
93  _value._string = 0;
94  break;
95  case kTypeTable:
96  delete _value._table;
97  _value._table = 0;
98  break;
99  case kTypeFunction:
100  delete _value._function;
101  _value._function = 0;
102  break;
103  default:
104  break;
105  }
106 
107  _type = type;
108 
109  switch (_type) {
110  case kTypeNone:
111  _exactType = "none";
112  break;
113  case kTypeNil:
114  _exactType = "nil";
115  break;
116  case kTypeBoolean:
117  _value._bool = false;
118  _exactType = "boolean";
119  break;
120  case kTypeNumber:
121  _value._float = 0.0f;
122  _exactType = "number";
123  break;
124  case kTypeString:
125  _value._string = new Common::UString();
126  _exactType = "string";
127  break;
128  case kTypeTable:
129  _value._table = new TableRef();
130  _exactType = "table";
131  break;
132  case kTypeFunction:
133  _value._function = new FunctionRef();
134  _exactType = "function";
135  break;
136  case kTypeUserType:
137  assert(!exactType.empty());
138 
139  _value._data = 0;
140  _exactType = exactType;
141  break;
142  default:
143  throw Common::Exception("Variable::setType(): Invalid type %d", type);
144  break;
145  }
146 }
147 
149  if (this == &var) {
150  return *this;
151  }
152 
153  setType(var._type, var._exactType);
154 
155  switch (_type) {
156  case kTypeString:
157  *_value._string = *var._value._string;
158  break;
159  case kTypeTable:
160  *_value._table = *var._value._table;
161  _exactType = _value._table->getExactType();
162  break;
163  case kTypeFunction:
164  *_value._function = *var._value._function;
165  break;
166  default:
167  _value = var._value;
168  break;
169  }
170  return *this;
171 }
172 
174  if (_type != kTypeBoolean) {
175  throw Common::Exception("Can't assign a boolean value to a non-boolean variable");
176  }
177 
178  _value._bool = value;
179  return *this;
180 }
181 
183  if (_type != kTypeNumber) {
184  throw Common::Exception("Can't assign an int value to a non-number variable");
185  }
186 
187  _value._float = value;
188  return *this;
189 }
190 
192  if (_type != kTypeNumber) {
193  throw Common::Exception("Can't assign a float value to a non-number variable");
194  }
195 
196  _value._float = value;
197  return *this;
198 }
199 
200 Variable &Variable::operator=(const char *value) {
201  if (_type != kTypeString) {
202  throw Common::Exception("Can't assign a string value to a non-string variable");
203  }
204 
205  *_value._string = value;
206  return *this;
207 }
208 
210  if (_type != kTypeString) {
211  throw Common::Exception("Can't assign a string value to a non-string variable");
212  }
213 
214  *_value._string = value;
215  return *this;
216 }
217 
219  if (_type != kTypeTable) {
220  throw Common::Exception("Can't assign a table value to a non-table variable");
221  }
222 
223  *_value._table = value;
224  _exactType = _value._table->getExactType();
225  return *this;
226 }
227 
229  if (_type != kTypeFunction) {
230  throw Common::Exception("Can't assign a function value to a non-function variable");
231  }
232 
233  *_value._function = value;
234  return *this;
235 }
236 
238  if (_type != kTypeUserType) {
239  throw Common::Exception("Can't assign a raw value to a non-usertype variable");
240  }
241 
242  _value._data = value;
243  return *this;
244 }
245 
246 bool Variable::operator==(const Variable &var) const {
247  if (this == &var) {
248  return true;
249  }
250 
251  if (_type != var._type) {
252  return false;
253  }
254 
255  switch (_type) {
256  case kTypeNil:
257  return true;
258  case kTypeBoolean:
259  return _value._bool == var._value._bool;
260  case kTypeNumber:
261  return _value._float == var._value._float;
262  case kTypeString:
263  return *_value._string == *var._value._string;
264  case kTypeTable:
265  case kTypeFunction:
266  return false; // TODO
267  case kTypeUserType:
268  return _value._data == var._value._data;
269  default:
270  break;
271  }
272  return false;
273 }
274 
275 bool Variable::operator!=(const Variable &var) const {
276  return !(*this == var);
277 }
278 
280  return _type;
281 }
282 
284  return _exactType;
285 }
286 
287 bool Variable::getBool() const {
288  if (_type != kTypeBoolean) {
289  throw Common::Exception("Can't get a boolean value from a non-boolean variable");
290  }
291 
292  return _value._bool;
293 }
294 
296  if (_type != kTypeNumber) {
297  throw Common::Exception("Can't get an int value from a non-number variable");
298  }
299 
300  return _value._float;
301 }
302 
303 float Variable::getFloat() const {
304  if (_type != kTypeNumber) {
305  throw Common::Exception("Can't get a float value from a non-number variable");
306  }
307 
308  return _value._float;
309 }
310 
312  if (_type != kTypeString) {
313  throw Common::Exception("Can't get a string value from a non-string variable");
314  }
315 
316  return *_value._string;
317 }
318 
320  if (_type != kTypeString) {
321  throw Common::Exception("Can't get a string value from a non-string variable");
322  }
323 
324  return *_value._string;
325 }
326 
328  if (_type != kTypeTable) {
329  throw Common::Exception("Can't get a table value from a non-table variable");
330  }
331 
332  return *_value._table;
333 }
334 
335 const TableRef &Variable::getTable() const {
336  if (_type != kTypeTable) {
337  throw Common::Exception("Can't get a table value from a non-table variable");
338  }
339 
340  return *_value._table;
341 }
342 
344  if (_type != kTypeFunction) {
345  throw Common::Exception("Can't get a function value from a non-function variable");
346  }
347 
348  return *_value._function;
349 }
350 
352  if (_type != kTypeUserType) {
353  throw Common::Exception("Can't get a raw usertype value from a non-usertype variable");
354  }
355 
356  return _value._data;
357 }
358 
359 } // End of namespace Lua
360 
361 } // End of namespace Aurora
void * getRawUserType() const
Definition: variable.cpp:351
FunctionRef * _function
Definition: variable.h:91
A class holding an UTF-8 string.
Definition: ustring.h:48
int32 getInt() const
Definition: variable.cpp:295
A reference to a Lua function.
const FunctionRef & getFunction() const
Definition: variable.cpp:343
TableRef & getTable()
Definition: variable.cpp:327
Type getType() const
Definition: variable.cpp:279
void setType(Type type, const Common::UString &exactType="")
Definition: variable.cpp:89
bool operator==(const Variable &var) const
Definition: variable.cpp:246
float getFloat() const
Definition: variable.cpp:303
Common::UString _exactType
Definition: variable.h:84
Basic exceptions to throw.
Common::UString & getString()
Definition: variable.cpp:319
bool getBool() const
Definition: variable.cpp:287
union Aurora::Lua::Variable::@5 _value
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
Variable & operator=(const Variable &var)
Definition: variable.cpp:148
Variable(Type type, const Common::UString &exactType="")
Definition: variable.cpp:35
StackException Exception
Definition: error.h:59
A reference to a Lua function.
Definition: function.h:35
const Common::UString & getExactType() const
Definition: variable.cpp:283
TableRef * _table
Definition: variable.h:90
A reference to a Lua table.
Definition: table.h:37
A reference to a Lua table.
Common::UString * _string
Definition: variable.h:89
Lua variable.
bool operator!=(const Variable &var) const
Definition: variable.cpp:275
int32_t int32
Definition: types.h:203