Heart 1.3.842.34c2ab5
Heart is base back end library for your c++ Qt projects.
datapack.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2018-2024 QuasarApp.
3 * Distributed under the lgplv3 software license, see the accompanying
4 * Everyone is permitted to copy and distribute verbatim copies
5 * of this license document, but changing it is not allowed.
6*/
7
8#ifndef DATAPACK_H
9#define DATAPACK_H
10
11#include <abstractdata.h>
12
13namespace QH {
14namespace PKG {
15
24template<class Package>
25class DataPack final: public AbstractData
26{
27 QH_PACKAGE(Package::commandText() + "Pack")
28
29public:
30
31 DataPack(const QList<QSharedPointer<Package>> &newPackData = {}) {
32 setPackData(newPackData);
33#ifdef HEART_VALIDATE_PACKS
34 static_assert(std::is_base_of_v<UniversalData, Package> &&
35 "The template class of DataPack must be child of the UniversalData class");
36#endif
37 }
38
43 unsigned int size() const {
44 return _packData.size();
45 }
46
51 const QList<QSharedPointer<Package>> &packData() const {
52 return _packData;
53 }
54
59 void setPackData(const QList<QSharedPointer<Package>> &newPackData) {
60 _packData = newPackData;
61 }
62
67 void push(const QSharedPointer<Package>& data) {
68 _packData.push_back(data);
69 };
70
75 void push(const Package& data) {
76 _packData.push_back(QSharedPointer<Package>::create(data));
77 };
78
83 bool isValid() const override {
84
85 if (!_packData.size()) {
86 return false;
87 }
88
89 for (const auto& it: _packData) {
90 if (!it->isValid()) {
91 return false;
92 }
93 }
94
95 return AbstractData::isValid();
96 };
97
104 const QByteArray &customData() const {
105 return _data;
106 }
107
112 void setCustomData(const QByteArray &newToken) {
113 _data = newToken;
114 }
115
116protected:
117 QDataStream &fromStream(QDataStream &stream) override {
118
119 int size = 0;
120 stream >> size;
121
122 for (int i = 0; i < size; ++i) {
123 auto data = QSharedPointer<Package>::create();
124 stream >> *data;
125
126 _packData.push_back(data);
127 }
128
129 stream >> _data;
130
131 return stream;
132 };
133
134 QDataStream &toStream(QDataStream &stream) const override {
135 stream << static_cast<int>(_packData.size());
136
137 for (const auto &data: std::as_const(_packData)) {
138 stream << *data;
139 }
140
141 stream << _data;
142
143 return stream;
144 }
145
146private:
147 QList<QSharedPointer<Package>> _packData;
148 QByteArray _data;
149
150};
151
152}
153}
154#endif // DATAPACK_H
#define QH_PACKAGE(S)
QH_PACKAGE This macross prepare data to send and create a global id for package. For get global id us...
The AbstractData class is provide base functions for transport data by network For create you own pac...
virtual bool isValid() const
isValid This method check current object to valid.
DataPack this is conteiner is wraqper of the QList for transport arrays to another node.
Definition datapack.h:26
bool isValid() const override
isValid This implementation check all items of the pack to valid and packa size. The pack size should...
Definition datapack.h:83
void setCustomData(const QByteArray &newToken)
setCustomData This method sets custom data block
Definition datapack.h:112
const QByteArray & customData() const
token is custom data block.
Definition datapack.h:104
void push(const QSharedPointer< Package > &data)
push This method append data to end of list.
Definition datapack.h:67
QDataStream & fromStream(QDataStream &stream) override
fromStream This method should be read all bytes from the stream object and full the current object.
Definition datapack.h:117
void setPackData(const QList< QSharedPointer< Package > > &newPackData)
setPackData This method sets new lsit of pacakges.
Definition datapack.h:59
unsigned int size() const
size This method return of the items count of this pack.
Definition datapack.h:43
DataPack(const QList< QSharedPointer< Package > > &newPackData={})
Definition datapack.h:31
QDataStream & toStream(QDataStream &stream) const override
fromStream This method should be write all members of the current object to the stream object.
Definition datapack.h:134
const QList< QSharedPointer< Package > > & packData() const
packData This method return source list of the elements.
Definition datapack.h:51
void push(const Package &data)
push This method append data to end of list.
Definition datapack.h:75
The Package struct. This is base structure for transporting data by network between QH nodes....
Definition package.h:23
The QH namespace - QuasarApp Heart namespace. This namespace contains all classes of the Heart librar...
Definition heart.cpp:13