Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <QCoreApplication>
- #include <QtWidgets/QtWidgets>
- struct Small {
- int a;
- friend QDataStream& operator<<(QDataStream& out,const Small& s) {
- out << s.a;
- return out;
- }
- friend QDataStream& operator>>(QDataStream& in,Small& s){
- in >> s.a;
- return in;
- }
- bool operator==(const Small& other) const {
- return a == other.a;
- }
- };
- struct Medium {
- int a;
- double b;
- Small s;
- friend QDataStream& operator<<(QDataStream& out,const Medium& s) {
- out << s.a << s.b << s.s;
- return out;
- }
- friend QDataStream& operator>>(QDataStream& in,Medium& s){
- in >> s.a >> s.b >> s.s;
- return in;
- }
- bool operator==(const Medium& other) const {
- return a == other.a && b == other.b && s == other.s;
- }
- };
- struct Large {
- int k;
- Medium m;
- friend QDataStream& operator<<(QDataStream& out,const Large& s) {
- out << s.k << s.m;
- return out;
- }
- friend QDataStream& operator>>(QDataStream& in,Large& s){
- in >> s.k >> s.m;
- return in;
- }
- bool operator==(const Large& other) const {
- return k == other.k && m == other.m;
- }
- };
- struct Aggregate {
- Large l;
- Medium m;
- QVector<Small> s;
- friend QDataStream& operator<<(QDataStream& out,const Aggregate& s) {
- out << s.l << s.m << s.s;
- return out;
- }
- friend QDataStream& operator>>(QDataStream& in,Aggregate& s){
- in >> s.l >> s.m >> s.s;
- return in;
- }
- bool operator==(const Aggregate& other) const {
- return l == other.l && m == other.m && s == other.s;
- }
- };
- int main(int argc, char *argv[])
- {
- QCoreApplication a(argc, argv);
- QFile file("test.txt");
- Aggregate ag{{5,{1,2.5f,{6}}},{2,3.5f,{25}},{{1},{2},{3}}};
- if (file.open(QIODevice::WriteOnly)) {
- QDataStream s(&file);
- s << ag;
- }
- file.close();
- QFile file_in("test.txt");
- if (file_in.open(QIODevice::ReadOnly)) {
- Aggregate g;
- QDataStream in(&file_in);
- in >> g;
- qDebug() << (g == ag);
- }
- QDataStream s;
- return a.exec();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement