From 03b8758781bd82874024d5fa1f156ce326184769 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Mon, 1 Jul 2019 13:25:03 +0300 Subject: [PATCH] All classes which work with QDataStream must provide a header. --HG-- branch : develop --- src/libs/vmisc/def.cpp | 39 ++++++++++++++++++++++++++++ src/libs/vmisc/def.h | 14 ++++++---- src/libs/vpatterndb/vpiecenode.cpp | 3 +++ src/libs/vpatterndb/vpiecenode_p.h | 41 ++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 5 deletions(-) diff --git a/src/libs/vmisc/def.cpp b/src/libs/vmisc/def.cpp index 72c46aa7d..bbcfe5f8d 100644 --- a/src/libs/vmisc/def.cpp +++ b/src/libs/vmisc/def.cpp @@ -57,6 +57,7 @@ #include "vabstractapplication.h" #include "vdatastreamenum.h" +#include "../ifc/exception/vexception.h" const qreal defCurveApproximationScale = 0.5; const qreal minCurveApproximationScale = 0.2; @@ -722,26 +723,64 @@ void InitLanguages(QComboBox *combobox) } } +const quint32 CustomSARecord::streamHeader = 0xEBFF7586; // CRC-32Q string "CustomSARecord" +const quint16 CustomSARecord::classVersion = 1; + // Friend functions //--------------------------------------------------------------------------------------------------------------------- QDataStream &operator<<(QDataStream &out, const CustomSARecord &record) { + out << CustomSARecord::streamHeader << CustomSARecord::classVersion; + + // Added in classVersion = 1 out << record.startPoint; out << record.path; out << record.endPoint; out << record.reverse; out << record.includeType; + + // Added in classVersion = 2 + return out; } //--------------------------------------------------------------------------------------------------------------------- QDataStream &operator>>(QDataStream &in, CustomSARecord &record) { + quint32 actualStreamHeader = 0; + in >> actualStreamHeader; + + if (actualStreamHeader != CustomSARecord::streamHeader) + { + QString message = QCoreApplication::tr("CustomSARecord prefix mismatch error: actualStreamHeader = 0x%1 " + "and streamHeader = 0x%2") + .arg(actualStreamHeader, 8, 0x10, QChar('0')) + .arg(CustomSARecord::streamHeader, 8, 0x10, QChar('0')); + throw VException(message); + } + + quint16 actualClassVersion = 0; + in >> actualClassVersion; + + if (actualClassVersion > CustomSARecord::classVersion) + { + QString message = QCoreApplication::tr("CustomSARecord compatibility error: actualClassVersion = %1 and " + "classVersion = %2") + .arg(actualClassVersion).arg(CustomSARecord::classVersion); + throw VException(message); + } + in >> record.startPoint; in >> record.path; in >> record.endPoint; in >> record.reverse; in >> record.includeType; + +// if (actualClassVersion >= 2) +// { + +// } + return in; } diff --git a/src/libs/vmisc/def.h b/src/libs/vmisc/def.h index bdb656c16..d71440bb5 100644 --- a/src/libs/vmisc/def.h +++ b/src/libs/vmisc/def.h @@ -666,11 +666,15 @@ struct CustomSARecord friend QDataStream& operator<<(QDataStream& out, const CustomSARecord& record); friend QDataStream& operator>>(QDataStream& in, CustomSARecord& record); - quint32 startPoint; - quint32 path; - quint32 endPoint; - bool reverse; - PiecePathIncludeType includeType; + quint32 startPoint{0}; + quint32 path{0}; + quint32 endPoint{0}; + bool reverse{false}; + PiecePathIncludeType includeType{PiecePathIncludeType::AsCustomSA}; + +private: + static const quint32 streamHeader; + static const quint16 classVersion; }; Q_DECLARE_METATYPE(CustomSARecord) diff --git a/src/libs/vpatterndb/vpiecenode.cpp b/src/libs/vpatterndb/vpiecenode.cpp index fbda85c74..05a448da1 100644 --- a/src/libs/vpatterndb/vpiecenode.cpp +++ b/src/libs/vpatterndb/vpiecenode.cpp @@ -36,6 +36,9 @@ #include #include +const quint32 VPieceNodeData::streamHeader = 0x2198CBC8; // CRC-32Q string "VPieceNodeData" +const quint16 VPieceNodeData::classVersion = 1; + //--------------------------------------------------------------------------------------------------------------------- VPieceNode::VPieceNode() : d(new VPieceNodeData) diff --git a/src/libs/vpatterndb/vpiecenode_p.h b/src/libs/vpatterndb/vpiecenode_p.h index b4873e60f..056dd5797 100644 --- a/src/libs/vpatterndb/vpiecenode_p.h +++ b/src/libs/vpatterndb/vpiecenode_p.h @@ -31,7 +31,10 @@ #include #include +#include + #include "../ifc/ifcdef.h" +#include "../ifc/exception/vexception.h" #include "../vmisc/diagnostic.h" #include "../vmisc/vdatastreamenum.h" @@ -119,12 +122,18 @@ public: private: Q_DISABLE_ASSIGN(VPieceNodeData) + + static const quint32 streamHeader; + static const quint16 classVersion; }; // Friend functions //--------------------------------------------------------------------------------------------------------------------- QDataStream &operator<<(QDataStream &out, const VPieceNodeData &p) { + out << VPieceNodeData::streamHeader << VPieceNodeData::classVersion; + + // Added in classVersion = 1 out << p.m_id << p.m_typeTool << p.m_reverse @@ -139,12 +148,38 @@ QDataStream &operator<<(QDataStream &out, const VPieceNodeData &p) << p.m_isShowSecondPassmark << p.m_checkUniqueness << p.m_manualPassmarkLength; + + // Added in classVersion = 2 + return out; } //--------------------------------------------------------------------------------------------------------------------- QDataStream &operator>>(QDataStream &in, VPieceNodeData &p) { + quint32 actualStreamHeader = 0; + in >> actualStreamHeader; + + if (actualStreamHeader != VPieceNodeData::streamHeader) + { + QString message = QCoreApplication::tr("VPieceNodeData prefix mismatch error: actualStreamHeader = 0x%1 " + "and streamHeader = 0x%2") + .arg(actualStreamHeader, 8, 0x10, QChar('0')) + .arg(VPieceNodeData::streamHeader, 8, 0x10, QChar('0')); + throw VException(message); + } + + quint16 actualClassVersion = 0; + in >> actualClassVersion; + + if (actualClassVersion > VPieceNodeData::classVersion) + { + QString message = QCoreApplication::tr("VPieceNodeData compatibility error: actualClassVersion = %1 and " + "classVersion = %2") + .arg(actualClassVersion).arg(VPieceNodeData::classVersion); + throw VException(message); + } + in >> p.m_id >> p.m_typeTool >> p.m_reverse @@ -159,6 +194,12 @@ QDataStream &operator>>(QDataStream &in, VPieceNodeData &p) >> p.m_isShowSecondPassmark >> p.m_checkUniqueness >> p.m_manualPassmarkLength; + +// if (actualClassVersion >= 2) +// { + +// } + return in; }