Heart 1.3.842.34c2ab5
Heart is base back end library for your c++ Qt projects.
streambase.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 STREAMBASE_H
9#define STREAMBASE_H
10
11#include <QByteArray>
12#include <QDataStream>
13#include <QVariantMap>
14#include "heart_global.h"
15#include <type_traits>
16
17class QDataStream;
18namespace QH {
19
20class Package;
21
28{
29public:
31 virtual ~StreamBase();
32
37 bool fromBytes(const QByteArray &data);
38
43 QByteArray toBytes() const;
44
51 bool fromBase64(const QString &data);
52
58 bool fromBase64(const QByteArray &data);
59
64 QByteArray toBase64() const;
65
72 HEARTSHARED_EXPORT friend QDataStream& operator<< (QDataStream& stream, const StreamBase& obj);
73
80 HEARTSHARED_EXPORT friend QDataStream& operator>> (QDataStream& stream, StreamBase& obj);
81
89 template<class T>
90 T& copy(const StreamBase& right) {
91 static_assert(std::is_base_of_v<StreamBase, T>,
92 "The argument of the copy method must be base type of the StreamBase class");
93
94 fromBytes(right.toBytes());
95 return static_cast<T&>(*this);
96 }
97
98 template<class T>
105 bool compare(const T& right) {
106 static_assert(std::is_base_of_v<StreamBase, T>,
107 "The argument of the compare method must be base type of the StreamBase class");
108
109 if (static_cast<unsigned int>(typeid (T).hash_code()) != typeId()) {
110 return false;
111 }
112
113 return toBytes() == right.toBytes();
114 }
115
116protected:
117
123 virtual int parsingVersion() const;
124
141 virtual QDataStream& fromStream(QDataStream& stream) = 0;
142
158 virtual QDataStream& toStream(QDataStream& stream) const = 0;
159
164 virtual unsigned int typeId() const;
165
166};
167}
168#endif // STREAMBASE_H
The StreamBase class add support streaming data for all children classes. For correctly working all s...
Definition streambase.h:28
QByteArray toBytes() const
toBytes This method convert a current object to bytes array.
virtual QDataStream & fromStream(QDataStream &stream)=0
fromStream This method should be read all bytes from the stream object and full the current object.
virtual QDataStream & toStream(QDataStream &stream) const =0
fromStream This method should be write all members of the current object to the stream object.
bool compare(const T &right)
compare This method compare array signatures of this and right objects.
Definition streambase.h:105
virtual ~StreamBase()
T & copy(const StreamBase &right)
copy This is base copy method for all StreamBase structures. Default implementation it is copy from b...
Definition streambase.h:90
#define HEARTSHARED_EXPORT
The QH namespace - QuasarApp Heart namespace. This namespace contains all classes of the Heart librar...
Definition heart.cpp:13