29.8 Serializing a CRuntimeClass


29.8 Serializing a CRuntimeClass

Here's a special bit of MFC arcana that took quite a while to figure out! How do we serialize a CRuntimeClass ? Here's an example of how to do it from force.cpp file. The long comment explains why this was a difficult thing to do.

 void cForceClassEvade::Serialize(CArchive &ar)  {      cForce::Serialize(ar);          /* I had a hard time figuring out how to serialize the              CRuntimeClass *_pnodeclass.              (1) You can't call _pnodeclass.Serialize(ar), as this is                  not a method of CRuntimeClass.              (2) Nor can you save off the CRuntimeClass m_lpszClassName                  field and try and use RUNTIME_CLASS to reconstruct it,                  as RUNTIME_CLASS requires a literal argument and not a                  CString.              (3) MFC provides a CArchive::SerializeClass(CRuntimeClass                  *prtc). But it doesn't do the job either. It's                  actually designed more for use as a compatibility                  type-checker. If I put in                  ar.SerializeClass(_pnodeclass), when I save and load,                  I don't get anything read back into _pnodeclass                  because SerializeClass treats its argument as a const                  and won't change it in the load!              (4) What DOES work is to save with                  ar.WriteClass(_pnodeclass) and load with                  _pnodeclass = ar.ReadClass(). */      if (ar.IsStoring()) //Writing data.      {          ar.WriteClass(_pnodeclass);          ar << _evadechildclasses << _dartacceleration << _dartspeedup;      }      else //reading data      {          _pnodeclass = ar.ReadClass();          ar >> _evadechildclasses >> _dartacceleration >> _dartspeedup;      }  } 


Software Engineering and Computer Games
Software Engineering and Computer Games
ISBN: B00406LVDU
EAN: N/A
Year: 2002
Pages: 272

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net