Serialization preliminary (translation)
Original: http://www.codeproject.com/cpp/serialization_primer1.asp Serialization is a process of reading and writing an object from a fixed storage medium, such as a disk file. Serialized an object requires three elements: Ø a CFILE object that describes the data file
Ø A CARCHIVE object that provides serialization context
Ø A object that can be serialized
Step 1: Open the data file
Open the foo.dat file for saving serialization information with an appropriate access. In this example, the file will open in an exclusive readable write.
// Open file "foo.dat"
CFILE * PFILE = New cfile ();
AskERT (Pfile! = NULL);
IF (! pfile-> open ("foo.dat", cfile :: moderadwrite | cfile :: shareExclusive)) {
// Handle Error
Return;
}
Step 2: And archive hook
Next, hook a CARCHIVE object and file. The archive object provides a valid connection between fixed storage. So you can replace directly from the file by reading and writing data from the archive. The archive object must know if you need to read or write data by it. In the following example, we assume that it is necessary to write data.
// 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: Serialized object
Finally, we call the serialize () function serialized object. The serialize () function is the function we constructed by our own, there is no relationship with the COBject :: serialize () function of the MFC. So you don't need to inherit the object from the COBJECT class. Our serialize () method needs to pass a CARCHIVE object pointer and return a representation of an integer.
Int cfoo :: serialize
(CARCHIVE * PARCHIVE)
{
INT NSTATUS = SUCCESS;
// Serialize the Object ...
...
Return (NSTATUS);
}
We understand the actual serialization process within a minute. Now let us know a pair of points:
1. Write data from the fixed storage and write data to the fixed storage use the cfoo :: serialize () function.
2. Cfoo :: serialize () function does not know any access to data files.
Suppose Cfoo represents an employee record containing 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 read and write data from the archive with CARCHIVE's flow operation << and >>. CARCHIVE knows how to serialize simple types, such as int, float, dword, and object types, such as CString. The archive also knows that it is reading or writing mode. You can query the archived read and write mode through the carchive :: isstoring () function. The serialization method of the CFOO class can be written:
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 {
// r rEee name and id
(* PARCHIVE) >> M_STRNAME;
(* PARCHIVE) >> M_NID;
}
}
Catch (CEXCEPTION * PEXCEPTION)
{
NSTATUS = Error;
}
Return (NSTATUS);
}
Step 4: Clear
When we complete serialization, we should turn off the archive and data files to clear.
Parchive-> close ();
Delete parchive;
Pfile-> close ();
Delete pfile ();