class Wrapper{public: explicit Wrapper(DataStream& dataStream) : dataStream_(dataStream) {} Wrapper& operator<<(qint8 i) { dataStream << i; if (OK != dataStream_.status()) throw std::runtime_error(getStatusDescription()); } ... and so onprivate: DataStream& dataStream_;}template <typename T>T& operator<<( T& out, const SerializableObject& obj){ out << obj.name(); ...}Usage:1.DataStream stream;SerializableObject obj;stream << obj;2.DataStream stream;Wrapper wrapper(stream);wrapper << obj; // exception aware
template <typename T>T& operator<<( T& out, const SerializableObject& obj){ out << obj.name(); ...}
C++ (Qt) Wrapper& operator<<(QList <MyData> & lst) { dataStream >> lst; if (OK != dataStream_.status()) throw std::runtime_error(getStatusDescription()); }
C++ (Qt)QDataStream & operator >> ( QDataStream & strm, MyClass & dst ){ return strm >> dst.data1 >> dst.data2 >>... }
C++ (Qt)int main(int argc, char *argv[]){ QCoreApplication app(argc, argv); qint8 a = 0, b = 0, c = 0, d = 0, e = 0, f = 0; try { QFile file( "test.dat" ); ExProxyDev dev( file ); dev.open( QIODevice::ReadOnly ); QDataStream in( &dev ); in >> a >> b >> c >> d >> e >> f; qDebug() << in.status(); } catch( std::exception &ex ) { qDebug() << ex.what(); } qDebug() << a << b << c << d << e << f; return 0;}