/[svn]/libgig/trunk/src/Serialization.h
ViewVC logotype

Diff of /libgig/trunk/src/Serialization.h

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3142 by schoenebeck, Wed May 3 17:37:33 2017 UTC revision 3167 by schoenebeck, Tue May 9 16:32:07 2017 UTC
# Line 34  Line 34 
34  #include <string>  #include <string>
35  #include <vector>  #include <vector>
36  #include <map>  #include <map>
37    #include <time.h>
38    #if __cplusplus < 201103L
39    # include <tr1/type_traits>
40    # define LIBGIG_IS_CLASS(type) std::tr1::__is_union_or_class<type>::value //NOTE: without compiler support we cannot distinguish union from class
41    #else
42    # define LIBGIG_IS_CLASS(type) __is_class(type)
43    #endif
44    
45  /** @brief Serialization / deserialization framework.  /** @brief Serialization / deserialization framework.
46   *   *
# Line 80  Line 87 
87   */   */
88  namespace Serialization {  namespace Serialization {
89    
90        // just symbol prototyping
91        class DataType;
92        class Object;
93        class Member;
94      class Archive;      class Archive;
95        class ObjectPool;
96      class Exception;      class Exception;
97    
98      typedef std::string String;      typedef std::string String;
# Line 97  namespace Serialization { Line 109  namespace Serialization {
109          OPERATION_DESERIALIZE          OPERATION_DESERIALIZE
110      };      };
111    
112        enum time_base_t {
113            LOCAL_TIME,
114            UTC_TIME
115        };
116    
117      template<typename T>      template<typename T>
118      bool IsEnum(const T& data) {      bool IsEnum(const T& data) {
119            #if __cplusplus < 201103L
120            return std::tr1::is_enum<T>::value;
121            #else
122          return __is_enum(T);          return __is_enum(T);
123            #endif
124      }      }
125    
126      template<typename T>      template<typename T>
127      bool IsUnion(const T& data) {      bool IsUnion(const T& data) {
128            #if __cplusplus < 201103L
129            return false; // without compiler support we cannot distinguish union from class
130            #else
131          return __is_union(T);          return __is_union(T);
132            #endif
133      }      }
134    
135      template<typename T>      template<typename T>
136      bool IsClass(const T& data) {      bool IsClass(const T& data) {
137            #if __cplusplus < 201103L
138            return std::tr1::__is_union_or_class<T>::value; // without compiler support we cannot distinguish union from class
139            #else
140          return __is_class(T);          return __is_class(T);
141            #endif
142      }      }
143    
144      /*template<typename T>      /*template<typename T>
# Line 151  namespace Serialization { Line 180  namespace Serialization {
180          template<typename T>          template<typename T>
181          struct Resolver {          struct Resolver {
182              static UID resolve(const T& obj) {              static UID resolve(const T& obj) {
183                  return (UID) { (ID) &obj, sizeof(obj) };                  return UID { (ID) &obj, sizeof(obj) };
184              }              }
185          };          };
186    
# Line 159  namespace Serialization { Line 188  namespace Serialization {
188          template<typename T>          template<typename T>
189          struct Resolver<T*> {          struct Resolver<T*> {
190              static UID resolve(const T* const & obj) {              static UID resolve(const T* const & obj) {
191                  return (UID) { (ID) obj, sizeof(*obj) };                  return UID { (ID) obj, sizeof(*obj) };
192              }              }
193          };          };
194      };      };
# Line 172  namespace Serialization { Line 201  namespace Serialization {
201    
202      typedef std::vector<UID> UIDChain;      typedef std::vector<UID> UIDChain;
203    
204        // prototyping of private internal friend functions
205        static String _encodePrimitiveValue(const Object& obj);
206        static DataType _popDataTypeBlob(const char*& p, const char* end);
207        static Member _popMemberBlob(const char*& p, const char* end);
208        static Object _popObjectBlob(const char*& p, const char* end);
209        static void _popPrimitiveValue(const char*& p, const char* end, Object& obj);
210        static String _primitiveObjectValueToString(const Object& obj);
211    
212      /** @brief Abstract reflection of a native C++ data type.      /** @brief Abstract reflection of a native C++ data type.
213       *       *
214       * Provides detailed information about a C++ data type, whether it is a       * Provides detailed information about a C++ data type, whether it is a
# Line 275  namespace Serialization { Line 312  namespace Serialization {
312          bool m_isPointer;          bool m_isPointer;
313    
314          friend DataType _popDataTypeBlob(const char*& p, const char* end);          friend DataType _popDataTypeBlob(const char*& p, const char* end);
315            friend class Archive;
316      };      };
317    
318      /** @brief Abstract reflection of a native C++ class/struct's member variable.      /** @brief Abstract reflection of a native C++ class/struct's member variable.
# Line 348  namespace Serialization { Line 386  namespace Serialization {
386          std::vector<Member>& members() { return m_members; }          std::vector<Member>& members() { return m_members; }
387          const std::vector<Member>& members() const { return m_members; }          const std::vector<Member>& members() const { return m_members; }
388          Member memberNamed(String name) const;          Member memberNamed(String name) const;
389          void remove(const Member& member);          Member memberByUID(const UID& uid) const;
390          std::vector<Member> membersOfType(const DataType& type) const;          std::vector<Member> membersOfType(const DataType& type) const;
391          int sequenceIndexOf(const Member& member) const;          int sequenceIndexOf(const Member& member) const;
392          bool isValid() const;          bool isValid() const;
# Line 359  namespace Serialization { Line 397  namespace Serialization {
397          bool operator<(const Object& other) const;          bool operator<(const Object& other) const;
398          bool operator>(const Object& other) const;          bool operator>(const Object& other) const;
399    
400        protected:
401            void remove(const Member& member);
402    
403      private:      private:
404          DataType m_type;          DataType m_type;
405          UIDChain m_uid;          UIDChain m_uid;
# Line 367  namespace Serialization { Line 408  namespace Serialization {
408          RawData m_data;          RawData m_data;
409          std::vector<Member> m_members;          std::vector<Member> m_members;
410    
411            friend String _encodePrimitiveValue(const Object& obj);
412          friend Object _popObjectBlob(const char*& p, const char* end);          friend Object _popObjectBlob(const char*& p, const char* end);
413          friend void _popPrimitiveValue(const char*& p, const char* end, Object& obj);          friend void _popPrimitiveValue(const char*& p, const char* end, Object& obj);
414            friend String _primitiveObjectValueToString(const Object& obj);
415            friend class Archive;
416      };      };
417    
418      /** @brief Destination container for serialization, and source container for deserialization.      /** @brief Destination container for serialization, and source container for deserialization.
# Line 522  namespace Serialization { Line 566  namespace Serialization {
566              deserialize(&obj);              deserialize(&obj);
567          }          }
568    
569          const RawData& rawData() const { return m_rawData; }          const RawData& rawData();
570          virtual String rawDataFormat() const;          virtual String rawDataFormat() const;
571    
572          template<typename T_classType, typename T_memberType>          template<typename T_classType, typename T_memberType>
# Line 556  namespace Serialization { Line 600  namespace Serialization {
600          virtual void decode(const RawData& data);          virtual void decode(const RawData& data);
601          virtual void decode(const uint8_t* data, size_t size);          virtual void decode(const uint8_t* data, size_t size);
602          void clear();          void clear();
603            bool isModified() const;
604            void removeMember(Object& parent, const Member& member);
605          void remove(const Object& obj);          void remove(const Object& obj);
606          Object& rootObject();          Object& rootObject();
607          Object& objectByUID(const UID& uid);          Object& objectByUID(const UID& uid);
608            void setAutoValue(Object& object, String value);
609            void setIntValue(Object& object, int64_t value);
610            void setRealValue(Object& object, double value);
611            void setBoolValue(Object& object, bool value);
612            void setEnumValue(Object& object, uint64_t value);
613            String valueAsString(const Object& object);
614            String name() const;
615            void setName(String name);
616            String comment() const;
617            void setComment(String comment);
618            time_t timeStampCreated() const;
619            time_t timeStampModified() const;
620            tm dateTimeCreated(time_base_t base = LOCAL_TIME) const;
621            tm dateTimeModified(time_base_t base = LOCAL_TIME) const;
622    
623      protected:      protected:
624          // UID resolver for non-pointer types          // UID resolver for non-pointer types
# Line 580  namespace Serialization { Line 640  namespace Serialization {
640          class UIDChainResolver<T*> {          class UIDChainResolver<T*> {
641          public:          public:
642              UIDChainResolver(const T*& data) {              UIDChainResolver(const T*& data) {
643                  m_uid.push_back((UID) { &data, sizeof(data) });                  m_uid.push_back(UID { &data, sizeof(data) });
644                  m_uid.push_back((UID) { data, sizeof(*data) });                  m_uid.push_back(UID { data, sizeof(*data) });
645              }              }
646    
647              operator UIDChain() const { return m_uid; }              operator UIDChain() const { return m_uid; }
# Line 621  namespace Serialization { Line 681  namespace Serialization {
681    
682          // Automatically handles recursion for class/struct types, while ignoring all primitive types.          // Automatically handles recursion for class/struct types, while ignoring all primitive types.
683          template<typename T>          template<typename T>
684          struct SerializationRecursion : SerializationRecursionImpl<T, __is_class(T)> {          struct SerializationRecursion : SerializationRecursionImpl<T, LIBGIG_IS_CLASS(T)> {
685          };          };
686    
687          class ObjectPool : public std::map<UID,Object> {          class ObjectPool : public std::map<UID,Object> {
# Line 665  namespace Serialization { Line 725  namespace Serialization {
725          operation_t m_operation;          operation_t m_operation;
726          UID m_root;          UID m_root;
727          RawData m_rawData;          RawData m_rawData;
728            bool m_isModified;
729            String m_name;
730            String m_comment;
731            time_t m_timeCreated;
732            time_t m_timeModified;
733      };      };
734    
735      /**      /**

Legend:
Removed from v.3142  
changed lines
  Added in v.3167

  ViewVC Help
Powered by ViewVC