المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Overriding void AFXAPI SerializeElements [modified]



C++ Programming
09-24-2009, 09:11 AM
Hi all,

As you probably know, there is a problem with CString types in CList, CMap or CArray with regards to serialization. I have attempted to remedy this by overriding void AFXAPI SerializeElements. Before I post my code though, I'd like to ask if any other MFC container class can serialize CStrings correctly, for example CTypedPtrList? I'm not real particular on the container class for my current app and figuring out the peculiarities of serialize is exhausting.

The problem with the code below is that even after overriding SerializeElements, I get a mem copy error when doing a serialize load operation. A breakpoint in SerializeElements is never hit so for some reason it's not being used. I also have a serialize() function for the base class elements but it's not used either, presumably because all base elements are CSTRINGs? There are 2 classes, one for the base elements and another with the CList and functions. BTW, the functions that save and load the list are called from inside the Doc class serialize function.

Thanks in advance -

///////.h file

class CChartInfo : public CObject
{

DECLARE_SERIAL(CChartInfo)
public:

CString m_ChartName;
CString m_Portfolio;
CString m_DataPath;
CString m_DataFile;



CChartInfo(const CChartInfo &s)
{m_ChartName = s.m_ChartName;
m_Portfolio = s.m_Portfolio;
m_DataPath = s.m_DataPath;
m_DataFile = s.m_DataFile;}

CChartInfo& operator=(const CChartInfo &s)
{m_ChartName = s.m_ChartName;
m_Portfolio = s.m_Portfolio;
m_DataPath = s.m_DataPath;
m_DataFile = s.m_DataFile;
return *this;}

public:
void Serialize(CArchive& ar);

public:
CChartInfo();
virtual ~CChartInfo();
};

class CChartManager : public CObject
{
DECLARE_SERIAL(CChartManager)

public:

CList m_ChartList;



void AddChart(CChartInfo ChartInfo);
bool GetChart(CString ChartName, CChartInfo &ChartInfo);
bool LoadChartList();
bool SaveChartList();


CChartManager();
virtual ~CChartManager();
};


void AFXAPI SerializeElements(CArchive& ar,CChartInfo* pChartInfo, int nCount);

////////////////////////////////////////.cpp file


IMPLEMENT_SERIAL(CChartInfo, CObject, VERSION_NUMBER)
IMPLEMENT_SERIAL(CChartManager, CObject, VERSION_NUMBER)


CChartInfo::CChartInfo()
{
}

CChartInfo::~CChartInfo()
{
}

void CChartInfo::Serialize(CArchive& ar)
{
CObject::Serialize(ar);
if (ar.IsStoring()){
ar m_Portfolio
>> m_DataPath
>> m_DataFile;
}
}

CChartManager::CChartManager()
{
}

CChartManager::~CChartManager()
{
}


// CChartManager member functions


bool CChartManager::SaveChartList()
{

//assumes m_ChartList has been loaded with correct data

char acPath[256];
if ( GetModuleFileName( NULL, acPath, 256 ) != 0) {
// guaranteed file name of at least one character after path
* ( strrchr( acPath, '\\' ) + 1 ) = '\0';
//AfxMessageBox( acPath ); // Use it
}
strcat(acPath, "ChartManager.dat");

CFile ChartManagerFile;
CFileException Err;
if (ChartManagerFile.Open(acPath, CFile::modeCreate | CFile::modeWrite, &Err))
{
CArchive archive(&ChartManagerFile, CArchive::store);
m_ChartList.Serialize(archive);
archive.Close();
ChartManagerFile.Close();

return 1;

}
else {
Err.ReportError();//for debug only
return 0;
}

}

bool CChartManager::LoadChartList()
{

//read chartlist file and load data into list
//if file not found returns false

m_ChartList.RemoveAll();

char acPath[256];
if ( GetModuleFileName( NULL, acPath, 256 ) != 0) {
// guaranteed file name of at least one character after path
* ( strrchr( acPath, '\\' ) + 1 ) = '\0';
//AfxMessageBox( acPath ); // Use it
}
strcat(acPath, "ChartManager.dat");

CFile ChartManagerFile;
CFileException Err;
if (ChartManagerFile.Open(acPath, CFile::modeRead, &Err))
{
CArchive archive(&ChartManagerFile, CArchive::load);
m_ChartList.Serialize(archive);
archive.Close();
ChartManagerFile.Close();
return 1;

}
else {
Err.ReportError();//for debug only
return 0;
}



}


void AFXAPI SerializeElements(CArchive& ar, CChartInfo* pChartInfo, int nCount)
{
for(int i=0;nCount;i++,pChartInfo++)
{
pChartInfo->Serialize(ar);
}
}

modified on Wednesday, September 23, 2009 3:09 PM