Click to See Complete Forum and Search --> : I need help serializing different versions of an document.
Timothy Eyring
May 10th, 1999, 04:08 PM
I hope someone can help...
I am having problems while serializing the objects in my document. No matter what I've tried "ar.GetObjectSchema()" always returns -1. I'm trying to allow my program to read a previous version of the data file. (I added objects that can be empty, so I just want the program to leave those things empty) All I ever get as a message is "Invalid file format" and the load fails if I try and read the old format. As far as I've been able to find, its because CArchive won't tell me what the version is (of the oblect being loaded). If I knew the version, I could skip the statements that read the new objects! What else is needed besides the basic serialization stuff?
I didn't include any code because it's just basic serialization, nothing REALLY conplicated.
thanks,
-Timothy Eyring
November 5th, 1999, 01:48 PM
Timothy,
I have written many program with multiple versions of documents. The simplest way I have found to do this is the following:
1. At the beginning of you document files write out a header string giving specifics to what the file version is.
2. When reading in the file the serialization checks the version and creates the appropriate objcet type.
3. With the appropriate object call a serialization function contained within that versions Object (passing in the Archive ar).
Example:
//Header file
#define MYOBJ_VER1_0 "My Document Version 1.0"
#ifndef MYOBJ
#define MYOBJ
class CMyObj:public CObject
{
public:
void Serialize(CArchive&);
};
//Document class' implementation of Serialize
if(ar.IsStoring())
{
}
else
{
CString Version;
ar >> Version;
if (Version == MYOBJ_VER1_0)
{
CMyObj* MyObj1 = new CMyObj;
MyObj1->Serialize(ar);
}
if (Version == MYOBJ_VER1_5)
{
CMyObj15* MyObj = new CMyObj15;
MyObj->Serialize(ar);
}
if (Version == MYOBJ_VER2_0)
{
CMyObj20* MyObj = new CMyObj20;
MyObj->Serialize(ar);
}
}
This way you allow each object to handle it's own serialization.
For every object (Version) you create, you can add an extra "if" statement to handle it.
Hope this helps.
Ken Kirkhope.
November 5th, 1999, 01:49 PM
Timothy,
I have written many program with multiple versions of documents. The simplest way I have found to do this is the following:
1. At the beginning of you document files write out a header string giving specifics to what the file version is.
2. When reading in the file the serialization checks the version and creates the appropriate objcet type.
3. With the appropriate object call a serialization function contained within that versions Object (passing in the Archive ar).
Example:
//Header file
#define MYOBJ_VER1_0 "My Document Version 1.0"
#ifndef MYOBJ
#define MYOBJ
class CMyObj:public CObject
{
public:
void Serialize(CArchive&);
};
//Document class' implementation of Serialize
if(ar.IsStoring())
{
}
else
{
CString Version;
ar >> Version;
if (Version == MYOBJ_VER1_0)
{
CMyObj10* MyObj = new CMyObj10;
MyObj->Serialize(ar);
}
if (Version == MYOBJ_VER1_5)
{
CMyObj15* MyObj = new CMyObj15;
MyObj->Serialize(ar);
}
if (Version == MYOBJ_VER2_0)
{
CMyObj20* MyObj = new CMyObj20;
MyObj->Serialize(ar);
}
}
This way you allow each object to handle it's own serialization.
For every object (Version) you create, you can add an extra "if" statement to handle it.
Hope this helps.
Ken Kirkhope.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.