In the first two parts, we learned how to provide strong support for serialization in general. In this section, we will learn special rules for serializing any object. Here are four common reference examples. Each example is composed of the first one.
Serialize a simple class
Serialize a derived class
Serialize a same-source aggregation class
Serialize a heterogeneous aggregation class
Our Searialize () method returns one of the following status codes:
L Success
L InvalidFormat
L UnsupportedVersion
L ReadError
L WriteError
Serialize a simple class
A "simple class" is defined as an object with no parent class or clustering class. To serialize a simple class, follow these steps:
1. serialize the object signature and version
2. serialize object members (if any)
In the following example, a Point class contains two int-type variables, indicating the coordinate of a Point. The object signature and version are defined as static members (m_strSignature and m_nVersion), so each Point class instance shares them.
Int Point: serialize
(CArchive * pArchive)
{
ASSERT (pArchive! = NULL );
// Step 1: Serialize signature and version
Int nVersion;
Try {
If (pArchive-> IsStoring ()){
(* PArchive) <Point: m_strSignature;
(* PArchive) <Point: m_nVersion;
} Else {
CString strSignature;
(* PArchive)> strSignature;
If (strSignature! = Point: m_strSignature)
Return (Status: InvalidFormat );
(* PArchive)> nVersion;
If (nVersion> Point: m_nVersion ;)
Return (Status: UnsupportedVersion );
}
// Step 2: Serialize members
If (pArchive-> IsStoring ()){
(* PArchive) <m_nX;
(* PArchive) <m_nY;
} Else {
(* PArchive)> m_nX;
(* PArchive)> m_nY;
}
}
Catch (CException * pException ){
// A read/write error occured
PException-> Delete ();
If (pArchive-> IsStoring ())
Return (Status: WriteError );
Return (Status: ReadError );
}
// Object was successfully serialized
Return (Status: Success );
}
Serialize a derived class
A derived class is a class that is assigned to a simple class and not a clustering class. To serialize a derived class, follow these steps:
1. serialize the object signature and version
2. serialize the base class of the object <additional steps
3. serialize the member of the object (if any)
In the following example, the ColoredPoint class derives from the Point class and adds an int variable called m_nColor to indicate the color of the vertex. Like all serialized classes, the ColoredPoint class also defines a static signature and version.
Int ColoredPoint: serialize
(CArchive * pArchive)
{
ASSERT (pArchive! = NULL );
// Step 1: Serialize signature and version
Int nVersion;
Try {
If (pArchive-> IsStoring ()){
(* PArchive) <ColoredPoint: m_strSignature;
(* PArchive) <ColoredPoint: m_nVersion;
} Else {
CString strSignature;
(* PArchive)> strSignature;
If (strSignature! = ColoredPoint: m_strSignature)
Return (Status: InvalidFormat );
(* PArchive)> nVersion;
If (nVersion> ColoredPoint: m_nVersion ;)
Return (Status: UnsupportedVersion );
}
// Step 2: Serialize the base class
Int nStatus = Point: serialize (pArchive );
If (nStatus! = Status: Success)
Return (nStatus );
// Step 3: Serialize members
If (pArchive-> IsStoring ())
(* PArchive) <m_nColor;
Else
(* PArchive)> m_nColor;
}
Catch (CException * pException ){
// A read/write error occured
PException-> Delete ();
If (pArchive-> IsStoring ())
Return (Status: WriteError );
Return (Status: ReadError );
}
// Object was successfully serialized
Return (Status: Success );
}
Serialize a same-source aggregation class
The same-source clustering class is often used to store a certain number of objects of the same type. To serialize a same-source clustering class, follow these steps:
1. serialize the object signature and version
2. serialize the base class of the object (if any)
3. serialize the number of clustered objects <additional steps
4. serialize each clustered object <additional steps
5. serialize other members of an object (if any)
In the following example, ColoredPointList is the aggregation of A ColoredPoint object. For simplicity, the ColoredPointList class uses CPtrArray to store objects. Like all serialization classes, the ColoredPointList class also defines a static signature and version. The following is an example of the ColoredPointList class:
Class ColoredPointList
{
// Construction/destruction
Public:
Col