4
1
mirror of https://github.com/QuasarApp/Heart.git synced 2025-05-12 01:19:42 +00:00

simple fixes of the universal package

This commit is contained in:
Andrei Yankovich 2023-04-24 08:26:46 +02:00
parent 1859522855
commit 01429a41c2
4 changed files with 36 additions and 15 deletions

@ -4,12 +4,3 @@
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
#include "datapack.h"
namespace QH {
namespace PKG {
}
}

@ -8,7 +8,7 @@
#ifndef DATAPACK_H
#define DATAPACK_H
#include "abstractdata.h"
#include "universaldata.h"
namespace QH {
@ -17,6 +17,10 @@ namespace PKG {
/**
* @brief DataPack this is conteiner is wraqper of the QList for transport arrays to another node.
* @tparam Package This is type of trasported pacakges item.
* @note All packs data objects should be inherited of the UniversalData class.
* This is due to classes base on abstract data may broken transporting data between nodes if you change toStream and fromStream methods.
* To fix this issue, we use QVariantMap container for parsing data.
* @see UniversalData
*/
template<class Package>
class DataPack final: public AbstractData
@ -27,6 +31,8 @@ public:
DataPack(const QList<QSharedPointer<Package>> &newPackData = {}) {
setPackData(newPackData);
static_assert(std::is_base_of_v<UniversalData, Package> &&
"The template class of DataPack must be child of the UniversalData class");
}
/**

@ -11,7 +11,6 @@
namespace QH {
namespace PKG {
UniversalData::UniversalData()
{
@ -21,8 +20,26 @@ void UniversalData::setValue(int key, const QVariant &value) {
_data[key] = value;
}
QVariant UniversalData::value(int key, const QVariant &defaultVal) const {
return _data.value(key, defaultVal);
const QVariant &UniversalData::value(int key, const QVariant &defaultVal) const {
auto nonConstPtr = const_cast<QHash<int, QVariant>*>(&_data);
if (!nonConstPtr)
return defaultVal;
if (nonConstPtr->contains(key)) {
return nonConstPtr->operator[](key);
}
return defaultVal;
}
QVariant *UniversalData::ref(int key) {
if (_data.contains(key)) {
return &_data.operator[](key);
}
return nullptr;
}
QDataStream &UniversalData::fromStream(QDataStream &stream) {

@ -63,14 +63,21 @@ public:
* @brief value this method return value of the key.
* @return value of the key.
*/
QVariant value(int key, const QVariant& defaultVal = {}) const;
const QVariant& value(int key, const QVariant& defaultVal = {}) const;
/**
* @brief ref This method return pointer to the object If this object is not exists return nullptr
* @param key This is key of object
* @return pointer to the required object.
*/
QVariant* ref(int key);
protected:
QDataStream &fromStream(QDataStream &stream) override final;
QDataStream &toStream(QDataStream &stream) const override final;
private:
QMap<int, QVariant> _data;
QHash<int, QVariant> _data;
};
}