The meaning of a serialization operation is, by definition, saving data to a storage device in a certain order, or reading a sequence of data from a storage device, and the data type is consistent (that is, the data you read is the type I know). The process knows, how to do it?
I am a simple program implemented with the help of the QT framework. (The framework, of course, implements the complex part.) If in your encapsulated data, meaning non-QT standard data types, then it is necessary to implement the "<<" and ">>" operators to implement a custom type of serialization operations).
The specific implementation process is as follows:
Order.h
#include <QDataStream>
#include <QDateTime>
#include <QFile>
typedef struct __MY_STRCT
{
QString _name;
unsigned int _id;
}SMY_STRCT;
Qdatastream & operator << (qdatastream& out, const SMY_STRCT &data);
Qdatastream &operator >> (qdatastream& in, smy_strct &data);
Class Orderclstest
{
Public
Orderclstest ();
void Store ();
void load ();
Private
Qdatetime _lasttime;
};
Order.cpp
#include "Orderclstest.h"
#include <QDebug>
Orderclstest::orderclstest ()
{
Store ();
}
void Orderclstest::load ()
{
QFile file ("./data.dat");
if (!file.open (qiodevice::readonly)) return;
SMY_STRCT _my_strct;
Qdatastream in (&file);
In >> _lasttime >> _my_strct;
File.close ();
}
void Orderclstest::store ()
{
QFile file ("./data.dat");
if (!file.open (qiodevice::writeonly)) {return;}
_lasttime = Qdatetime::currentdatetime ();
Qdatastream out (&file);
SMY_STRCT _my_strct;
_my_strct._id = 123;
_my_strct._name = "Hello";
Out << _lasttime << _my_strct;
File.close ();
}
qdatastream& operator << (qdatastream &out, const SMY_STRCT &data)
{
Out << data._id << data._name;
return out;
}
qdatastream& operator >> (Qdatastream &in, smy_strct &data)
{
In >> data._id >> data._name;
return in;
}
int main (int argc, char *argv[])
{
Orderclstest test;
Test.load ();
}
Isn't it simple? Working on the Framework Development Guide These are all right, if you want to achieve the true meaning of the serialization operation, you can refer to the MFC persistence mechanism, that is, my previous blog. You'll see a lot.
This article is from the "GDT Commentary" blog, make sure to keep this source http://844133395.blog.51cto.com/3483632/1723255
C++/QT serialization Operation