Click to See Complete Forum and Search --> : Serializing


Alfred
May 18th, 1999, 02:50 PM
Is it possible to save a object of this type?
class memory {
public:
memory();
int m_Mem[MAXMEM];
CString Noun[MAXWORDS];
CString Adjective[MAXWORDS];
CString Verb[MAXWORDS];
CString Nothing[MAXWORDS];
void CheckWord(CString word);
void SortIn(CString word, int type);
private:
CExtraDlg ExtraDlg;
int NounPosition;
int AdjectivePosition;
int VerbPosition;
int NothingPosition;
};
It should be saved in this way:

CFile f;
f.Open("backup.iq",
CFile::modeCreate | CFile::modeWrite);
memory ar(&f, CArchive::store);
ar << brain; //Brain is the memory object

What should I rewrite?
Please help me!%

Astrausity
May 18th, 1999, 05:19 PM
First, you need to go into the header file and add a couple line. The First one is to make the class public to CObject and second you need to declare the class as serial.

the things chaged are " : public CObject" and "DECLARE_SERIAL(memory)"

Here is an example:


class memory : public CObject
{
DECLARE_SERIAL(memory)

public:
memory();
int m_Mem[MAXMEM];
CString Noun[MAXWORDS];
CString Adjective[MAXWORDS];
CString Verb[MAXWORDS];
CString Nothing[MAXWORDS];
void CheckWord(CString word);
void SortIn(CString word, int type);
private:
CExtraDlg ExtraDlg;
int NounPosition;
int AdjectivePosition;
int VerbPosition;
int NothingPosition;
};




You can pretty much put the DECLARE_SERIAL() call anywhere in the class definition just to let you know. Now the next step is to open the CPP file and add the line "IMPLEMENT_SERIAL(memory, CObject, 1)" BEFORE any function definitions. Here is an example of what it might look like:


#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

IMPLEMENT_SERIAL(memory,CObject,1)

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

memoryr::memory()
{
...



It MUST be called before any function definitions I stress this fact...

Also since these are macro calls and not function calls you do not need the semicolin ';' at the end of the line.

Now one last step, override the default Serialize class. Simply right click the "memory" class and click "add member function" and add this:

void Serialize(CArchive& ar)

In the Serialize function add the code:


void memory::Serialize(CArchive& ar)
{
// ALWAYS call the base class serialize first!
CObject::Serialize(ar);

if (ar.IsStoring())
{
ar << var1 << var2 << var3 ... and so on;
}
else
{
ar >> var1 >> var2 >> var3 ... and so on;
}
}




If you have an array somewhere you can do something like:


if (ar.IsStoring())
{
for (int i = 0; i < m_pArray.GetSize(); i++)
{
ar << m_pArray.GetAt(i);
}
}




Okay, your class should now be serializable so now you need to "serialize" it. That's easy as well. Simply call this somewhere and it will be written out to a file.


// Save the class to a file
CFile* pFile = new CFile;
if (pFile->Open("Test.mem", CFile::modeCreate | CFile::modeWrite | CFile::typeBinary) != 0)
{
// create the archive
CArchive ar(pFile, CArchive::store);

// write out the data
mem.Serialize(ar);

// clean up a bit
ar.Close();
pFile->Close();
}
else
{
// the file didn't open
}




Now that should work, and to read in the file you'd do something like this:


CFile* pFile = new CFile;
if (pFile->Open("Test.mem", CFile::modeRead | CFile::typeBinary))
{
CArchive ar(pFile, CArchive::load);
mem.Serialize(ar);

ar.Close();
pFile->Close();
}




well, I hope that's enough info. I assumed that you learn by example because that's how I learn best. If you need further help or maybe in a more written out form feel free to write me at astrausity@jado.org

- Mike

jaylauriano
May 19th, 1999, 06:32 PM
Don't forget IMPLEMENT_SERIAL in the implementation of the object you are serializing.


Freedom is knowing you can burn all your software without losing a minute's sleep.