分享
 
 
 

Archive class for WTL

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

http://www.codeproject.com/wtl/ArchiveclassWTL.asp

Archive class for WTL

By Igor Vigdorchik

Download demo project - 14.4 Kb Introduction WTL (Windows Template Library) is another library from Microsoft that we can use to create Windows applications. WTL was created by a few Microsoft developers as an extension to ATL and was designed to make a GUI creation a much simpler process. WTL is based on C++ templates and allows developers to create small in size and fast applications. But, unfortunately, since WTL is not supported by Microsoft and it is being developed by just a few programmers it is missing some nice features long existing in MFC.

One of such features is a serialization support that is provided by a CArchive class in MFC. To remedy this I decided to port CArchive class to WTL to simplify the serialization in WTL and non-MFC applications. Thus CXArchive class was born! CXArchive declarationclass CXArchive { public: // Flag values enum Mode { store = 0, load = 1, bNoFlushOnDelete = 2 }; CXArchive(CXFile* pFile, UINT nMode, int nBufSize = 4096, void* lpBuf = NULL); ~CXArchive(); // These functions access the mode to determine the state // of the CXArchive object BOOL IsLoading() const; BOOL IsStoring() const; CXFile* GetFile() const; // Operations UINT Read(void* lpBuf, UINT nMax); 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(LPTSTR lpsz, UINT nMax) throw(CXArchiveException); BOOL ReadString(CString& rString); // Insertion operations CXArchive& operator<<(BYTE by); CXArchive& operator<<(WORD w); CXArchive& operator<<(LONG l); CXArchive& operator<<(DWORD dw); CXArchive& operator<<(float f); CXArchive& operator<<(double d); CXArchive& operator<<(LONGLONG dwdw); CXArchive& operator<<(ULONGLONG dwdw); CXArchive& operator<<(int i); CXArchive& operator<<(short w); CXArchive& operator<<(char ch); #ifdef _NATIVE_WCHAR_T_DEFINED CXArchive& operator<<(wchar_t ch); #endif CXArchive& operator<<(unsigned u); CXArchive& operator<<(bool b); // Extraction operations CXArchive& operator>>(BYTE& by); CXArchive& operator>>(WORD& w); CXArchive& operator>>(DWORD& dw); CXArchive& operator>>(LONG& l); CXArchive& operator>>(float& f); CXArchive& operator>>(double& d); CXArchive& operator>>(LONGLONG& dwdw); CXArchive& operator>>(ULONGLONG& dwdw); CXArchive& operator>>(int& i); CXArchive& operator>>(short& w); CXArchive& operator>>(char& ch); #ifdef _NATIVE_WCHAR_T_DEFINED CXArchive& operator>>(wchar_t& ch); #endif CXArchive& operator>>(unsigned& u); CXArchive& operator>>(bool& b); protected: // Archive objects can not be copied or assigned CXArchive(const CXArchive& arSrc) {} void operator=(const CXArchive& arSrc) {} void FillBuffer(UINT nBytesNeeded) throw(CXArchiveException); CXFile * m_pFile; string m_strFileName; BOOL m_bDirectBuffer; BOOL m_bBlocking; BOOL m_nMode; BOOL m_bUserBuf; int m_nBufSize; BYTE * m_lpBufCur; BYTE * m_lpBufMax; BYTE * m_lpBufStart; };

CXArchive description Internally CXArchive is using CXFile class to write and read data to and from a file. (CXFile is another class developed by me and it is basically a wrapper class for Windows file APIs.) Class CXArchive is tied to a CXFile class pointer and implements buffering for better performance. To understand how CXArchive is working we'll take a look at the implementation of the insertion and extraction operators for LONG. Insertion operator:

CXArchive& CXArchive::operator<<(LONGl) { if (m_lpBufCur + sizeof(LONG) > m_lpBufMax) Flush(); *(UNALIGNED LONG*)m_lpBufCur = l; m_lpBufCur += sizeof(LONG); return *this; }

The insertion operator checks to see if it needs to flush the internal CXArchive buffer, places the LONG in the buffer and increments the buffer pointer. It returns a CXArchive reference to allow the insertion operators to be cascaded. Extraction operator:

CXArchive& CXArchive::operator>>(LONG&l) { if (m_lpBufCur + sizeof(LONG) > m_lpBufMax) FillBuffer((UINT)(sizeof(LONG) - (m_lpBufMax - m_lpBufCur))); l = *(UNALIGNED LONG*)m_lpBufCur; m_lpBufCur += sizeof(LONG); return *this; }

The extraction operator checks to see if it needs to get more data from the file into the internal buffer, then it places a LONG worth of data from the buffer into the LONG reference argument. As a last step it increments the buffer pointer to prepare for the next extraction. Using the code You use the CXArchive class in exactly the same way as you would use the CArchive class. In your class implement a Serialize() function like this:

void CDriver::Serialize(CXArchive& ar) { if (ar.IsStoring()) { ar << m_byte; ar << m_word; } else { ar >> m_byte; ar >> m_word; } }

Then somewhere in your code add the following lines. To serialize your class:

string strFile = "demo.txt"; try { CXFile file1; file1.Open(strFile, GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); CXArchive ar1(&file1, CXArchive::store); CDriver driver1; driver1.Serialize(ar1); ar1.Close(); } catch (CXException& Ex) { MessageBox(NULL, Ex.GetErrorDesc().c_str(), "Archive Demo", MB_OK); }

To de-serialize it:

try { CXFile file2; file2.Open(strFile, GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); CXArchive ar2(&file2, CXArchive::load); CDriver driver2; driver2.Serialize(ar2); ar2.Close(); } catch (CXException& Ex) { MessageBox(NULL, Ex.GetErrorDesc().c_str(), "Archive Demo", MB_OK); }

One note: if you used new to allocate the CFile object on the heap, then you must delete it after closing the file. Close sets file pointer to NULL. Conclusion It's very easy to serialize your objects using CXArchive class. For basic types just use the insertion /extraction operators. For more complex types call their Serialize method, just do not forget to pass the CXArchive argument to them. History 01/12/2004 - Initial release. Disclaimer THIS SOFTWARE AND THE ACCOMPANYING FILES ARE DISTRIBUTED "AS IS" AND WITHOUT ANY WARRANTIES WHETHER EXPRESSED OR IMPLIED. NO RESPONSIBILITIES FOR POSSIBLE DAMAGES CAN BE TAKEN. THE USER MUST ASSUME THE ENTIRE RISK OF USING THIS SOFTWARE.

Igor Vigdorchik

Click here to view Igor Vigdorchik's online profile.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有