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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3168 - (hide annotations) (download) (as text)
Tue May 9 19:12:32 2017 UTC (6 years, 10 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 29345 byte(s)
- More ancient 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 3168 const UID uid = { (ID) &obj, sizeof(obj) };
184     return uid;
185 schoenebeck 3138 }
186     };
187    
188     // UID resolver for pointer types (of 1st degree)
189     template<typename T>
190     struct Resolver<T*> {
191     static UID resolve(const T* const & obj) {
192 schoenebeck 3168 const UID uid = { (ID) obj, sizeof(*obj) };
193     return uid;
194 schoenebeck 3138 }
195     };
196     };
197    
198     /**
199     * Reflects an invalid UID and behaves similar to NULL as invalid value for
200     * pointer types.
201     */
202     extern const UID NO_UID;
203    
204     typedef std::vector<UID> UIDChain;
205    
206 schoenebeck 3146 // prototyping of private internal friend functions
207 schoenebeck 3150 static String _encodePrimitiveValue(const Object& obj);
208 schoenebeck 3146 static DataType _popDataTypeBlob(const char*& p, const char* end);
209     static Member _popMemberBlob(const char*& p, const char* end);
210     static Object _popObjectBlob(const char*& p, const char* end);
211     static void _popPrimitiveValue(const char*& p, const char* end, Object& obj);
212 schoenebeck 3150 static String _primitiveObjectValueToString(const Object& obj);
213 schoenebeck 3146
214 schoenebeck 3138 /** @brief Abstract reflection of a native C++ data type.
215     *
216     * Provides detailed information about a C++ data type, whether it is a
217     * fundamental C/C++ data type (like int, float, char, etc.) or custom
218     * defined data type like a C++ class, struct, enum, as well as other
219     * features of the data type like its native memory size and more.
220     */
221     class DataType {
222     public:
223     DataType();
224     size_t size() const { return m_size; }
225     bool isValid() const;
226     bool isPointer() const;
227     bool isClass() const;
228     bool isPrimitive() const;
229     bool isInteger() const;
230     bool isReal() const;
231     bool isBool() const;
232     bool isEnum() const;
233     bool isSigned() const;
234     operator bool() const { return isValid(); }
235     //bool operator()() const { return isValid(); }
236     bool operator==(const DataType& other) const;
237     bool operator!=(const DataType& other) const;
238     bool operator<(const DataType& other) const;
239     bool operator>(const DataType& other) const;
240     String asLongDescr() const;
241     String baseTypeName() const { return m_baseTypeName; }
242     String customTypeName() const { return m_customTypeName; }
243    
244     template<typename T>
245     static DataType dataTypeOf(const T& data) {
246     return Resolver<T>::resolve(data);
247     }
248    
249     protected:
250     DataType(bool isPointer, int size, String baseType, String customType = "");
251    
252     template<typename T, bool T_isPointer>
253     struct ResolverBase {
254     static DataType resolve(const T& data) {
255     const std::type_info& type = typeid(data);
256     const int sz = sizeof(data);
257    
258     // for primitive types we are using our own type names instead of
259     // using std:::type_info::name(), because the precise output of the
260     // latter may vary between compilers
261     if (type == typeid(int8_t)) return DataType(T_isPointer, sz, "int8");
262     if (type == typeid(uint8_t)) return DataType(T_isPointer, sz, "uint8");
263     if (type == typeid(int16_t)) return DataType(T_isPointer, sz, "int16");
264     if (type == typeid(uint16_t)) return DataType(T_isPointer, sz, "uint16");
265     if (type == typeid(int32_t)) return DataType(T_isPointer, sz, "int32");
266     if (type == typeid(uint32_t)) return DataType(T_isPointer, sz, "uint32");
267     if (type == typeid(int64_t)) return DataType(T_isPointer, sz, "int64");
268     if (type == typeid(uint64_t)) return DataType(T_isPointer, sz, "uint64");
269     if (type == typeid(bool)) return DataType(T_isPointer, sz, "bool");
270     if (type == typeid(float)) return DataType(T_isPointer, sz, "real32");
271     if (type == typeid(double)) return DataType(T_isPointer, sz, "real64");
272    
273     if (IsEnum(data)) return DataType(T_isPointer, sz, "enum", rawCppTypeNameOf(data));
274     if (IsUnion(data)) return DataType(T_isPointer, sz, "union", rawCppTypeNameOf(data));
275     if (IsClass(data)) return DataType(T_isPointer, sz, "class", rawCppTypeNameOf(data));
276    
277     return DataType();
278     }
279     };
280    
281     // DataType resolver for non-pointer types
282     template<typename T>
283     struct Resolver : ResolverBase<T,false> {
284     static DataType resolve(const T& data) {
285     return ResolverBase<T,false>::resolve(data);
286     }
287     };
288    
289     // DataType resolver for pointer types (of 1st degree)
290     template<typename T>
291     struct Resolver<T*> : ResolverBase<T,true> {
292     static DataType resolve(const T*& data) {
293     return ResolverBase<T,true>::resolve(*data);
294     }
295     };
296    
297     template<typename T>
298     static String rawCppTypeNameOf(const T& data) {
299     #if defined _MSC_VER // Microsoft compiler ...
300     # warning type_info::raw_name() demangling has not been tested yet with Microsoft compiler! Feedback appreciated!
301     String name = typeid(data).raw_name(); //NOTE: I haven't checked yet what MSC actually outputs here exactly
302     #else // i.e. especially GCC and clang ...
303     String name = typeid(data).name();
304     #endif
305     //while (!name.empty() && name[0] >= 0 && name[0] <= 9)
306     // name = name.substr(1);
307     return name;
308     }
309    
310     private:
311     String m_baseTypeName;
312     String m_customTypeName;
313     int m_size;
314     bool m_isPointer;
315    
316     friend DataType _popDataTypeBlob(const char*& p, const char* end);
317 schoenebeck 3150 friend class Archive;
318 schoenebeck 3138 };
319    
320     /** @brief Abstract reflection of a native C++ class/struct's member variable.
321     *
322     * Provides detailed information about a specific C++ member variable of
323     * serialized C++ object, like its C++ data type, offset of this member
324     * within its containing data structure/class, its C++ member variable name
325     * and more.
326     */
327     class Member {
328     public:
329     Member();
330     UID uid() const { return m_uid; }
331     String name() const { return m_name; }
332     size_t offset() const { return m_offset; }
333     const DataType& type() const { return m_type; }
334     bool isValid() const;
335     operator bool() const { return isValid(); }
336     //bool operator()() const { return isValid(); }
337     bool operator==(const Member& other) const;
338     bool operator!=(const Member& other) const;
339     bool operator<(const Member& other) const;
340     bool operator>(const Member& other) const;
341    
342     protected:
343     Member(String name, UID uid, size_t offset, DataType type);
344     friend class Archive;
345    
346     private:
347     UID m_uid;
348     size_t m_offset;
349     String m_name;
350     DataType m_type;
351    
352     friend Member _popMemberBlob(const char*& p, const char* end);
353     };
354    
355     /** @brief Abstract reflection of a native C++ class/struct instance.
356     *
357     * Provides detailed information about a specific serialized C++ object,
358     * like its C++ member variables, its C++ class/struct name, its native
359     * memory size and more.
360     */
361     class Object {
362     public:
363     Object();
364     Object(UIDChain uidChain, DataType type);
365    
366     UID uid(int index = 0) const {
367     return (index < m_uid.size()) ? m_uid[index] : NO_UID;
368     }
369    
370     const UIDChain& uidChain() const { return m_uid; }
371     const DataType& type() const { return m_type; }
372     const RawData& rawData() const { return m_data; }
373    
374     Version version() const { return m_version; }
375    
376     void setVersion(Version v) {
377     m_version = v;
378     }
379    
380     Version minVersion() const { return m_minVersion; }
381    
382     void setMinVersion(Version v) {
383     m_minVersion = v;
384     }
385    
386     bool isVersionCompatibleTo(const Object& other) const;
387    
388     std::vector<Member>& members() { return m_members; }
389     const std::vector<Member>& members() const { return m_members; }
390     Member memberNamed(String name) const;
391 schoenebeck 3153 Member memberByUID(const UID& uid) const;
392 schoenebeck 3138 std::vector<Member> membersOfType(const DataType& type) const;
393     int sequenceIndexOf(const Member& member) const;
394     bool isValid() const;
395     operator bool() const { return isValid(); }
396     //bool operator()() const { return isValid(); }
397     bool operator==(const Object& other) const;
398     bool operator!=(const Object& other) const;
399     bool operator<(const Object& other) const;
400     bool operator>(const Object& other) const;
401    
402 schoenebeck 3153 protected:
403     void remove(const Member& member);
404    
405 schoenebeck 3138 private:
406     DataType m_type;
407     UIDChain m_uid;
408     Version m_version;
409     Version m_minVersion;
410     RawData m_data;
411     std::vector<Member> m_members;
412    
413 schoenebeck 3150 friend String _encodePrimitiveValue(const Object& obj);
414 schoenebeck 3138 friend Object _popObjectBlob(const char*& p, const char* end);
415     friend void _popPrimitiveValue(const char*& p, const char* end, Object& obj);
416 schoenebeck 3150 friend String _primitiveObjectValueToString(const Object& obj);
417     friend class Archive;
418 schoenebeck 3138 };
419    
420     /** @brief Destination container for serialization, and source container for deserialization.
421     *
422     * This is the main class for implementing serialization and deserialization
423     * with your C++ application. This framework does not require a a tree
424     * structured layout of your C++ objects being serialized/deserialized, it
425     * uses a concept of a "root" object though. So to start serialization
426     * construct an empty Archive object and then instruct it to serialize your
427     * C++ objects by pointing it to your "root" object:
428     * @code
429     * Archive a;
430     * a.serialize(&myRootObject);
431 schoenebeck 3142 * @endcode
432 schoenebeck 3138 * Or if you prefer the look of operator based code:
433     * @code
434     * Archive a;
435     * a << myRootObject;
436 schoenebeck 3142 * @endcode
437 schoenebeck 3138 * The Archive object will then serialize all members of the passed C++
438     * object, and will recursively serialize all other C++ objects which it
439     * contains or points to. So the root object is the starting point for the
440     * overall serialization. After the serialize() method returned, you can
441     * then access the serialized data stream by calling rawData() and send that
442     * data stream over "wire", or store it on disk or whatever you may intend
443     * to do with it.
444     *
445     * Then on receiver side likewise, you create a new Archive object, pass the
446     * received data stream i.e. via constructor to the Archive object and call
447     * deserialize() by pointing it to the root object on receiver side:
448     * @code
449     * Archive a(rawDataStream);
450     * a.deserialize(&myRootObject);
451 schoenebeck 3142 * @endcode
452 schoenebeck 3138 * Or with operator instead:
453     * @code
454     * Archive a(rawDataStream);
455     * a >> myRootObject;
456 schoenebeck 3142 * @endcode
457 schoenebeck 3138 * Now this framework automatically handles serialization and
458     * deserialization of fundamental data types automatically for you (like
459     * i.e. char, int, long int, float, double, etc.). However for your own
460     * custom C++ classes and structs you must implement one method which
461     * defines which members of your class should actually be serialized and
462     * deserialized. That method to be added must have the following signature:
463     * @code
464     * void serialize(Serialization::Archive* archive);
465     * @endcode
466     * So let's say you have the following simple data structures:
467     * @code
468     * struct Foo {
469     * int a;
470     * bool b;
471     * double c;
472     * };
473     *
474     * struct Bar {
475     * char one;
476     * float two;
477     * Foo foo1;
478     * Foo* pFoo2;
479     * Foo* pFoo3DontTouchMe; // shall not be serialized/deserialized
480     * };
481     * @endcode
482     * So in order to be able to serialize and deserialize objects of those two
483     * structures you would first add the mentioned method to each struct
484     * definition (i.e. in your header file):
485     * @code
486     * struct Foo {
487     * int a;
488     * bool b;
489     * double c;
490     *
491     * void serialize(Serialization::Archive* archive);
492     * };
493     *
494     * struct Bar {
495     * char one;
496     * float two;
497     * Foo foo1;
498     * Foo* pFoo2;
499     * Foo* pFoo3DontTouchMe; // shall not be serialized/deserialized
500     *
501     * void serialize(Serialization::Archive* archive);
502     * };
503     * @endcode
504     * And then you would implement those two new methods like this (i.e. in
505     * your .cpp file):
506     * @code
507     * #define SRLZ(member) \
508     * archive->serializeMember(*this, member, #member);
509     *
510     * void Foo::serialize(Serialization::Archive* archive) {
511     * SRLZ(a);
512     * SRLZ(b);
513     * SRLZ(c);
514     * }
515     *
516     * void Bar::serialize(Serialization::Archive* archive) {
517     * SRLZ(one);
518     * SRLZ(two);
519     * SRLZ(foo1);
520     * SRLZ(pFoo2);
521     * // leaving out pFoo3DontTouchMe here
522     * }
523     * @endcode
524     * Now when you serialize such a Bar object, this framework will also
525     * automatically serialize the respective Foo object(s) accordingly, also
526     * for the pFoo2 pointer for instance (as long as it is not a NULL pointer
527     * that is).
528     *
529     * Note that there is only one method that you need to implement. So the
530     * respective serialize() method implementation of your classes/structs are
531     * both called for serialization, as well as for deserialization!
532     */
533     class Archive {
534     public:
535     Archive();
536     Archive(const RawData& data);
537     Archive(const uint8_t* data, size_t size);
538     virtual ~Archive();
539    
540     template<typename T>
541     void serialize(const T* obj) {
542     m_operation = OPERATION_SERIALIZE;
543     m_allObjects.clear();
544     m_rawData.clear();
545     m_root = UID::from(obj);
546     const_cast<T*>(obj)->serialize(this);
547     encode();
548     m_operation = OPERATION_NONE;
549     }
550    
551     template<typename T>
552     void deserialize(T* obj) {
553     Archive a;
554     m_operation = OPERATION_DESERIALIZE;
555     obj->serialize(&a);
556     a.m_root = UID::from(obj);
557     Syncer s(a, *this);
558     m_operation = OPERATION_NONE;
559     }
560    
561     template<typename T>
562     void operator<<(const T& obj) {
563     serialize(&obj);
564     }
565    
566     template<typename T>
567     void operator>>(T& obj) {
568     deserialize(&obj);
569     }
570    
571 schoenebeck 3150 const RawData& rawData();
572 schoenebeck 3138 virtual String rawDataFormat() const;
573    
574     template<typename T_classType, typename T_memberType>
575     void serializeMember(const T_classType& nativeObject, const T_memberType& nativeMember, const char* memberName) {
576     const size_t offset =
577     ((const uint8_t*)(const void*)&nativeMember) -
578     ((const uint8_t*)(const void*)&nativeObject);
579     const UIDChain uids = UIDChainResolver<T_memberType>(nativeMember);
580     const DataType type = DataType::dataTypeOf(nativeMember);
581     const Member member(memberName, uids[0], offset, type);
582     const UID parentUID = UID::from(nativeObject);
583     Object& parent = m_allObjects[parentUID];
584     if (!parent) {
585     const UIDChain uids = UIDChainResolver<T_classType>(nativeObject);
586     const DataType type = DataType::dataTypeOf(nativeObject);
587     parent = Object(uids, type);
588     }
589     parent.members().push_back(member);
590     const Object obj(uids, type);
591     const bool bExistsAlready = m_allObjects.count(uids[0]);
592     const bool isValidObject = obj;
593     const bool bExistingObjectIsInvalid = !m_allObjects[uids[0]];
594     if (!bExistsAlready || (bExistingObjectIsInvalid && isValidObject)) {
595     m_allObjects[uids[0]] = obj;
596     // recurse serialization for all members of this member
597     // (only for struct/class types, noop for primitive types)
598     SerializationRecursion<T_memberType>::serializeObject(this, nativeMember);
599     }
600     }
601    
602     virtual void decode(const RawData& data);
603     virtual void decode(const uint8_t* data, size_t size);
604     void clear();
605 schoenebeck 3150 bool isModified() const;
606 schoenebeck 3153 void removeMember(Object& parent, const Member& member);
607 schoenebeck 3138 void remove(const Object& obj);
608     Object& rootObject();
609     Object& objectByUID(const UID& uid);
610 schoenebeck 3150 void setAutoValue(Object& object, String value);
611     void setIntValue(Object& object, int64_t value);
612     void setRealValue(Object& object, double value);
613     void setBoolValue(Object& object, bool value);
614     void setEnumValue(Object& object, uint64_t value);
615     String valueAsString(const Object& object);
616 schoenebeck 3156 String name() const;
617     void setName(String name);
618     String comment() const;
619     void setComment(String comment);
620     time_t timeStampCreated() const;
621     time_t timeStampModified() const;
622     tm dateTimeCreated(time_base_t base = LOCAL_TIME) const;
623     tm dateTimeModified(time_base_t base = LOCAL_TIME) const;
624 schoenebeck 3138
625     protected:
626     // UID resolver for non-pointer types
627     template<typename T>
628     class UIDChainResolver {
629     public:
630     UIDChainResolver(const T& data) {
631     m_uid.push_back(UID::from(data));
632     }
633    
634     operator UIDChain() const { return m_uid; }
635     UIDChain operator()() const { return m_uid; }
636     private:
637     UIDChain m_uid;
638     };
639    
640     // UID resolver for pointer types (of 1st degree)
641     template<typename T>
642     class UIDChainResolver<T*> {
643     public:
644     UIDChainResolver(const T*& data) {
645 schoenebeck 3168 const UID uids[2] = {
646     { &data, sizeof(data) },
647     { data, sizeof(*data) }
648     };
649     m_uid.push_back(uids[0]);
650     m_uid.push_back(uids[1]);
651 schoenebeck 3138 }
652    
653     operator UIDChain() const { return m_uid; }
654     UIDChain operator()() const { return m_uid; }
655     private:
656     UIDChain m_uid;
657     };
658    
659     // SerializationRecursion for non-pointer class/struct types.
660     template<typename T, bool T_isRecursive>
661     struct SerializationRecursionImpl {
662     static void serializeObject(Archive* archive, const T& obj) {
663     const_cast<T&>(obj).serialize(archive);
664     }
665     };
666    
667     // SerializationRecursion for pointers (of 1st degree) to class/structs.
668     template<typename T, bool T_isRecursive>
669     struct SerializationRecursionImpl<T*,T_isRecursive> {
670     static void serializeObject(Archive* archive, const T*& obj) {
671     if (!obj) return;
672     const_cast<T*&>(obj)->serialize(archive);
673     }
674     };
675    
676     // NOOP SerializationRecursion for primitive types.
677     template<typename T>
678     struct SerializationRecursionImpl<T,false> {
679     static void serializeObject(Archive* archive, const T& obj) {}
680     };
681    
682     // NOOP SerializationRecursion for pointers (of 1st degree) to primitive types.
683     template<typename T>
684     struct SerializationRecursionImpl<T*,false> {
685     static void serializeObject(Archive* archive, const T*& obj) {}
686     };
687    
688     // Automatically handles recursion for class/struct types, while ignoring all primitive types.
689     template<typename T>
690 schoenebeck 3167 struct SerializationRecursion : SerializationRecursionImpl<T, LIBGIG_IS_CLASS(T)> {
691 schoenebeck 3138 };
692    
693     class ObjectPool : public std::map<UID,Object> {
694     public:
695     // prevent passing obvious invalid UID values from creating a new pair entry
696     Object& operator[](const UID& k) {
697     static Object invalid;
698     if (!k.isValid()) {
699     invalid = Object();
700     return invalid;
701     }
702     return std::map<UID,Object>::operator[](k);
703     }
704     };
705    
706     friend String _encode(const ObjectPool& objects);
707    
708     private:
709     String _encodeRootBlob();
710     void _popRootBlob(const char*& p, const char* end);
711     void _popObjectsBlob(const char*& p, const char* end);
712    
713     protected:
714     class Syncer {
715     public:
716     Syncer(Archive& dst, Archive& src);
717     protected:
718     void syncObject(const Object& dst, const Object& src);
719     void syncPrimitive(const Object& dst, const Object& src);
720     void syncPointer(const Object& dst, const Object& src);
721     void syncMember(const Member& dstMember, const Member& srcMember);
722     static Member dstMemberMatching(const Object& dstObj, const Object& srcObj, const Member& srcMember);
723     private:
724     Archive& m_dst;
725     Archive& m_src;
726     };
727    
728     virtual void encode();
729    
730     ObjectPool m_allObjects;
731     operation_t m_operation;
732     UID m_root;
733     RawData m_rawData;
734 schoenebeck 3150 bool m_isModified;
735 schoenebeck 3156 String m_name;
736     String m_comment;
737     time_t m_timeCreated;
738     time_t m_timeModified;
739 schoenebeck 3138 };
740    
741     /**
742     * Will be thrown whenever an error occurs during an serialization or
743     * deserialization process.
744     */
745     class Exception {
746     public:
747     String Message;
748    
749     Exception(String Message) { Exception::Message = Message; }
750     void PrintMessage();
751     virtual ~Exception() {}
752     };
753    
754     } // namespace Serialization
755    
756     #endif // LIBGIG_SERIALIZATION_H

  ViewVC Help
Powered by ViewVC