Featured

    Featured Posts

Serialization

Objects can be persistent which means they can be saved on disk when a program exits and then can be restored, when program is restarted.  This process of saving and restoring objects is called serialization.  In the MFC(Microsoft Foundation Classes) library, designated classes have a member function named serialize.  When the application framework calls serialize for a particular object, for example - object of class cstudent - the data for the student is either saved on disk or read from disk.

         In the MFC library, serialization is not a substitute for a database management system.  All the objects associated with a document are sequentially read from or written to a single disk file.  If you need database capacity in your application consider using the Microsoft open database connectivity(ODBC) software or data Data Access Object(DAO).

# Disk files and Archives :->

       How do you know whether serialize should read or write data ?  How serialize connected to a disk file.

The Serialization  Process :->  With the MFC library, the objects of class CFile represents disk file.  A CFile objects encapsulates the binary file handle, that you get through the win32 function create file.  The application framework uses this file handle for win32 Read file, write file, and set file pointer calls.
      The CArchive object buffers data for the C File object, and it maintains an internal flag that indicates whether the archive is strong (writing to disk) or loading (reading from disk).  Only one archive is associated with a file at any one time.  The application framework takes care of constructing the CFile and CArchive objects.  The application frame work calls the document serialize function during the file open and file save processes.

Make a class serializable :->  A serializable class must be derived directly or indirectly from Cobject.  In addition(with some acceptance), the class declaration must contains the DECLARE_SERIAL macro call.


# Serializing a class :->
        To make a class serializable, we first make sure that it is derived from the MFC Cobject class :
class CData : public Cobject
{
Private :
Cstring data ;
DECLARE_SERIAL(CData) ;
Public :
CData() {
data = Cstring(" ") ; }
void AddText( Cstring.text)
{
   data += text ; }
void DrawText ( CDC*pDC)
{ pDC -> Textout ( 0,0,data) ; }
void clearText () { data = " " ; }
void serialize ( CArchive & archive ) ;
};
# include " stdaFx.h "
# include " SerializeDoc.h"
void CData :: Serialize ( CArchive &archive)
{
Cobject :: Serialize( archive )
If (archive Isstring() )
{
archive.Isstring()
{
 archive << data;
}
else
     archive >> data ;
}
}
void CserializeDoc : : Serialize ( CArchive &ar )
{
 Dataobject.Serialize(ar) ; }

Post a Comment

www.CodeNirvana.in

www.posthatke.com. Powered by Blogger.

Google+ Followers

Copyright © www.posthatke.com | Blogger Templates | Designed By posthatke.com