Programmers Reference - Serialization (1)
The ConSys System Serialization scheme
It has been necessary to implement a dynamic creation scheme in parallel to the scheme used by Microsoft. The scheme used by Microsoft MFC uses a linear string search to identify the class for creation, and is not very effective. The dynamic creation of objects is the central part of the serialization scheme, which we use heavily. To create a class based on a string in MFC, you have to use undocumented features, a thing that is undesirable. For these reasons we have decided to implement a scheme that is a merge of the Borland Pascal scheme, and the MFC.
The idea of the dynamic creation scheme is to use a unique identifier for each class in the system that is possible to create dynamically. This is the price to pay for fast dynamic creation. It is still possible to create the object by class name, but it will still be a slow process.
When a class is declared serial by the ConSys System scheme, it is also declared serial by the MFC. Instead of using the macros provided by MFC, you should instead use the macros provided by the LabLib library. The use of the macros is nearly the same as for the macros from MFC. The two macros is:
DECLARE_CONSYS_SERIAL(className) IMPLEMENT_CONSYS_SERIAL(className, baseName, classVersion, classId);
The extra parameter 'classId' is used for quick recognition of the class, and must be unique for all interacting applications.
Instead of using the CArchive class used by MFC, the laboratory System scheme uses the class CTransport as abstraction layer. This creates a need for a new Serialize function for a class that has arguments to serialize. The LabLib library declares two macros for declaring and implementing the serialization functions. The macro DECLARE_SERIAL_FUNC() is used in the class declaration if the class has anything to serialize. When implementing the serialization function use the macro IMPLEMENT_SERIAL_FUNC(className, baseName, argument, body). The first entry in the macro is the name of the CTransport/CArchive variable to be used as argument in the body of the function. The body entry is the body part of the serialization function. Below is an example:
class CClassToSerialize : public CBaseClass { // member functions DECLARE_CONSYS_SERIAL(CClassToSerialize); DECLARE_SERIAL_FUNC(); };
and now the implementation of the class
IMPLEMENT_SERIAL_FUNC(CClassToSerialize, CBaseClass, arg, // This is the body part if( arg.IsStoring() ){ // Put members to arg arg << m_member; } else { // Get members from arg arg >> m_member; }; // end of body part )
Because that the connection between the client and the server can't be guaranteed at all times, the serialization function can at any point start to return NULL. This fact has to be taken into account when designing the function. It shut be possible to destruct the serialized class even if the serialization has started to fault.
Se also:
References:
Last Modified 11 January 2019