402 |
return s; |
return s; |
403 |
} |
} |
404 |
|
|
405 |
|
template<typename T> |
406 |
|
static T _primitiveObjectValueToNumber(const Object& obj) { |
407 |
|
T value = 0; |
408 |
|
const DataType& type = obj.type(); |
409 |
|
const ID& id = obj.uid().id; |
410 |
|
void* ptr = obj.m_data.empty() ? (void*)id : (void*)&obj.m_data[0]; |
411 |
|
if (!obj.m_data.empty()) |
412 |
|
assert(type.size() == obj.m_data.size()); |
413 |
|
if (type.isPrimitive() && !type.isPointer()) { |
414 |
|
if (type.isInteger() || type.isEnum()) { |
415 |
|
if (type.isSigned()) { |
416 |
|
if (type.size() == 1) |
417 |
|
value = (T)*(int8_t*)ptr; |
418 |
|
else if (type.size() == 2) |
419 |
|
value = (T)*(int16_t*)ptr; |
420 |
|
else if (type.size() == 4) |
421 |
|
value = (T)*(int32_t*)ptr; |
422 |
|
else if (type.size() == 8) |
423 |
|
value = (T)*(int64_t*)ptr; |
424 |
|
else |
425 |
|
assert(false /* unknown signed int type size */); |
426 |
|
} else { |
427 |
|
if (type.size() == 1) |
428 |
|
value = (T)*(uint8_t*)ptr; |
429 |
|
else if (type.size() == 2) |
430 |
|
value = (T)*(uint16_t*)ptr; |
431 |
|
else if (type.size() == 4) |
432 |
|
value = (T)*(uint32_t*)ptr; |
433 |
|
else if (type.size() == 8) |
434 |
|
value = (T)*(uint64_t*)ptr; |
435 |
|
else |
436 |
|
assert(false /* unknown unsigned int type size */); |
437 |
|
} |
438 |
|
} else if (type.isReal()) { |
439 |
|
if (type.size() == sizeof(float)) |
440 |
|
value = (T)*(float*)ptr; |
441 |
|
else if (type.size() == sizeof(double)) |
442 |
|
value = (T)*(double*)ptr; |
443 |
|
else |
444 |
|
assert(false /* unknown floating point type */); |
445 |
|
} else if (type.isBool()) { |
446 |
|
value = (T)*(bool*)ptr; |
447 |
|
} else { |
448 |
|
assert(false /* unknown primitive type */); |
449 |
|
} |
450 |
|
} |
451 |
|
return value; |
452 |
|
} |
453 |
|
|
454 |
static String _encodePrimitiveValue(const Object& obj) { |
static String _encodePrimitiveValue(const Object& obj) { |
455 |
return _encodeBlob( _primitiveObjectValueToString(obj) ); |
return _encodeBlob( _primitiveObjectValueToString(obj) ); |
456 |
} |
} |
1043 |
return _primitiveObjectValueToString(*pObject); |
return _primitiveObjectValueToString(*pObject); |
1044 |
} |
} |
1045 |
|
|
1046 |
|
int64_t Archive::valueAsInt(const Object& object) { |
1047 |
|
if (!object) |
1048 |
|
throw Exception("Invalid object"); |
1049 |
|
if (!object.type().isInteger() && !object.type().isEnum()) |
1050 |
|
throw Exception("Object is neither an integer nor an enum"); |
1051 |
|
const Object* pObject = &object; |
1052 |
|
if (object.type().isPointer()) { |
1053 |
|
const Object& obj = objectByUID(object.uid(1)); |
1054 |
|
if (!obj) return 0; |
1055 |
|
pObject = &obj; |
1056 |
|
} |
1057 |
|
return _primitiveObjectValueToNumber<int64_t>(*pObject); |
1058 |
|
} |
1059 |
|
|
1060 |
|
double Archive::valueAsReal(const Object& object) { |
1061 |
|
if (!object) |
1062 |
|
throw Exception("Invalid object"); |
1063 |
|
if (!object.type().isReal()) |
1064 |
|
throw Exception("Object is not an real type"); |
1065 |
|
const Object* pObject = &object; |
1066 |
|
if (object.type().isPointer()) { |
1067 |
|
const Object& obj = objectByUID(object.uid(1)); |
1068 |
|
if (!obj) return 0; |
1069 |
|
pObject = &obj; |
1070 |
|
} |
1071 |
|
return _primitiveObjectValueToNumber<double>(*pObject); |
1072 |
|
} |
1073 |
|
|
1074 |
|
bool Archive::valueAsBool(const Object& object) { |
1075 |
|
if (!object) |
1076 |
|
throw Exception("Invalid object"); |
1077 |
|
if (!object.type().isBool()) |
1078 |
|
throw Exception("Object is not a bool"); |
1079 |
|
const Object* pObject = &object; |
1080 |
|
if (object.type().isPointer()) { |
1081 |
|
const Object& obj = objectByUID(object.uid(1)); |
1082 |
|
if (!obj) return 0; |
1083 |
|
pObject = &obj; |
1084 |
|
} |
1085 |
|
return _primitiveObjectValueToNumber<bool>(*pObject); |
1086 |
|
} |
1087 |
|
|
1088 |
// *************** Archive::Syncer *************** |
// *************** Archive::Syncer *************** |
1089 |
// * |
// * |
1090 |
|
|