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

added UniversalData class

This commit is contained in:
Andrei Yankovich 2023-04-19 22:50:28 +02:00
parent 94b8c9bba7
commit 0c264fba89
5 changed files with 141 additions and 1 deletions

@ -1,6 +1,6 @@
/*
* Copyright (C) 2018-2023 QuasarApp.
* Distributed under the lgplv3 software license, see the accompanying
* Distributed under the GPLv3 software license, see the accompanying
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/

@ -0,0 +1,40 @@
/*
* Copyright (C) 2023-2023 QuasarApp.
* Distributed under the GPLv3 software license, see the accompanying
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
#include "universaldata.h"
namespace QH {
namespace PKG {
UniversalData::UniversalData()
{
}
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);
}
QDataStream &UniversalData::fromStream(QDataStream &stream) {
stream >> _data;
return stream;
}
QDataStream &UniversalData::toStream(QDataStream &stream) const {
stream << _data;
return stream;
}
}
}

@ -0,0 +1,79 @@
/*
* Copyright (C) 2023-2023 QuasarApp.
* Distributed under the GPLv3 software license, see the accompanying
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
#ifndef UNIVERSALDATA_H
#define UNIVERSALDATA_H
#include "abstractdata.h"
namespace QH {
namespace PKG {
/**
* @brief The UniversalData class contains all data of object in the QMap
* Use this package if you want to change your packages in prodaction.
*
* **Example:**
*
* @code{cpp}
* class AuthRequest: public QH::PKG::UniversalData
{
QH_PACKAGE_AUTO(RC::API::V4::AuthRequest)
enum Filds{
UserId = 0
};
public:
AuthRequest();
bool isValid() const override;
QByteArray userId() const;
void setUserId(const QByteArray &newUserId);
};
QByteArray AuthRequest::userId() const {
return value(UserId).toByteArray();
}
void AuthRequest::setUserId(const QByteArray &newUserId) {
setValue(UserId, newUserId);
}
* @endcode
*
*/
class HEARTSHARED_EXPORT UniversalData: public QH::PKG::AbstractData
{
public:
UniversalData();
/**
* @brief setValue This method sets new value for the filed. Works like a insert method of the QMap
* @param key This is key of the filed.
* @param value This is value of the key.
*/
void setValue(int key, const QVariant& value);
/**
* @brief value this method return value of the key.
* @return value of the key.
*/
QVariant value(int key, const QVariant& defaultVal = {}) const;
protected:
QDataStream &fromStream(QDataStream &stream) override;
QDataStream &toStream(QDataStream &stream) const override;
private:
QMap<int, QVariant> _data;
};
}
}
#endif // UNIVERSALDATA_H

@ -23,6 +23,11 @@ bool StreamBase::fromBytes(const QByteArray &data) {
return false;
QDataStream stream(data);
if (parsingVersion()) {
stream.setVersion(parsingVersion());
}
fromStream(stream);
return true;
}
@ -30,6 +35,11 @@ bool StreamBase::fromBytes(const QByteArray &data) {
QByteArray StreamBase::toBytes() const {
QByteArray res;
QDataStream stream(&res, QIODevice::WriteOnly);
if (parsingVersion()) {
stream.setVersion(parsingVersion());
}
toStream(stream);
return res;
}
@ -46,6 +56,10 @@ QByteArray StreamBase::toBase64() const {
return toBytes().toBase64(QByteArray::Base64UrlEncoding);
}
int StreamBase::parsingVersion() const {
return 0;
}
unsigned int StreamBase::typeId() const {
return typeid (*this).hash_code();
}

@ -115,6 +115,13 @@ public:
protected:
/**
* @brief parsingVersion this method return parsing version of Qt. By Default is 0 (last available parsing).
* see https://doc.qt.io/qt-6/qdatastream.html#Version-enum
* @return version number.
*/
virtual int parsingVersion() const;
/**
* @brief fromStream This method should be read all bytes from the stream object and full the current object.
* @note The implementation of this method should be invoke a method of base class.