Sat Nov 08, 2008 10:53 pm
int Point::serialize
(CArchive* pArchive)
{
ASSERT (pArchive != NULL);
// Step 1: Serialize signature and version
int nVersion;
try {
if (pArchive->IsStoring()) {
(*pArchive) << Point::m_strSignature;
(*pArchive) << Point::m_nVersion;
} else {
CString strSignature;
(*pArchive) >> strSignature;
if (strSignature != Point::m_strSignature)
return (Status::InvalidFormat);
(*pArchive) >> nVersion;
if (nVersion > Point::m_nVersion;)
return (Status::UnsupportedVersion);
}
// Step 2: Serialize members
if (pArchive->IsStoring()) {
(*pArchive) << m_nX;
(*pArchive) << m_nY;
} else {
(*pArchive) >> m_nX;
(*pArchive) >> m_nY;
}
}
catch (CException* pException) {
// A read/write error occured
pException->Delete();
if (pArchive->IsStoring())
return (Status::WriteError);
return (Status::ReadError);
}
// Object was successfully serialized
return (Status::Success);
}
int ColoredPoint::serialize
(CArchive* pArchive)
{
ASSERT (pArchive != NULL);
// Step 1: Serialize signature and version
int nVersion;
try {
if (pArchive->IsStoring()) {
(*pArchive) << ColoredPoint::m_strSignature;
(*pArchive) << ColoredPoint::m_nVersion;
} else {
CString strSignature;
(*pArchive) >> strSignature;
if (strSignature != ColoredPoint::m_strSignature)
return (Status::InvalidFormat);
(*pArchive) >> nVersion;
if (nVersion > ColoredPoint::m_nVersion;)
return (Status::UnsupportedVersion);
}
// Step 2: Serialize the base class
int nStatus = Point::serialize (pArchive);
if (nStatus != Status::Success)
return (nStatus);
// Step 3: Serialize members
if (pArchive->IsStoring())
(*pArchive) << m_nColor;
else
(*pArchive) >> m_nColor;
}
catch (CException* pException) {
// A read/write error occured
pException->Delete();
if (pArchive->IsStoring())
return (Status::WriteError);
return (Status::ReadError);
}
// Object was successfully serialized
return (Status::Success);
}
class ColoredPointList
{
// Construction/destruction
public:
ColoredPointList::ColoredPointList();
virtual ColoredPointList::~ColoredPointList();
// Attributes
public:
static const CString m_strSignature;
static const int m_nVersion;
// Operations
public:
int serialize (CArchive* pArchive);
// Members
protected:
CPtrArray m_coloredPoints;
}
nt ColoredPointList::serialize
(CArchive* pArchive)
{
ASSERT (pArchive != NULL);
int nStatus = Status::Success;
// Step 1: Serialize signature and version
int nVersion;
try {
if (pArchive->IsStoring()) {
(*pArchive) << ColoredPointList::m_strSignature;
(*pArchive) << ColoredPointList::m_nVersion;
} else {
CString strSignature;
(*pArchive) >> strSignature;
if (strSignature != ColoredPointList::m_strSignature)
return (Status::InvalidFormat);
(*pArchive) >> nVersion;
if (nVersion > ColoredPointList::m_nVersion;)
return (Status::UnsupportedVersion);
}
// Step 2: Serialize base class (if any)
//
// Nothing to do since ColoredPointList isn't derived from anything.
// But if it was derived from BaseColoredPointList, we'd do:
//
// nStatus = BaseColoredPointList::serialize (pArchive);
// if (nStatus != Status::Success)
// return (nStatus);
// Step 3: Serialize number of items in collection
int nItems = 0;
if (pArchive->IsStoring()) {
nItems = m_coloredPoints.GetSize();
(*pArchive) << nItems;
} else
(*pArchive) >> nItems;
// Step 4: Serialize each object in collection
for (int nObject=0; (nObject < nItems); nObject++) {
// 4a: Point to object being serialized
ColoredPoint* pColoredPoint = NULL;
if (pArchive->IsStoring())
pColoredPoint = (ColoredPoint *) m_coloredPoints.GetAt (nObject);
else
pColoredPoint = new ColoredPoint();
ASSERT (pColoredPoint != NULL);
// 4b: Serialize it
nStatus = pColoredPoint->serialize (pArchive);
if (nStatus != Status::Success)
return (nStatus);
if (!pArchive->IsStoring())
m_coloredPoints.Add (pColoredPoint);
}
// Step 5: Serialize object's other members (if any)
//
// Nothing to do since ColoredPointList doesn't have any other
// members. But if it contained an int (m_nSomeInt) and a Foo
// object (m_foo), we'd do:
//
// if (pArchive->IsStoring())
// (*pArchive) << m_nSomeInt;
// else
// (*pArchive) >> m_nColor;
//
// nStatus = m_foo::serialize (pArchive);
// if (nStatus != Status::Success)
// return (nStatus);
}
catch (CException* pException) {
// A read/write error occured
pException->Delete();
if (pArchive->IsStoring())
return (Status::WriteError);
return (Status::ReadError);
}
// Object was successfully serialized
return (Status::Success);
}
ColoredPoint* pColoredPoint = new ColoredPoint();
nStatus = pColoredPoint->serialize (pArchive);
// Read object signature
CString strSignature;
pArchive >> strSignature;
// Construct object of appropriate type
ISerializable* pObject = NULL;
if (strSignature == ColoredPoint::m_strSignature)
pObject = new ColoredPoint();
else
if (strSignature == Line::m_strSignature)
pObject = new Line();
else
if (strSignature == Rectangle::m_strSignature)
pObject = new Rectangle();
else
return (Status::InvalidFormat);
ASSERT (pObject != NULL);
// Read it back in
nStatus = pObject->serialize (pArchive);
class ISerializable
{
// Construction/destruction
public:
ISerializable::ISerializable()
{ }
virtual ISerializable::~ISerializable()
{ }
// Operations
public:
// Get the object's signature
virtual CString getSignature() = 0;
// Get the object's version
virtual int getVersion() = 0;
// Serialize the object
virtual int serialize (CArchive* pArchive) = 0;
}
int ShapeList::serialize
(CArchive* pArchive)
{
ASSERT (pArchive != NULL);
int nStatus = Status::Success;
// Step 1: Serialize signature and version
int nVersion;
try {
if (pArchive->IsStoring()) {
(*pArchive) << ShapeList::m_strSignature;
(*pArchive) << ShapeList::m_nVersion;
} else {
CString strSignature;
(*pArchive) >> strSignature;
if (strSignature != ShapeList::m_strSignature)
return (Status::InvalidFormat);
(*pArchive) >> nVersion;
if (nVersion > ShapeList::m_nVersion;)
return (Status::UnsupportedVersion);
}
// Step 2: Serialize base class (if any)
//
// Nothing to do since ShapeList isn't derived from anything.
// But if it was derived from BaseShapeList, we'd do:
//
// nStatus = BaseShapeList::serialize (pArchive);
// if (nStatus != Status::Success)
// return (nStatus);
// Step 3: Serialize number of items in collection
int nItems = 0;
if (pArchive->IsStoring()) {
nItems = m_shapes.GetSize();
(*pArchive) << nItems;
} else
(*pArchive) >> nItems;
// Step 4: Serialize each object in collection
for (int nObject=0; (nObject < nItems); nObject++) {
// 4a: First serialize object's signature
CString strSignature;
if (pArchive->IsStoring())
(*pArchive) << pObject->getSignature();
else
(*pArchive) >> strSignature;
//
// 4b: Then serialize object
//
// 4b (1): Point to object being serialized
ISerializable* pObject = NULL;
if (pArchive->IsStoring())
pObject = (ISerializable *) m_shapes.GetAt (nObject);
else {
if (strSignature == ColoredPoint::m_strSignature)
pObject = new ColoredPoint();
else
if (strSignature == Line::m_strSignature)
pObject = new Line();
else
if (strSignature == Rectangle::m_strSignature)
pObject = new Rectangle();
else
return (Status::InvalidFormat);
}
ASSERT (pObject != NULL);
// 4b (2): Serialize it
nStatus = pObject->serialize (pArchive);
if (nStatus != Status::Success)
return (nStatus);
if (!pArchive->IsStoring())
m_shapes.Add (pObject);
}
// Step 5: Serialize object's other members (if any)
//
// Nothing to do since ShapeList doesn't have any other
// members. But if it contained an int (m_nSomeInt) and
// a Foo object (m_foo), we'd do:
//
// if (pArchive->IsStoring())
// (*pArchive) << m_nSomeInt;
// else
// (*pArchive) >> m_nColor;
//
// nStatus = m_foo::serialize (pArchive);
// if (nStatus != Status::Success)
// return (nStatus);
}
catch (CException* pException) {
// A read/write error occured
pException->Delete();
if (pArchive->IsStoring())
return (Status::WriteError);
return (Status::ReadError);
}
// Object was successfully serialized
return (Status::Success);
}
...
// Construct object of appropriate type
ISerializable* pObject = MyClassFactory::create (strSignature);
ASSERT (pObject != NULL);
...
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.