?
Solved

Serialization with version

Posted on 1999-06-25
2
Medium Priority
?
360 Views
Last Modified: 2013-11-20
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
0
Comment
Question by:Tomers
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 23

Accepted Solution

by:
naveenkohli earned 200 total points
ID: 1307941
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
 

Author Comment

by:Tomers
ID: 1307942
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

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question