APPLYING SERIALIZATION

To implement serialization in Sketcher, you must implement the Serialize() function in CSketcherDoc so that it deals with all of the data members of that class. You need to add serialization to each of the classes that define objects that may be included in a document. Before you start adding serialization to your classes, you should make some small changes to the program to record when a user changes a sketch document. This isn’t essential, but it is highly desirable because it enables the program to guard against the document being closed without saving changes.

Recording Document Changes

There’s already a mechanism for noting when a document changes. It uses an inherited member of the CSketcherDoc class, SetModifiedFlag(). By calling this function consistently whenever the document changes, you can record the fact that the document has been altered in a data member of the document class object. This causes a prompt to be automatically displayed when you try to exit the application without saving the modified document. The argument to the SetModifiedFlag() function is a value of type BOOL, and the default value is TRUE. When you have occasion to specify that the document was unchanged, you can call this function with the argument FALSE.

At present, there are four occasions when you alter a sketch in the document object:

  • When you call the AddElement() member of CSketcherDoc to add a new element.
  • When you call the DeleteElement() member of CSketcherDoc

Get Ivor Horton's Beginning Visual C++ 2012 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.