How to assign a version number (configurable version mode) to a serialized class

Source: Internet
Author: User
Tags serialization

When writing a serializable class, MFC uses the pattern number you specify to develop a rough version control method. When writing data to a file, MFC uses patterns to mark instances of the class, and when the data is read, MFC compares the pattern numbers of records in the archive to the pattern numbers of the class objects used in the application, and if the two mode numbers do not match, MFC sends a carchiveexception whose m_ Cause equals Carchiveexception::badschema. A class exception that is not handled will cause MFC to display a dialog box that prompts for "unexpected file format." If you can increase the mode number each time you modify the serialized storage format of the object, you are not afraid of this unintentional action-trying to read the old version of the object in memory into a new version of the object.

A problem often occurs suddenly in an application that uses a serializable class, which is backward compatible. In other words, how to parallel the objects created in the old version of the application. If the object's persistent storage format changes with the application version of the update, you may want the new version of the application to be readable for both formats. However, once MFC discovers the pattern number that is not matched, it sends an exception. In view of the structural features of MFC, it is best to deal with exceptions and terminate the serialization process in MFC mode. The visual mode is also generated.

The visual mode is just the pattern number that contains the VERSIONABLE_SCHEMA flag. Flags tell MFC applications to handle multiple serialization data formats for a particular class. This pattern prohibits carchiveexception and allows applications to respond to different pattern numbers in a judgmental way. Applications that use Visual mode can provide the backward compatibility that users want. If you want to write a serializable class with MFC Visual mode support, you typically need two steps:

Versionable_schema the pattern number in the Implement_serlal macro to or from the value.

If you need to call CArchive::GetObjectSchema when you load an object from a file, you modify the Serialize function of the class and adjust its parallelization routines accordingly. GetObjectSchema returns the pattern number of the object to be parallelization.

There are several rules to be aware of when calling GetObjectSchema. First, only objects can be invoked when they are parallelization. Second, it must be called before the file object data is read. Furthermore, it can only be invoked once. Returns-1 if the getobjectschema is called two times before and after the call to serialize. Let's take a look at an example, which is the Cline class of version 1:

class CLine : public CObject
{
DECLARE_SERIAL (CLine)
protected:
  CPoint m_ptFrom;
  CPoint m_ptTo;
public:
  CLine () {} // Required!
  CLine (CPoint from, CPoint to) { m_ptFrom = from; m_ptTo = to; }
  void Serialize (CArchive& ar);
};
This is the Serialize function: The statement that appears during the implementation of a void CLine::Serialize (CArchive& ar)
{
  CObject::Serialize (ar);
  if (ar.IsStoring ())
    ar << m_ptFrom << m_ptTo;
  else // Loading, not storing
    ar >> m_ptFrom >> m_ptTo;
}
class:IMPLEMENT_SERIAL (CLine, CObject, 1)

This class can be serialized. The current version number is 1 and if you later add a persistent data member to Cline, increase the version number to 2 so that the main structure can be serialized to the Cline object of the disk according to the different versions of the program. Otherwise, the Cline of version 1 on disk may be read into the Cline in memory version 2, which can have serious consequences.

Suppose in the 2nd version of the application you want to modify the Cline class to add a member variable to hold the line's color. The following is a declaration of the modified class:class CLine : public CObject
{
DECLARE_SERIAL (CLine)
protected:
  CPoint m_ptFrom;
  CPoint m_ptTo;
  COLORREF m_clrLine; // Line color (new in version 2)
public:
  CLine () {}
  CLine (CPoint from, CPoint to, COLORREF color)
    { m_ptFrom = from; m_ptTo = to; m_clrLine = color }
  void Serialize (CArchive& ar);
};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.