PDA

View Full Version : Serialization arbitary data types



amiref
19th March 2011, 18:11
Hi
I wrote a program ( that will come below ) that has 2 class with names : keyNode and valueNode and every class has 2 private member . now I create QMap<keyNode , valueNode > and I want to serialize this QMap with QDataStream and I know that I must override operator<<() and operator>>() but I dont know how . please help me .
thanks



#include <QMap>
#include <QString>
#include <conio.h>
#include <QFile>
#include <QDataStream>
#include <QIODevice>
#include <QtCore/QCoreApplication>


class valueNode
{
public:
valueNode()
{
docId = 0 ;
termFreq = 0 ;
}
valueNode( qint32 doc , qint32 term )
{
docId = doc ;
termFreq = term ;
}
void setDocId( qint32 newDocId )
{
docId = newDocId ;
}
void setTermFreq( qint32 newTermFreq )
{
termFreq = newTermFreq ;
}
qint32 getDocId() const
{
return docId ;
}
qint32 getTermFreq() const
{
return termFreq ;
}

private:
qint32 docId ;
qint32 termFreq ;
};


class keyNode
{
public:
keyNode()
{

docFreq = 0 ;
}
keyNode( QString k , qint32 doc )
{
key = k ;
docFreq = doc ;
}
void setKey( QString newKey )
{
key = newKey ;
}
void setDFreocq( qint32 newDocFreq )
{
docFreq = newDocFreq ;
}
QString getKey() const
{
return key ;
}
qint32 getDocFreq() const
{
return docFreq ;
}

private:
QString key ;
qint32 docFreq ;
};



inline bool operator<(const keyNode &e1, const keyNode &e2)
{
return e1.getKey() < e2.getKey() ;
}



int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

QMap< keyNode , valueNode > map ;
QMap< QString , valueNode > mmap ;

map[ keyNode( "school" , 18 ) ] = valueNode( 12 , 17 ) ;
map[ keyNode( "class" , 45 ) ] = valueNode( 122 , 76 ) ;
map[ keyNode( "student" , 23 ) ] = valueNode( 1 , 5 ) ;
map[ keyNode( "amir" , 32 ) ] = valueNode( 187 , 58 ) ;
map[ keyNode( "xoom" , 179 ) ] = valueNode( 139 , 25 ) ;
map[ keyNode( "class" , 1115 ) ] = valueNode( 1002 , 876 ) ;

mmap[ "v" ] = valueNode( 11 , 12 ) ;
mmap[ "a" ] = valueNode( 112 , 3022 ) ;
mmap[ "c" ] = valueNode( 91 , 762 ) ;
mmap[ "x" ] = valueNode( 654 , 102 ) ;

QFile file("file.dat");
file.open(QIODevice::WriteOnly);
QDataStream out(&file); // we will serialize the data into the file
out << map ; // serialize a string
file.close() ;
map.clear() ;


file.open(QIODevice::ReadOnly);
QDataStream in(&file); // read the data serialized from the file
in >> map;
file.close() ;
getch() ;
return a.exec();
}

JohannesMunk
20th March 2011, 11:04
Hi!

You will have to implement these functions (see http://doc.qt.nokia.com/4.7/qdatastream.html#reading-and-writing-other-qt-classes)



QDataStream & operator<< (QDataStream& stream, const keyNode& key)
{
// break down the composite class to primitives and stream it..
stream << key.getKey() << key.getDocFreq();
return stream;
}
QDataStream & operator>> (QDataStream& stream, keyNode& key)
{
QString keyS;qint32 docFreq;
stream >> keyS >> docFreq;
key.setKey(keyS);
key.setDFreocq(docFreq);
return stream;
}
QDataStream & operator<< (QDataStream& stream, const valueNode& value);
QDataStream & operator>> (QDataStream& stream, valueNode& value);
You could also use the private fields instead of the accessor-functions but then you need to declare the operators as friends to the class.

HIH

Joh