I have a problem with serialisation using the Marmalade SDK. I understand the principles like serialising primitives but what about polymorphic objects?
According to the Marmalade examples you serialise objects like so:
void Serialise()
{
uint8 isObject;
if (IwSerialiseIsWriting())
// writing
{
// record whether m_Happy exists
isObject = m_Happy ? 1 : 0;
IwSerialiseUInt8(isObject);
// serialise out m_Happy if it exists
if (m_Happy)
m_Happy->Serialise();
}
else
// reading
{
IwSerialiseUInt8(isObject);
// if an object was written out then read it in
if (isObject)
{
// if m_Happy already exists then delete it
if (m_Happy)
delete m_Happy;
m_Happy = new CHappy;
// serialise in m_Happy
m_Happy->Serialise();
}
}
}
My question is how can you use this method when you have a polymorphic type? For example:
I have an object:
AbstractClass *object;
When serialising, how can I initiate this object correctly? According to the simple Marmalade example I would:
// reading
if(objectExists) {
object = new AbstractClass(); // uh oh
}