Index of object serializing
1. How to use object serializing
This example shows how to use the object serializing mechanism. The Zeus-Framework helps you to serialize and deserialize various objects and object trees.
The serializing stores all attributes into a byte stream. This stream can be sent to a remote system. The remote system has to recreate the object aut of the stream. Therefore this system has to know the class only. All attributes will be set when the object has been created by a factory.
Every serializable class inherits the ISerializable Interface. Following example shows a class declaration of a serializable object.
//Definition of interfaces for test classes
class ISerialTestObject : ISerializable
{
public:
...
};
class ISerialTestSubObject : ISerializable
{
public:
...
};
//Declaration of serializable object
class TSerialTestObject : public TZObject, public ISerialTestObject
{
public:
TSerialTestObject();
...
//Serializing macros
SERIAL_START(TSerialTestObject, L"TSerialTestObject");
SERIAL_INT32(m_lValue)
SERIAL_FLOAT64(m_dValue)
SERIAL_STRING(m_strValue)
SERIAL_OBJECT(INTERFACE_ISerialTestSubObject, m_pObject)
SERIAL_END
...
private:
///Int32 value
Int32 m_lValue;
///Float64 value
Float64 m_dValue;
///String value
TString m_strValue;
///An other serializable Object
ISerialTestSubObject* m_pObject;
};
In the implemenation part of this example class following macro must be placed. This macro implements the factory methods and constructors to create such an object out of the stream.
SERIAL_CONSTRUCTOR(TSerialTestObject);
The class must be registered at the object factory. The factory will create this object, when a stream is processed having its signature. For all Framework classes this is done inside the TAbstractFrameLoader::registerClasses() method. For custom classes this must eigther placed inside a customized frameloader class, or at the beginning of the program.
OBJECTFACTORY_REGISTER_CLASS(TSerialTestObject);
OBJECTFACTORY_REGISTER_CLASS(TSerialTestSubObject);
2. Serializing objects
This part shows how to use the serializing mechanism implemented above. First the serializing:
//-------------------------------
//Serializing objects
TAutoPtr<TSerialTestObject> pObject2 = new TSerialTestObject();
TByteArray aStream;
if (pObject2->serialize(aStream) == RET_NOERROR)
{
//Send to a remote system
...
}
Then the deserializing:
#include <zeusbase/System/ByteArray.hpp>
#include <zeusbase/System/ZObjectFactory.h>
//-------------------------------
//Deserializing Objects
//Receiving the stream
TByteArray aStream;
...
TAutoPtr<ISerialTestObject> ptrObject;
if (ZObjectFactory.createObjectFromStream(aStream,
INTERFACE_ISerialTestObject, ptrObject.getInterfaceReference()) == RET_NOERROR)
{
//Object created
...
}