Serialization

Serialization is the process of reading or writing an object to or from a stream. This stream can be a storage medium like a disk file, stream or socket file. Serialization is necessary for situations where it is desired to maintain the state of an object (object of C++ classes or structures). Program can store the data members of the object in file and exit if needed. Later it can load the members in the same way it was saved and thus maintain the same state of the object as before. Serialization is not limited to disk file. File stream can be in the form of network socket or byte stream for serial line or in any other form.

Need of Serialization

An object with members of primitive types such as char, short, int, float, or double can be stored with easy file operations. Members are saved serially one by one and can be retrieved in the same sequence. Saving an C++/MFC object is some what different from this traditional structured approache. MFC class object can contain another objects like CString, CArray. These objects are of variable lenghts. Again we need to takecare both save and restore of parent class before do the same for current class. MFC defines an uniform way to save and restore any object to or from file stream and this support is there from top level CObject in the MFC hiararchy. Any object that supports Serialization should call the parent class Serialization routine before processing its own members. Thus the entire hiararchy state members of the object are saved and restored. MFC also defines uniform ways to CString, CArray and other variable length objects.

Serialization implementation

It starts with the CObject class that is the ancestor to most MFC classes, which is equipped with a Serialize() member function. The class should declare with the macros DECLARE_SERIAL(< class name >) and also should add implement macro IMPLEMENT_SERIAL(< class name >, < parent class name >, < Schema version >). The class should implement Serialize(CArchive&) routine and it should save and restore class members in this routine. Let us look into a simple example by creating a new MFC project. Here we declared a class called CStudent to store and retrieve fields of a student. It contains standard, enrollment number, name, marks and age. This demo application is based on a dialog box which retrieves these files when "Open" button is pressed. It reads and shows these fields. User can enter new values and save those with "Save" button.

Source Code

/* ---CStudent class in Student.h--- */
class CStudent : public CObject
{
public :
  CStudent(void);
  ~CStudent(void);


  WORD    m_wStd;
  WORD    m_wRoll;
  CString m_sName;
  double  m_dMarks;
  WORD    m_wAge;

public :
  void Serialize(CArchive& ar);

  DECLARE_SERIAL(CStudent);
};

/* ---CStudent class in Student.cpp--- */

IMPLEMENT_SERIAL(CStudent, CObject, 0)
CStudent::CStudent(void)
{
}
CStudent::~CStudent(void)
{
}
void CStudent::Serialize(CArchive& ar)
{
  CObject::Serialize(ar);
  if( ar.IsStoring() ) {
    ar << m_wStd << m_wRoll << m_sName << m_dMarks << m_wAge;
  } else {
    ar >> m_wStd >> m_wRoll >> m_sName >> m_dMarks >> m_wAge;
  }
}
/* ---CStdDialogDlg in StdDialogDlg.h--- */

class CStudentDlg : public CDialog
{
public :
  /* standard constructor */
  CStudentDlg(CWnd* pParent = NULL);

  enum { IDD = IDD_STDDIALOG_DIALOG };

protected :
  CStudent m_Student;
  /* DDX/DDV support */
  virtual void DoDataExchange(CDataExchange* pDX);
  virtual BOOL OnInitDialog();
  afx_msg void OnOpen();
  afx_msg void OnSave();

  DECLARE_MESSAGE_MAP()
};
/* ---CStdDialogDlg in StudentDlg.cpp--- */
CStudentDlg::CStudentDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CStudentDlg::IDD, pParent)
{

  /* Set default initial values */
  m_Student.m_sName = "";
  m_Student.m_wStd = 1;
  m_Student.m_wRoll = 1;
  m_Student.m_dMarks = 100;
  m_Student.m_wAge = 5;
}
void CStudentDlg::DoDataExchange(CDataExchange* pDX)
{
  CDialog::DoDataExchange(pDX);
  DDX_Text(pDX, IDC_NAME, m_Student.m_sName );
  DDX_Text(pDX, IDC_STD, (short&)m_Student.m_wStd );
  DDX_Text(pDX, IDC_ROLL, (short&)m_Student.m_wRoll );
  DDX_Text(pDX, IDC_MARKS, m_Student.m_dMarks );
  DDX_Text(pDX, IDC_AGE, (short&)m_Student.m_wAge );
}
BEGIN_MESSAGE_MAP(CStudentDlg, CDialog)
  ON_COMMAND(ID_SAVE, OnSave)
  ON_COMMAND(ID_OPEN, OnOpen)
END_MESSAGE_MAP()

BOOL CStudentDlg::OnInitDialog()
{
  CDialog::OnInitDialog();
  return TRUE;
}
void CStudentDlg::OnSave() 
{
  UpdateData(TRUE);
  CFile file;
  file.Open("student.dat", CFile::modeCreate | CFile::modeWrite);
  CArchive ar(&file, CArchive::store);
  m_Student.Serialize(ar);
  ar.Close();
  file.Close();
}
void CStudentDlg::OnOpen() 
{
  CFile file;
  file.Open("student.dat",  CFile::modeRead);
  CArchive ar(&file, CArchive::load);
  m_Student.Serialize(ar);
  ar.Close();
  file.Close();
  UpdateData(FALSE);
}
/* ---CStdDialogApp in StdDialogApp.h--- */

class CStdDialogApp : public CWinApp
{
public :
  CStdDialogApp();
  virtual BOOL InitInstance();
};
/* ---CStdDialogApp in StdDialogApp.cpp--- */

/* CStdDialogApp construction */

CStdDialogApp::CStdDialogApp()
{
}
/* CStdDialogApp app object */
CStdDialogApp theApp;
/* CStdDialogApp initialization */
BOOL CStdDialogApp::InitInstance()
{
  CStudentDlg dlg;
  m_pMainWnd = &dlg;
  int nResponse = dlg.DoModal();
  if (nResponse == IDOK)
  {
  }
  else if (nResponse == IDCANCEL)
  {
  }
  return FALSE;
}

Output

Student records serialization demo app

About our authors: Team EQA

Further readings

Where is WinMain() function in MFC application ?

MFC hides WinMain in its framework and includes source file on WinMain(). This explains how framework calls global CWinApp::Initinstance() from entry WinMain.

What is the utility of CWinApp class?

This is constructed during global C++ objects are constructed and is already available when Windows calls the WinMain function, which is supplied by the ...

Basic steps in Win32 GUI Application with source code.

Define a custom Window class structure, Register the class name, CreateWindow, Show windows and write message get and dispatch loop statements. Define the Window CallBack procedure and write the handlers.

What is a Window CallBack procedure and what is its utility?

DispatchMessage() is a API which indirectly triggers the Window CallBack procedure. Message structure members from this function are passed to the CallBack procedure. CallBack procedure should implement event handlers depending on the need of the application.

What are LPARAM and WPARAM in window proc function?

LPARAM and WPARAM are the two parameters in Window CallBack procedure. They signifies parameters of various events. They are used in handing individual events.

What are the basic steps of a typical MFC based application?

We need to write WinMain and need to follow all these in a Win32 application. However we need not to write much if we are writing an application with MFC ...

#