aboutsummaryrefslogtreecommitdiff
path: root/engines/zvision/object.cpp
blob: 253eaf7ace2a084b7e4d92904bc326532826d09d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "common/scummsys.h"
#include "common/textconsole.h"

#include "zvision/object.h"

namespace ZVision {

Object::Object(ObjectType type) {
	_objectType = type;

	switch (type) {
	case BOOL:
		_value.boolVal = new bool;
		break;
	case BYTE:
		_value.byteVal = new byte;
		break;
	case INT16:
		_value.int16Val = new int16;
		break;
	case UINT16:
		_value.uint16Val = new uint16;
		break;
	case INT32:
		_value.int32Val = new int32;
		break;
	case UINT32:
		_value.uint32Val = new uint32;
		break;
	case FLOAT:
		_value.floatVal = new float;
		break;
	case DOUBLE:
		_value.doubleVal = new double;
		break;
	case STRING:
		_value.stringVal = new Common::String;
		break;
	}
}

Object::Object(bool value) : _objectType(BOOL) {
	_value.boolVal = new bool(value);
}

Object::Object(byte value) : _objectType(BYTE) {
	_value.byteVal = new byte(value);
}

Object::Object(int16 value) : _objectType(INT16) {
	_value.int16Val = new int16(value);
}

Object::Object(uint16 value) : _objectType(UINT16) {
	_value.uint16Val = new uint16(value);
}

Object::Object(int32 value) : _objectType(INT32) {
	_value.int32Val = new int32(value);
}

Object::Object(uint32 value) : _objectType(UINT32) {
	_value.uint32Val = new uint32(value);
}

Object::Object(float value) : _objectType(FLOAT) {
	_value.floatVal = new float(value);
}

Object::Object(double value) : _objectType(DOUBLE) {
	_value.doubleVal = new double(value);
}

Object::Object(Common::String value) : _objectType(BYTE) {
	_value.stringVal = new Common::String(value);
}

Object::Object(const Object& other) {
	_objectType = other._objectType;

	switch (_objectType) {
	case BOOL:
		_value.boolVal = new bool(*other._value.boolVal);
		break;
	case BYTE:
		_value.byteVal = new byte(*other._value.byteVal);
		break;
	case INT16:
		_value.int16Val = new int16(*other._value.int16Val);
		break;
	case UINT16:
		_value.uint16Val = new uint16(*other._value.uint16Val);
		break;
	case INT32:
		_value.int32Val = new int32(*other._value.int32Val);
		break;
	case UINT32:
		_value.uint32Val = new uint32(*other._value.uint32Val);
		break;
	case FLOAT:
		_value.floatVal = new float(*other._value.floatVal);
		break;
	case DOUBLE:
		_value.doubleVal = new double(*other._value.doubleVal);
		break;
	case STRING:
		_value.stringVal = new Common::String(*other._value.stringVal);
		break;
	}
}

Object::~Object() {
	deleteValue();
}

void Object::deleteValue() {
	// Call delete on the correct part of the union.
	// Even though they all point to the same memory and will all be cast 
	// to a void *, compiler optimizations could cause undefined behavior.
	switch (_objectType) {
	case BOOL:
		delete _value.boolVal;
		break;
	case BYTE:
		delete _value.byteVal;
		break;
	case INT16:
		delete _value.int16Val;
		break;
	case UINT16:
		delete _value.uint16Val;
		break;
	case INT32:
		delete _value.int32Val;
		break;
	case UINT32:
		delete _value.uint32Val;
		break;
	case FLOAT:
		delete _value.floatVal;
		break;
	case DOUBLE:
		delete _value.doubleVal;
		break;
	case STRING:
		delete _value.stringVal;
		break;
	}
}


Object& Object::operator=(const bool& rhs) {
	if (_objectType == BOOL)
		*_value.boolVal = rhs;
	else {
		deleteValue();
		_objectType = BOOL;
		_value.boolVal = new bool(rhs);
	}

	return *this;
}

Object& Object::operator=(const byte& rhs) {
	if (_objectType == BYTE)
		*_value.byteVal = rhs;
	else {
		deleteValue();
		_objectType = BYTE;
		_value.byteVal = new byte(rhs);
	}

	return *this;
}

Object& Object::operator=(const int16& rhs) {
	if (_objectType == INT16)
		*_value.int16Val = rhs;
	else {
		deleteValue();
		_objectType = INT16;
		_value.int16Val = new int16(rhs);
	}

	return *this;
}

Object& Object::operator=(const uint16& rhs) {
	if (_objectType == UINT16)
		*_value.uint16Val = rhs;
	else {
		deleteValue();
		_objectType = UINT16;
		_value.uint16Val = new uint16(rhs);
	}

	return *this;
}

Object& Object::operator=(const int32& rhs) {
	if (_objectType == INT32)
		*_value.int32Val = rhs;
	else {
		deleteValue();
		_objectType = INT32;
		_value.int32Val = new int32(rhs);
	}

	return *this;
}

Object& Object::operator=(const uint32& rhs) {
	if (_objectType == UINT32)
		*_value.uint32Val = rhs;
	else {
		deleteValue();
		_objectType = UINT32;
		_value.uint32Val = new uint32(rhs);
	}

	return *this;
}

Object& Object::operator=(const float& rhs) {
	if (_objectType == FLOAT)
		*_value.floatVal = rhs;
	else {
		deleteValue();
		_objectType = FLOAT;
		_value.floatVal = new float(rhs);
	}

	return *this;
}

Object& Object::operator=(const double& rhs) {
	if (_objectType == DOUBLE)
		*_value.doubleVal = rhs;
	else {
		deleteValue();
		_objectType = DOUBLE;
		_value.doubleVal = new double(rhs);
	}

	return *this;
}

Object& Object::operator=(const Common::String& rhs) {
	if (_objectType == STRING)
		*_value.stringVal = rhs;
	else {
		deleteValue();
		_objectType = STRING;
		_value.stringVal = new Common::String(rhs);
	}

	return *this;
}

Object& Object::operator=(const Object& rhs) {
	switch (_objectType) {
	case BOOL:
		return operator=(*rhs._value.boolVal);
	case BYTE:
		return operator=(*rhs._value.byteVal);
	case INT16:
		return operator=(*rhs._value.int16Val);
	case UINT16:
		return operator=(*rhs._value.uint16Val);
	case INT32:
		return operator=(*rhs._value.int32Val);
	case UINT32:
		return operator=(*rhs._value.uint32Val);
	case FLOAT:
		return operator=(*rhs._value.floatVal);
	case DOUBLE:
		return operator=(*rhs._value.doubleVal);
	case STRING:
		return operator=(*rhs._value.stringVal);
	}

	return *this;
}


Object::operator bool() {
	if (_objectType !=  BOOL)
		warning("'Object' not of type bool. Bad cast");

	return *_value.boolVal;
}

Object::operator byte() {
	if (_objectType !=  BYTE)
		warning("'Object' not of type byte. Bad cast");

	return *_value.byteVal;
}

Object::operator int16() {
	if (_objectType !=  INT16)
		warning("'Object' not of type int16. Bad cast");

	return *_value.int16Val;
}

Object::operator uint16() {
	if (_objectType !=  UINT16)
		warning("'Object' not of type uint16. Bad cast");

	return *_value.uint16Val;
}

Object::operator int32() {
	if (_objectType !=  INT32)
		warning("'Object' not of type int32. Bad cast");

	return *_value.int32Val;
}

Object::operator uint32() {
	if (_objectType !=  UINT32)
		warning("'Object' not of type uint32. Bad cast");

	return *_value.uint32Val;
}

Object::operator float() {
	if (_objectType !=  FLOAT)
		warning("'Object' not of type float. Bad cast");

	return *_value.floatVal;
}

Object::operator double() {
	if (_objectType !=  DOUBLE)
		warning("'Object' not of type double. Bad cast");

	return *_value.doubleVal;
}

Object::operator Common::String() {
	if (_objectType !=  STRING)
		warning("'Object' not of type Common::String. Bad cast");

	return *_value.stringVal;
}

} // End namespace ZVision