Total members 11895 |It is currently Sun Dec 22, 2024 11:12 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Coding serialization concepts in C++, 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:


  • A CFile object representing the datafile
  • A CArchive object that provides the serialization context
  • The object being serialized

Serialization data flow
Step 1 - Open the datafile
To serialize an object to the file "foo.dat", open the file with the appropriate access mode. In this example, the file is opened for exclusive read/write access.

Attachment:
serialization1_flow.gif
serialization1_flow.gif [ 1.25 KiB | Viewed 5459 times ]

cpp code
// Open file "foo.dat"
CFile* pFile = new CFile();
ASSERT (pFile != NULL);
if (!pFile->Open ("foo.dat", CFile::modeReadWrite | CFile::shareExclusive)) {
// Handle error
return;
}


Step 2 - Hook up the archive
Next, a 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.


cpp code
// 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 serialize the object by calling its 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.

cpp code
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:
  • The same method CFoo::serialize() is used to read/write the object from/to persistent storage.
  • CFoo::serialize() doesn't know anything about the datafile bring accessed.
Assume CFoo represents an employee record that contains a couple of data members.

cpp code
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:
cpp code
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 - Clean up
When you've finished serializing, you should clean up by closing the archive and datafile.

cpp code
pArchive->Close();
delete pArchive;
pFile->Close();
delete pFile();

Conclusion
Well, there you have it - serialization in a (very small) nutshell. In Part 2, we'll see how to gracefully handle reading invalid data stores and support different versions of our object. In Part 3, we'll see how to serialize complex objects.

Continue Reading :
c-c/serialization-implementation-in-c-part-2-t2772.html
c-c/how-to-implement-serialization-using-c-part-3-t2774.html



_________________
Please recommend my post if you found it helpful. ,
java,j2ee,ccna ,ccnp certified .


Author:
Expert
User avatar Posts: 838
Have thanks: 2 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Coding serialization concepts in C++
 serialization implementation in C++ - Part 2     -  
 How to implement serialization using C++ - Part 3     -  
 Free practice tests on SAP and SCM Concepts     -  
 I am new in PHP, want coding or PHP file     -  
 Java Coding     -  
 servlet coding     -  
 Standard Huffman Coding     -  
 Huffman coding for image compression     -  
 source code of huffman coding ?     -  
 coding a simple packet sniffer     -  



Topic Tags

C++ Files and I/O
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com