Application persistence is the act of saving information about the application between program sessions. MFC provides support for two forms of persistence: serialization and application-state persistence.
Serialization is a mechanism for preserving application data and consists of streaming data out to a data file. MFC provides much of the structure for implementing serialization through its application framework. The developer completes the serialization structure by implementing application-specific code to serialize the data that is to be preserved.
Application-state persistence saves information about the user's session. The state information that is saved depends upon the application and can include such information as the application window's size and position on the desktop and any operating modes that the user last selected, as well as an application's option settings. While serialization saves data to a data file, state persistence uses the operating system's registry to store its data.
This chapter discusses both forms of persistence: implementing document serialization through the use of data files, and preserving application state information through the use of the operating system's registry.
Objectives
After completing this chapter, you will be able to:
® Define serialization and how it is integrated in MFC.
® Describe how MFC supports persistent storage.
® Create a serializable class.
® Create and use serializable objects.
® Describe the registry.
® View and modify the registry.
® Programmatically update the registry.
Serialization is the process of storing data from the application to a data file or loading data from a data file to the application. Serialization is integrated within the MFC's document/view architecture and can occur as a result of the user's explicitly saving or loading a data file. Serialization can also occur as a side effect of modifying the document data. In this case, the MFC application framework will prompt the user to save the data before deleting the data from the document.
The approach to serialization within MFC is that the document object begins the serialization of data. The document object either serializes data directly, or it delegates the task of serialization to the data objects contained within the document. The objects, in turn, implement the functionality to save their data members. Later, these objects can restore their data members by reading, or "deserializing," the data from persistent storage. A key point is that the objects themselves are responsible for reading and writing their own state. For a class to be persistent, it must implement the basic serialization operations.
In the document/view architecture the serialization process starts at the developer's CDocument-derived class where AppWizard has generated a Serialize function. From the Serialize function, the developer creates application-specific code that transfers the data. The framework passes in a reference to an archive object when the Serialize function is called. The archive object acts as an intermediate object between the document and the data file. The document's Serialize function uses the archive object directly or, for more complicated document objects, the document will pass the archive object to the document's data objects, which will then serialize themselves.
To see an illustration of the serialization process, click this icon.
Serialization in MFC depends upon a number of classes in your application's framework. The following table lists the primary classes and their purpose.
Class Purpose
CObject Serves as a base class for objects that are to be serialized. The member function CObject::Serialize is overridden by the developer to implement serialization for the data object.
CDocument Contains the information (data and objects) to be serialized. The member function CDocument::Serialize specifies what portion of this information is to be serialized.
CArchive Provides a context for serialization. CArchive handles process-dependent factors, such as media access and buffering. During construction, a CFile object is "attached" to the archive. A single CArchive object can be used for either storing or loading data, but not for both.
CFile Represents the file on a storage device, such as a hard disk. CFile directly provides unbuffered, binary disk input/output services, and it indirectly supports text files and memory files through its derived classes.
This section explains the default behavior for serialization that is provided by the framework. It also reviews the process of declaring, implementing, and using a new user-defined serializable class.
This section includes the following topics:
The FileNew, FileOpen, and FileSave commands are three common ways to explicitly initiate serialization. This topic describes the MFC classes and functions available to help you implement your serialization code, including the differences between SDI and MDI applications during the serialization operation.
Serialization and MFC
Important points to remember about serialization support as implemented by MFC are:
® Actual data serialization begins with the CDocument::Serialize function.
® All objects are transferred in totality — that is, partial serialization is not allowed.
® Objects are loaded in the same order that they were saved.
® Synchronize documents by using a schema number.
No comments:
Post a Comment