/src/assimp/contrib/openddlparser/code/Value.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /*----------------------------------------------------------------------------------------------- |
2 | | The MIT License (MIT) |
3 | | |
4 | | Copyright (c) 2014-2020 Kim Kulling |
5 | | |
6 | | Permission is hereby granted, free of charge, to any person obtaining a copy of |
7 | | this software and associated documentation files (the "Software"), to deal in |
8 | | the Software without restriction, including without limitation the rights to |
9 | | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
10 | | the Software, and to permit persons to whom the Software is furnished to do so, |
11 | | subject to the following conditions: |
12 | | |
13 | | The above copyright notice and this permission notice shall be included in all |
14 | | copies or substantial portions of the Software. |
15 | | |
16 | | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17 | | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
18 | | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
19 | | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
20 | | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
21 | | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
22 | | -----------------------------------------------------------------------------------------------*/ |
23 | | #include <openddlparser/OpenDDLStream.h> |
24 | | #include <openddlparser/Value.h> |
25 | | |
26 | | #include <cassert> |
27 | | |
28 | | BEGIN_ODDLPARSER_NS |
29 | | |
30 | | static Value::Iterator end(nullptr); |
31 | | |
32 | | Value::Iterator::Iterator() : |
33 | 0 | m_start(nullptr), |
34 | 0 | m_current(nullptr) { |
35 | | // empty |
36 | 0 | } |
37 | | |
38 | | Value::Iterator::Iterator(Value *start) : |
39 | 2 | m_start(start), |
40 | 2 | m_current(start) { |
41 | | // empty |
42 | 2 | } |
43 | | |
44 | | Value::Iterator::Iterator(const Iterator &rhs) : |
45 | 0 | m_start(rhs.m_start), |
46 | 0 | m_current(rhs.m_current) { |
47 | | // empty |
48 | 0 | } |
49 | | |
50 | 0 | Value::Iterator::~Iterator() { |
51 | | // empty |
52 | 0 | } |
53 | | |
54 | 0 | bool Value::Iterator::hasNext() const { |
55 | 0 | if (nullptr == m_current) { |
56 | 0 | return false; |
57 | 0 | } |
58 | 0 | return (nullptr != m_current->getNext()); |
59 | 0 | } |
60 | | |
61 | 0 | Value *Value::Iterator::getNext() { |
62 | 0 | if (!hasNext()) { |
63 | 0 | return nullptr; |
64 | 0 | } |
65 | | |
66 | 0 | Value *v(m_current->getNext()); |
67 | 0 | m_current = v; |
68 | |
|
69 | 0 | return v; |
70 | 0 | } |
71 | | |
72 | 0 | const Value::Iterator Value::Iterator::operator++(int) { |
73 | 0 | if (nullptr == m_current) { |
74 | 0 | return end; |
75 | 0 | } |
76 | | |
77 | 0 | m_current = m_current->getNext(); |
78 | 0 | Iterator inst(m_current); |
79 | |
|
80 | 0 | return inst; |
81 | 0 | } |
82 | | |
83 | 0 | Value::Iterator &Value::Iterator::operator++() { |
84 | 0 | if (nullptr == m_current) { |
85 | 0 | return end; |
86 | 0 | } |
87 | | |
88 | 0 | m_current = m_current->getNext(); |
89 | |
|
90 | 0 | return *this; |
91 | 0 | } |
92 | | |
93 | 0 | bool Value::Iterator::operator==(const Iterator &rhs) const { |
94 | 0 | return (m_current == rhs.m_current); |
95 | 0 | } |
96 | | |
97 | 0 | Value *Value::Iterator::operator->() const { |
98 | 0 | if (nullptr == m_current) { |
99 | 0 | return nullptr; |
100 | 0 | } |
101 | 0 | return m_current; |
102 | 0 | } |
103 | | |
104 | | Value::Value(ValueType type) : |
105 | 549 | m_type(type), |
106 | 549 | m_size(0), |
107 | 549 | m_data(nullptr), |
108 | 549 | m_next(nullptr) { |
109 | | // empty |
110 | 549 | } |
111 | | |
112 | 549 | Value::~Value() { |
113 | 549 | if (m_data != nullptr) { |
114 | 549 | if (m_type == ValueType::ddl_ref) { |
115 | 0 | Reference *tmp = (Reference *)m_data; |
116 | 0 | if (tmp != nullptr) { |
117 | 0 | delete tmp; |
118 | 0 | } |
119 | 549 | } else { |
120 | 549 | delete[] m_data; |
121 | 549 | } |
122 | 549 | } |
123 | 549 | delete m_next; |
124 | 549 | } |
125 | | |
126 | 0 | void Value::setBool(bool value) { |
127 | 0 | assert(ValueType::ddl_bool == m_type); |
128 | 0 | ::memcpy(m_data, &value, m_size); |
129 | 0 | } |
130 | | |
131 | 0 | bool Value::getBool() { |
132 | 0 | assert(ValueType::ddl_bool == m_type); |
133 | 0 | return (*m_data == 1); |
134 | 0 | } |
135 | | |
136 | 0 | void Value::setInt8(int8 value) { |
137 | 0 | assert(ValueType::ddl_int8 == m_type); |
138 | 0 | ::memcpy(m_data, &value, m_size); |
139 | 0 | } |
140 | | |
141 | 0 | int8 Value::getInt8() { |
142 | 0 | assert(ValueType::ddl_int8 == m_type); |
143 | 0 | return (int8)(*m_data); |
144 | 0 | } |
145 | | |
146 | 0 | void Value::setInt16(int16 value) { |
147 | 0 | assert(ValueType::ddl_int16 == m_type); |
148 | 0 | ::memcpy(m_data, &value, m_size); |
149 | 0 | } |
150 | | |
151 | 0 | int16 Value::getInt16() { |
152 | 0 | assert(ValueType::ddl_int16 == m_type); |
153 | 0 | int16 i; |
154 | 0 | ::memcpy(&i, m_data, m_size); |
155 | 0 | return i; |
156 | 0 | } |
157 | | |
158 | 0 | void Value::setInt32(int32 value) { |
159 | 0 | assert(ValueType::ddl_int32 == m_type); |
160 | 0 | ::memcpy(m_data, &value, m_size); |
161 | 0 | } |
162 | | |
163 | 0 | int32 Value::getInt32() { |
164 | 0 | assert(ValueType::ddl_int32 == m_type); |
165 | 0 | int32 i; |
166 | 0 | ::memcpy(&i, m_data, m_size); |
167 | 0 | return i; |
168 | 0 | } |
169 | | |
170 | 0 | void Value::setInt64(int64 value) { |
171 | 0 | assert(ValueType::ddl_int64 == m_type); |
172 | 0 | ::memcpy(m_data, &value, m_size); |
173 | 0 | } |
174 | | |
175 | 0 | int64 Value::getInt64() { |
176 | 0 | assert(ValueType::ddl_int64 == m_type); |
177 | 0 | int64 i; |
178 | 0 | ::memcpy(&i, m_data, m_size); |
179 | 0 | return i; |
180 | 0 | } |
181 | | |
182 | 0 | void Value::setUnsignedInt8(uint8 value) { |
183 | 0 | assert(ValueType::ddl_unsigned_int8 == m_type); |
184 | 0 | ::memcpy(m_data, &value, m_size); |
185 | 0 | } |
186 | | |
187 | 0 | uint8 Value::getUnsignedInt8() const { |
188 | 0 | assert(ValueType::ddl_unsigned_int8 == m_type); |
189 | 0 | uint8 i; |
190 | 0 | ::memcpy(&i, m_data, m_size); |
191 | 0 | return i; |
192 | 0 | } |
193 | | |
194 | 0 | void Value::setUnsignedInt16(uint16 value) { |
195 | 0 | assert(ValueType::ddl_unsigned_int16 == m_type); |
196 | 0 | ::memcpy(m_data, &value, m_size); |
197 | 0 | } |
198 | | |
199 | 0 | uint16 Value::getUnsignedInt16() const { |
200 | 0 | assert(ValueType::ddl_unsigned_int16 == m_type); |
201 | 0 | uint16 i; |
202 | 0 | ::memcpy(&i, m_data, m_size); |
203 | 0 | return i; |
204 | 0 | } |
205 | | |
206 | 0 | void Value::setUnsignedInt32(uint32 value) { |
207 | 0 | assert(ValueType::ddl_unsigned_int32 == m_type); |
208 | 0 | ::memcpy(m_data, &value, m_size); |
209 | 0 | } |
210 | | |
211 | 0 | uint32 Value::getUnsignedInt32() const { |
212 | 0 | assert(ValueType::ddl_unsigned_int32 == m_type); |
213 | 0 | uint32 i; |
214 | 0 | ::memcpy(&i, m_data, m_size); |
215 | 0 | return i; |
216 | 0 | } |
217 | | |
218 | 4 | void Value::setUnsignedInt64(uint64 value) { |
219 | 4 | assert(ValueType::ddl_unsigned_int64 == m_type); |
220 | 4 | ::memcpy(m_data, &value, m_size); |
221 | 4 | } |
222 | | |
223 | 0 | uint64 Value::getUnsignedInt64() const { |
224 | 0 | assert(ValueType::ddl_unsigned_int64 == m_type); |
225 | 0 | uint64 i; |
226 | 0 | ::memcpy(&i, m_data, m_size); |
227 | 0 | return i; |
228 | 0 | } |
229 | | |
230 | 545 | void Value::setFloat(float value) { |
231 | 545 | assert(ValueType::ddl_float == m_type); |
232 | 545 | ::memcpy(m_data, &value, m_size); |
233 | 545 | } |
234 | | |
235 | 0 | float Value::getFloat() const { |
236 | 0 | if (m_type == ValueType::ddl_float) { |
237 | 0 | float v; |
238 | 0 | ::memcpy(&v, m_data, m_size); |
239 | 0 | return (float)v; |
240 | 0 | } else { |
241 | 0 | float tmp; |
242 | 0 | ::memcpy(&tmp, m_data, 4); |
243 | 0 | return (float)tmp; |
244 | 0 | } |
245 | 0 | } |
246 | | |
247 | 0 | void Value::setDouble(double value) { |
248 | 0 | assert(ValueType::ddl_double == m_type); |
249 | 0 | ::memcpy(m_data, &value, m_size); |
250 | 0 | } |
251 | | |
252 | 0 | double Value::getDouble() const { |
253 | 0 | if (m_type == ValueType::ddl_double) { |
254 | 0 | double v; |
255 | 0 | ::memcpy(&v, m_data, m_size); |
256 | 0 | return (float)v; |
257 | 0 | } else { |
258 | 0 | double tmp; |
259 | 0 | ::memcpy(&tmp, m_data, 4); |
260 | 0 | return (double)tmp; |
261 | 0 | } |
262 | 0 | } |
263 | | |
264 | 0 | void Value::setString(const std::string &str) { |
265 | 0 | assert(ValueType::ddl_string == m_type); |
266 | 0 | ::memcpy(m_data, str.c_str(), str.size()); |
267 | 0 | m_data[str.size()] = '\0'; |
268 | 0 | } |
269 | | |
270 | 0 | const char *Value::getString() const { |
271 | 0 | assert(ValueType::ddl_string == m_type); |
272 | 0 | return (const char *)m_data; |
273 | 0 | } |
274 | | |
275 | 0 | void Value::setRef(Reference *ref) { |
276 | 0 | assert(ValueType::ddl_ref == m_type); |
277 | | |
278 | 0 | if (nullptr != ref) { |
279 | 0 | const size_t sizeInBytes(ref->sizeInBytes()); |
280 | 0 | if (sizeInBytes > 0) { |
281 | 0 | if (nullptr != m_data) { |
282 | 0 | delete[] m_data; |
283 | 0 | } |
284 | |
|
285 | 0 | m_data = (unsigned char *)new Reference(*ref); |
286 | 0 | } |
287 | 0 | } |
288 | 0 | } |
289 | | |
290 | 0 | Reference *Value::getRef() const { |
291 | 0 | assert(ValueType::ddl_ref == m_type); |
292 | | |
293 | 0 | return (Reference *)m_data; |
294 | 0 | } |
295 | | |
296 | 0 | void Value::dump(IOStreamBase &stream) { |
297 | 0 | switch (m_type) { |
298 | 0 | case ValueType::ddl_none: |
299 | 0 | stream.write("None\n"); |
300 | 0 | break; |
301 | 0 | case ValueType::ddl_bool: |
302 | 0 | stream.write(std::to_string(getBool()) + "\n"); |
303 | 0 | break; |
304 | 0 | case ValueType::ddl_int8: |
305 | 0 | stream.write(std::to_string(getInt8()) + "\n"); |
306 | 0 | break; |
307 | 0 | case ValueType::ddl_int16: |
308 | 0 | stream.write(std::to_string(getInt16()) + "\n"); |
309 | 0 | break; |
310 | 0 | case ValueType::ddl_int32: |
311 | 0 | stream.write(std::to_string(getInt32()) + "\n"); |
312 | 0 | break; |
313 | 0 | case ValueType::ddl_int64: |
314 | 0 | stream.write(std::to_string(getInt64()) + "\n"); |
315 | 0 | break; |
316 | 0 | case ValueType::ddl_unsigned_int8: |
317 | 0 | stream.write("Not supported\n"); |
318 | 0 | break; |
319 | 0 | case ValueType::ddl_unsigned_int16: |
320 | 0 | stream.write("Not supported\n"); |
321 | 0 | break; |
322 | 0 | case ValueType::ddl_unsigned_int32: |
323 | 0 | stream.write("Not supported\n"); |
324 | 0 | break; |
325 | 0 | case ValueType::ddl_unsigned_int64: |
326 | 0 | stream.write("Not supported\n"); |
327 | 0 | break; |
328 | 0 | case ValueType::ddl_half: |
329 | 0 | stream.write("Not supported\n"); |
330 | 0 | break; |
331 | 0 | case ValueType::ddl_float: |
332 | 0 | stream.write(std::to_string(getFloat()) + "\n"); |
333 | 0 | break; |
334 | 0 | case ValueType::ddl_double: |
335 | 0 | stream.write(std::to_string(getDouble()) + "\n"); |
336 | 0 | break; |
337 | 0 | case ValueType::ddl_string: |
338 | 0 | stream.write(std::string(getString()) + "\n"); |
339 | 0 | break; |
340 | 0 | case ValueType::ddl_ref: |
341 | 0 | stream.write("Not supported\n"); |
342 | 0 | break; |
343 | 0 | default: |
344 | 0 | break; |
345 | 0 | } |
346 | 0 | } |
347 | | |
348 | 538 | void Value::setNext(Value *next) { |
349 | 538 | m_next = next; |
350 | 538 | } |
351 | | |
352 | 0 | Value *Value::getNext() const { |
353 | 0 | return m_next; |
354 | 0 | } |
355 | | |
356 | 0 | size_t Value::size() const { |
357 | 0 | size_t result = 1; |
358 | 0 | Value *n = m_next; |
359 | 0 | while (n != nullptr) { |
360 | 0 | result++; |
361 | 0 | n = n->m_next; |
362 | 0 | } |
363 | 0 | return result; |
364 | 0 | } |
365 | | |
366 | 549 | Value *ValueAllocator::allocPrimData(Value::ValueType type, size_t len) { |
367 | 549 | if (type == Value::ValueType::ddl_none || Value::ValueType::ddl_types_max == type) { |
368 | 0 | return nullptr; |
369 | 0 | } |
370 | | |
371 | 549 | Value *data = new Value(type); |
372 | 549 | switch (type) { |
373 | 0 | case Value::ValueType::ddl_bool: |
374 | 0 | data->m_size = sizeof(bool); |
375 | 0 | break; |
376 | 0 | case Value::ValueType::ddl_int8: |
377 | 0 | data->m_size = sizeof(int8); |
378 | 0 | break; |
379 | 0 | case Value::ValueType::ddl_int16: |
380 | 0 | data->m_size = sizeof(int16); |
381 | 0 | break; |
382 | 0 | case Value::ValueType::ddl_int32: |
383 | 0 | data->m_size = sizeof(int32); |
384 | 0 | break; |
385 | 0 | case Value::ValueType::ddl_int64: |
386 | 0 | data->m_size = sizeof(int64); |
387 | 0 | break; |
388 | 0 | case Value::ValueType::ddl_unsigned_int8: |
389 | 0 | data->m_size = sizeof(uint8); |
390 | 0 | break; |
391 | 0 | case Value::ValueType::ddl_unsigned_int16: |
392 | 0 | data->m_size = sizeof(uint16); |
393 | 0 | break; |
394 | 0 | case Value::ValueType::ddl_unsigned_int32: |
395 | 0 | data->m_size = sizeof(uint32); |
396 | 0 | break; |
397 | 4 | case Value::ValueType::ddl_unsigned_int64: |
398 | 4 | data->m_size = sizeof(uint64); |
399 | 4 | break; |
400 | 0 | case Value::ValueType::ddl_half: |
401 | 0 | data->m_size = sizeof(short); |
402 | 0 | break; |
403 | 545 | case Value::ValueType::ddl_float: |
404 | 545 | data->m_size = sizeof(float); |
405 | 545 | break; |
406 | 0 | case Value::ValueType::ddl_double: |
407 | 0 | data->m_size = sizeof(double); |
408 | 0 | break; |
409 | 0 | case Value::ValueType::ddl_string: |
410 | 0 | data->m_size = sizeof(char) * (len + 1); |
411 | 0 | break; |
412 | 0 | case Value::ValueType::ddl_ref: |
413 | 0 | data->m_size = 0; |
414 | 0 | break; |
415 | 0 | case Value::ValueType::ddl_none: |
416 | 0 | case Value::ValueType::ddl_types_max: |
417 | 0 | default: |
418 | 0 | break; |
419 | 549 | } |
420 | | |
421 | 549 | if (data->m_size) { |
422 | 549 | data->m_data = new unsigned char[data->m_size]; |
423 | 549 | ::memset(data->m_data, 0, data->m_size); |
424 | 549 | } |
425 | | |
426 | 549 | return data; |
427 | 549 | } |
428 | | |
429 | 0 | void ValueAllocator::releasePrimData(Value **data) { |
430 | 0 | if (!data) { |
431 | 0 | return; |
432 | 0 | } |
433 | | |
434 | 0 | delete *data; |
435 | 0 | *data = nullptr; |
436 | 0 | } |
437 | | |
438 | | END_ODDLPARSER_NS |