Heart 1.4.918.d927d9c
Heart is base back end library for your c++ Qt projects.
Loading...
Searching...
No Matches
streambase.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2018-2025 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
33 Q_ASSERT_X(isValid(), __FUNCTION__, "Parsing error detected, check your parsing chain");
34
35 return true;
36}
37
38QByteArray StreamBase::toBytes() const {
39 QByteArray res;
40 QDataStream stream(&res, QIODevice::WriteOnly);
41
42 if (parsingVersion()) {
43 stream.setVersion(parsingVersion());
44 }
45
46 Q_ASSERT_X(isValid(), __FUNCTION__, "Parsing error detected, check your parsing chain");
47
48 toStream(stream);
49 return res;
50}
51
52bool StreamBase::fromBase64(const QString &data) {
53 return fromBase64(data.toLatin1());
54}
55
56bool StreamBase::fromBase64(const QByteArray &data) {
57 return fromBytes(QByteArray::fromBase64(data, QByteArray::Base64UrlEncoding));
58}
59
60QByteArray StreamBase::toBase64() const {
61 return toBytes().toBase64(QByteArray::Base64UrlEncoding);
62}
63
65 return 0;
66}
67
68unsigned int StreamBase::typeId() const {
69 return typeid (this).hash_code();
70}
71
72QDataStream &operator<<(QDataStream &stream, const StreamBase &obj) {
73
74 Q_ASSERT_X(obj.isValid(), __FUNCTION__, "Parsing error detected, check your parsing chain");
75
76 return (&obj)->toStream(stream);
77}
78
79QDataStream &operator>>(QDataStream &stream, StreamBase &obj) {
80 (&obj)->fromStream(stream);
81 Q_ASSERT_X(obj.isValid(), __FUNCTION__, "Parsing error detected, check your parsing chain");
82 return stream;
83}
84
85}
virtual unsigned int typeId() const
typeId This method return id of type.
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.
HEARTSHARED_EXPORT friend QDataStream & operator>>(QDataStream &stream, StreamBase &obj)
This is wraper over fromStream. For see more information see toStream method.
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.
HEARTSHARED_EXPORT friend QDataStream & operator<<(QDataStream &stream, const StreamBase &obj)
This is wrapper over toStream.
virtual ~StreamBase()
QByteArray toBase64() const
toBase64 This method convert a current object to the base64 string.
bool fromBytes(const QByteArray &dataz)
fromBytes This method provide initialization of object from byte array.
The QH namespace - QuasarApp Heart namespace. This namespace contains all classes of the Heart librar...
Definition heart.cpp:13