Advertisement
Guest User

Untitled

a guest
Apr 25th, 2020
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. #include <QCoreApplication>
  2. #include <QtWidgets/QtWidgets>
  3.  
  4. struct Small {
  5. int a;
  6. friend QDataStream& operator<<(QDataStream& out,const Small& s) {
  7. out << s.a;
  8. return out;
  9. }
  10. friend QDataStream& operator>>(QDataStream& in,Small& s){
  11. in >> s.a;
  12. return in;
  13. }
  14. bool operator==(const Small& other) const {
  15. return a == other.a;
  16. }
  17.  
  18. };
  19. struct Medium {
  20. int a;
  21. double b;
  22. Small s;
  23. friend QDataStream& operator<<(QDataStream& out,const Medium& s) {
  24. out << s.a << s.b << s.s;
  25. return out;
  26. }
  27. friend QDataStream& operator>>(QDataStream& in,Medium& s){
  28. in >> s.a >> s.b >> s.s;
  29. return in;
  30. }
  31. bool operator==(const Medium& other) const {
  32. return a == other.a && b == other.b && s == other.s;
  33. }
  34.  
  35. };
  36. struct Large {
  37. int k;
  38. Medium m;
  39. friend QDataStream& operator<<(QDataStream& out,const Large& s) {
  40. out << s.k << s.m;
  41. return out;
  42. }
  43. friend QDataStream& operator>>(QDataStream& in,Large& s){
  44. in >> s.k >> s.m;
  45. return in;
  46. }
  47. bool operator==(const Large& other) const {
  48. return k == other.k && m == other.m;
  49. }
  50.  
  51.  
  52. };
  53. struct Aggregate {
  54. Large l;
  55. Medium m;
  56. QVector<Small> s;
  57. friend QDataStream& operator<<(QDataStream& out,const Aggregate& s) {
  58. out << s.l << s.m << s.s;
  59. return out;
  60. }
  61. friend QDataStream& operator>>(QDataStream& in,Aggregate& s){
  62. in >> s.l >> s.m >> s.s;
  63. return in;
  64. }
  65. bool operator==(const Aggregate& other) const {
  66. return l == other.l && m == other.m && s == other.s;
  67. }
  68.  
  69. };
  70.  
  71. int main(int argc, char *argv[])
  72. {
  73. QCoreApplication a(argc, argv);
  74. QFile file("test.txt");
  75. Aggregate ag{{5,{1,2.5f,{6}}},{2,3.5f,{25}},{{1},{2},{3}}};
  76. if (file.open(QIODevice::WriteOnly)) {
  77. QDataStream s(&file);
  78. s << ag;
  79. }
  80. file.close();
  81. QFile file_in("test.txt");
  82. if (file_in.open(QIODevice::ReadOnly)) {
  83. Aggregate g;
  84. QDataStream in(&file_in);
  85. in >> g;
  86. qDebug() << (g == ag);
  87. }
  88. QDataStream s;
  89. return a.exec();
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement