--- libgig/trunk/src/Serialization.h 2017/05/08 17:18:07 3156 +++ libgig/trunk/src/Serialization.h 2017/05/11 23:06:40 3178 @@ -36,6 +36,33 @@ #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. * * See class Archive as starting point for how to implement serialization and @@ -110,17 +137,29 @@ 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 @@ -162,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; } }; @@ -170,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; } }; }; @@ -190,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. * @@ -219,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) { @@ -394,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; }; @@ -593,6 +641,9 @@ 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; @@ -622,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; } @@ -663,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 {