#ifndef _COMMON_RESDATA_HPP_ #define _COMMON_RESDATA_HPP_ #ifndef _COMMON_WINDOWS_HPP_ #include #endif #ifndef _COMMON_STRING_HPP_ #include #endif #ifndef _COMMON_BLOCK_HPP_ #include #endif class ResType { public: enum Resource {RtAccelerator = (int)RT_ACCELERATOR, RtBitmap = (int)RT_BITMAP, RtDialog = (int)RT_DIALOG, RtFont=(int)RT_FONT,RtFontDir=(int)RT_FONTDIR,RtMenu=(int)RT_MENU,RtData=(int)RT_RCDATA, RtString=(int)RT_STRING,RtMessageTable=(int)RT_MESSAGETABLE,RtCursor=(int)RT_CURSOR, RtGroupCursor=(int)RT_GROUP_CURSOR,RtIcon=(int)RT_ICON,RtGroupIcon=(int)RT_GROUP_ICON, RtVersion=(int)RT_VERSION}; }; class ResDisposition { public: enum Disposition{Assume,Delete,Invalid}; }; template class ResData { public: ResData(void); ResData(const ResData &someResData); ResData(WORD resourceId,ResType::Resource resType=ResType::RtData,HMODULE hResModule=GetModuleHandle(0)); ResData(const String& resName, ResType::Resource resType = ResType::RtData, HMODULE hResModule = GetModuleHandle(0)); virtual ~ResData(); ResData &operator=(const ResData &someResData); ResData &operator=(const String &resName); operator T*(void)const; DWORD getSize()const; WORD isOkay(void)const; private: void create(const String &resName,ResType::Resource resType=ResType::RtData,HMODULE hResModule=::GetModuleHandle(0)); void create(WORD resourceId, ResType::Resource resType = ResType::RtData, HMODULE hResModule = ::GetModuleHandle(0)); void destroy(void); HRSRC resHandle(void)const; void resHandle(HRSRC resHandle); HGLOBAL resData(void)const; void resData(HGLOBAL resData); ResDisposition::Disposition resDisp(void)const; void resDisp(ResDisposition::Disposition resDisp); HRSRC mhResHandle; // returned from ::FindResource() HGLOBAL mhResData; // returned from ::LoadResource(); HMODULE mhResModule; // the module T* pData; ResDisposition::Disposition mDisposition; }; template inline ResData::ResData(void) : mhResHandle(0), mhResData(0), mDisposition(ResDisposition::Invalid) { } template inline ResData::ResData(const ResData &someResData) : mhResHandle(0), mhResData(0), mDisposition(ResDisposition::Invalid) { *this=someResData; } template inline ResData::ResData(const String &resName,ResType::Resource resType,HMODULE hResModule) : mhResHandle(0), mhResData(0), mhResModule(hResModule), mDisposition(ResDisposition::Delete) { create(resName,resType,hResModule); } template inline ResData::ResData(WORD resourceId, ResType::Resource resType, HMODULE hResModule) : mhResHandle(0), mhResData(0), mhResModule(hResModule), mDisposition(ResDisposition::Delete) { create(resourceId, resType, hResModule); } template inline ResData::~ResData() { destroy(); } template inline ResData &ResData::operator=(const ResData &someResData) { if(!someResData.isOkay())return *this; destroy(); resData(someResData.resData()); resHandle(someResData.resHandle()); resDisp(ResDisposition::Assume); return *this; } template inline ResData &ResData::operator=(const String &resName) { if(resName.isNull())return *this; destroy(); create(resName); return *this; } template inline DWORD ResData::getSize()const { if (!isOkay())return 0; return ::SizeofResource(mhResModule, mhResHandle); } template inline ResData::operator T*(void)const { return pData; } template inline void ResData::create(const String &resName,ResType::Resource resType,HMODULE hResModule) { ResType resourceType = ResType(); destroy(); resHandle(::FindResource(hResModule, resName, MAKEINTRESOURCE(resType))); if(!resHandle())return; resData(::LoadResource(hResModule,mhResHandle)); resDisp(ResDisposition::Delete); pData = (T*)::LockResource(resData()); } template inline void ResData::create(WORD resourceId, ResType::Resource resType, HMODULE hResModule) { ResType resourceType = ResType(); destroy(); // resHandle(::FindResource(hResModule, resName, (const char*)resType)); resHandle(::FindResource(hResModule, MAKEINTRESOURCE(resourceId), MAKEINTRESOURCE(resType))); if (!resHandle())return; resData(::LoadResource(hResModule, mhResHandle)); resDisp(ResDisposition::Delete); pData = (T*)::LockResource(resData()); } template inline void ResData::destroy(void) { if(!resData()||ResDisposition::Assume==resDisp())return; ::FreeResource(resData()); resData(0); resHandle(0); pData = 0; resDisp(ResDisposition::Invalid); } template inline HRSRC ResData::resHandle(void)const { return mhResHandle; } template inline void ResData::resHandle(HRSRC resHandle) { mhResHandle=resHandle; } template inline HGLOBAL ResData::resData(void)const { return mhResData; } template inline void ResData::resData(HGLOBAL resData) { mhResData=resData; } template inline ResDisposition::Disposition ResData::resDisp(void)const { return mDisposition; } template inline void ResData::resDisp(ResDisposition::Disposition resDisp) { mDisposition=resDisp; } template inline WORD ResData::isOkay(void)const { return (resData()&&resHandle()); } #endif