Serialization with version

I added a data member to a class that had a Serialize() function. The Serialize() function didn't support versioning. Now I want to serialize the new data member but to be compatible with previous serializaions.
My class has DECLARE_SERIAL(...) and IMPLEMENT_SERIAL(...,...,VERSIONABLE_SCHEMA|1) but when I use the function CArchive::GetObjectSchema() I always get -1.
I can't distinguish between old versions and new ones.
The class I serialize is an item in a CMap object that I serialize in another class.
How can I distinguish the different versions?
Thanks, Tomer
TomersAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
naveenkohliConnect With a Mentor Commented:
Here are couple of cpomments regrading using GetObjectSchema
1. Only call it one object is being loaded.
2. Make sure that it is called before you start reading any data from the object.
3. Call it only once. If you will call ut second time, it will return -1.

Example of impelementing two version...
two versions 0 and 1.
version 0 has two variables var1, var2...
version 1 has three m_Var1, m_var2, m_Var3

IMPLEMENT_SERIAL (CMyObject, CObject, 1 | VERSIONABLE_SCHEMA)

void CMyObject::Serialize (CArchive &ar)
{
 CObject:;Serialize (ar);
 if (ar.IsStoring ()) {
  ar << m_Var1 << m_Var2 << m_Var3;
 }
 else {
  UINT nSchema = ar.GetObjectSchema ();
  switch (nSchema) {
   case 0:
     ar >> m_Var1 >> m_Var2;
     m_Var3 = /* Some default value*/;
     break;
   case 1:
     ar >> m_Var1 >> m_Var2 >> m_Var3;
     break;
   default:
     AfxThrowArchiveException (CArchiveException::badSchema);
     break;
  }
 }
}

0
 
TomersAuthor Commented:
The answer I got was like what I already read in the VC++ documents. The problem
is it didn't work, maybe because it was items in a CMap.
Anyway I solved it by trying to read it once with 3 arguments and if I encounter an
exception, I close the archive and open it again trying to read 2 arguments.
0
All Courses

From novice to tech pro — start learning today.