Total members 11890 |It is currently Sat Apr 20, 2024 1:03 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Serialization implementation in C++, How to serialize a simple object via a CArchive using a serialize() method like this:

cpp code
int CFoo::serialize
(CArchive* pArchive)
{
int nStatus = SUCCESS;

// Serialize the object ...
ASSERT (pArchive != NULL);
TRY
{
if (pArchive->IsStoring()) {
// Write employee name and id
(*pArchive) << m_strName;
(*pArchive) << m_nId;
}
else {
// Read employee name and id
(*pArchive) >> m_strName;
(*pArchive) >> m_nId;
}
}
CATCH_ALL (pException)
{
nStatus = ERROR;
}
END_CATCH_ALL

return (nStatus);
}

There's a problem with this code. What if we mistakenly read a datafile that doesn't contain the expected information? If the datafile doesn't contain a CString followed by an int, our serialize() method would return ERROR. That's nice, but it would be better if we could recognize the situation and return a more specific status code like INVALID_DATAFILE. We can check that we're reading a valid datafile (i.e. one that contains a CFoo object) by using an object signature.
Object signatures
An object signature is just a character string (eg: "FooObject") that identifies an object. We add a signature to CFoo by modifying the class definition:

cpp code
class CFoo
{
...

// Methods
public:
...
CString getSignature();

// Data members
...
protected:
static const CString Signature; // object signature
};


The signature is declared in Foo.cpp.

cpp code
// Static constants
const CString CFoo::Signature = "FooObject";


Next, we modify the serialize() method to serialize the signature before serializing the object's data members. If an invalid signature is encountered, or if the signature is missing, it's likely that we're attempting to read a data store that doesn't contain a CFoo object. Here's the logic for reading a signed object:
Attachment:
serialization2_signature.gif
serialization2_signature.gif [ 2.54 KiB | Viewed 4206 times ]


Using a signature to validate a data store

And here's the code:

cpp code
int CFoo::serialize
(CArchive* pArchive)
{
int nStatus = SUCCESS;
bool bSignatureRead = false;

// Serialize the object ...
ASSERT (pArchive != NULL);
TRY
{
if (pArchive->IsStoring()) {
// Write signature
(*pArchive) << getSignature();

// Write employee name and id
(*pArchive) << m_strName;
(*pArchive) << m_nId;
}
else {
// Read signature - complain if invalid
CString strSignature;
(*pArchive) >> strSignature;
bSignatureRead = true;
if (strSignature.Compare (getSignature()) != 0) {
return (INVALID_DATAFILE);
}

// Read employee name and id
(*pArchive) >> m_strName;
(*pArchive) >> m_nId;
}
}
CATCH_ALL (pException)
{
nStatus = bSignatureRead ? ERROR : INVALID_DATAFILE;
}
END_CATCH_ALL

return (nStatus);
}

You should ensure that all your objects have unique signatures. It's less important what the actual signature is. If you're developing a suite of products, it's helpful to have a process for registering object signatures companywide. That way, developers won't mistakenly use the same signature for different objects. If you want to make it harder to reverse engineer your datafiles, you should use signatures that have no obvious connection to object names.
Creating Versions:
As you upgrade your product during its lifecycle, you may find it necessary to modify the structure of CFoo by adding or removing data members. If you simply released a new version of CFoo, attempts to read old versions of the object from a data store would fail. This is obviously not acceptable. Any version of CFoo should be able to restore itself from an older serialized version. In other words, CFoo's serialization method should always be backward compatible. This is easily accomplished by versioning the object. Just as we added an object signature, we add an integer constant that specifies the object's version number.

cpp code
class CFoo
{
...

// Methods
public:
...
CString getSignature();
int getVersion();

// Data members
...
protected:
static const CString Signature; // object signature
static const int Version; // object version
};

The object's version is declared in Foo.cpp.

cpp code
// Static constants
const CString CFoo::Signature = "FooObject";
const int CFoo::Version = 1;

