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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3167 - (hide annotations) (download) (as text)
Tue May 9 16:32:07 2017 UTC (6 years, 11 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 29173 byte(s)
* More old compiler backward compatibility fixes.

1 schoenebeck 3138 /***************************************************************************
2     * *
3     * Copyright (C) 2017 Christian Schoenebeck *
4     * <cuse@users.sourceforge.net> *
5     * *
6     * This library is part of libgig. *
7     * *
8     * This library is free software; you can redistribute it and/or modify *
9     * it under the terms of the GNU General Public License as published by *
10     * the Free Software Foundation; either version 2 of the License, or *
11     * (at your option) any later version. *
12     * *
13     * This library is distributed in the hope that it will be useful, *
14     * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16     * GNU General Public License for more details. *
17     * *
18     * You should have received a copy of the GNU General Public License *
19     * along with this library; if not, write to the Free Software *
20     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21     * MA 02111-1307 USA *
22     ***************************************************************************/
23    
24     #ifndef LIBGIG_SERIALIZATION_H
25     #define LIBGIG_SERIALIZATION_H
26    
27     #ifdef HAVE_CONFIG_H
28     # include <config.h>
29     #endif
30    
31     #include <stdint.h>
32     #include <stdio.h>
33     #include <typeinfo>
34     #include <string>
35     #include <vector>
36     #include <map>
37 schoenebeck 3156 #include <time.h>
38 schoenebeck 3163 #if __cplusplus < 201103L
39     # include <tr1/type_traits>
40 schoenebeck 3167 # 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 schoenebeck 3163 #endif
44 schoenebeck 3138
45     /** @brief Serialization / deserialization framework.
46     *
47     * See class Archive as starting point for how to implement serialization and
48     * deserialization with your application.
49     *
50     * The classes in this namespace allow to serialize and deserialize native
51     * C++ objects in a portable, easy and flexible way. Serialization is a
52     * technique that allows to transform the current state and data of native
53     * (in this case C++) objects into a data stream (including all other objects
54     * the "serialized" objects relate to); the data stream may then be sent over
55     * "wire" (for example via network connection to another computer, which might
56     * also have a different OS, CPU architecture, native memory word size and
57     * endian type); and finally the data stream would be "deserialized" on that
58     * receiver side, that is transformed again to modify all objects and data
59     * structures on receiver side to resemble the objects' state and data as it
60     * was originally on sender side.
61     *
62     * In contrast to many other already existing serialization frameworks, this
63     * implementation has a strong focus on robustness regarding long-term changes
64     * to the serialized C++ classes of the serialized objects. So even if sender
65     * and receiver are using different versions of their serialized/deserialized
66     * C++ classes, structures and data types (thus having different data structure
67     * layout to a certain extent), this framework aims trying to automatically
68     * adapt its serialization and deserialization process in that case so that
69     * the deserialized objects on receiver side would still reflect the overall
70     * expected states and overall data as intended by the sender. For being able to
71     * do so, this framework stores all kind of additional information about each
72     * serialized object and each data structure member (for example name of each
73     * data structure member, but also the offset of each member within its
74     * containing data structure, precise data types, and more).
75     *
76     * Like most other serialization frameworks, this frameworks does not require a
77     * tree-structured layout of the serialized data structures. So it automatically
78     * handles also cyclic dependencies between serialized data structures
79     * correctly, without i.e. causing endless recursion or redundancy.
80     *
81     * Additionally this framework also allows partial deserialization. Which means
82     * the receiver side may for example decide that it wants to restrict
83     * deserialization so that it would only modify certain objects or certain
84     * members by the deserialization process, leaving all other ones untouched.
85     * So this partial deserialization technique for example allows to implement
86     * flexible preset features for applications in a powerful and easy way.
87     */
88     namespace Serialization {
89    
90 schoenebeck 3146 // just symbol prototyping
91     class DataType;
92     class Object;
93     class Member;
94 schoenebeck 3138 class Archive;
95 schoenebeck 3146 class ObjectPool;
96 schoenebeck 3138 class Exception;
97    
98     typedef std::string String;
99    
100     typedef std::vector<uint8_t> RawData;
101    
102     typedef void* ID;
103    
104     typedef uint32_t Version;
105    
106     enum operation_t {
107     OPERATION_NONE,
108     OPERATION_SERIALIZE,
109     OPERATION_DESERIALIZE
110     };
111    
112 schoenebeck 3156 enum time_base_t {
113     LOCAL_TIME,
114     UTC_TIME
115     };
116    
117 schoenebeck 3138 template<typename T>
118     bool IsEnum(const T& data) {
119 schoenebeck 3164 #if __cplusplus < 201103L
120 schoenebeck 3165 return std::tr1::is_enum<T>::value;
121 schoenebeck 3164 #else
122 schoenebeck 3138 return __is_enum(T);
123 schoenebeck 3164 #endif
124 schoenebeck 3138 }
125    
126     template<typename T>
127     bool IsUnion(const T& data) {
128 schoenebeck 3164 #if __cplusplus < 201103L
129 schoenebeck 3166 return false; // without compiler support we cannot distinguish union from class
130 schoenebeck 3164 #else
131 schoenebeck 3138 return __is_union(T);
132 schoenebeck 3164 #endif
133 schoenebeck 3138 }
134    
135     template<typename T>
136     bool IsClass(const T& data) {
137 schoenebeck 3164 #if __cplusplus < 201103L
138 schoenebeck 3166 return std::tr1::__is_union_or_class<T>::value; // without compiler support we cannot distinguish union from class
139 schoenebeck 3164 #else
140 schoenebeck 3138 return __is_class(T);
141 schoenebeck 3164 #endif
142 schoenebeck 3138 }
143    
144     /*template<typename T>
145     bool IsTrivial(T data) {
146     return __is_trivial(T);
147     }*/
148    
149     /*template<typename T>
150     bool IsPOD(T data) {
151     return __is_pod(T);
152     }*/
153    
154     /** @brief Unique identifier for one specific C++ object, member or fundamental variable.
155     *
156     * Reflects a unique identifier for one specific serialized C++ class
157     * instance, struct instance, member, primitive pointer, or fundamental
158     * variables.
159     */
160     class UID {
161     public:
162     ID id;
163     size_t size;
164    
165     bool isValid() const;
166     operator bool() const { return isValid(); }
167     //bool operator()() const { return isValid(); }
168     bool operator==(const UID& other) const { return id == other.id && size == other.size; }
169     bool operator!=(const UID& other) const { return id != other.id || size != other.size; }
170     bool operator<(const UID& other) const { return id < other.id || (id == other.id && size < other.size); }
171     bool operator>(const UID& other) const { return id > other.id || (id == other.id && size > other.size); }
172    
173     template<typename T>
174     static UID from(const T& obj) {
175     return Resolver<T>::resolve(obj);
176     }
177    
178     protected:
179     // UID resolver for non-pointer types
180     template<typename T>
181     struct Resolver {
182     static UID resolve(const T& obj) {
183 schoenebeck 3167 return UID { (ID) &obj, sizeof(obj) };
184 schoenebeck 3138 }
185     };
186    
187     // UID resolver for pointer types (of 1st degree)
188     template<typename T>
189     struct Resolver<T*> {
190     static UID resolve(const T* const & obj) {
191 schoenebeck 3167 return UID { (ID) obj, sizeof(*obj) };
192 schoenebeck 3138 }
193     };
194     };
195    
196     /**
197     * Reflects an invalid UID and behaves similar to NULL as invalid value for
198     * pointer types.
199     */
200     extern const UID NO_UID;
201    
202     typedef std::vector<UID> UIDChain;
203    
204 schoenebeck 3146 // prototyping of private internal friend functions
205 schoenebeck 3150 static String _encodePrimitiveValue(const Object& obj);
206 schoenebeck 3146 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 schoenebeck 3150 static String _primitiveObjectValueToString(const Object& obj);
211 schoenebeck 3146
212 schoenebeck 3138 /** @brief Abstract reflection of a native C++ data type.
213     *
214     * Provides detailed information about a C++ data type, whether it is a
215     * fundamental C/C++ data type (like int, float, char, etc.) or custom
216     * defined data type like a C++ class, struct, enum, as well as other
217     * features of the data type like its native memory size and more.
218     */
219     class DataType {
220     public:
221     DataType();
222     size_t size() const { return m_size; }
223     bool isValid() const;
224     bool isPointer() const;
225     bool isClass() const;
226     bool isPrimitive() const;
227     bool isInteger() const;
228     bool isReal() const;
229     bool isBool() const;
230     bool isEnum() const;
231     bool isSigned() const;
232     operator bool() const { return isValid(); }
233     //bool operator()() const { return isValid(); }
234     bool operator==(const DataType& other) const;
235     bool operator!=(const DataType& other) const;
236     bool operator<(const DataType& other) const;
237     bool operator>(const DataType& other) const;
238     String asLongDescr() const;
239     String baseTypeName() const { return m_baseTypeName; }
240     String customTypeName() const { return m_customTypeName; }
241    
242     template<typename T>
243     static DataType dataTypeOf(const T& data) {
244     return Resolver<T>::resolve(data);
245     }
246    
247     protected:
248     DataType(bool isPointer, int size, String baseType, String customType = "");
249    
250     template<typename T, bool T_isPointer>
251     struct ResolverBase {
252     static DataType resolve(const T& data) {
253     const std::type_info& type = typeid(data);
254     const int sz = sizeof(data);
255    
256     // for primitive types we are using our own type names instead of
257     // using std:::type_info::name(), because the precise output of the
258     // latter may vary between compilers
259     if (type == typeid(int8_t)) return DataType(T_isPointer, sz, "int8");
260     if (type == typeid(uint8_t)) return DataType(T_isPointer, sz, "uint8");
261     if (type == typeid(int16_t)) return DataType(T_isPointer, sz, "int16");
262     if (type == typeid(uint16_t)) return DataType(T_isPointer, sz, "uint16");
263     if (type == typeid(int32_t)) return DataType(T_isPointer, sz, "int32");
264     if (type == typeid(uint32_t)) return DataType(T_isPointer, sz, "uint32");
265     if (type == typeid(int64_t)) return DataType(T_isPointer, sz, "int64");
266     if (type == typeid(uint64_t)) return DataType(T_isPointer, sz, "uint64");
267     if (type == typeid(bool)) return DataType(T_isPointer, sz, "bool");
268     if (type == typeid(float)) return DataType(T_isPointer, sz, "real32");
269     if (type == typeid(double)) return DataType(T_isPointer, sz, "real64");
270    
271     if (IsEnum(data)) return DataType(T_isPointer, sz, "enum", rawCppTypeNameOf(data));
272     if (IsUnion(data)) return DataType(T_isPointer, sz, "union", rawCppTypeNameOf(data));
273     if (IsClass(data)) return DataType(T_isPointer, sz, "class", rawCppTypeNameOf(data));
274    
275     return DataType();
276     }
277     };
278    
279     // DataType resolver for non-pointer types
280     template<typename T>
281     struct Resolver : ResolverBase<T,false> {
282     static DataType resolve(const T& data) {
283     return ResolverBase<T,false>::resolve(data);
284     }
285     };
286    
287     // DataType resolver for pointer types (of 1st degree)
288     template<typename T>
289     struct Resolver<T*> : ResolverBase<T,true> {
290     static DataType resolve(const T*& data) {
291     return ResolverBase<T,true>::resolve(*data);
292     }
293     };
294    
295     template<typename T>
296     static String rawCppTypeNameOf(const T& data) {
297     #if defined _MSC_VER // Microsoft compiler ...
298     # warning type_info::raw_name() demangling has not been tested yet with Microsoft compiler! Feedback appreciated!
299     String name = typeid(data).raw_name(); //NOTE: I haven't checked yet what MSC actually outputs here exactly
300     #else // i.e. especially GCC and clang ...
301     String name = typeid(data).name();
302     #endif
303     //while (!name.empty() && name[0] >= 0 && name[0] <= 9)
304     // name = name.substr(1);
305     return name;
306     }
307    
308     private:
309     String m_baseTypeName;
310     String m_customTypeName;
311     int m_size;
312     bool m_isPointer;
313    
314     friend DataType _popDataTypeBlob(const char*& p, const char* end);
315 schoenebeck 3150 friend class Archive;
316 schoenebeck 3138 };
317    
318     /** @brief Abstract reflection of a native C++ class/struct's member variable.
319     *
320     * Provides detailed information about a specific C++ member variable of
321     * serialized C++ object, like its C++ data type, offset of this member
322     * within its containing data structure/class, its C++ member variable name
323     * and more.
324     */
325     class Member {
326     public:
327     Member();
328     UID uid() const { return m_uid; }
329     String name() const { return m_name; }
330     size_t offset() const { return m_offset; }
331     const DataType& type() const { return m_type; }
332     bool isValid() const;
333     operator bool() const { return isValid(); }
334     //bool operator()() const { return isValid(); }
335     bool operator==(const Member& other) const;
336     bool operator!=(const Member& other) const;
337     bool operator<(const Member& other) const;
338     bool operator>(const Member& other) const;
339    
340     protected:
341     Member(String name, UID uid, size_t offset, DataType type);
342     friend class Archive;
343    
344     private:
345     UID m_uid;
346     size_t m_offset;
347     String m_name;
348     DataType m_type;
349    
350     friend Member _popMemberBlob(const char*& p, const char* end);
351     };
352    
353     /** @brief Abstract reflection of a native C++ class/struct instance.
354     *
355     * Provides detailed information about a specific serialized C++ object,
356     * like its C++ member variables, its C++ class/struct name, its native
357     * memory size and more.
358     */
359     class Object {
360     public:
361     Object();
362     Object(UIDChain uidChain, DataType type);
363    
364     UID uid(int index = 0) const {
365     return (index < m_uid.size()) ? m_uid[index] : NO_UID;
366     }
367    
368     const UIDChain& uidChain() const { return m_uid; }
369     const DataType& type() const { return m_type; }
370     const RawData& rawData() const { return m_data; }
371    
372     Version version() const { return m_version; }
373    
374     void setVersion(Version v) {
375     m_version = v;
376     }
377    
378     Version minVersion() const { return m_minVersion; }
379    
380     void setMinVersion(Version v) {
381     m_minVersion = v;
382     }
383    
384     bool isVersionCompatibleTo(const Object& other) const;
385    
386     std::vector<Member>& members() { return m_members; }
387     const std::vector<Member>& members() const { return m_members; }
388     Member memberNamed(String name) const;
389 schoenebeck 3153 Member memberByUID(const UID& uid) const;
390 schoenebeck 3138 std::vector<Member> membersOfType(const DataType& type) const;
391     int sequenceIndexOf(const Member& member) const;
392     bool isValid() const;
393     operator bool() const { return isValid(); }
394     //bool operator()() const { return isValid(); }
395     bool operator==(const Object& other) const;
396     bool operator!=(const Object& other) const;
397     bool operator<(const Object& other) const;
398     bool operator>(const Object& other) const;
399    
400 schoenebeck 3153 protected:
401     void remove(const Member& member);
402    
403 schoenebeck 3138 private:
404     DataType m_type;
405     UIDChain m_uid;
406     Version m_version;
407     Version m_minVersion;
408     RawData m_data;
409     std::vector<Member> m_members;
410    
411 schoenebeck 3150 friend String _encodePrimitiveValue(const Object& obj);
412 schoenebeck 3138 friend Object _popObjectBlob(const char*& p, const char* end);
413     friend void _popPrimitiveValue(const char*& p, const char* end, Object& obj);
414 schoenebeck 3150 friend String _primitiveObjectValueToString(const Object& obj);
415     friend class Archive;
416 schoenebeck 3138 };
417    
418     /** @brief Destination container for serialization, and source container for deserialization.
419     *
420     * This is the main class for implementing serialization and deserialization
421     * with your C++ application. This framework does not require a a tree
422     * structured layout of your C++ objects being serialized/deserialized, it
423     * uses a concept of a "root" object though. So to start serialization
424     * construct an empty Archive object and then instruct it to serialize your
425     * C++ objects by pointing it to your "root" object:
426     * @code
427     * Archive a;
428     * a.serialize(&myRootObject);
429 schoenebeck 3142 * @endcode
430 schoenebeck 3138 * Or if you prefer the look of operator based code:
431     * @code
432     * Archive a;
433     * a << myRootObject;
434 schoenebeck 3142 * @endcode
435 schoenebeck 3138 * The Archive object will then serialize all members of the passed C++
436     * object, and will recursively serialize all other C++ objects which it
437     * contains or points to. So the root object is the starting point for the
438     * overall serialization. After the serialize() method returned, you can
439     * then access the serialized data stream by calling rawData() and send that
440     * data stream over "wire", or store it on disk or whatever you may intend
441     * to do with it.
442     *
443     * Then on receiver side likewise, you create a new Archive object, pass the
444     * received data stream i.e. via constructor to the Archive object and call
445     * deserialize() by pointing it to the root object on receiver side:
446     * @code
447     * Archive a(rawDataStream);
448     * a.deserialize(&myRootObject);
449 schoenebeck 3142 * @endcode
450 schoenebeck 3138 * Or with operator instead:
451     * @code
452     * Archive a(rawDataStream);
453     * a >> myRootObject;
454 schoenebeck 3142 * @endcode
455 schoenebeck 3138 * Now this framework automatically handles serialization and
456     * deserialization of fundamental data types automatically for you (like
457     * i.e. char, int, long int, float, double, etc.). However for your own
458     * custom C++ classes and structs you must implement one method which
459     * defines which members of your class should actually be serialized and
460     * deserialized. That method to be added must have the following signature:
461     * @code
462     * void serialize(Serialization::Archive* archive);
463     * @endcode
464     * So let's say you have the following simple data structures:
465     * @code
466     * struct Foo {
467     * int a;
468     * bool b;
469     * double c;
470     * };
471     *
472     * struct Bar {
473     * char one;
474     * float two;
475     * Foo foo1;
476     * Foo* pFoo2;
477     * Foo* pFoo3DontTouchMe; // shall not be serialized/deserialized
478     * };
479     * @endcode
480     * So in order to be able to serialize and deserialize objects of those two
481     * structures you would first add the mentioned method to each struct
482     * definition (i.e. in your header file):
483     * @code
484     * struct Foo {
485     * int a;
486     * bool b;
487     * double c;
488     *
489     * void serialize(Serialization::Archive* archive);
490     * };
491     *
492     * struct Bar {
493     * char one;
494     * float two;
495     * Foo foo1;
496     * Foo* pFoo2;
497     * Foo* pFoo3DontTouchMe; // shall not be serialized/deserialized
498     *
499     * void serialize(Serialization::Archive* archive);
500     * };
501     * @endcode
502     * And then you would implement those two new methods like this (i.e. in
503     * your .cpp file):
504     * @code
505     * #define SRLZ(member) \
506     * archive->serializeMember(*this, member, #member);
507     *
508     * void Foo::serialize(Serialization::Archive* archive) {
509     * SRLZ(a);
510     * SRLZ(b);
511     * SRLZ(c);
512     * }
513     *
514     * void Bar::serialize(Serialization::Archive* archive) {
515     * SRLZ(one);
516     * SRLZ(two);
517     * SRLZ(foo1);
518     * SRLZ(pFoo2);
519     * // leaving out pFoo3DontTouchMe here
520     * }
521     * @endcode
522     * Now when you serialize such a Bar object, this framework will also
523     * automatically serialize the respective Foo object(s) accordingly, also
524     * for the pFoo2 pointer for instance (as long as it is not a NULL pointer
525     * that is).
526     *
527     * Note that there is only one method that you need to implement. So the
528     * respective serialize() method implementation of your classes/structs are
529     * both called for serialization, as well as for deserialization!
530     */
531     class Archive {
532     public:
533     Archive();
534     Archive(const RawData& data);
535     Archive(const uint8_t* data, size_t size);
536     virtual ~Archive();
537    
538     template<typename T>
539     void serialize(const T* obj) {
540     m_operation = OPERATION_SERIALIZE;
541     m_allObjects.clear();
542     m_rawData.clear();
543     m_root = UID::from(obj);
544     const_cast<T*>(obj)->serialize(this);
545     encode();
546     m_operation = OPERATION_NONE;
547     }
548    
549     template<typename T>
550     void deserialize(T* obj) {
551     Archive a;
552     m_operation = OPERATION_DESERIALIZE;
553     obj->serialize(&a);
554     a.m_root = UID::from(obj);
555     Syncer s(a, *this);
556     m_operation = OPERATION_NONE;
557     }
558    
559     template<typename T>
560     void operator<<(const T& obj) {
561     serialize(&obj);
562     }
563    
564     template<typename T>
565     void operator>>(T& obj) {
566     deserialize(&obj);
567     }
568    
569 schoenebeck 3150 const RawData& rawData();
570 schoenebeck 3138 virtual String rawDataFormat() const;
571    
572     template<typename T_classType, typename T_memberType>
573     void serializeMember(const T_classType& nativeObject, const T_memberType& nativeMember, const char* memberName) {
574     const size_t offset =
575     ((const uint8_t*)(const void*)&nativeMember) -
576     ((const uint8_t*)(const void*)&nativeObject);
577     const UIDChain uids = UIDChainResolver<T_memberType>(nativeMember);
578     const DataType type = DataType::dataTypeOf(nativeMember);
579     const Member member(memberName, uids[0], offset, type);
580     const UID parentUID = UID::from(nativeObject);
581     Object& parent = m_allObjects[parentUID];
582     if (!parent) {
583     const UIDChain uids = UIDChainResolver<T_classType>(nativeObject);
584     const DataType type = DataType::dataTypeOf(nativeObject);
585     parent = Object(uids, type);
586     }
587     parent.members().push_back(member);
588     const Object obj(uids, type);
589     const bool bExistsAlready = m_allObjects.count(uids[0]);
590     const bool isValidObject = obj;
591     const bool bExistingObjectIsInvalid = !m_allObjects[uids[0]];
592     if (!bExistsAlready || (bExistingObjectIsInvalid && isValidObject)) {
593     m_allObjects[uids[0]] = obj;
594     // recurse serialization for all members of this member
595     // (only for struct/class types, noop for primitive types)
596     SerializationRecursion<T_memberType>::serializeObject(this, nativeMember);
597     }
598     }
599    
600     virtual void decode(const RawData& data);
601     virtual void decode(const uint8_t* data, size_t size);
602     void clear();
603 schoenebeck 3150 bool isModified() const;
604 schoenebeck 3153 void removeMember(Object& parent, const Member& member);
605 schoenebeck 3138 void remove(const Object& obj);
606     Object& rootObject();
607     Object& objectByUID(const UID& uid);
608 schoenebeck 3150 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 schoenebeck 3156 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 schoenebeck 3138
623     protected:
624     // UID resolver for non-pointer types
625     template<typename T>
626     class UIDChainResolver {
627     public:
628     UIDChainResolver(const T& data) {
629     m_uid.push_back(UID::from(data));
630     }
631    
632     operator UIDChain() const { return m_uid; }
633     UIDChain operator()() const { return m_uid; }
634     private:
635     UIDChain m_uid;
636     };
637    
638     // UID resolver for pointer types (of 1st degree)
639     template<typename T>
640     class UIDChainResolver<T*> {
641     public:
642     UIDChainResolver(const T*& data) {
643 schoenebeck 3167 m_uid.push_back(UID { &data, sizeof(data) });
644     m_uid.push_back(UID { data, sizeof(*data) });
645 schoenebeck 3138 }
646    
647     operator UIDChain() const { return m_uid; }
648     UIDChain operator()() const { return m_uid; }
649     private:
650     UIDChain m_uid;
651     };
652    
653     // SerializationRecursion for non-pointer class/struct types.
654     template<typename T, bool T_isRecursive>
655     struct SerializationRecursionImpl {
656     static void serializeObject(Archive* archive, const T& obj) {
657     const_cast<T&>(obj).serialize(archive);
658     }
659     };
660    
661     // SerializationRecursion for pointers (of 1st degree) to class/structs.
662     template<typename T, bool T_isRecursive>
663     struct SerializationRecursionImpl<T*,T_isRecursive> {
664     static void serializeObject(Archive* archive, const T*& obj) {
665     if (!obj) return;
666     const_cast<T*&>(obj)->serialize(archive);
667     }
668     };
669    
670     // NOOP SerializationRecursion for primitive types.
671     template<typename T>
672     struct SerializationRecursionImpl<T,false> {
673     static void serializeObject(Archive* archive, const T& obj) {}
674     };
675    
676     // NOOP SerializationRecursion for pointers (of 1st degree) to primitive types.
677     template<typename T>
678     struct SerializationRecursionImpl<T*,false> {
679     static void serializeObject(Archive* archive, const T*& obj) {}
680     };
681    
682     // Automatically handles recursion for class/struct types, while ignoring all primitive types.
683     template<typename T>
684 schoenebeck 3167 struct SerializationRecursion : SerializationRecursionImpl<T, LIBGIG_IS_CLASS(T)> {
685 schoenebeck 3138 };
686    
687     class ObjectPool : public std::map<UID,Object> {
688     public:
689     // prevent passing obvious invalid UID values from creating a new pair entry
690     Object& operator[](const UID& k) {
691     static Object invalid;
692     if (!k.isValid()) {
693     invalid = Object();
694     return invalid;
695     }
696     return std::map<UID,Object>::operator[](k);
697     }
698     };
699    
700     friend String _encode(const ObjectPool& objects);
701    
702     private:
703     String _encodeRootBlob();
704     void _popRootBlob(const char*& p, const char* end);
705     void _popObjectsBlob(const char*& p, const char* end);
706    
707     protected:
708     class Syncer {
709     public:
710     Syncer(Archive& dst, Archive& src);
711     protected:
712     void syncObject(const Object& dst, const Object& src);
713     void syncPrimitive(const Object& dst, const Object& src);
714     void syncPointer(const Object& dst, const Object& src);
715     void syncMember(const Member& dstMember, const Member& srcMember);
716     static Member dstMemberMatching(const Object& dstObj, const Object& srcObj, const Member& srcMember);
717     private:
718     Archive& m_dst;
719     Archive& m_src;
720     };
721    
722     virtual void encode();
723    
724     ObjectPool m_allObjects;
725     operation_t m_operation;
726     UID m_root;
727     RawData m_rawData;
728 schoenebeck 3150 bool m_isModified;
729 schoenebeck 3156 String m_name;
730     String m_comment;
731     time_t m_timeCreated;
732     time_t m_timeModified;
733 schoenebeck 3138 };
734    
735     /**
736     * Will be thrown whenever an error occurs during an serialization or
737     * deserialization process.
738     */
739     class Exception {
740     public:
741     String Message;
742    
743     Exception(String Message) { Exception::Message = Message; }
744     void PrintMessage();
745     virtual ~Exception() {}
746     };
747    
748     } // namespace Serialization
749    
750     #endif // LIBGIG_SERIALIZATION_H

  ViewVC Help
Powered by ViewVC