|
One of the most regular ways you will use the CArchive
class consists of storing or retrieving values through their Serialize()
method. To do this directly and easily in the class, the CArchive
class overloads two operations: << and >>.
To store one or more values, the CArchive class
provides the << operator. The values you can store are of MFC, BYTE,
short, WORD, int, unsigned int, long, LONG,
DWORD, float, or double types. The class also
provides an exception handler for each kind. Therefore, the syntaxes for
this operator are:
friend CArchive& operator <<( CArchive& ar, const CObject* pOb );
throw( CArchiveException, CFileException );
|
CArchive& operator <<( BYTE by );
throw( CArchiveException, CFileException );
|
CArchive& operator <<( WORD w );
throw( CArchiveException, CFileException );
|
CArchive& operator <<( int i );
throw( CArchiveException, CFileException );
|
CArchive& operator <<( LONG l );
throw( CArchiveException, CFileException );
|
CArchive& operator <<( DWORD dw );
throw( CArchiveException, CFileException );
|
CArchive& operator <<( float f );
throw( CArchiveException, CFileException );
|
CArchive& operator <<( double d );
throw( CArchiveException, CFileException );
|
As you may have realized, there is only one version of
the Serialize() method. That is, this member function is called once for
both storing and retrieving. Therefore, when using it, you must specify
what operation you want to perform. This checking process can be performed
by calling the CArchive::IsStoring() Boolean method. Its syntax is:
BOOL IsStoring() const;
This method returns TRUE if you are storing data. If
the answer is then TRUE, you can use the << operator to store each
necessary value.
As opposed to storing value(s), to retrieve a value,
the CArchive class provides the >> operator. It comes in the
following syntaxes:
friend CArchive& operator >>( CArchive& ar, CObject *& pOb );
throw( CArchiveException, CFileException, CMemoryException );
|
friend CArchive& operator >>( CArchive& ar, const CObject *& pOb );
throw( CArchiveException, CFileException, CMemoryException );
|
CArchive& operator >>( BYTE& by );
throw( CArchiveException, CFileException );
|
CArchive& operator >>( WORD& w );
throw( CArchiveException, CFileException );
|
CArchive& operator >>( int& i );
throw( CArchiveException, CFileException );
|
CArchive& operator >>( LONG& l );
throw( CArchiveException, CFileException );
|
CArchive& operator >>( DWORD& dw );
throw( CArchiveException, CFileException );
|
CArchive& operator >>( float& f );
throw( CArchiveException, CFileException );
|
CArchive& operator >>( double& d );
throw( CArchiveException, CFileException );
|
This indicates that you can retrieve an MFC object, a BYTE,
a short, a WORD, an int, an unsigned int, a long,
a LONG, a DWORD, a float, or a double value.
As mentioned above, the Serialize() method is used for
both operations. Therefore, when using this method, you should first check
what operation is being performed. This can be done by calling the
IsLoading() method. Its syntax is:
BOOL IsLoading() const;
This method returns TRUE if you are retrieving a value
or some values. If it is true, you can then use the >> operator to
retrieve the value(s). |