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", "Array", "Set", "enum", "union" or "class" |
105 |
|
* @param customType - this is only used for base types "enum", "union", |
106 |
|
* "class", "Array" or "Set", in which case this |
107 |
|
* identifies the user defined type name (e.g. "Foo" for |
108 |
|
* @c class @c Foo or e.g. "Bar" for @c Array<Bar> |
109 |
|
* respectively), for all other types this is empty |
110 |
|
*/ |
111 |
DataType::DataType(bool isPointer, int size, String baseType, String customType) { |
DataType::DataType(bool isPointer, int size, String baseType, String customType) { |
112 |
m_size = size; |
m_size = size; |
113 |
m_isPointer = isPointer; |
m_isPointer = isPointer; |
144 |
* (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 |
145 |
* type. |
* type. |
146 |
* |
* |
147 |
|
* @note: Data types which enjoy out of the box serialization support by |
148 |
|
* this framework, like @c String and @c Array<> are @b NOT handled as class |
149 |
|
* data types by this framwork. So @c isClass() returns @c false for e.g. |
150 |
|
* @c String and any @c Array<> based data type. |
151 |
|
* |
152 |
* Note that in the following example: |
* Note that in the following example: |
153 |
* @code |
* @code |
154 |
* struct Foo { |
* struct Foo { |
173 |
* (this DataType instance is reflecting) is a primitive, fundamental C/C++ |
* (this DataType instance is reflecting) is a primitive, fundamental C/C++ |
174 |
* data type. Those are fundamental data types which are already predefined |
* data type. Those are fundamental data types which are already predefined |
175 |
* 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, |
176 |
* @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 |
177 |
* including pointers to user defined types like: |
* @c int*, @c double**, but including pointers to user defined types like: |
178 |
* @code |
* @code |
179 |
* struct Foo { |
* struct Foo { |
180 |
* int a; |
* int a; |
188 |
* @see isPointer() |
* @see isPointer() |
189 |
*/ |
*/ |
190 |
bool DataType::isPrimitive() const { |
bool DataType::isPrimitive() const { |
191 |
return !isClass(); |
return !isClass() && !isArray() && !isSet(); |
192 |
|
} |
193 |
|
|
194 |
|
/** @brief Whether this is a C++ @c String data type. |
195 |
|
* |
196 |
|
* Returns @c true if the respective native C/C++ object, member or variable |
197 |
|
* (this DataType instance is reflecting) is a C++ @c String object (a.k.a. |
198 |
|
* @c std::string from the C++ STL). |
199 |
|
* |
200 |
|
* Note that this framework handles @c String objects as if they were a |
201 |
|
* fundamental, primitive C/C++ data type, so @c isPrimitive() returns |
202 |
|
* @c true for strings. |
203 |
|
*/ |
204 |
|
bool DataType::isString() const { |
205 |
|
return m_baseTypeName == "String"; |
206 |
} |
} |
207 |
|
|
208 |
/** @brief Whether this is an integer C/C++ data type. |
/** @brief Whether this is an integer C/C++ data type. |
270 |
return m_baseTypeName == "enum"; |
return m_baseTypeName == "enum"; |
271 |
} |
} |
272 |
|
|
273 |
|
/** @brief Whether this is a C++ @c Array<> object type. |
274 |
|
* |
275 |
|
* Returns @c true if the respective native C/C++ object, member or variable |
276 |
|
* (this DataType instance is reflecting) is a C++ @c Array<> container |
277 |
|
* object type. |
278 |
|
* |
279 |
|
* @note: This framework handles @c Array<> types neither as primitive |
280 |
|
* types, nor as class types. So @c isPrimitive() and @c isClass() both |
281 |
|
* return @c false for arrays. |
282 |
|
* |
283 |
|
* @see isPointer() |
284 |
|
*/ |
285 |
|
bool DataType::isArray() const { |
286 |
|
return m_baseTypeName == "Array"; |
287 |
|
} |
288 |
|
|
289 |
|
/** @brief Whether this is a C++ @c Set<> object type. |
290 |
|
* |
291 |
|
* Returns @c true if the respective native C/C++ object, member or variable |
292 |
|
* (this DataType instance is reflecting) is a C++ @c Set<> unique container |
293 |
|
* object type. |
294 |
|
* |
295 |
|
* @note: This framework handles @c Set<> types neither as primitive |
296 |
|
* types, nor as class types. So @c isPrimitive() and @c isClass() both |
297 |
|
* return @c false for sets. |
298 |
|
* |
299 |
|
* @see isPointer() |
300 |
|
*/ |
301 |
|
bool DataType::isSet() const { |
302 |
|
return m_baseTypeName == "Set"; |
303 |
|
} |
304 |
|
|
305 |
/** @brief Whether this is a signed integer C/C++ data type. |
/** @brief Whether this is a signed integer C/C++ data type. |
306 |
* |
* |
307 |
* 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 |
332 |
* 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* |
333 |
* 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 |
334 |
* would return @c false. |
* would return @c false. |
335 |
|
* |
336 |
|
* As an exception here, classes and structs with the same class/struct name |
337 |
|
* but different sizes are also considered to be "equal". This relaxed |
338 |
|
* requirement is necessary to retain backward compatiblity to older |
339 |
|
* versions of the same native C++ classes/structs. |
340 |
*/ |
*/ |
341 |
bool DataType::operator==(const DataType& other) const { |
bool DataType::operator==(const DataType& other) const { |
342 |
return m_baseTypeName == other.m_baseTypeName && |
return m_baseTypeName == other.m_baseTypeName && |
343 |
m_customTypeName == other.m_customTypeName && |
m_customTypeName == other.m_customTypeName && |
344 |
m_size == other.m_size && |
(m_size == other.m_size || (isClass() && other.isClass())) && |
345 |
m_isPointer == other.m_isPointer; |
m_isPointer == other.m_isPointer; |
346 |
} |
} |
347 |
|
|
368 |
bool DataType::operator<(const DataType& other) const { |
bool DataType::operator<(const DataType& other) const { |
369 |
return m_baseTypeName < other.m_baseTypeName || |
return m_baseTypeName < other.m_baseTypeName || |
370 |
(m_baseTypeName == other.m_baseTypeName && |
(m_baseTypeName == other.m_baseTypeName && |
371 |
m_customTypeName < other.m_customTypeName || |
(m_customTypeName < other.m_customTypeName || |
372 |
(m_customTypeName == other.m_customTypeName && |
(m_customTypeName == other.m_customTypeName && |
373 |
m_size < other.m_size || |
(m_size < other.m_size || |
374 |
(m_size == other.m_size && |
(m_size == other.m_size && |
375 |
m_isPointer < other.m_isPointer))); |
m_isPointer < other.m_isPointer))))); |
376 |
} |
} |
377 |
|
|
378 |
/** @brief Greater than comparison. |
/** @brief Greater than comparison. |
448 |
|
|
449 |
/** @brief The user defined C/C++ data type name of this data type. |
/** @brief The user defined C/C++ data type name of this data type. |
450 |
* |
* |
451 |
* Call this method on user defined C/C++ data types like @c enum, @c struct |
* Call this method on user defined C/C++ data types like @c enum, |
452 |
* and @c class types to retrieve the user defined type name portion of |
* @c struct, @c class or @c Array<> types to retrieve the user defined type |
453 |
* those data types. Note that this method is only intended for such user |
* name portion of those data types. Note that this method is only intended |
454 |
* defined data types. For all fundamental, primitive data types (like i.e. |
* for such user defined data types. For all fundamental, primitive data |
455 |
* @c int) this method returns an empty string instead. |
* types (like i.e. @c int) this method returns an empty string instead. |
456 |
* |
* |
457 |
* This method takes an optional boolean argument @b demangle, which allows |
* This method takes an optional boolean argument @b demangle, which allows |
458 |
* you define whether you are interested in the raw C++ type name or rather |
* you define whether you are interested in the raw C++ type name or rather |
477 |
* In the latter example @c customTypeName(true) would return for both |
* In the latter example @c customTypeName(true) would return for both |
478 |
* @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. |
479 |
* |
* |
480 |
|
* @b Windows: please note that the current implementation of this method |
481 |
|
* on Windows is @b not thread safe! |
482 |
|
* |
483 |
* @see isPointer(), baseTypeName() |
* @see isPointer(), baseTypeName() |
484 |
*/ |
*/ |
485 |
String DataType::customTypeName(bool demangle) const { |
String DataType::customTypeName(bool demangle) const { |
486 |
if (!demangle) return m_customTypeName; |
if (!demangle) return m_customTypeName; |
487 |
|
#ifdef _MSC_VER |
488 |
|
const size_t MAXLENGTH = 1024; |
489 |
|
char result[MAXLENGTH]; |
490 |
|
|
491 |
|
//FIXME: calling UnDecorateSymbolName() is not thread safe! |
492 |
|
//Skip the first char |
493 |
|
size_t size = UnDecorateSymbolName(m_customTypeName.c_str() +1, result, MAXLENGTH, UNDNAME_32_BIT_DECODE | UNDNAME_NO_ARGUMENTS); |
494 |
|
if (size) |
495 |
|
{ |
496 |
|
return result; |
497 |
|
} |
498 |
|
return m_customTypeName; |
499 |
|
#else |
500 |
int status; |
int status; |
501 |
const char* result = |
char* result = |
502 |
abi::__cxa_demangle(m_customTypeName.c_str(), 0, 0, &status); |
abi::__cxa_demangle(m_customTypeName.c_str(), 0, 0, &status); |
503 |
return (status == 0) ? result : m_customTypeName; |
String sResult = result; |
504 |
|
free(result); |
505 |
|
return (status == 0) ? sResult : m_customTypeName; |
506 |
|
#endif |
507 |
} |
} |
508 |
|
|
509 |
// *************** Member *************** |
// *************** Member *************** |
526 |
m_offset = 0; |
m_offset = 0; |
527 |
} |
} |
528 |
|
|
529 |
Member::Member(String name, UID uid, size_t offset, DataType type) { |
Member::Member(String name, UID uid, ssize_t offset, DataType type) { |
530 |
m_name = name; |
m_name = name; |
531 |
m_uid = uid; |
m_uid = uid; |
532 |
m_offset = offset; |
m_offset = offset; |
608 |
* @c b and @c c instead. For most 64 bit architectures this example would |
* @c b and @c c instead. For most 64 bit architectures this example would |
609 |
* now still return @c 0 for member @c a, but @c 8 for member @c b and @c 16 |
* now still return @c 0 for member @c a, but @c 8 for member @c b and @c 16 |
610 |
* for member @c c. |
* for member @c c. |
611 |
|
* |
612 |
|
* @note Offset is intended for native members only, that is member |
613 |
|
* variables which are memory located directly within the associated parent |
614 |
|
* data structure. For members allocated on the heap @c offset() always |
615 |
|
* returns @c -1 instead since there is no constant, static offset |
616 |
|
* relationship between data on the heap and the parent structure owning |
617 |
|
* their life-time control. |
618 |
*/ |
*/ |
619 |
size_t Member::offset() const { |
ssize_t Member::offset() const { |
620 |
return m_offset; |
return m_offset; |
621 |
} |
} |
622 |
|
|
681 |
bool Member::operator<(const Member& other) const { |
bool Member::operator<(const Member& other) const { |
682 |
return m_uid < other.m_uid || |
return m_uid < other.m_uid || |
683 |
(m_uid == other.m_uid && |
(m_uid == other.m_uid && |
684 |
m_offset < other.m_offset || |
(m_offset < other.m_offset || |
685 |
(m_offset == other.m_offset && |
(m_offset == other.m_offset && |
686 |
m_name < other.m_name || |
(m_name < other.m_name || |
687 |
(m_name == other.m_name && |
(m_name == other.m_name && |
688 |
m_type < other.m_type))); |
m_type < other.m_type))))); |
689 |
} |
} |
690 |
|
|
691 |
/** @brief Greater than comparison. |
/** @brief Greater than comparison. |
777 |
return (index < m_uid.size()) ? m_uid[index] : NO_UID; |
return (index < m_uid.size()) ? m_uid[index] : NO_UID; |
778 |
} |
} |
779 |
|
|
780 |
|
static void _setNativeValueFromString(void* ptr, const DataType& type, const char* s) { |
781 |
|
if (type.isPrimitive() && !type.isPointer()) { |
782 |
|
if (type.isInteger() || type.isEnum()) { |
783 |
|
if (type.isSigned()) { |
784 |
|
if (type.size() == 1) |
785 |
|
*(int8_t*)ptr = (int8_t) atoll(s); |
786 |
|
else if (type.size() == 2) |
787 |
|
*(int16_t*)ptr = (int16_t) atoll(s); |
788 |
|
else if (type.size() == 4) |
789 |
|
*(int32_t*)ptr = (int32_t) atoll(s); |
790 |
|
else if (type.size() == 8) |
791 |
|
*(int64_t*)ptr = (int64_t) atoll(s); |
792 |
|
else |
793 |
|
assert(false /* unknown signed int type size */); |
794 |
|
} else { |
795 |
|
if (type.size() == 1) |
796 |
|
*(uint8_t*)ptr = (uint8_t) atoll(s); |
797 |
|
else if (type.size() == 2) |
798 |
|
*(uint16_t*)ptr = (uint16_t) atoll(s); |
799 |
|
else if (type.size() == 4) |
800 |
|
*(uint32_t*)ptr = (uint32_t) atoll(s); |
801 |
|
else if (type.size() == 8) |
802 |
|
*(uint64_t*)ptr = (uint64_t) atoll(s); |
803 |
|
else |
804 |
|
assert(false /* unknown unsigned int type size */); |
805 |
|
} |
806 |
|
} else if (type.isReal()) { |
807 |
|
if (type.size() == sizeof(float)) |
808 |
|
*(float*)ptr = (float) atof(s); |
809 |
|
else if (type.size() == sizeof(double)) |
810 |
|
*(double*)ptr = (double) atof(s); |
811 |
|
else |
812 |
|
assert(false /* unknown floating point type */); |
813 |
|
} else if (type.isBool()) { |
814 |
|
String lower = toLowerCase(s); |
815 |
|
const bool b = lower != "0" && lower != "false" && lower != "no"; |
816 |
|
*(bool*)ptr = b; |
817 |
|
} else if (type.isString()) { |
818 |
|
*(String*)ptr = s; |
819 |
|
} else { |
820 |
|
assert(false /* no built-in cast from string support for this data type */); |
821 |
|
} |
822 |
|
} |
823 |
|
} |
824 |
|
|
825 |
|
/** @brief Cast from string to object's data type and assign value natively. |
826 |
|
* |
827 |
|
* The passed String @a s is decoded from its string representation to this |
828 |
|
* object's corresponding native data type, then that casted value is |
829 |
|
* assigned to the native memory location this Object is referring to. |
830 |
|
* |
831 |
|
* Note: This method may only be called for data types which enjoy built-in |
832 |
|
* support for casting from string to their native data type, which are |
833 |
|
* basically primitive data types (e.g. @c int, @c bool, @c double, etc.) or |
834 |
|
* @c String objects. For all other data types calling this method will |
835 |
|
* cause an assertion fault at runtime. |
836 |
|
* |
837 |
|
* @param s - textual string representation of the value to be assigned to |
838 |
|
* this object |
839 |
|
*/ |
840 |
|
void Object::setNativeValueFromString(const String& s) { |
841 |
|
const ID& id = uid().id; |
842 |
|
void* ptr = (void*)id; |
843 |
|
_setNativeValueFromString(ptr, m_type, s.c_str()); |
844 |
|
} |
845 |
|
|
846 |
/** @brief Unique identifier chain of this Object. |
/** @brief Unique identifier chain of this Object. |
847 |
* |
* |
848 |
* Returns the entire unique identifier chain of this Object. |
* Returns the entire unique identifier chain of this Object. |
893 |
* 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 |
894 |
* 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 |
895 |
* or @c class layout or implementation. For primitive, fundamental C/C++ |
* or @c class layout or implementation. For primitive, fundamental C/C++ |
896 |
* data types the return value of this method has no meaning. |
* data types (including @c String objects) the return value of this method |
897 |
|
* has no meaning. |
898 |
* |
* |
899 |
* @see Archive::setVersion() for more details about this overall topic. |
* @see Archive::setVersion() for more details about this overall topic. |
900 |
*/ |
*/ |
907 |
* 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 |
908 |
* 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++ |
909 |
* @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 |
910 |
* with. For primitive, fundamental C/C++ data types the return value of |
* with. For primitive, fundamental C/C++ data types (including @c String |
911 |
* this method has no meaning. |
* objects) the return value of this method has no meaning. |
912 |
* |
* |
913 |
* @see Archive::setVersion() and Archive::setMinVersion() for more details |
* @see Archive::setVersion() and Archive::setMinVersion() for more details |
914 |
* about this overall topic. |
* about this overall topic. |
1238 |
m_root = NO_UID; |
m_root = NO_UID; |
1239 |
m_isModified = false; |
m_isModified = false; |
1240 |
m_timeCreated = m_timeModified = LIBGIG_EPOCH_TIME; |
m_timeCreated = m_timeModified = LIBGIG_EPOCH_TIME; |
1241 |
decode(m_rawData); |
decode(data); |
1242 |
} |
} |
1243 |
|
|
1244 |
/** @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. |
1374 |
assert(false /* unknown floating point type */); |
assert(false /* unknown floating point type */); |
1375 |
} else if (type.isBool()) { |
} else if (type.isBool()) { |
1376 |
s = ToString(*(bool*)ptr); |
s = ToString(*(bool*)ptr); |
1377 |
|
} else if (type.isString()) { |
1378 |
|
s = obj.m_data.empty() ? *(String*)ptr : String((const char*)ptr); |
1379 |
} else { |
} else { |
1380 |
assert(false /* unknown primitive type */); |
assert(false /* unknown primitive type */); |
1381 |
} |
} |
|
|
|
1382 |
} |
} |
1383 |
return s; |
return s; |
1384 |
} |
} |
1385 |
|
|
1386 |
template<typename T> |
template<typename T> |
1387 |
|
inline T _stringToNumber(const String& s) { |
1388 |
|
assert(false /* String cast to unknown primitive number type */); |
1389 |
|
} |
1390 |
|
|
1391 |
|
template<> |
1392 |
|
inline int64_t _stringToNumber(const String& s) { |
1393 |
|
return atoll(s.c_str()); |
1394 |
|
} |
1395 |
|
|
1396 |
|
template<> |
1397 |
|
inline double _stringToNumber(const String& s) { |
1398 |
|
return atof(s.c_str()); |
1399 |
|
} |
1400 |
|
|
1401 |
|
template<> |
1402 |
|
inline bool _stringToNumber(const String& s) { |
1403 |
|
return (bool) atoll(s.c_str()); |
1404 |
|
} |
1405 |
|
|
1406 |
|
template<typename T> |
1407 |
static T _primitiveObjectValueToNumber(const Object& obj) { |
static T _primitiveObjectValueToNumber(const Object& obj) { |
1408 |
T value = 0; |
T value = 0; |
1409 |
const DataType& type = obj.type(); |
const DataType& type = obj.type(); |
1445 |
assert(false /* unknown floating point type */); |
assert(false /* unknown floating point type */); |
1446 |
} else if (type.isBool()) { |
} else if (type.isBool()) { |
1447 |
value = (T)*(bool*)ptr; |
value = (T)*(bool*)ptr; |
1448 |
|
} else if (type.isString()) { |
1449 |
|
value = _stringToNumber<T>( |
1450 |
|
obj.m_data.empty() ? *(String*)ptr : String((const char*)ptr) |
1451 |
|
); |
1452 |
} else { |
} else { |
1453 |
assert(false /* unknown primitive type */); |
assert(false /* unknown primitive type */); |
1454 |
} |
} |
1482 |
return _encodeBlob(s); |
return _encodeBlob(s); |
1483 |
} |
} |
1484 |
|
|
1485 |
|
/* |
1486 |
|
* Srx format history: |
1487 |
|
* - 1.0: Initial version. |
1488 |
|
* - 1.1: Adds "String", "Array" and "Set" data types. |
1489 |
|
*/ |
1490 |
#define MAGIC_START "Srx1v" |
#define MAGIC_START "Srx1v" |
1491 |
#define ENCODING_FORMAT_MINOR_VERSION 0 |
#define ENCODING_FORMAT_MINOR_VERSION 1 |
1492 |
|
|
1493 |
String Archive::_encodeRootBlob() { |
String Archive::_encodeRootBlob() { |
1494 |
String s; |
String s; |
1614 |
return s; |
return s; |
1615 |
} |
} |
1616 |
|
|
1617 |
|
static void _popStringBlob(const char*& p, const char* end, RawData& rawData) { |
1618 |
|
String s = _popStringBlob(p, end); |
1619 |
|
rawData.resize(s.length() + 1); |
1620 |
|
strcpy((char*)&rawData[0], &s[0]); |
1621 |
|
} |
1622 |
|
|
1623 |
static time_t _popTimeBlob(const char*& p, const char* end) { |
static time_t _popTimeBlob(const char*& p, const char* end) { |
1624 |
const uint64_t i = _popIntBlob<uint64_t>(p, end); |
const uint64_t i = _popIntBlob<uint64_t>(p, end); |
1625 |
return (time_t) i; |
return (time_t) i; |
1626 |
} |
} |
1627 |
|
|
1628 |
DataType _popDataTypeBlob(const char*& p, const char* end) { |
static DataType _popDataTypeBlob(const char*& p, const char* end) { |
1629 |
_Blob blob = _decodeBlob(p, end); |
_Blob blob = _decodeBlob(p, end); |
1630 |
p = blob.p; |
p = blob.p; |
1631 |
end = blob.end; |
end = blob.end; |
1676 |
if (p >= end) return m; |
if (p >= end) return m; |
1677 |
|
|
1678 |
m.m_uid = _popUIDBlob(p, end); |
m.m_uid = _popUIDBlob(p, end); |
1679 |
m.m_offset = _popIntBlob<size_t>(p, end); |
m.m_offset = _popIntBlob<ssize_t>(p, end); |
1680 |
m.m_name = _popStringBlob(p, end); |
m.m_name = _popStringBlob(p, end); |
1681 |
m.m_type = _popDataTypeBlob(p, end); |
m.m_type = _popDataTypeBlob(p, end); |
1682 |
assert(m.type()); |
assert(m.type()); |
1738 |
assert(false /* unknown floating point type */); |
assert(false /* unknown floating point type */); |
1739 |
} else if (type.isBool()) { |
} else if (type.isBool()) { |
1740 |
_popIntBlob<uint8_t>(p, end, obj.m_data); |
_popIntBlob<uint8_t>(p, end, obj.m_data); |
1741 |
|
} else if (type.isString()) { |
1742 |
|
_popStringBlob(p, end, obj.m_data); |
1743 |
} else { |
} else { |
1744 |
assert(false /* unknown primitive type */); |
assert(false /* unknown primitive type */); |
1745 |
} |
} |
2280 |
m_isModified = true; |
m_isModified = true; |
2281 |
} |
} |
2282 |
|
|
2283 |
|
/** @brief Set new textual string for given String object. |
2284 |
|
* |
2285 |
|
* Sets the new textual string @a value to the given String @a object. |
2286 |
|
* |
2287 |
|
* @param object - the String object to be changed |
2288 |
|
* @param value - the new textual string to be assigned to the @a object |
2289 |
|
* @throws Exception if @a object is not a String type. |
2290 |
|
*/ |
2291 |
|
void Archive::setStringValue(Object& object, String value) { |
2292 |
|
if (!object) return; |
2293 |
|
if (!object.type().isString()) |
2294 |
|
throw Exception("Not a String data type"); |
2295 |
|
Object* pObject = &object; |
2296 |
|
if (object.type().isPointer()) { |
2297 |
|
Object& obj = objectByUID(object.uid(1)); |
2298 |
|
if (!obj) return; |
2299 |
|
pObject = &obj; |
2300 |
|
} |
2301 |
|
pObject->m_data.resize(value.length() + 1); |
2302 |
|
char* ptr = (char*) &pObject->m_data[0]; |
2303 |
|
strcpy(ptr, &value[0]); |
2304 |
|
m_isModified = true; |
2305 |
|
} |
2306 |
|
|
2307 |
/** @brief Automatically cast and assign appropriate value to object. |
/** @brief Automatically cast and assign appropriate value to object. |
2308 |
* |
* |
2309 |
* This method automatically converts the given @a value from textual string |
* This method automatically converts the given @a value from textual string |
2332 |
setBoolValue(object, false); |
setBoolValue(object, false); |
2333 |
else |
else |
2334 |
setBoolValue(object, atof(value.c_str())); |
setBoolValue(object, atof(value.c_str())); |
2335 |
} else if (type.isEnum()) |
} else if (type.isString()) |
2336 |
|
setStringValue(object, value); |
2337 |
|
else if (type.isEnum()) |
2338 |
setEnumValue(object, atoll(value.c_str())); |
setEnumValue(object, atoll(value.c_str())); |
2339 |
else |
else |
2340 |
throw Exception("Not a primitive data type"); |
throw Exception("Not a primitive data type"); |
2431 |
return _primitiveObjectValueToNumber<bool>(*pObject); |
return _primitiveObjectValueToNumber<bool>(*pObject); |
2432 |
} |
} |
2433 |
|
|
2434 |
|
Archive::operation_t Archive::operation() const { |
2435 |
|
return m_operation; |
2436 |
|
} |
2437 |
|
|
2438 |
// *************** Archive::Syncer *************** |
// *************** Archive::Syncer *************** |
2439 |
// * |
// * |
2440 |
|
|
2456 |
memcpy(pDst, &srcObj.rawData()[0], dstObj.type().size()); |
memcpy(pDst, &srcObj.rawData()[0], dstObj.type().size()); |
2457 |
} |
} |
2458 |
|
|
2459 |
|
void Archive::Syncer::syncString(const Object& dstObj, const Object& srcObj) { |
2460 |
|
assert(dstObj.type().isString()); |
2461 |
|
assert(dstObj.type() == srcObj.type()); |
2462 |
|
String* pDst = (String*)(void*)dstObj.uid().id; |
2463 |
|
*pDst = (String) (const char*) &srcObj.rawData()[0]; |
2464 |
|
} |
2465 |
|
|
2466 |
|
void Archive::Syncer::syncArray(const Object& dstObj, const Object& srcObj) { |
2467 |
|
assert(dstObj.type().isArray()); |
2468 |
|
assert(dstObj.type() == srcObj.type()); |
2469 |
|
dstObj.m_sync(const_cast<Object&>(dstObj), srcObj, this); |
2470 |
|
} |
2471 |
|
|
2472 |
|
void Archive::Syncer::syncSet(const Object& dstObj, const Object& srcObj) { |
2473 |
|
assert(dstObj.type().isSet()); |
2474 |
|
assert(dstObj.type() == srcObj.type()); |
2475 |
|
dstObj.m_sync(const_cast<Object&>(dstObj), srcObj, this); |
2476 |
|
} |
2477 |
|
|
2478 |
void Archive::Syncer::syncPointer(const Object& dstObj, const Object& srcObj) { |
void Archive::Syncer::syncPointer(const Object& dstObj, const Object& srcObj) { |
2479 |
assert(dstObj.type().isPointer()); |
assert(dstObj.type().isPointer()); |
2480 |
assert(dstObj.type() == srcObj.type()); |
assert(dstObj.type() == srcObj.type()); |
2501 |
m_dst.m_allObjects.erase(dstObj.uid()); |
m_dst.m_allObjects.erase(dstObj.uid()); |
2502 |
|
|
2503 |
if (dstObj.type().isPrimitive() && !dstObj.type().isPointer()) { |
if (dstObj.type().isPrimitive() && !dstObj.type().isPointer()) { |
2504 |
syncPrimitive(dstObj, srcObj); |
if (dstObj.type().isString()) |
2505 |
|
syncString(dstObj, srcObj); |
2506 |
|
else |
2507 |
|
syncPrimitive(dstObj, srcObj); |
2508 |
return; // end of recursion |
return; // end of recursion |
2509 |
} |
} |
2510 |
|
|
2511 |
|
if (dstObj.type().isArray()) { |
2512 |
|
syncArray(dstObj, srcObj); |
2513 |
|
return; |
2514 |
|
} |
2515 |
|
|
2516 |
|
if (dstObj.type().isSet()) { |
2517 |
|
syncSet(dstObj, srcObj); |
2518 |
|
return; |
2519 |
|
} |
2520 |
|
|
2521 |
if (dstObj.type().isPointer()) { |
if (dstObj.type().isPointer()) { |
2522 |
syncPointer(dstObj, srcObj); |
syncPointer(dstObj, srcObj); |
2523 |
return; |
return; |