Next, we modify the serialize() method to serialize the version after serializing the signature, and before serializing the object's data members. If a newer version is encountered, we're attempting to read an unsupported version of the object. In this case, we simply return the status UNSUPPORTED_VERSION.

cpp code
int CFoo::serialize
(CArchive* pArchive)
{
int nStatus = SUCCESS;
bool bSignatureRead = false;
bool bVersionRead = false;

// Serialize the object ...
ASSERT (pArchive != NULL);
TRY
{
if (pArchive->IsStoring()) {
// Write signature and version
(*pArchive) << getSignature();
(*pArchive) << getVersion();

// Write employee name and id
(*pArchive) << m_strName;
(*pArchive) << m_nId;
}
else {
// Read signature - complain if invalid
CString strSignature;
(*pArchive) >> strSignature;
bSignatureRead = true;
if (strSignature.Compare (getSignature()) != 0) {
return (INVALID_DATAFILE);
}

// Read version - complain if unsupported
int nVersion;
(*pArchive) >> nVersion;
bVersionRead = true;
if (nVersion > getVersion()) {
return (UNSUPPORTED_VERSION);
}

// Read employee name and id
(*pArchive) >> m_strName;
(*pArchive) >> m_nId;
}
}
CATCH_ALL (pException)
{
nStatus = bSignatureRead && bVersionRead ? ERROR : INVALID_DATAFILE;
}
END_CATCH_ALL

return (nStatus);
}

Version 1 of our CFoo contained 2 data members - a CString (m_strName) and an int (m_nId). If we add a third member (eg: int m_nDept) in version 2, we need to decide what m_nDept should be initialized to when reading an older version of the object. In this example, we'll initialize m_nDept to -1 implying that the employee's department code is "Unknown".

cpp code
class CFoo
{
...
// Data members
public:
CString m_strName; // employee name
int m_nId; // employee id
int m_nDept; // department code (-1 = unknown)
};

We also need to increase the object's version number in Foo.cpp to 2.


cpp code
const int CFoo::Version = 2;


Finally, we modify the part of serialize() that reads the object so that m_nDept is initialized to -1 if we're reading an older version of the datafile. Note that the file is always saved as the latest version.
cpp code
int CFoo::serialize
(CArchive* pArchive)
{
...
// Serialize the object ...
ASSERT (pArchive != NULL);
TRY
{
if (pArchive->IsStoring()) {
...
// Write employee name, id and department code
(*pArchive) << m_strName;
(*pArchive) << m_nId;
(*pArchive) << m_nDept;
}
else {
...
// Read employee name and id
(*pArchive) >> m_strName;
(*pArchive) >> m_nId;

// Read department code (new in version 2)
if (nVersion >= 2) {
(*pArchive) >> m_nDept;
}
else {
m_nDept = -1; // unknown
}
}
}
CATCH_ALL (pException)
{
nStatus = bSignatureRead && bVersionRead ? ERROR : INVALID_DATAFILE;
}
END_CATCH_ALL

return (nStatus);
}

Conclusion
So far, we've dealt with providing robust support for serializing simple objects - i.e. that those that contain readily serializable data types. In Part 3, we'll see how to serialize any kind of object.


Continue Reading :
c-c/how-to-implement-serialization-using-c-part-3-t2774.html
c-c/a-serialization-primer-part-1-t2771.html



_________________
Please recommend my post if you found it helpful. ,
java,j2ee,ccna ,ccnp certified .


Author:
Expert
User avatar Posts: 838
Have thanks: 2 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : serialization implementation in C++ - Part 2
 How to implement serialization using C++ - Part 3     -  
 Coding serialization concepts in C++     -  
 Part Time Online Jobs     -  
 List C++ implementation     -  
 Implementation of List     -  
 Associate Analyst- Implementation     -  
 Implementation of association rules     -  
 wiener filter implementation     -  
 merge sort implementation in C++     -  
 Implementation of the DES & Triple DES in C++ or Java     -  



Topic Tags

C++ Files and I/O
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
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