--- libgig/trunk/src/Serialization.h 2017/05/14 20:40:02 3182 +++ libgig/trunk/src/Serialization.h 2017/05/15 18:44:32 3183 @@ -118,17 +118,54 @@ typedef std::string String; + /** @brief Raw data stream of serialized C++ objects. + * + * This data type is used for the data stream as a result of serializing + * your C++ objects with Archive::serialize(), and for native raw data + * representation of individual serialized C/C++ objects, members and variables. + * + * @see Archive::rawData(), Object::rawData() + */ typedef std::vector RawData; + /** @brief Abstract identifier for serialized C++ objects. + * + * This data type is used for identifying serialized C++ objects and members + * of your C++ objects. It is important to know that such an ID might not + * necessarily be unique. For example the ID of one C++ object might often + * be identical to the ID of the first member of that particular C++ object. + * That's why there is additionally the concept of an UID in this framework. + * + * @see UID + */ typedef void* ID; + /** @brief Version number data type. + * + * This data type is used for maintaining version number information of + * your C++ class implementations. + * + * @see Archive::setVersion() and Archive::setMinVersion() + */ typedef uint32_t Version; + /** @brief To which time zone a certain timing information relates to. + * + * The constants in this enum type are used to define to which precise time + * zone a time stamp relates to. + */ enum time_base_t { - LOCAL_TIME, - UTC_TIME + LOCAL_TIME, ///< The time stamp relates to the machine's local time zone. Request a time stamp in local time if you want to present that time stamp to the end user. + UTC_TIME ///< The time stamp relates to "Greenwhich Mean Time" zone, also known as "Coordinated Universal Time". Request time stamp with UTC if you want to compare that time stamp with other time stamps. }; + /** @brief Check whether data is a C/C++ @c enum type. + * + * Returns true if the supplied C++ variable or object is of a C/C++ @c enum + * type. + * + * @param data - the variable or object whose data type shall be checked + */ template bool IsEnum(const T& data) { #if !HAS_BUILTIN_TYPE_TRAITS @@ -138,6 +175,16 @@ #endif } + /** @brief Check whether data is a C++ @c union type. + * + * Returns true if the supplied C++ variable or object is of a C/C++ @c union + * type. Note that the result of this function is only reliable if the C++ + * compiler you are using has support for built-in type traits. If your C++ + * compiler does not have built-in type traits support, then this function + * will simply return @c false on all your calls. + * + * @param data - the variable or object whose data type shall be checked + */ template bool IsUnion(const T& data) { #if !HAS_BUILTIN_TYPE_TRAITS @@ -147,6 +194,15 @@ #endif } + /** @brief Check whether data is a C/C++ @c struct or C++ @c class type. + * + * Returns true if the supplied C++ variable or object is of C/C++ @c struct + * or C++ @c class type. Note that if you are using a C++ compiler which + * does have built-in type traits support, then this function will also + * return @c true on C/C++ @c union types. + * + * @param data - the variable or object whose data type shall be checked + */ template bool IsClass(const T& data) { #if !HAS_BUILTIN_TYPE_TRAITS @@ -169,22 +225,37 @@ /** @brief Unique identifier for one specific C++ object, member or fundamental variable. * * Reflects a unique identifier for one specific serialized C++ class - * instance, struct instance, member, primitive pointer, or fundamental - * variables. + * instance, C/C++ struct instance, member, primitive pointer, or + * fundamental variables. + * + * A unique identifier is composed of an id (an identifier which is not + * necessarily unique) and a size. Since the underlying ID is derived from + * the original C++ object's memory location, such an ID is not sufficient + * to distinguish a particular C++ object from the first member of that C++ + * object, since both typically share the same memory address. So + * additionally the memory size of the respective object or member is + * bundled with UID objects to make them unique and distinguishable. */ class UID { public: - ID id; - size_t size; + ID id; ///< Abstract non-unique ID of the object or member in question. + size_t size; ///< Memory size of the object or member in question. bool isValid() const; - operator bool() const { return isValid(); } + operator bool() const { return isValid(); } ///< Same as calling isValid(). //bool operator()() const { return isValid(); } bool operator==(const UID& other) const { return id == other.id && size == other.size; } bool operator!=(const UID& other) const { return id != other.id || size != other.size; } bool operator<(const UID& other) const { return id < other.id || (id == other.id && size < other.size); } bool operator>(const UID& other) const { return id > other.id || (id == other.id && size > other.size); } + /** @brief Create an unique indentifier for a native C++ object/member/variable. + * + * Creates and returns an unique identifier for the passed native C++ + * object, object member or variable. For the same C++ object/member/variable + * this function will always return the same UID. For all other ones, + * this function is guaranteed to return a different UID. + */ template static UID from(const T& obj) { return Resolver::resolve(obj); @@ -212,10 +283,42 @@ /** * Reflects an invalid UID and behaves similar to NULL as invalid value for - * pointer types. + * pointer types. All UID objects are first initialized with this value, + * and it essentially an all zero object. */ extern const UID NO_UID; + /** @brief Chain of UIDs. + * + * This data type is used for native C++ pointers. The first member of the + * UID chain is the unique identifier of the C++ pointer itself, then the + * following UIDs are the respective objects or variables the pointer is + * pointing to. The size (the amount of elements) of the UIDChain depends + * solely on the degree of the pointer type. For example the following C/C++ + * pointer: + * @code + * int* pNumber; + * @endcode + * is an integer pointer of first degree. Such a pointer would have a + * UIDChain with 2 members: the first element would be the UID of the + * pointer itself, the second element of the chain would be the integer data + * that pointer is pointing to. In the following example: + * @code + * bool*** pppSomeFlag; + * @endcode + * That boolean pointer would be of third degree, and thus its UIDChain + * would have a size of 4 (elements). + * + * Accordingly a non pointer type like: + * @code + * float f; + * @endcode + * would yield in a UIDChain of size 1. + * + * Since however this serialization framework currently only supports + * pointers of first degree yet, all UIDChains are currently either of + * size 1 or 2, which might change in future though. + */ typedef std::vector UIDChain; // prototyping of private internal friend functions @@ -231,15 +334,24 @@ /** @brief Abstract reflection of a native C++ data type. * - * Provides detailed information about a C++ data type, whether it is a - * fundamental C/C++ data type (like int, float, char, etc.) or custom - * defined data type like a C++ class, struct, enum, as well as other - * features of the data type like its native memory size and more. + * Provides detailed information about a serialized C++ data type, whether + * it is a fundamental C/C++ data type (like @c int, @c float, @c char, + * etc.) or custom defined data types like a C++ @c class, C/C++ @c struct, + * @c enum, as well as other features of the respective data type like its + * native memory size and more. + * + * All informations provided by this class are retrieved from the + * respective individual C++ objects, their members and other data when + * they are serialized, and all those information are stored with the + * serialized archive and its resulting data stream. Due to the availability + * of these extensive data type information within serialized archives, this + * framework is capable to use them in order to adapt its deserialization + * process upon subsequent changes to your individual C++ classes. */ class DataType { public: DataType(); - size_t size() const { return m_size; } + size_t size() const { return m_size; } ///< Returns native memory size of the respective C++ object or variable. bool isValid() const; bool isPointer() const; bool isClass() const; @@ -249,16 +361,26 @@ bool isBool() const; bool isEnum() const; bool isSigned() const; - operator bool() const { return isValid(); } + operator bool() const { return isValid(); } ///< Same as calling isValid(). //bool operator()() const { return isValid(); } bool operator==(const DataType& other) const; bool operator!=(const DataType& other) const; bool operator<(const DataType& other) const; bool operator>(const DataType& other) const; String asLongDescr() const; - String baseTypeName() const { return m_baseTypeName; } + String baseTypeName() const; String customTypeName(bool demangle = false) const; + /** @brief Construct a DataType object for the given native C++ data. + * + * Use this function to create corresponding DataType objects for + * native C/C++ objects, members and variables. + * + * @param data - native C/C++ object/member/variable a DataType object + * shall be created for + * @returns corresponding DataType object for the supplied native C/C++ + * object/member/variable + */ template static DataType dataTypeOf(const T& data) { return Resolver::resolve(data); @@ -341,16 +463,30 @@ * serialized C++ object, like its C++ data type, offset of this member * within its containing data structure/class, its C++ member variable name * and more. + * + * Consider you defined the following user defined C/C++ @c struct type in + * your application: + * @code + * struct Foo { + * int a; + * bool b; + * double someValue; + * }; + * @endcode + * Then @c a, @c b and @c someValue are "members" of @c struct @c Foo for + * instance. So that @c struct would have 3 members in the latter example. + * + * @see Object::members() */ class Member { public: Member(); - UID uid() const { return m_uid; } - String name() const { return m_name; } - size_t offset() const { return m_offset; } - const DataType& type() const { return m_type; } + UID uid() const; + String name() const; + size_t offset() const; + const DataType& type() const; bool isValid() const; - operator bool() const { return isValid(); } + operator bool() const { return isValid(); } ///< Same as calling isValid(). //bool operator()() const { return isValid(); } bool operator==(const Member& other) const; bool operator!=(const Member& other) const; @@ -374,31 +510,41 @@ * * Provides detailed information about a specific serialized C++ object, * like its C++ member variables, its C++ class/struct name, its native - * memory size and more. + * memory size and more. When your native C++ objects are serialized, all + * native data is translated reflected by such an Object reflection. So each + * instance of your serialized C++ class objects become available as an + * Object, but also each member variable of your C++ objects, and all other + * native C/C++ data. So essentially every native data is turned into its + * own Object and accessible by this API. + * + * Even though this framework allows you to adjust abstract Object instances + * to a certain extent, most of the methods of this Object class are + * read-only though and the actual modifyable methods are made available + * not as part of this Object class, but as part of the Archive class + * instead. This was designed like this for performance and safety reasons. + * + * @see Archive::setIntValue() as an example for modifying Object instances. */ class Object { public: Object(); Object(UIDChain uidChain, DataType type); - UID uid(int index = 0) const { - return (index < m_uid.size()) ? m_uid[index] : NO_UID; - } - - const UIDChain& uidChain() const { return m_uid; } - const DataType& type() const { return m_type; } - const RawData& rawData() const { return m_data; } - Version version() const { return m_version; } - Version minVersion() const { return m_minVersion; } + UID uid(int index = 0) const; + const UIDChain& uidChain() const; + const DataType& type() const; + const RawData& rawData() const; + Version version() const; + Version minVersion() const; bool isVersionCompatibleTo(const Object& other) const; - std::vector& members() { return m_members; } - const std::vector& members() const { return m_members; } + std::vector& members(); + const std::vector& members() const; Member memberNamed(String name) const; Member memberByUID(const UID& uid) const; std::vector membersOfType(const DataType& type) const; int sequenceIndexOf(const Member& member) const; bool isValid() const; - operator bool() const { return isValid(); } + operator bool() const { return isValid(); } ///< Same as calling isValid(). //bool operator()() const { return isValid(); } bool operator==(const Object& other) const; bool operator!=(const Object& other) const; @@ -553,6 +699,31 @@ Archive(const uint8_t* data, size_t size); virtual ~Archive(); + /** @brief Initiate serialization. + * + * Initiates serialization of all native C++ objects, which means + * capturing and storing the current data of all your C++ objects as + * content of this Archive. + * + * This framework has a concept of a "root" object which you must pass + * to this method. The root object is the starting point for + * serialization of your C++ objects. The framework will then + * recursively serialize all members of that C++ object an continue to + * serialize all other C++ objects that it might contain or point to. + * + * After this method returned, you might traverse all serialized objects + * by walking them starting from the rootObject(). You might then modify + * that abstract reflection of your C++ objects and finally you might + * call rawData() to get an encoded raw data stream which you might use + * for sending it "over wire" to somewhere where it is going to be + * deserialized later on. + * + * Note that whenever you call this method, the previous content of this + * Archive will first be cleared. + * + * @param obj - native C++ root object where serialization shall start + * @see Archive::operator<<() + */ template void serialize(const T* obj) { m_operation = OPERATION_SERIALIZE; @@ -564,6 +735,30 @@ m_operation = OPERATION_NONE; } + /** @brief Initiate deserialization. + * + * Initiates deserialization of all native C++ objects, which means all + * your C++ objects will be restored with the values contained in this + * Archive. So that also means calling deserialize() only makes sense if + * this a non-empty Archive, which i.e. is the case if you either called + * serialize() with this Archive object before or if you passed a + * previously serialized raw data stream to the constructor of this + * Archive object. + * + * This framework has a concept of a "root" object which you must pass + * to this method. The root object is the starting point for + * deserialization of your C++ objects. The framework will then + * recursively deserialize all members of that C++ object an continue to + * deserialize all other C++ objects that it might contain or point to, + * according to the values stored in this Archive. + * + * @param obj - native C++ root object where deserialization shall start + * @see Archive::operator>>() + * + * @throws Exception if the data stored in this Archive cannot be + * restored to the C++ objects passed to this method, i.e. + * because of version or type incompatibilities. + */ template void deserialize(T* obj) { Archive a; @@ -574,11 +769,43 @@ m_operation = OPERATION_NONE; } + /** @brief Initiate serialization of your C++ objects. + * + * Same as calling @c serialize(), this is just meant if you prefer + * to use operator based code instead, which you might find to be more + * intuitive. + * + * Example: + * @code + * Archive a; + * a << myRootObject; + * @endcode + * + * @see Archive::serialize() for more details. + */ template void operator<<(const T& obj) { serialize(&obj); } + /** @brief Initiate deserialization of your C++ objects. + * + * Same as calling @c deserialize(), this is just meant if you prefer + * to use operator based code instead, which you might find to be more + * intuitive. + * + * Example: + * @code + * Archive a(rawDataStream); + * a >> myRootObject; + * @endcode + * + * @throws Exception if the data stored in this Archive cannot be + * restored to the C++ objects passed to this method, i.e. + * because of version or type incompatibilities. + * + * @see Archive::deserialize() for more details. + */ template void operator>>(T& obj) { deserialize(&obj); @@ -587,6 +814,51 @@ const RawData& rawData(); virtual String rawDataFormat() const; + /** @brief Serialize a native C/C++ member variable. + * + * This method is usually called by the serialize() method + * implementation of your C/C++ structs and classes, for each of the + * member variables that shall be serialized and deserialized + * automatically with this framework. It is recommend that you are not + * using this method name directly, but rather define a short hand C + * macro in your .cpp file like: + * @code + * #define SRLZ(member) \ + * archive->serializeMember(*this, member, #member); + * + * void Foo::serialize(Serialization::Archive* archive) { + * SRLZ(a); + * SRLZ(b); + * SRLZ(c); + * } + * @endcode + * As you can see, using such a macro makes your code more readable and + * less error prone. + * + * It is completely up to you to decide which ones of your member + * variables shall automatically be serialized and deserialized with + * this framework. Only those member variables which are registered by + * calling this method will be serialized and deserialized. It does not + * really matter in which order you register your individiual member + * variables by calling this method, but the sequence is actually stored + * as meta information with the resulting archive and the resulting raw + * data stream. That meta information might then be used by this + * framework to automatically correct and adapt deserializing that + * archive later on for a future (or older) and potentially heavily + * modified version of your software. So it is recommended, even though + * also not required, that you may retain the sequence of your + * serializeMember() calls for your individual C++ classes' members over + * all your software versions, to retain backward compatibility of older + * archives as much as possible. + * + * @param nativeObject - native C++ object to be registered for + * serialization / deserialization + * @param nativeMember - native C++ member variable of @a nativeObject + * to be registered for serialization / + * deserialization + * @param memberName - name of @a nativeMember to be stored with this + * archive + */ template void serializeMember(const T_classType& nativeObject, const T_memberType& nativeMember, const char* memberName) { const size_t offset = @@ -615,17 +887,19 @@ } } - /** @brief Set version number for your C++ class. + /** @brief Set current version number for your C++ class. * - * By calling this method you can store a version number for your + * By calling this method you can define a version number for your * current C++ class (that is a version for its current data structure - * layout and method implementations) with serialized archive. + * layout and method implementations) that is going to be stored along + * with the serialized archive. Only call this method if you really want + * to constrain compatibility of your C++ class. * * Along with calling @c setMinVersion() this provides a way for you - * to constrain backward compatiblity regarding serialization and - * deserialization of your class which the Archive class will obey to. - * If required, then typically you might do so in your @c serialize() - * method implementation like: + * to constrain backward compatibility regarding serialization and + * deserialization of your C++ class which the Archive class will obey + * to. If required, then typically you might do so in your + * @c serialize() method implementation like: * @code * #define SRLZ(member) \ * archive->serializeMember(*this, member, #member); @@ -634,8 +908,8 @@ * // when serializing: the current version of this class that is * // going to be stored with the serialized archive * archive->setVersion(*this, 6); - * // when deserializing: the minimum allowed version of this class - * // being serialized in the past + * // when deserializing: the minimum version this C++ class is + * // compatible with * archive->setMinVersion(*this, 3); * // actual data mebers to serialize / deserialize * SRLZ(a); @@ -643,9 +917,10 @@ * SRLZ(c); * } * @endcode - * In this example above, the C++ clas "Foo" would be serialized along - * with the version number @c 6 in the resulting archive (and its raw - * data stream respectively). + * In this example above, the C++ class "Foo" would be serialized along + * with the version number @c 6 and minimum version @c 3 as additional + * meta information in the resulting archive (and its raw data stream + * respectively). * * When deserializing archives with the example C++ class code above, * the Archive object would check whether your originally serialized @@ -654,12 +929,27 @@ * @c Serialization::Exception, claiming that the classes are version * incompatible. * + * But also consider the other way around: you might have serialized + * your latest version of your C++ class, and might deserialize that + * archive with an older version of your C++ class. In that case it will + * likewise be checked whether the version of that old C++ class is at + * least as high as the minimum version set with the already seralized + * bleeding edge C++ class. + * * Since this Serialization / deserialization framework is designed to * be robust on changes to your C++ classes and aims trying to * deserialize all your C++ objects correctly even if your C++ classes * have seen substantial software changes in the meantime; you might - * sometimes see it as necessary to constrain backward compatiblity - * this way. + * sometimes see it as necessary to constrain backward compatibility + * this way. Because obviously there are certain things this framework + * can cope with, like for example that you renamed a data member while + * keeping the layout consistent, or that you have added new members to + * your C++ class or simply changed the order of your members in your + * C++ class. But what this framework cannot detect is for example if + * you changed the semantics of the values stored with your members, or + * even substantially changed the algorithms in your class methods such + * that they would not handle the data of your C++ members in the same + * and correct way anymore. * * @param nativeObject - your C++ object you want to set a version for * @param v - the version number to set for your C++ class (by default, @@ -682,10 +972,30 @@ * * Call this method to define a minimum version that your current C++ * class implementation would be compatible with when it comes to - * deserialization of an archive containing an object with an older - * version of your C++ class. + * deserialization of an archive containing an object of your C++ class. + * Like the version information, the minimum version will also be stored + * for objects of your C++ class with the resulting archive (and its + * resulting raw data stream respectively). + * + * When you start to constrain version compatibility of your C++ class + * you usually start by using 1 as version and 1 as minimum version. + * So it is eligible to set the same number to both version and minimum + * version. However you must @b not set a minimum version higher than + * version. Doing so would not raise an exception, but the resulting + * behavior would be undefined. + * + * It is not relevant whether you first set version and then minimum + * version or vice versa. It is also not relevant when exactly you set + * those two numbers, even though usually you would set both in your + * serialize() method implementation. * * @see @c setVersion() for more details about this overall topic. + * + * @param nativeObject - your C++ object you want to set a version for + * @param v - the minimum version you want to define for your C++ class + * (by default, that is if you do not explicitly call this + * method, then a minium version of @c 0 is assumed for your + * C++ class instead). */ template void setMinVersion(const T_classType& nativeObject, Version v) {