CArchive in MFC (1)

Source: Internet
Author: User
Tags assert constructor throw exception

1. First put on this category:

Class CArchive
{
Protected
Enum Schemamapreservedrefs {objtypearrayref = 1};
Enum loadarrayobjtype{typeundefined = 0, Typecruntimeclass = 1, typecobject = 2};
Public
Flag values
Enum Mode {store = 0, load = 1, Bnoflushondelete = 2, Bnobyteswap = 4};


CArchive (cfile* pFile, UINT nmode, int nbufsize = 4096, void* lpbuf = NULL);
~carchive ();


Attributes
BOOL isloading () const;
BOOL isstoring () const;
BOOL isbyteswapping () const;
BOOL isbufferempty () const;


cfile* GetFile () const;
UINT GetObjectSchema (); Valid when reading a cobject*
void Setobjectschema (UINT nschema);


Pointer to document being serialized – must set to serialize
Coleclientitems in a document!
cdocument* m_pdocument;


Operations
UINT Read (void* lpbuf, UINT nMax);
void Ensureread (void *lpbuf, UINT ncount);
void Write (const void* lpbuf, UINT nMax);
void Flush ();
void Close ();
void Abort (); Close and shutdown without exceptions


Reading and writing strings
void WriteString (LPCTSTR lpsz);
LPTSTR ReadString (_OUT_Z_CAP_ (nmax+1) LPTSTR lpsz, _in_ UINT nMax);
BOOL ReadString (cstring& rString);


Public
Object I/O is pointer based to avoid added construction overhead.
Use the Serialize member function directly for embedded objects.
Friend carchive& AFXAPI operator<< (carchive& ar, const cobject* pOb);


Friend carchive& AFXAPI operator>> (carchive& ar, cobject*& pOb);
Friend carchive& AFXAPI operator>> (carchive& ar, const cobject*& pOb);


Insertion operations
carchive& operator<< (BYTE by);
carchive& operator<< (WORD W);
carchive& operator<< (LONG L);
carchive& operator<< (DWORD DW);
carchive& operator<< (float f);
carchive& operator<< (double D);
carchive& operator<< (Longlong DWDW);
carchive& operator<< (Ulonglong DWDW);


carchive& operator<< (int i);
carchive& operator<< (short w);
carchive& operator<< (char ch);
#ifdef _native_wchar_t_defined
carchive& operator<< (wchar_t ch);
#endif
carchive& operator<< (unsigned u);


Template < typename BaseType, BOOL T_bmfcdll>
carchive& operator<< (const atl::csimplestringt<basetype, t_bmfcdll>& str);


template< TypeName BaseType, Class Stringtraits >
carchive& operator<< (const atl::cstringt<basetype, stringtraits>& str);

Template < typename BaseType, BOOL T_bmfcdll>
carchive& operator>> (Atl::csimplestringt<basetype, t_bmfcdll>& str);


template< TypeName BaseType, Class Stringtraits >
carchive& operator>> (Atl::cstringt<basetype, stringtraits>& str);


carchive& operator<< (bool b);


Extraction operations
carchive& operator>> (byte& by);
carchive& operator>> (word& W);
carchive& operator>> (dword& DW);
carchive& operator>> (long& L);
carchive& operator>> (float& f);
carchive& operator>> (double& D);
carchive& operator>> (longlong& DWDW);
carchive& operator>> (ulonglong& DWDW);


carchive& operator>> (int& i);
carchive& operator>> (short& W);
carchive& operator>> (char& ch);
#ifdef _native_wchar_t_defined
carchive& operator>> (wchar_t& ch);
#endif
carchive& operator>> (unsigned& u);
carchive& operator>> (bool& b);


Object Read/write
cobject* readobject (const cruntimeclass* pClass);
void WriteObject (const cobject* pOb);
Advanced Object Mapping (used for forced references)
void MapObject (const cobject* pOb);


Advanced Versioning Support
void WriteClass (const cruntimeclass* pclassref);
cruntimeclass* readclass (const cruntimeclass* pclassrefrequested = NULL,
uint* Pschema = null, dword* pobtag = null);
void SerializeClass (const cruntimeclass* pclassref);


Advanced operations (used when storing/loading many objects)
void Setstoreparams (UINT nhashsize = 2053, uint nblocksize = 128);
void Setloadparams (UINT ngrowby = 1024);


void Ensureschemamapexists (Carray<loadarrayobjtype, const loadarrayobjtype&>** ppobjtypearray = NULL);
Implementation
Public
BOOL M_bforceflat; For COleClientItem implementation (default TRUE)
BOOL M_bdirectbuffer; TRUE if M_pfile supports direct buffering
BOOL m_bblocking; TRUE if M_pfile can block for unbounded periods of time
void Fillbuffer (UINT nbytesneeded);
void Checkcount (); Throw exception if M_nmapcount is too large


Special functions for reading and writing (16-bit compatible) counts
Dword_ptr Readcount ();
void Writecount (Dword_ptr dwcount);


Public for advanced use
UINT M_nobjectschema;
CString M_strfilename;


Protected
Archive objects cannot be copied or assigned
CArchive (const carchive& ARSRC);
void operator= (const carchive& ARSRC);


BOOL M_nmode;
BOOL M_buserbuf;
int m_nbufsize;
Cfile* M_pfile;
Byte* m_lpbufcur;
Byte* M_lpbufmax;
Byte* M_lpbufstart;


Array/map for cobject* and cruntimeclass* Load/store
UINT M_nmapcount;
Union
{
cptrarray* M_ploadarray;
cmapptrtoptr* m_pStoreMap;
};
Map to keep track of mismatched schemas
cmapptrtoptr* M_pschemamap;


Advanced parameters (controls performance with large archives)
UINT m_ngrowsize;
UINT m_nhashsize;
};

