Heart 1.4.984.b4712811
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-2027 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__,
34 QString("Parsing error detected, check your parsing chain %0").
35 arg(typeid(*this).name()).toLatin1());
36 return true;
37}
38
39QByteArray StreamBase::toBytes() const {
40 QByteArray res;
41 QDataStream stream(&res, QIODevice::WriteOnly);
42
43 if (parsingVersion()) {
44 stream.setVersion(parsingVersion());
45 }
46
47 Q_ASSERT_X(isValid(), __FUNCTION__,
48 QString("Parsing error detected, check your parsing chain %0").
49 arg(typeid(*this).name()).toLatin1());
50
51 toStream(stream);
52 return res;
53}
54
55bool StreamBase::fromBase64(const QString &data) {
56 return fromBase64(data.toLatin1());
57}
58
59bool StreamBase::fromBase64(const QByteArray &data) {
60 return fromBytes(QByteArray::fromBase64(data, QByteArray::Base64UrlEncoding));
61}
62
63QByteArray StreamBase::toBase64() const {
64 return toBytes().toBase64(QByteArray::Base64UrlEncoding);
65}
66
68 return 0;
69}
70
71unsigned int StreamBase::typeId() const {
72 return typeid (this).hash_code();
73}
74
75QDataStream &operator<<(QDataStream &stream, const StreamBase &obj) {
76
77 Q_ASSERT_X(obj.isValid(), __FUNCTION__, "Parsing error detected, check your parsing chain");
78
79 return (&obj)->toStream(stream);
80}
81
82QDataStream &operator>>(QDataStream &stream, StreamBase &obj) {
83 (&obj)->fromStream(stream);
84 Q_ASSERT_X(obj.isValid(), __FUNCTION__, "Parsing error detected, check your parsing chain");
85 return stream;
86}
87
88}
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 wrapper 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