Serialization refers to the process of reading and writing an object from a fixed storage medium, such as a disk file. Serialization of an object requires three elements:
A CFile object that describes data files
A Carchive object that provides the serialization Context
A serializable object
Step 1: open a data file
Open the foo. dat file used to save serialization information in proper access mode. In this example, the file is opened in an exclusive read/write mode.
// Open file "foo. dat"
CFile * pFile = new CFile ();
ASSERT (pFile! = NULL );
If (! PFile-> Open ("foo. dat", CFile: modeReadWrite | CFile: writable exclusive )){
// Handle error
Return;
}
Step 2: hook up with the archive
Next, hook a CArchive object to a file. The archive object provides a valid connection with the fixed storage. Therefore, you can read and write data from the archive in sequence instead of Directly Reading and Writing data from the file. The archived object must know whether you need to read or write data through it. In the following example, we assume that data needs to be written.
// Create archive...
Bool bReading = false; //... for writing
CArchive * pArchive = NULL;
Try
{
PFile-> SeekToBegin ();
UINT uMode = (bReading? CArchive: load: CArchive: store );
PArchive = new CArchive (pFile, uMode );
ASSERT (pArchive! = NULL );
}
Catch (CException * pException)
{
// Handle error
Return;
}
Step 3: serialize the object
Finally, we call the Serialize () function to Serialize the object. The Serialize () function is a self-constructed function. It has nothing to do with the CObject: Serialize () function of MFC. Therefore, you do not need to inherit objects from the CObject class. Our Serialize () method needs to pass in a CArchive object pointer and return an integer representing the status.
Int CFoo: serialize
(CArchive * pArchive)
{
Int nStatus = SUCCESS;
// Serialize the object...
...
Return (nStatus );
}
We learned about the actual serialization process within one minute. Now let's take a look at the following points:
1. The Cfoo: Serialize () function is used to read data from and write data to the fixed storage.
2. Cfoo: The Serialize () function does not know how to access data files.
Assume that Cfoo represents an employee record that contains two data members.
Class CFoo
{
// Construction/destruction
Public:
CFoo: CFoo ();
Virtual CFoo ::~ CFoo ();
// Methods
Public:
Int serialize (CArchive * pArchive );
// Data members
Public:
CString m_strName; // employee name
Int m_nId; // employee id
};
We use the stream operations of CArchive <and> to read and write data from the archive respectively. CArchive knows how to serialize simple types, such as int, float, DWORD, and object types, such as CString. The Archive also knows whether it is a read or write mode. You can use the CArchive: IsStoring () function to query the read/write modes of an archive. The serialization method of the Cfoo class can be written as follows:
Int CFoo: serialize
(CArchive * pArchive)
{
Int nStatus = SUCCESS;
// Serialize the object...
ASSERT (pArchive! = NULL );
Try
{
If (pArchive-> IsStoring ()){
// Write employee name and id
(* PArchive) <m_strName;
(* PArchive) <m_nId;
}
Else {
// Read employee name and id
(* PArchive)> m_strName;
(* PArchive)> m_nId;
}
}
Catch (CException * pException)
{
NStatus = ERROR;
}
Return (nStatus );
}
Step 4: Clear
After serialization, we should close the archive and data files to clear them.
PArchive-> Close ();
Delete pArchive;
PFile-> Close ();
Delete pFile ();