The constructor for this class has two default parameters: CArchive (cfile* pFile, UINT nmode, int nbufsize = 4096, void* lpbuf = NULL);
M_nmode indicate read-write mode
BOOL M_bdirectbuffer; Indicates whether the direct read/write

The buffer pointer byte* M_lpbufstart, pointing to the buffer, which is likely to be provided by the underlying cfile (such as a derived class CMemFile) object, but is generally built by CArchive itself.
Buffer tail pointer byte* M_lpbufmax;
Buffer current position pointer byte* m_lpbufcur;
When initializing, if it is read mode, the current position is at the tail, if it is write mode, the current position is in the header: M_lpbufcur = (isloading ())? M_lpbufmax:m_lpbufstart;

where _afx_inline BOOL carchive::isloading () const
{return (M_nmode & carchive::load)! = 0;} To determine whether it is an input or an output


Here's the source code for the constructor:

Carchive::carchive (cfile* pFile, UINT nmode, int nbufsize, void* lpbuf)
{
Assert_valid (PFile);
if (PFile = = NULL)
{
Afxthrowinvalidargexception ();
}

M_strfilename = Pfile->getfilepath ();


Initialize members not dependent on allocated buffer
M_nmode = Nmode;
M_pfile = PFile;
M_pschemamap = NULL;
M_ploadarray = NULL;
M_pdocument = NULL;
M_bforceflat = TRUE;
M_nobjectschema = (UINT)-1; Start with invalid schema
if (IsStoring ())
M_ngrowsize = nblocksize;
Else
M_ngrowsize = ngrowsize;
M_nhashsize = nhashsize;


Initialize the buffer. Minimum size is 128
M_lpbufstart = (byte*) lpbuf;
M_buserbuf = TRUE;
M_bdirectbuffer = FALSE;
m_bblocking = M_pfile->getbufferptr (Cfile::buffercheck) &CFile::bufferBlocking;


if (Nbufsize < nbufsizemin)
{
Force use of private buffer of minimum size
M_nbufsize = Nbufsizemin;
M_lpbufstart = NULL;
}
Else
M_nbufsize = nbufsize;


Nbufsize = m_nbufsize;
if (M_lpbufstart = = NULL)
{
Check for CFile providing buffering support
M_bdirectbuffer = M_pfile->getbufferptr (Cfile::buffercheck) &CFile::bufferDirect;
if (!m_bdirectbuffer)
{
No support for direct buffering, allocate new buffer
M_lpbufstart = new Byte[m_nbufsize];
M_buserbuf = FALSE;
}
Else
{
cfile* supports direct buffering!
nbufsize = 0; Would trigger initial Fillbuffer
}
}


if (!m_bdirectbuffer)
{
ASSERT (M_lpbufstart! = NULL);
ASSERT (AfxIsValidAddress (M_lpbufstart, Nbufsize, IsStoring ()));
}
M_lpbufmax = M_lpbufstart + nbufsize;
M_lpbufcur = (isloading ())? M_lpbufmax:m_lpbufstart;


ASSERT (m_pStoreMap = = NULL); Same as M_ploadarray
}


Where Getbufferptr is a function in the CFile class CFile the class itself does not have a buffer, so directly return 0 but its subclasses cmemfile buffer data members CArchive can directly use the M_bdirectbuffer directly read and write variables to mark the above two cases.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.