--- libgig/trunk/src/Serialization.h 2017/05/06 13:43:43 3153 +++ libgig/trunk/src/Serialization.h 2017/05/11 23:06:40 3178 @@ -34,6 +34,34 @@ #include #include #include +#include + +#ifndef __has_extension +# define __has_extension(x) 0 +#endif + +#ifndef HAS_BUILTIN_TYPE_TRAITS +# if __cplusplus >= 201103L +# define HAS_BUILTIN_TYPE_TRAITS 1 +# elif ( __has_extension(is_class) && __has_extension(is_enum) ) +# define HAS_BUILTIN_TYPE_TRAITS 1 +# elif ( __GNUC__ > 4 || ( __GNUC__ == 4 && __GNUC_MINOR__ >= 3 ) ) +# define HAS_BUILTIN_TYPE_TRAITS 1 +# elif _MSC_VER >= 1400 /* MS Visual C++ 8.0 (Visual Studio 2005) */ +# define HAS_BUILTIN_TYPE_TRAITS 1 +# elif __INTEL_COMPILER >= 1100 +# define HAS_BUILTIN_TYPE_TRAITS 1 +# else +# define HAS_BUILTIN_TYPE_TRAITS 0 +# endif +#endif + +#if !HAS_BUILTIN_TYPE_TRAITS +# include +# define LIBGIG_IS_CLASS(type) std::tr1::__is_union_or_class::value //NOTE: without compiler support we cannot distinguish union from class +#else +# define LIBGIG_IS_CLASS(type) __is_class(type) +#endif /** @brief Serialization / deserialization framework. * @@ -102,19 +130,36 @@ OPERATION_DESERIALIZE }; + enum time_base_t { + LOCAL_TIME, + UTC_TIME + }; + template bool IsEnum(const T& data) { + #if !HAS_BUILTIN_TYPE_TRAITS + return std::tr1::is_enum::value; + #else return __is_enum(T); + #endif } template bool IsUnion(const T& data) { + #if !HAS_BUILTIN_TYPE_TRAITS + return false; // without compiler support we cannot distinguish union from class + #else return __is_union(T); + #endif } template bool IsClass(const T& data) { + #if !HAS_BUILTIN_TYPE_TRAITS + return std::tr1::__is_union_or_class::value; // without compiler support we cannot distinguish union from class + #else return __is_class(T); + #endif } /*template @@ -156,7 +201,8 @@ template struct Resolver { static UID resolve(const T& obj) { - return (UID) { (ID) &obj, sizeof(obj) }; + const UID uid = { (ID) &obj, sizeof(obj) }; + return uid; } }; @@ -164,7 +210,8 @@ template struct Resolver { static UID resolve(const T* const & obj) { - return (UID) { (ID) obj, sizeof(*obj) }; + const UID uid = { (ID) obj, sizeof(*obj) }; + return uid; } }; }; @@ -184,6 +231,9 @@ static Object _popObjectBlob(const char*& p, const char* end); static void _popPrimitiveValue(const char*& p, const char* end, Object& obj); static String _primitiveObjectValueToString(const Object& obj); + // | + template + static T _primitiveObjectValueToNumber(const Object& obj); /** @brief Abstract reflection of a native C++ data type. * @@ -213,7 +263,7 @@ bool operator>(const DataType& other) const; String asLongDescr() const; String baseTypeName() const { return m_baseTypeName; } - String customTypeName() const { return m_customTypeName; } + String customTypeName(bool demangle = false) const; template static DataType dataTypeOf(const T& data) { @@ -388,6 +438,10 @@ friend Object _popObjectBlob(const char*& p, const char* end); friend void _popPrimitiveValue(const char*& p, const char* end, Object& obj); friend String _primitiveObjectValueToString(const Object& obj); + + template + friend T _primitiveObjectValueToNumber(const Object& obj); + friend class Archive; }; @@ -587,6 +641,17 @@ void setBoolValue(Object& object, bool value); void setEnumValue(Object& object, uint64_t value); String valueAsString(const Object& object); + int64_t valueAsInt(const Object& object); + double valueAsReal(const Object& object); + bool valueAsBool(const Object& object); + String name() const; + void setName(String name); + String comment() const; + void setComment(String comment); + time_t timeStampCreated() const; + time_t timeStampModified() const; + tm dateTimeCreated(time_base_t base = LOCAL_TIME) const; + tm dateTimeModified(time_base_t base = LOCAL_TIME) const; protected: // UID resolver for non-pointer types @@ -608,8 +673,12 @@ class UIDChainResolver { public: UIDChainResolver(const T*& data) { - m_uid.push_back((UID) { &data, sizeof(data) }); - m_uid.push_back((UID) { data, sizeof(*data) }); + const UID uids[2] = { + { &data, sizeof(data) }, + { data, sizeof(*data) } + }; + m_uid.push_back(uids[0]); + m_uid.push_back(uids[1]); } operator UIDChain() const { return m_uid; } @@ -649,7 +718,7 @@ // Automatically handles recursion for class/struct types, while ignoring all primitive types. template - struct SerializationRecursion : SerializationRecursionImpl { + struct SerializationRecursion : SerializationRecursionImpl { }; class ObjectPool : public std::map { @@ -694,6 +763,10 @@ UID m_root; RawData m_rawData; bool m_isModified; + String m_name; + String m_comment; + time_t m_timeCreated; + time_t m_timeModified; }; /**