24 #ifndef LIBGIG_SERIALIZATION_H 25 #define LIBGIG_SERIALIZATION_H 45 #ifndef __has_extension 46 # define __has_extension(x) 0 49 #ifndef HAS_BUILTIN_TYPE_TRAITS 50 # if __cplusplus >= 201103L 51 # define HAS_BUILTIN_TYPE_TRAITS 1 52 # elif ( __has_extension(is_class) && __has_extension(is_enum) ) 53 # define HAS_BUILTIN_TYPE_TRAITS 1 54 # elif ( __GNUC__ > 4 || ( __GNUC__ == 4 && __GNUC_MINOR__ >= 3 ) ) 55 # define HAS_BUILTIN_TYPE_TRAITS 1 56 # elif _MSC_VER >= 1400 57 # define HAS_BUILTIN_TYPE_TRAITS 1 58 # elif __INTEL_COMPILER >= 1100 59 # define HAS_BUILTIN_TYPE_TRAITS 1 61 # define HAS_BUILTIN_TYPE_TRAITS 0 65 #if !HAS_BUILTIN_TYPE_TRAITS 66 # include <tr1/type_traits> 67 # define LIBGIG_IS_CLASS(type) std::tr1::__is_union_or_class<type>::value //NOTE: without compiler support we cannot distinguish union from class 69 # define LIBGIG_IS_CLASS(type) __is_class(type) 132 typedef std::string
String;
179 template<
class T_key,
class T_value>
180 using Map = std::map<T_key,T_value>;
232 #if !HAS_BUILTIN_TYPE_TRAITS 233 return std::tr1::is_enum<T>::value;
251 #if !HAS_BUILTIN_TYPE_TRAITS 254 return __is_union(T);
269 #if !HAS_BUILTIN_TYPE_TRAITS 270 return std::tr1::__is_union_or_class<T>::value;
272 return __is_class(T);
296 template<
typename T>
inline 297 String toString(
const T& value) {
298 return std::to_string(value);
302 String toString(
const String& value) {
329 bool operator==(
const UID& other)
const {
return id == other.
id && size == other.
size; }
330 bool operator!=(
const UID& other)
const {
return id != other.
id || size != other.
size; }
331 bool operator<(
const UID& other)
const {
return id < other.
id || (
id == other.
id && size < other.
size); }
332 bool operator>(
const UID& other)
const {
return id > other.
id || (
id == other.
id && size > other.
size); }
343 return Resolver<T>::resolve(obj);
350 static UID resolve(
const T& obj) {
351 const UID uid = { (
ID) &obj,
sizeof(obj) };
358 struct Resolver<T*> {
359 static UID resolve(
const T*
const & obj) {
360 const UID uid = { (
ID) obj,
sizeof(*obj) };
406 #if LIBGIG_SERIALIZATION_INTERNAL 408 static String _encodePrimitiveValue(
const Object& obj);
409 static DataType _popDataTypeBlob(
const char*& p,
const char* end);
410 static Member _popMemberBlob(
const char*& p,
const char* end);
411 static Object _popObjectBlob(
const char*& p,
const char* end);
412 static void _popPrimitiveValue(
const char*& p,
const char* end,
Object& obj);
413 static String _primitiveObjectValueToString(
const Object& obj);
416 static T _primitiveObjectValueToNumber(
const Object& obj);
417 #endif // LIBGIG_SERIALIZATION_INTERNAL 438 size_t size()
const {
return m_size; }
440 bool isPointer()
const;
441 bool isClass()
const;
442 bool isPrimitive()
const;
443 bool isString()
const;
444 bool isInteger()
const;
448 bool isArray()
const;
451 bool isSigned()
const;
454 bool operator==(
const DataType& other)
const;
455 bool operator!=(
const DataType& other)
const;
456 bool operator<(
const DataType& other)
const;
457 bool operator>(
const DataType& other)
const;
458 String asLongDescr()
const;
459 String baseTypeName()
const;
460 String customTypeName(
bool demangle =
false)
const;
461 String customTypeName2(
bool demangle =
false)
const;
475 return Resolver<T>::resolve(data);
480 String customType1 =
"", String customType2 =
"");
482 template<
typename T,
bool T_isPo
inter>
483 struct ResolverBase {
484 static DataType resolve(
const T& data) {
485 const std::type_info& type =
typeid(data);
486 const int sz =
sizeof(data);
491 if (type ==
typeid(int8_t))
return DataType(T_isPointer, sz,
"int8");
492 if (type ==
typeid(uint8_t))
return DataType(T_isPointer, sz,
"uint8");
493 if (type ==
typeid(int16_t))
return DataType(T_isPointer, sz,
"int16");
494 if (type ==
typeid(uint16_t))
return DataType(T_isPointer, sz,
"uint16");
495 if (type ==
typeid(int32_t))
return DataType(T_isPointer, sz,
"int32");
496 if (type ==
typeid(uint32_t))
return DataType(T_isPointer, sz,
"uint32");
497 if (type ==
typeid(int64_t))
return DataType(T_isPointer, sz,
"int64");
498 if (type ==
typeid(uint64_t))
return DataType(T_isPointer, sz,
"uint64");
499 if (type ==
typeid(
size_t)) {
500 if (sz == 1)
return DataType(T_isPointer, sz,
"uint8");
501 if (sz == 2)
return DataType(T_isPointer, sz,
"uint16");
502 if (sz == 4)
return DataType(T_isPointer, sz,
"uint32");
503 if (sz == 8)
return DataType(T_isPointer, sz,
"uint64");
506 if (type ==
typeid(ssize_t)) {
507 if (sz == 1)
return DataType(T_isPointer, sz,
"int8");
508 if (sz == 2)
return DataType(T_isPointer, sz,
"int16");
509 if (sz == 4)
return DataType(T_isPointer, sz,
"int32");
510 if (sz == 8)
return DataType(T_isPointer, sz,
"int64");
513 if (type ==
typeid(
bool))
return DataType(T_isPointer, sz,
"bool");
514 if (type ==
typeid(
float))
return DataType(T_isPointer, sz,
"real32");
515 if (type ==
typeid(
double))
return DataType(T_isPointer, sz,
"real64");
516 if (type ==
typeid(String))
return DataType(T_isPointer, sz,
"String");
518 if (
IsEnum(data))
return DataType(T_isPointer, sz,
"enum", rawCppTypeNameOf(data));
519 if (
IsUnion(data))
return DataType(T_isPointer, sz,
"union", rawCppTypeNameOf(data));
520 if (
IsClass(data))
return DataType(T_isPointer, sz,
"class", rawCppTypeNameOf(data));
528 struct Resolver : ResolverBase<T,false> {
529 static DataType resolve(
const T& data) {
530 return ResolverBase<T,false>::resolve(data);
536 struct Resolver<T*> : ResolverBase<T,true> {
537 static DataType resolve(
const T*& data) {
538 return ResolverBase<T,true>::resolve(*data);
544 struct Resolver<Array<T>> {
546 const int sz =
sizeof(data);
548 return DataType(
false, sz,
"Array", rawCppTypeNameOf(unused));
554 struct Resolver<Array<T>*> {
556 const int sz =
sizeof(*data);
558 return DataType(
true, sz,
"Array", rawCppTypeNameOf(unused));
564 struct Resolver<Set<T>> {
566 const int sz =
sizeof(data);
568 return DataType(
false, sz,
"Set", rawCppTypeNameOf(unused));
574 struct Resolver<Set<T>*> {
576 const int sz =
sizeof(*data);
578 return DataType(
true, sz,
"Set", rawCppTypeNameOf(unused));
583 template<
typename T_key,
typename T_value>
584 struct Resolver<Map<T_key,T_value>> {
586 const int sz =
sizeof(data);
589 return DataType(
false, sz,
"Map", rawCppTypeNameOf(unused1),
590 rawCppTypeNameOf(unused2));
595 template<
typename T_key,
typename T_value>
596 struct Resolver<Map<T_key,T_value>*> {
598 const int sz =
sizeof(*data);
601 return DataType(
true, sz,
"Map", rawCppTypeNameOf(unused1),
602 rawCppTypeNameOf(unused2));
607 static String rawCppTypeNameOf(
const T& data) {
608 #if defined _MSC_VER // Microsoft compiler ... 609 String name =
typeid(data).raw_name();
610 #else // i.e. especially GCC and clang ... 611 String name =
typeid(data).name();
619 String m_baseTypeName;
620 String m_customTypeName;
621 String m_customTypeName2;
625 #if LIBGIG_SERIALIZATION_INTERNAL 626 friend DataType _popDataTypeBlob(
const char*& p,
const char* end);
657 ssize_t offset()
const;
662 bool operator==(
const Member& other)
const;
663 bool operator!=(
const Member& other)
const;
664 bool operator<(
const Member& other)
const;
665 bool operator>(
const Member& other)
const;
677 #if LIBGIG_SERIALIZATION_INTERNAL 678 friend Member _popMemberBlob(
const char*& p,
const char* end);
711 UID uid(
int index = 0)
const;
712 const UIDChain& uidChain()
const;
714 const RawData& rawData()
const;
715 Version version()
const;
716 Version minVersion()
const;
717 bool isVersionCompatibleTo(
const Object& other)
const;
718 std::vector<Member>& members();
719 const std::vector<Member>& members()
const;
720 Member memberNamed(String name)
const;
721 Member memberByUID(
const UID& uid)
const;
722 std::vector<Member> membersOfType(
const DataType& type)
const;
723 int sequenceIndexOf(
const Member& member)
const;
727 bool operator==(
const Object& other)
const;
728 bool operator!=(
const Object& other)
const;
729 bool operator<(
const Object& other)
const;
730 bool operator>(
const Object& other)
const;
731 void setNativeValueFromString(
const String& s);
734 void remove(
const Member& member);
735 void setVersion(Version v);
736 void setMinVersion(Version v);
742 Version m_minVersion;
744 std::vector<Member> m_members;
745 std::function<void(Object& dstObj, const Object& srcObj, void* syncer)> m_sync;
747 #if LIBGIG_SERIALIZATION_INTERNAL 748 friend String _encodePrimitiveValue(
const Object& obj);
749 friend Object _popObjectBlob(
const char*& p,
const char* end);
750 friend void _popPrimitiveValue(
const char*& p,
const char* end,
Object& obj);
751 friend String _primitiveObjectValueToString(
const Object& obj);
754 friend T _primitiveObjectValueToNumber(
const Object& obj);
755 #endif // LIBGIG_SERIALIZATION_INTERNAL 904 OPERATION_DESERIALIZE
939 m_operation = OPERATION_SERIALIZE;
940 m_allObjects.clear();
943 const_cast<T*
>(obj)->serialize(
this);
945 m_operation = OPERATION_NONE;
975 a.m_operation = m_operation = OPERATION_DESERIALIZE;
979 a.m_operation = m_operation = OPERATION_NONE;
1019 template<
typename T>
1024 const RawData& rawData();
1025 virtual String rawDataFormat()
const;
1083 template<
typename T_
classType,
typename T_memberType>
1084 void serializeMember(
const T_classType& nativeObject,
const T_memberType& nativeMember,
const char* memberName) {
1085 const ssize_t offset =
1086 ((
const uint8_t*)(
const void*)&nativeMember) -
1087 ((
const uint8_t*)(
const void*)&nativeObject);
1088 const UIDChain uids = UIDChainResolver<T_memberType>(nativeMember);
1090 const Member member(memberName, uids[0], offset, type);
1092 Object& parent = m_allObjects[parentUID];
1094 const UIDChain uids = UIDChainResolver<T_classType>(nativeObject);
1096 parent =
Object(uids, type);
1098 parent.
members().push_back(member);
1099 const Object obj(uids, type);
1100 const bool bExistsAlready = m_allObjects.count(uids[0]);
1101 const bool isValidObject = obj;
1102 const bool bExistingObjectIsInvalid = !m_allObjects[uids[0]];
1103 if (!bExistsAlready || (bExistingObjectIsInvalid && isValidObject)) {
1104 m_allObjects[uids[0]] = obj;
1107 SerializationRecursion<T_memberType>::serializeObject(
this, nativeMember);
1141 template<
typename T_
classType,
typename T_memberType>
1142 void serializeHeapMember(
const T_classType& nativeObject,
const T_memberType& heapMember,
const char* memberName) {
1143 const ssize_t offset = -1;
1144 const UIDChain uids = UIDChainResolver<T_memberType>(heapMember);
1146 const Member member(memberName, uids[0], offset, type);
1148 Object& parent = m_allObjects[parentUID];
1150 const UIDChain uids = UIDChainResolver<T_classType>(nativeObject);
1152 parent =
Object(uids, type);
1154 parent.
members().push_back(member);
1155 const Object obj(uids, type);
1156 const bool bExistsAlready = m_allObjects.count(uids[0]);
1157 const bool isValidObject = obj;
1158 const bool bExistingObjectIsInvalid = !m_allObjects[uids[0]];
1159 if (!bExistsAlready || (bExistingObjectIsInvalid && isValidObject)) {
1160 m_allObjects[uids[0]] = obj;
1163 SerializationRecursion<T_memberType>::serializeObject(
this, heapMember);
1246 template<
typename T_
classType>
1249 Object& obj = m_allObjects[uid];
1251 const UIDChain uids = UIDChainResolver<T_classType>(nativeObject);
1253 obj =
Object(uids, type);
1287 template<
typename T_
classType>
1290 Object& obj = m_allObjects[uid];
1292 const UIDChain uids = UIDChainResolver<T_classType>(nativeObject);
1294 obj =
Object(uids, type);
1296 setMinVersion(obj, v);
1299 virtual void decode(
const RawData& data);
1300 virtual void decode(
const uint8_t* data,
size_t size);
1302 bool isModified()
const;
1303 void removeMember(
Object& parent,
const Member& member);
1304 void remove(
const Object& obj);
1307 void setAutoValue(
Object&
object, String value);
1308 void setIntValue(
Object&
object, int64_t value);
1309 void setRealValue(
Object&
object,
double value);
1310 void setBoolValue(
Object&
object,
bool value);
1311 void setEnumValue(
Object&
object, uint64_t value);
1312 void setStringValue(
Object&
object, String value);
1313 String valueAsString(
const Object&
object);
1314 int64_t valueAsInt(
const Object&
object);
1315 double valueAsReal(
const Object&
object);
1316 bool valueAsBool(
const Object&
object);
1317 void setVersion(
Object&
object, Version v);
1318 void setMinVersion(
Object&
object, Version v);
1319 String name()
const;
1320 void setName(String name);
1321 String comment()
const;
1322 void setComment(String comment);
1323 time_t timeStampCreated()
const;
1324 time_t timeStampModified()
const;
1331 template<
typename T>
1332 class UIDChainResolver {
1334 UIDChainResolver(
const T& data) {
1338 operator UIDChain()
const {
return m_uid; }
1339 UIDChain operator()()
const {
return m_uid; }
1345 template<
typename T>
1346 class UIDChainResolver<T*> {
1348 UIDChainResolver(
const T*& data) {
1349 const UID uids[2] = {
1350 { &data,
sizeof(data) },
1351 { data,
sizeof(*data) }
1353 m_uid.push_back(uids[0]);
1354 m_uid.push_back(uids[1]);
1357 operator UIDChain()
const {
return m_uid; }
1358 UIDChain operator()()
const {
return m_uid; }
1364 template<
typename T,
bool T_isRecursive>
1365 struct SerializationRecursionImpl {
1366 static void serializeObject(
Archive* archive,
const T& obj) {
1367 const_cast<T&
>(obj).serialize(archive);
1372 template<
typename T,
bool T_isRecursive>
1373 struct SerializationRecursionImpl<T*,T_isRecursive> {
1374 static void serializeObject(
Archive* archive,
const T*& obj) {
1376 const_cast<T*&
>(obj)->serialize(archive);
1381 template<
typename T>
1382 struct SerializationRecursionImpl<T,false> {
1383 static void serializeObject(
Archive* archive,
const T& obj) {}
1387 template<
typename T>
1388 struct SerializationRecursionImpl<T*,
false> {
1389 static void serializeObject(
Archive* archive,
const T*& obj) {}
1393 template<
bool T_isRecursive>
1394 struct SerializationRecursionImpl<String,T_isRecursive> {
1395 static void serializeObject(
Archive* archive,
const String& obj) {}
1399 template<
bool T_isRecursive>
1400 struct SerializationRecursionImpl<String*,T_isRecursive> {
1401 static void serializeObject(
Archive* archive,
const String*& obj) {}
1405 template<
typename T,
bool T_isRecursive>
1406 struct SerializationRecursionImpl<Array<T>,T_isRecursive> {
1408 const UIDChain uids = UIDChainResolver<Array<T>>(obj);
1410 if (archive->operation() == OPERATION_SERIALIZE) {
1411 for (
size_t i = 0; i < obj.size(); ++i) {
1413 obj, obj[i], (
"[" + toString(i) +
"]").c_str()
1417 const_cast<Object&
>(object).m_sync =
1421 const size_t n = srcObj.
members().size();
1422 const_cast<Array<T>&
>(obj).resize(n);
1423 for (
size_t i = 0; i < obj.size(); ++i) {
1425 obj, obj[i], (
"[" + toString(i) +
"]").c_str()
1431 for (
size_t i = 0; i < obj.size(); ++i) {
1432 String name =
"[" + toString(i) +
"]";
1435 ((
Syncer*)syncer)->syncMember(dstMember, srcMember);
1443 template<
typename T,
bool T_isRecursive>
1444 struct SerializationRecursionImpl<Array<T>*,T_isRecursive> {
1447 SerializationRecursionImpl<Array<T>,T_isRecursive>::serializeObject(
1454 template<
typename T,
bool T_isRecursive>
1455 struct SerializationRecursionImpl<Set<T>,T_isRecursive> {
1456 static void serializeObject(
Archive* archive,
const Set<T>& obj) {
1457 const UIDChain uids = UIDChainResolver<Set<T>>(obj);
1459 if (archive->operation() == OPERATION_SERIALIZE) {
1460 for (
const T& key : obj) {
1462 obj, key, (
"[" + toString(key) +
"]").c_str()
1466 const_cast<Object&
>(object).m_sync =
1470 const size_t n = srcObj.
members().size();
1471 const_cast<Set<T>&
>(obj).clear();
1472 for (
size_t i = 0; i < n; ++i) {
1474 String name = member.
name();
1475 if (name.length() < 2 || name[0] !=
'[' ||
1476 *name.rbegin() !=
']')
continue;
1477 name = name.substr(1, name.length() - 2);
1479 const UIDChain uids = UIDChainResolver<T>(key);
1481 Object tmpObj(uids, type);
1483 const_cast<Set<T>&
>(obj).insert(key);
1485 for (
const T& key : obj) {
1487 obj, key, (
"[" + toString(key) +
"]").c_str()
1499 template<
typename T,
bool T_isRecursive>
1500 struct SerializationRecursionImpl<Set<T>*,T_isRecursive> {
1501 static void serializeObject(
Archive* archive,
const Set<T>*& obj) {
1503 SerializationRecursionImpl<Set<T>,T_isRecursive>::serializeObject(
1510 template<
typename T_key,
typename T_value,
bool T_isRecursive>
1511 struct SerializationRecursionImpl<Map<T_key,T_value>,T_isRecursive> {
1513 const UIDChain uids = UIDChainResolver<Map<T_key,T_value>>(obj);
1515 if (archive->operation() == OPERATION_SERIALIZE) {
1516 for (
const auto& it : obj) {
1518 obj, it.second, (
"[" + toString(it.first) +
"]").c_str()
1522 const_cast<Object&
>(object).m_sync =
1526 const size_t n = srcObj.
members().size();
1528 for (
size_t i = 0; i < n; ++i) {
1530 String name = member.
name();
1531 if (name.length() < 2 || name[0] !=
'[' ||
1532 *name.rbegin() !=
']')
continue;
1533 name = name.substr(1, name.length() - 2);
1535 const UIDChain uids = UIDChainResolver<T_key>(key);
1537 Object tmpObj(uids, type);
1541 for (
const auto& it : obj) {
1543 obj, it.second, (
"[" + toString(it.first) +
"]").c_str()
1549 for (
size_t i = 0; i < n; ++i) {
1552 ((
Syncer*)syncer)->syncMember(dstMember, srcMember);
1560 template<
typename T_key,
typename T_value,
bool T_isRecursive>
1561 struct SerializationRecursionImpl<Map<T_key,T_value>*,T_isRecursive> {
1564 SerializationRecursionImpl<Map<T_key,T_value>,T_isRecursive>::serializeObject(
1571 template<
typename T>
1572 struct SerializationRecursion : SerializationRecursionImpl<T, LIBGIG_IS_CLASS(T)> {
1575 class ObjectPool :
public std::map<UID,Object> {
1584 return std::map<UID,Object>::operator[](k);
1588 friend String _encode(
const ObjectPool& objects);
1591 String _encodeRootBlob();
1592 void _popRootBlob(
const char*& p,
const char* end);
1593 void _popObjectsBlob(
const char*& p,
const char* end);
1639 void syncPrimitive(
const Object& dst,
const Object& src);
1644 void syncPointer(
const Object& dst,
const Object& src);
1645 void syncMember(
const Member& dstMember,
const Member& srcMember);
1653 virtual void encode();
1655 ObjectPool m_allObjects;
1662 time_t m_timeCreated;
1663 time_t m_timeModified;
1676 void PrintMessage();
1681 static String assemble(String format, va_list arg);
1686 #endif // LIBGIG_SERIALIZATION_H void serializeHeapMember(const T_classType &nativeObject, const T_memberType &heapMember, const char *memberName)
Serialize a C/C++ member variable allocated on the heap.
Abstract reflection of some native serialized C/C++ data.
static UID from(const T &obj)
Create an unique indentifier for a native C++ object/member/variable.
Destination container for serialization, and source container for deserialization.
std::vector< UID > UIDChain
Chain of UIDs.
static DataType dataTypeOf(const T &data)
Construct a DataType object for the given native C++ data.
Object & objectByUID(const UID &uid)
Access object by its unique identifier.
bool IsUnion(const T &data)
Check whether data is a C++ union type.
Archive is currently neither serializing, nor deserializing.
void * ID
Abstract identifier for serialized C++ objects.
void serialize(const T *obj)
Initiate serialization.
Abstract reflection of a native C++ data type.
void setMinVersion(const T_classType &nativeObject, Version v)
Set a minimum version number for your C++ class.
void deserialize(T *obj)
Initiate deserialization.
std::string String
Textual string.
void setNativeValueFromString(const String &s)
Cast from string to object's data type and assign value natively.
uint32_t Version
Version number data type.
void serializeMember(const T_classType &nativeObject, const T_memberType &nativeMember, const char *memberName)
Serialize a native C/C++ member variable.
time_base_t
To which time zone a certain timing information relates to.
bool isValid() const
Check whether this is a valid unique identifier.
void setVersion(const T_classType &nativeObject, Version v)
Set current version number for your C++ class.
Unique identifier referring to one specific native C++ object, member, fundamental variable...
std::vector< Member > & members()
All members of the original native C/C++ struct or class instance.
void operator<<(const T &obj)
Initiate serialization of your C++ objects.
Synchronizes 2 archives with each other.
ID id
Abstract non-unique ID of the object or member in question.
std::vector< uint8_t > RawData
Raw data stream of serialized C++ objects.
The time stamp relates to the machine's local time zone. Request a time stamp in local time if you wa...
The time stamp relates to "Greenwhich Mean Time" zone, also known as "Coordinated Universal Time"...
Will be thrown whenever an error occurs during an serialization or deserialization process...
void operator>>(T &obj)
Initiate deserialization of your C++ objects.
bool IsEnum(const T &data)
Check whether data is a C/C++ enum type.
Archive is currently serializing.
std::set< T > Set
Set<> template.
operation_t
Current activity of Archive object.
Member memberNamed(String name) const
Get the member of this Object with given name.
std::vector< T > Array
Array<> template.
std::map< T_key, T_value > Map
Map<> template.
UID uid(int index=0) const
Unique identifier of this Object.
bool IsClass(const T &data)
Check whether data is a C/C++ struct or C++ class type.
size_t size() const
Returns native memory size of the respective C++ object or variable.
const UID NO_UID
Reflects an invalid UID and behaves similar to NULL as invalid value for pointer types.
Serialization / deserialization framework.
size_t size
Memory size of the object or member in question.
Abstract reflection of a native C++ class/struct's member variable.
String name() const
Name of the member.