Serializing the Document

Serializing a document occurs in two stages:

  1. The framework calls the document’s Serialize member function.

  2. That Serialize function calls the Serialize function of the stroke list.

In the following procedure, you’ll add code that implements serialization for Scribble’s documents.

To implement serialization for Scribble documents

Serialization uses an object of class CArchive to manage the connection to a disk file or other storage. A CArchive object, ar, is passed in as an argument.

A call to the archive object’s IsStoring member function determines whether this is a store or a load operation. If the archive is for storing (saving), the stroke-list object’s own Serialize member function is called to store the stroke’s data to disk. If the archive is for loading, its Serialize member function is called to load data from the disk file. This constructs new CStroke objects to fill the list. The stroke list for a document being read in from disk must already be empty.

Note that the stroke list already exists when Serialize reads data in. That’s because you declared it as an embedded object, like this:

CTypedPtrList <CObList, CStroke*> m_strokeList;

For an embedded object, as in Scribble, you call Serialize directly because you don’t want to create a second CTypedPtrList object (as you would if you used a pointer), and because you know the exact type of the object.