![]() |
Languages »
C / C++ Language »
Beginners
Beginner
License: The Code Project Open License (CPOL)
A serialization primer - Part 1By Ravi BhavnaniThis tutorial describes how to easily serialize a simple object. |
VC6, VC7Win2K, WinXP, Visual Studio, MFC, Dev
|
|
Advanced Search |
|
|
|
||||||||||||||||
This article is the first of a 3 part tutorial on serialization.
Serialization is the process of writing or reading an object to or from a persistent storage medium, such as a disk file. Serializing an object requires 3 ingredients:
CFile object representing the datafile CArchive object that provides the serialization context
// Open file "foo.dat" CFile* pFile = new CFile(); ASSERT (pFile != NULL); if (!pFile->Open ("foo.dat", CFile::modeReadWrite | CFile::shareExclusive)) { // Handle error return; }
CArchive object is hooked up to the file. The archive provides an efficient conduit to persistent storage. Instead of directly reading and writing the file, you serialize data to and from the archive. The archive needs to know if you're going to be using it to read or write data. In this example, we'll assume we're writing 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; }
serialize() method. serialize() is just a method we made up. It has nothing to with MFC's CObject::Serialize(). Also, you don't have to derive your object from CObject. Our serialize() method takes a pointer to a CArchive and returns an integer status.
int CFoo::serialize (CArchive* pArchive) { int nStatus = SUCCESS; // Serialize the object ... ... return (nStatus); }We'll get to the actual serialization process in a minute. Meanwhile, let's recognize a couple of important points:
CFoo::serialize() is used to read/write the object from/to persistent storage. CFoo::serialize() doesn't know anything about the datafile bring accessed. CFoo represents an employee record that contains a couple of 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
CArchive's streaming operators << and >> to read/write the data members from/to the archive. CArchive knows how to serialize simple data types like int, float, DWORD, and objects like CString. The archive also knows whether it's in read or write mode. You can query its mode by calling CArchive::IsStoring(). CFoo's serialization method can then be written as:
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); }
pArchive->Close(); delete pArchive; pFile->Close(); delete pFile();
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 25 Nov 2002 Editor: Sean Ewington |
Copyright 2002 by Ravi Bhavnani Everything else Copyright © CodeProject, 1999-2009 Web15 | Advertise on the Code Project |