Heart 1.3.842.34c2ab5
Heart is base back end library for your c++ Qt projects.
streambase.cpp
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#include "streambase.h"
9
10#include <QDataStream>
11#include <QIODevice>
12#include <QSharedPointer>
13
14namespace QH {
15
16
17StreamBase::StreamBase() = default;
18
19StreamBase::~StreamBase() = default;
20
21bool StreamBase::fromBytes(const QByteArray &data) {
22 if (data.isEmpty())
23 return false;
24
25 QDataStream stream(data);
26
27 if (parsingVersion()) {
28 stream.setVersion(parsingVersion());
29 }
30
31 fromStream(stream);
32 return true;
33}
34
35QByteArray StreamBase::toBytes() const {
36 QByteArray res;
37 QDataStream stream(&res, QIODevice::WriteOnly);
38
39 if (parsingVersion()) {
40 stream.setVersion(parsingVersion());
41 }
42
43 toStream(stream);
44 return res;
45}
46
47bool StreamBase::fromBase64(const QString &data) {
48 return fromBase64(data.toLatin1());
49}
50
51bool StreamBase::fromBase64(const QByteArray &data) {
52 return fromBytes(QByteArray::fromBase64(data, QByteArray::Base64UrlEncoding));
53}
54
55QByteArray StreamBase::toBase64() const {
56 return toBytes().toBase64(QByteArray::Base64UrlEncoding);
57}
58
60 return 0;
61}
62
63unsigned int StreamBase::typeId() const {
64 return typeid (this).hash_code();
65}
66
67QDataStream &operator<<(QDataStream &stream, const StreamBase &obj) {
68 return (&obj)->toStream(stream);
69}
70
71QDataStream &operator>>(QDataStream &stream, StreamBase &obj) {
72 return (&obj)->fromStream(stream);
73}
74
75}
The StreamBase class add support streaming data for all children classes. For correctly working all s...
Definition streambase.h:28
virtual unsigned int typeId() const
typeId This method return id of type.
bool fromBytes(const QByteArray &data)
fromBytes This method provide initialization of object from byte array.
virtual int parsingVersion() const
parsingVersion this method return parsing version of Qt. By Default is 0 (last available parsing)....
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 fromBase64(const QString &data)
fromBase64 This method provide initialization of object from the base64 string.
virtual ~StreamBase()
QByteArray toBase64() const
toBase64 This method convert a current object to the base64 string.
The QH namespace - QuasarApp Heart namespace. This namespace contains all classes of the Heart librar...
Definition heart.cpp:13
QDataStream & operator>>(QDataStream &stream, HostAddress &address)
QDataStream & operator<<(QDataStream &stream, const HostAddress &address)