// ctkSerializable.h: interface for the ctkSerializable class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_CTKSERIALIZABLE_H__D93BEE27_F124_4065_992B_F5B7A39D4D24__INCLUDED_)
#define AFX_CTKSERIALIZABLE_H__D93BEE27_F124_4065_992B_F5B7A39D4D24__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class ctkArchiveException
{
public:
ctkArchiveException(int nErrorType_, unsigned long data1_, unsigned long data2_)
{
nErrorType =nErrorType_;
data1 =data1_;
data2 =data2_;
}
public:
int nErrorType;
unsigned long data1;
unsigned long data2;
public:
enum
{
readerror_win,
writeerror_win,
readerror_size,
writeerror_size
};
};
class ctkArchive;
class ctkSerializable
{
public:
// Due to a problem with the compiler from VC 6.0,
// the function must not be pure (otherwise it won't
// compile the class ctkArchive)
virtual void vSerialize(ctkArchive&)
{ ASSERT(FALSE); };
};
class ctkArchive
{
public:
virtual bool fIsStoring() const PURE_VIRTUAL_FALSE;
virtual bool fIsReading() const { return !fIsStoring(); };
template<class T>
void vWrite(const T& ref)
{
vWriteBinary((const void*)&ref, sizeof(T));
}
template<class T>
void vWriteNum(const T* ptr, int nCnt)
{
vWriteBinary((const void*)ptr, sizeof(T) * nCnt);
}
template<class T>
void vRead(T& ref)
{
vReadBinary((void *)&ref, sizeof(T));
}
template<class T>
void vReadNum(T* ptr, int nCnt)
{
vReadBinary((void *)ptr, sizeof(T) * nCnt);
}
template<ctkSerializable>
void vWrite(ctkSerializable& ref)
{
ref.vSerialize(*this);
}
template<ctkSerializable>
void vRead(ctkSerializable& ref)
{
ref.vSerialize(*this);
}
virtual void vWriteBinary(const void *ptr, unsigned long size) PURE_VIRTUAL;
virtual void vReadBinary(void *ptr, unsigned long size) PURE_VIRTUAL;
};
class ctkWinFileArchive : public ctkArchive
{
public:
ctkWinFileArchive(bool fOutput, HANDLE hFile)
{
m_fOutput = fOutput;
m_hFile = hFile;
}
public:
bool m_fOutput;
HANDLE m_hFile;
public:
virtual void vWriteBinary(const void *ptr, unsigned long size)
{
ASSERT(m_fOutput);
DWORD dwWritten = 0;
if(!::WriteFile(m_hFile,ptr,size,&dwWritten,NULL))
{
throw ctkArchiveException(
ctkArchiveException::writeerror_win,
::GetLastError(),0);
}
if(dwWritten!=size)
throw ctkArchiveException(
ctkArchiveException::writeerror_size,
dwWritten,size);
}
virtual void vReadBinary(void *ptr, unsigned long size)
{
ASSERT(!m_fOutput);
DWORD dwRead = 0;
if(!::ReadFile(m_hFile,ptr,size,&dwRead,NULL))
{
throw ctkArchiveException(
ctkArchiveException::readerror_win,
::GetLastError(),0);
}
if(dwRead != size)
throw ctkArchiveException(
ctkArchiveException::readerror_size,
dwRead,size);
}
};
#endif // !defined(AFX_CTKSERIALIZABLE_H__D93BEE27_F124_4065_992B_F5B7A39D4D24__INCLUDED_)