1 |
/*************************************************************************** |
/*************************************************************************** |
2 |
* * |
* * |
3 |
* Copyright (C) 2017 Christian Schoenebeck * |
* Copyright (C) 2017-2020 Christian Schoenebeck * |
4 |
* <cuse@users.sourceforge.net> * |
* <cuse@users.sourceforge.net> * |
5 |
* * |
* * |
6 |
* This library is part of libgig. * |
* This library is part of libgig. * |
7 |
* * |
* * |
21 |
* MA 02111-1307 USA * |
* MA 02111-1307 USA * |
22 |
***************************************************************************/ |
***************************************************************************/ |
23 |
|
|
24 |
|
// enable implementation specific declarations in Serialization.h required to |
25 |
|
// build this C++ unit, which should be ignored in the public API though |
26 |
|
#define LIBGIG_SERIALIZATION_INTERNAL 1 |
27 |
|
|
28 |
#include "Serialization.h" |
#include "Serialization.h" |
29 |
|
|
30 |
#include <iostream> |
#include <iostream> |
|
#include <assert.h> |
|
31 |
#include <string.h> // for memcpy() |
#include <string.h> // for memcpy() |
32 |
#include <stdlib.h> // for atof() |
#include <stdlib.h> // for atof() |
33 |
#include <cxxabi.h> |
#ifdef _MSC_VER |
34 |
|
# include <windows.h> |
35 |
|
# include <dbghelp.h> |
36 |
|
#else |
37 |
|
# include <cxxabi.h> |
38 |
|
#endif |
39 |
#include "helper.h" |
#include "helper.h" |
40 |
|
|
41 |
#define LIBGIG_EPOCH_TIME ((time_t)0) |
#define LIBGIG_EPOCH_TIME ((time_t)0) |
70 |
// *************** DataType *************** |
// *************** DataType *************** |
71 |
// * |
// * |
72 |
|
|
73 |
/** @brief Default constructor. |
/** @brief Default constructor (as "invalid" DataType). |
74 |
* |
* |
75 |
* Initializes a DataType object as being an "invalid" DataType object. |
* Initializes a DataType object as being an "invalid" DataType object. |
76 |
* Thus calling isValid(), after creating a DataType object with this |
* Thus calling isValid(), after creating a DataType object with this |
84 |
m_isPointer = false; |
m_isPointer = false; |
85 |
} |
} |
86 |
|
|
87 |
|
/** @brief Constructs a valid DataType object. |
88 |
|
* |
89 |
|
* Initializes this object as "valid" DataType object, with specific and |
90 |
|
* useful data type information. |
91 |
|
* |
92 |
|
* This is a protected constructor which should not be called directly by |
93 |
|
* applications, as its argument list is somewhat implementation specific |
94 |
|
* and might change at any time. Applications should call the static |
95 |
|
* function DataType::dataTypeOf() instead. |
96 |
|
* |
97 |
|
* @param isPointer - whether pointer type (i.e. a simple memory address) |
98 |
|
* @param size - native size of data type in bytes (i.e. according to |
99 |
|
* @c sizeof() C/C++ keyword) |
100 |
|
* @param baseType - this framework's internal name for specifying the base |
101 |
|
* type in a coarse way, which must be either one of: |
102 |
|
* "int8", "uint8", "int16", "uint16", "int32", "uint32", |
103 |
|
* "int64", "uint64", "bool", "real32", "real64", |
104 |
|
* "String", "enum", "union" or "class" |
105 |
|
* @param customType - this is only used for base types "enum", "union" or |
106 |
|
* "class", in which case this identifies the user |
107 |
|
* defined type name (e.g. "Foo" for @c class @c Foo ), |
108 |
|
* for all other types this is empty |
109 |
|
*/ |
110 |
DataType::DataType(bool isPointer, int size, String baseType, String customType) { |
DataType::DataType(bool isPointer, int size, String baseType, String customType) { |
111 |
m_size = size; |
m_size = size; |
112 |
m_isPointer = isPointer; |
m_isPointer = isPointer; |
141 |
* |
* |
142 |
* Returns @c true if the respective native C/C++ object, member or variable |
* Returns @c true if the respective native C/C++ object, member or variable |
143 |
* (this DataType instance is reflecting) is a C/C++ @c struct or @c class |
* (this DataType instance is reflecting) is a C/C++ @c struct or @c class |
144 |
* type. |
* type, with one exception: @c String objects are handled by this framework |
145 |
|
* as if they were a primitive type. |
146 |
* |
* |
147 |
* Note that in the following example: |
* Note that in the following example: |
148 |
* @code |
* @code |
168 |
* (this DataType instance is reflecting) is a primitive, fundamental C/C++ |
* (this DataType instance is reflecting) is a primitive, fundamental C/C++ |
169 |
* data type. Those are fundamental data types which are already predefined |
* data type. Those are fundamental data types which are already predefined |
170 |
* by the C/C++ language, for example: @c char, @c int, @c float, @c double, |
* by the C/C++ language, for example: @c char, @c int, @c float, @c double, |
171 |
* @c bool, but also @b any pointer types like @c int*, @c double**, but |
* @c bool, but also @c String objects and @b any pointer types like |
172 |
* including pointers to user defined types like: |
* @c int*, @c double**, but including pointers to user defined types like: |
173 |
* @code |
* @code |
174 |
* struct Foo { |
* struct Foo { |
175 |
* int a; |
* int a; |
186 |
return !isClass(); |
return !isClass(); |
187 |
} |
} |
188 |
|
|
189 |
|
/** @brief Whether this is a C++ @c String data type. |
190 |
|
* |
191 |
|
* Returns @c true if the respective native C/C++ object, member or variable |
192 |
|
* (this DataType instance is reflecting) is a C++ @c String object (a.k.a. |
193 |
|
* @c std::string from the C++ STL). |
194 |
|
* |
195 |
|
* Note that this framework handles @c String objects as if they were a |
196 |
|
* fundamental, primitive C/C++ data type, so @c isPrimitive() returns |
197 |
|
* @c true for strings. |
198 |
|
*/ |
199 |
|
bool DataType::isString() const { |
200 |
|
return m_baseTypeName == "String"; |
201 |
|
} |
202 |
|
|
203 |
/** @brief Whether this is an integer C/C++ data type. |
/** @brief Whether this is an integer C/C++ data type. |
204 |
* |
* |
205 |
* Returns @c true if the respective native C/C++ object, member or variable |
* Returns @c true if the respective native C/C++ object, member or variable |
295 |
* characteristic is compared as well. So a @c double type and @c double* |
* characteristic is compared as well. So a @c double type and @c double* |
296 |
* type are also considered to be not equal data types and hence this method |
* type are also considered to be not equal data types and hence this method |
297 |
* would return @c false. |
* would return @c false. |
298 |
|
* |
299 |
|
* As an exception here, classes and structs with the same class/struct name |
300 |
|
* but different sizes are also considered to be "equal". This relaxed |
301 |
|
* requirement is necessary to retain backward compatiblity to older |
302 |
|
* versions of the same native C++ classes/structs. |
303 |
*/ |
*/ |
304 |
bool DataType::operator==(const DataType& other) const { |
bool DataType::operator==(const DataType& other) const { |
305 |
return m_baseTypeName == other.m_baseTypeName && |
return m_baseTypeName == other.m_baseTypeName && |
306 |
m_customTypeName == other.m_customTypeName && |
m_customTypeName == other.m_customTypeName && |
307 |
m_size == other.m_size && |
(m_size == other.m_size || (isClass() && other.isClass())) && |
308 |
m_isPointer == other.m_isPointer; |
m_isPointer == other.m_isPointer; |
309 |
} |
} |
310 |
|
|
331 |
bool DataType::operator<(const DataType& other) const { |
bool DataType::operator<(const DataType& other) const { |
332 |
return m_baseTypeName < other.m_baseTypeName || |
return m_baseTypeName < other.m_baseTypeName || |
333 |
(m_baseTypeName == other.m_baseTypeName && |
(m_baseTypeName == other.m_baseTypeName && |
334 |
m_customTypeName < other.m_customTypeName || |
(m_customTypeName < other.m_customTypeName || |
335 |
(m_customTypeName == other.m_customTypeName && |
(m_customTypeName == other.m_customTypeName && |
336 |
m_size < other.m_size || |
(m_size < other.m_size || |
337 |
(m_size == other.m_size && |
(m_size == other.m_size && |
338 |
m_isPointer < other.m_isPointer))); |
m_isPointer < other.m_isPointer))))); |
339 |
} |
} |
340 |
|
|
341 |
/** @brief Greater than comparison. |
/** @brief Greater than comparison. |
440 |
* In the latter example @c customTypeName(true) would return for both |
* In the latter example @c customTypeName(true) would return for both |
441 |
* @c foo and @c pFoo the string @c "Foo" as return value of this method. |
* @c foo and @c pFoo the string @c "Foo" as return value of this method. |
442 |
* |
* |
443 |
|
* @b Windows: please note that the current implementation of this method |
444 |
|
* on Windows is @b not thread safe! |
445 |
|
* |
446 |
* @see isPointer(), baseTypeName() |
* @see isPointer(), baseTypeName() |
447 |
*/ |
*/ |
448 |
String DataType::customTypeName(bool demangle) const { |
String DataType::customTypeName(bool demangle) const { |
449 |
if (!demangle) return m_customTypeName; |
if (!demangle) return m_customTypeName; |
450 |
|
#ifdef _MSC_VER |
451 |
|
const size_t MAXLENGTH = 1024; |
452 |
|
char result[MAXLENGTH]; |
453 |
|
|
454 |
|
//FIXME: calling UnDecorateSymbolName() is not thread safe! |
455 |
|
//Skip the first char |
456 |
|
size_t size = UnDecorateSymbolName(m_customTypeName.c_str() +1, result, MAXLENGTH, UNDNAME_32_BIT_DECODE | UNDNAME_NO_ARGUMENTS); |
457 |
|
if (size) |
458 |
|
{ |
459 |
|
return result; |
460 |
|
} |
461 |
|
return m_customTypeName; |
462 |
|
#else |
463 |
int status; |
int status; |
464 |
const char* result = |
char* result = |
465 |
abi::__cxa_demangle(m_customTypeName.c_str(), 0, 0, &status); |
abi::__cxa_demangle(m_customTypeName.c_str(), 0, 0, &status); |
466 |
return (status == 0) ? result : m_customTypeName; |
String sResult = result; |
467 |
|
free(result); |
468 |
|
return (status == 0) ? sResult : m_customTypeName; |
469 |
|
#endif |
470 |
} |
} |
471 |
|
|
472 |
// *************** Member *************** |
// *************** Member *************** |
637 |
bool Member::operator<(const Member& other) const { |
bool Member::operator<(const Member& other) const { |
638 |
return m_uid < other.m_uid || |
return m_uid < other.m_uid || |
639 |
(m_uid == other.m_uid && |
(m_uid == other.m_uid && |
640 |
m_offset < other.m_offset || |
(m_offset < other.m_offset || |
641 |
(m_offset == other.m_offset && |
(m_offset == other.m_offset && |
642 |
m_name < other.m_name || |
(m_name < other.m_name || |
643 |
(m_name == other.m_name && |
(m_name == other.m_name && |
644 |
m_type < other.m_type))); |
m_type < other.m_type))))); |
645 |
} |
} |
646 |
|
|
647 |
/** @brief Greater than comparison. |
/** @brief Greater than comparison. |
783 |
* In case this Object is reflecting a native C/C++ @c struct or @c class |
* In case this Object is reflecting a native C/C++ @c struct or @c class |
784 |
* type, then this method returns the version of that native C/C++ @c struct |
* type, then this method returns the version of that native C/C++ @c struct |
785 |
* or @c class layout or implementation. For primitive, fundamental C/C++ |
* or @c class layout or implementation. For primitive, fundamental C/C++ |
786 |
* data types the return value of this method has no meaning. |
* data types (including @c String objects) the return value of this method |
787 |
|
* has no meaning. |
788 |
* |
* |
789 |
* @see Archive::setVersion() for more details about this overall topic. |
* @see Archive::setVersion() for more details about this overall topic. |
790 |
*/ |
*/ |
797 |
* In case this Object is reflecting a native C/C++ @c struct or @c class |
* In case this Object is reflecting a native C/C++ @c struct or @c class |
798 |
* type, then this method returns the "minimum" version of that native C/C++ |
* type, then this method returns the "minimum" version of that native C/C++ |
799 |
* @c struct or @c class layout or implementation which it may be compatible |
* @c struct or @c class layout or implementation which it may be compatible |
800 |
* with. For primitive, fundamental C/C++ data types the return value of |
* with. For primitive, fundamental C/C++ data types (including @c String |
801 |
* this method has no meaning. |
* objects) the return value of this method has no meaning. |
802 |
* |
* |
803 |
* @see Archive::setVersion() and Archive::setMinVersion() for more details |
* @see Archive::setVersion() and Archive::setMinVersion() for more details |
804 |
* about this overall topic. |
* about this overall topic. |
1128 |
m_root = NO_UID; |
m_root = NO_UID; |
1129 |
m_isModified = false; |
m_isModified = false; |
1130 |
m_timeCreated = m_timeModified = LIBGIG_EPOCH_TIME; |
m_timeCreated = m_timeModified = LIBGIG_EPOCH_TIME; |
1131 |
decode(m_rawData); |
decode(data); |
1132 |
} |
} |
1133 |
|
|
1134 |
/** @brief Create and fill the archive with the given serialized raw C-buffer data. |
/** @brief Create and fill the archive with the given serialized raw C-buffer data. |
1264 |
assert(false /* unknown floating point type */); |
assert(false /* unknown floating point type */); |
1265 |
} else if (type.isBool()) { |
} else if (type.isBool()) { |
1266 |
s = ToString(*(bool*)ptr); |
s = ToString(*(bool*)ptr); |
1267 |
|
} else if (type.isString()) { |
1268 |
|
s = obj.m_data.empty() ? *(String*)ptr : String((const char*)ptr); |
1269 |
} else { |
} else { |
1270 |
assert(false /* unknown primitive type */); |
assert(false /* unknown primitive type */); |
1271 |
} |
} |
1275 |
} |
} |
1276 |
|
|
1277 |
template<typename T> |
template<typename T> |
1278 |
|
inline T _stringToNumber(const String& s) { |
1279 |
|
assert(false /* String cast to unknown primitive number type */); |
1280 |
|
} |
1281 |
|
|
1282 |
|
template<> |
1283 |
|
inline int64_t _stringToNumber(const String& s) { |
1284 |
|
return atoll(s.c_str()); |
1285 |
|
} |
1286 |
|
|
1287 |
|
template<> |
1288 |
|
inline double _stringToNumber(const String& s) { |
1289 |
|
return atof(s.c_str()); |
1290 |
|
} |
1291 |
|
|
1292 |
|
template<> |
1293 |
|
inline bool _stringToNumber(const String& s) { |
1294 |
|
return (bool) atoll(s.c_str()); |
1295 |
|
} |
1296 |
|
|
1297 |
|
template<typename T> |
1298 |
static T _primitiveObjectValueToNumber(const Object& obj) { |
static T _primitiveObjectValueToNumber(const Object& obj) { |
1299 |
T value = 0; |
T value = 0; |
1300 |
const DataType& type = obj.type(); |
const DataType& type = obj.type(); |
1336 |
assert(false /* unknown floating point type */); |
assert(false /* unknown floating point type */); |
1337 |
} else if (type.isBool()) { |
} else if (type.isBool()) { |
1338 |
value = (T)*(bool*)ptr; |
value = (T)*(bool*)ptr; |
1339 |
|
} else if (type.isString()) { |
1340 |
|
value = _stringToNumber<T>( |
1341 |
|
obj.m_data.empty() ? *(String*)ptr : String((const char*)ptr) |
1342 |
|
); |
1343 |
} else { |
} else { |
1344 |
assert(false /* unknown primitive type */); |
assert(false /* unknown primitive type */); |
1345 |
} |
} |
1373 |
return _encodeBlob(s); |
return _encodeBlob(s); |
1374 |
} |
} |
1375 |
|
|
1376 |
|
/* |
1377 |
|
* Srx format history: |
1378 |
|
* - 1.0: Initial version. |
1379 |
|
* - 1.1: Adds "String" data type. |
1380 |
|
*/ |
1381 |
#define MAGIC_START "Srx1v" |
#define MAGIC_START "Srx1v" |
1382 |
#define ENCODING_FORMAT_MINOR_VERSION 0 |
#define ENCODING_FORMAT_MINOR_VERSION 1 |
1383 |
|
|
1384 |
String Archive::_encodeRootBlob() { |
String Archive::_encodeRootBlob() { |
1385 |
String s; |
String s; |
1505 |
return s; |
return s; |
1506 |
} |
} |
1507 |
|
|
1508 |
|
static void _popStringBlob(const char*& p, const char* end, RawData& rawData) { |
1509 |
|
String s = _popStringBlob(p, end); |
1510 |
|
rawData.resize(s.length() + 1); |
1511 |
|
strcpy((char*)&rawData[0], &s[0]); |
1512 |
|
} |
1513 |
|
|
1514 |
static time_t _popTimeBlob(const char*& p, const char* end) { |
static time_t _popTimeBlob(const char*& p, const char* end) { |
1515 |
const uint64_t i = _popIntBlob<uint64_t>(p, end); |
const uint64_t i = _popIntBlob<uint64_t>(p, end); |
1516 |
return (time_t) i; |
return (time_t) i; |
1517 |
} |
} |
1518 |
|
|
1519 |
DataType _popDataTypeBlob(const char*& p, const char* end) { |
static DataType _popDataTypeBlob(const char*& p, const char* end) { |
1520 |
_Blob blob = _decodeBlob(p, end); |
_Blob blob = _decodeBlob(p, end); |
1521 |
p = blob.p; |
p = blob.p; |
1522 |
end = blob.end; |
end = blob.end; |
1629 |
assert(false /* unknown floating point type */); |
assert(false /* unknown floating point type */); |
1630 |
} else if (type.isBool()) { |
} else if (type.isBool()) { |
1631 |
_popIntBlob<uint8_t>(p, end, obj.m_data); |
_popIntBlob<uint8_t>(p, end, obj.m_data); |
1632 |
|
} else if (type.isString()) { |
1633 |
|
_popStringBlob(p, end, obj.m_data); |
1634 |
} else { |
} else { |
1635 |
assert(false /* unknown primitive type */); |
assert(false /* unknown primitive type */); |
1636 |
} |
} |
2171 |
m_isModified = true; |
m_isModified = true; |
2172 |
} |
} |
2173 |
|
|
2174 |
|
/** @brief Set new textual string for given String object. |
2175 |
|
* |
2176 |
|
* Sets the new textual string @a value to the given String @a object. |
2177 |
|
* |
2178 |
|
* @param object - the String object to be changed |
2179 |
|
* @param value - the new textual string to be assigned to the @a object |
2180 |
|
* @throws Exception if @a object is not a String type. |
2181 |
|
*/ |
2182 |
|
void Archive::setStringValue(Object& object, String value) { |
2183 |
|
if (!object) return; |
2184 |
|
if (!object.type().isString()) |
2185 |
|
throw Exception("Not a String data type"); |
2186 |
|
Object* pObject = &object; |
2187 |
|
if (object.type().isPointer()) { |
2188 |
|
Object& obj = objectByUID(object.uid(1)); |
2189 |
|
if (!obj) return; |
2190 |
|
pObject = &obj; |
2191 |
|
} |
2192 |
|
pObject->m_data.resize(value.length() + 1); |
2193 |
|
char* ptr = (char*) &pObject->m_data[0]; |
2194 |
|
strcpy(ptr, &value[0]); |
2195 |
|
m_isModified = true; |
2196 |
|
} |
2197 |
|
|
2198 |
/** @brief Automatically cast and assign appropriate value to object. |
/** @brief Automatically cast and assign appropriate value to object. |
2199 |
* |
* |
2200 |
* This method automatically converts the given @a value from textual string |
* This method automatically converts the given @a value from textual string |
2223 |
setBoolValue(object, false); |
setBoolValue(object, false); |
2224 |
else |
else |
2225 |
setBoolValue(object, atof(value.c_str())); |
setBoolValue(object, atof(value.c_str())); |
2226 |
} else if (type.isEnum()) |
} else if (type.isString()) |
2227 |
|
setStringValue(object, value); |
2228 |
|
else if (type.isEnum()) |
2229 |
setEnumValue(object, atoll(value.c_str())); |
setEnumValue(object, atoll(value.c_str())); |
2230 |
else |
else |
2231 |
throw Exception("Not a primitive data type"); |
throw Exception("Not a primitive data type"); |
2343 |
memcpy(pDst, &srcObj.rawData()[0], dstObj.type().size()); |
memcpy(pDst, &srcObj.rawData()[0], dstObj.type().size()); |
2344 |
} |
} |
2345 |
|
|
2346 |
|
void Archive::Syncer::syncString(const Object& dstObj, const Object& srcObj) { |
2347 |
|
assert(dstObj.type().isString()); |
2348 |
|
assert(dstObj.type() == srcObj.type()); |
2349 |
|
String* pDst = (String*)(void*)dstObj.uid().id; |
2350 |
|
*pDst = (String) (const char*) &srcObj.rawData()[0]; |
2351 |
|
} |
2352 |
|
|
2353 |
void Archive::Syncer::syncPointer(const Object& dstObj, const Object& srcObj) { |
void Archive::Syncer::syncPointer(const Object& dstObj, const Object& srcObj) { |
2354 |
assert(dstObj.type().isPointer()); |
assert(dstObj.type().isPointer()); |
2355 |
assert(dstObj.type() == srcObj.type()); |
assert(dstObj.type() == srcObj.type()); |
2376 |
m_dst.m_allObjects.erase(dstObj.uid()); |
m_dst.m_allObjects.erase(dstObj.uid()); |
2377 |
|
|
2378 |
if (dstObj.type().isPrimitive() && !dstObj.type().isPointer()) { |
if (dstObj.type().isPrimitive() && !dstObj.type().isPointer()) { |
2379 |
syncPrimitive(dstObj, srcObj); |
if (dstObj.type().isString()) |
2380 |
|
syncString(dstObj, srcObj); |
2381 |
|
else |
2382 |
|
syncPrimitive(dstObj, srcObj); |
2383 |
return; // end of recursion |
return; // end of recursion |
2384 |
} |
} |
2385 |
|
|
2431 |
// *************** Exception *************** |
// *************** Exception *************** |
2432 |
// * |
// * |
2433 |
|
|
2434 |
|
Exception::Exception() { |
2435 |
|
} |
2436 |
|
|
2437 |
|
Exception::Exception(String format, ...) { |
2438 |
|
va_list arg; |
2439 |
|
va_start(arg, format); |
2440 |
|
Message = assemble(format, arg); |
2441 |
|
va_end(arg); |
2442 |
|
} |
2443 |
|
|
2444 |
|
Exception::Exception(String format, va_list arg) { |
2445 |
|
Message = assemble(format, arg); |
2446 |
|
} |
2447 |
|
|
2448 |
/** @brief Print exception message to stdout. |
/** @brief Print exception message to stdout. |
2449 |
* |
* |
2450 |
* Prints the message of this Exception to the currently defined standard |
* Prints the message of this Exception to the currently defined standard |
2454 |
std::cout << "Serialization::Exception: " << Message << std::endl; |
std::cout << "Serialization::Exception: " << Message << std::endl; |
2455 |
} |
} |
2456 |
|
|
2457 |
|
String Exception::assemble(String format, va_list arg) { |
2458 |
|
char* buf = NULL; |
2459 |
|
vasprintf(&buf, format.c_str(), arg); |
2460 |
|
String s = buf; |
2461 |
|
free(buf); |
2462 |
|
return s; |
2463 |
|
} |
2464 |
|
|
2465 |
} // namespace Serialization |
} // namespace Serialization |