c++ - Deserialization of raw data using QDataStream -


i have data comes tcp socket raw data (i have specified format) , create object based on data. format this: 24 bytes of header [size of data , other info] , amount of data specified in header.

i read object (object has buffer data inside dynamic size data put). possible somehow override qdatastream or other way elegantly wrap it? take advantage of transactions methods read whole packets of data , not taking care of assembling them if come in pieces (half header, or not full amount of data).

so want sth this:

event event;          // custom class qdatastream datastream(tcpsocket);  datastream >> event; datastream.commit(); 

i believe case operator overloading. made small demonstrative example:

class test { public:     int i;     float f;     double d;      char empty[4];     int ii[3];      qstring s;  public:     friend qdatastream& operator>>(qdatastream& in, test& test);     friend qdatastream& operator<<(qdatastream& out, const test& test); };  qdatastream& operator>>(qdatastream& in, test& test) {     in >> test.i;      in.setfloatingpointprecision(qdatastream::singleprecision);     in >> test.f;      in.setfloatingpointprecision(qdatastream::doubleprecision);     in >> test.d;      in.skiprawdata(sizeof test.empty);     in.readrawdata(reinterpret_cast<char*>(test.ii), sizeof test.ii);      in >> test.s;      return in; }  qdatastream& operator<<(qdatastream& out, const test& test) {     out << test.i;      out.setfloatingpointprecision(qdatastream::singleprecision);     out << test.f;      out.setfloatingpointprecision(qdatastream::doubleprecision);     out << test.d;      out.writerawdata(reinterpret_cast<const char*>(test.empty), sizeof test.empty);     out.writerawdata(reinterpret_cast<const char*>(test.ii), sizeof test.ii);     out << test.s;      return out; } 

then can do:

 outputstream         << test1         << test2         << test3;  // ...  inputstream.starttransaction(); inputstream         >> test11         >> test22         >> test33; inputstream.committransaction(); 

also these operators predefined qt fundamental containers (qvector, qlist, qset etc.)


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -