From 1f462586cdaef29aae9410504145b163303cd371 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Tue, 26 Mar 2019 19:02:12 +0200 Subject: [PATCH] Convert to implicitly shared object. --HG-- branch : develop --- src/libs/vlayout/vbestsquare.cpp | 117 ++++++++++++++++++++++-------- src/libs/vlayout/vbestsquare.h | 81 ++++----------------- src/libs/vlayout/vbestsquare_p.h | 81 +++++++++++++++++++++ src/libs/vlayout/vlayout.pri | 3 +- src/libs/vlayout/vlayoutpaper.cpp | 4 +- 5 files changed, 187 insertions(+), 99 deletions(-) create mode 100644 src/libs/vlayout/vbestsquare_p.h diff --git a/src/libs/vlayout/vbestsquare.cpp b/src/libs/vlayout/vbestsquare.cpp index 1fedbaf26..50e2922ed 100644 --- a/src/libs/vlayout/vbestsquare.cpp +++ b/src/libs/vlayout/vbestsquare.cpp @@ -27,13 +27,14 @@ *************************************************************************/ #include "vbestsquare.h" +#include "vbestsquare_p.h" #include namespace { //--------------------------------------------------------------------------------------------------------------------- -qint64 Square(const QSizeF &size) +Q_DECL_CONSTEXPR inline qint64 Square(const QSizeF &size) { return static_cast(size.width()*size.height()); } @@ -41,16 +42,16 @@ qint64 Square(const QSizeF &size) //--------------------------------------------------------------------------------------------------------------------- VBestSquare::VBestSquare(const QSizeF &sheetSize, bool saveLength) - : resI(0), - resJ(0), - resMatrix(), - bestSize(QSizeF(sheetSize.width()+10, sheetSize.height()+10)), - sheetSize(sheetSize), - valideResult(false), - resMirror(false), - type(BestFrom::Rotation), - saveLength(saveLength), - position(INT_MAX) + : d(new VBestSquareData(sheetSize, saveLength)) +{} + +//--------------------------------------------------------------------------------------------------------------------- +VBestSquare::VBestSquare(const VBestSquare &res) + : d(res.d) +{} + +//--------------------------------------------------------------------------------------------------------------------- +VBestSquare::~VBestSquare() {} //--------------------------------------------------------------------------------------------------------------------- @@ -59,36 +60,36 @@ void VBestSquare::NewResult(const QSizeF &candidate, int i, int j, const QTransf { auto SaveResult = [this, candidate, i, j, matrix, mirror, type, position]() { - bestSize = candidate; - resI = i; - resJ = j; - resMatrix = matrix; - valideResult = true; - resMirror = mirror; - this->type = type; - this->position = position; + d->bestSize = candidate; + d->resI = i; + d->resJ = j; + d->resMatrix = matrix; + d->valideResult = true; + d->resMirror = mirror; + d->type = type; + d->position = position; }; - if (saveLength) + if (d->saveLength) { - const bool isPortrait = sheetSize.height() >= sheetSize.width(); - const QSizeF saveSpaceSize = isPortrait ? QSizeF(sheetSize.width(), candidate.height()) : - QSizeF(candidate.width(), sheetSize.height()); + const bool isPortrait = d->sheetSize.height() >= d->sheetSize.width(); + const QSizeF saveSpaceSize = isPortrait ? QSizeF(d->sheetSize.width(), candidate.height()) : + QSizeF(candidate.width(), d->sheetSize.height()); - const QSizeF saveSpaceBestSize = isPortrait ? QSizeF(sheetSize.width(), bestSize.height()) : - QSizeF(bestSize.width(), sheetSize.height()); + const QSizeF saveSpaceBestSize = isPortrait ? QSizeF(d->sheetSize.width(), d->bestSize.height()) : + QSizeF(d->bestSize.width(), d->sheetSize.height()); - if (Square(saveSpaceSize) <= Square(saveSpaceBestSize) && Square(candidate) <= Square(bestSize) - && position <= this->position && Square(saveSpaceSize) > 0 && Square(saveSpaceBestSize) > 0 - && type >= this->type) + if (Square(saveSpaceSize) <= Square(saveSpaceBestSize) && Square(candidate) <= Square(d->bestSize) + && position <= d->position && Square(saveSpaceSize) > 0 && Square(saveSpaceBestSize) > 0 + && type >= d->type) { SaveResult(); } } else { - if (Square(candidate) <= Square(bestSize) && Square(candidate) > 0 && position <= this->position - && type >= this->type) + if (Square(candidate) <= Square(d->bestSize) && Square(candidate) > 0 && position <= d->position + && type >= d->type) { SaveResult(); } @@ -98,9 +99,63 @@ void VBestSquare::NewResult(const QSizeF &candidate, int i, int j, const QTransf //--------------------------------------------------------------------------------------------------------------------- void VBestSquare::NewResult(const VBestSquare &best) { - if (best.ValidResult() && saveLength == best.IsSaveLength()) + if (best.IsValidResult() && d->saveLength == best.IsSaveLength()) { NewResult(best.BestSize(), best.GContourEdge(), best.DetailEdge(), best.Matrix(), best.Mirror(), best.Position(), best.Type()); } } + +//--------------------------------------------------------------------------------------------------------------------- +inline QSizeF VBestSquare::BestSize() const +{ + return d->bestSize; +} + +//--------------------------------------------------------------------------------------------------------------------- +inline int VBestSquare::GContourEdge() const +{ + return d->resI; +} + +//--------------------------------------------------------------------------------------------------------------------- +int VBestSquare::DetailEdge() const +{ + return d->resJ; +} + +//--------------------------------------------------------------------------------------------------------------------- +QTransform VBestSquare::Matrix() const +{ + return d->resMatrix; +} + +//--------------------------------------------------------------------------------------------------------------------- +bool VBestSquare::IsValidResult() const +{ + return d->valideResult; +} + +//--------------------------------------------------------------------------------------------------------------------- +bool VBestSquare::Mirror() const +{ + return d->resMirror; +} + +//--------------------------------------------------------------------------------------------------------------------- +BestFrom VBestSquare::Type() const +{ + return d->type; +} + +//--------------------------------------------------------------------------------------------------------------------- +qreal VBestSquare::Position() const +{ + return d->position; +} + +//--------------------------------------------------------------------------------------------------------------------- +bool VBestSquare::IsSaveLength() const +{ + return d->saveLength; +} diff --git a/src/libs/vlayout/vbestsquare.h b/src/libs/vlayout/vbestsquare.h index 1cb3b06e7..f400fecf0 100644 --- a/src/libs/vlayout/vbestsquare.h +++ b/src/libs/vlayout/vbestsquare.h @@ -32,13 +32,26 @@ #include #include #include +#include +#include #include "vlayoutdef.h" +class VBestSquareData; + class VBestSquare { public: VBestSquare(const QSizeF &sheetSize, bool saveLength); + VBestSquare(const VBestSquare &res); + virtual ~VBestSquare(); + +#ifdef Q_COMPILER_RVALUE_REFS + VBestSquare &operator=(VBestSquare &&res) Q_DECL_NOTHROW { Swap(res); return *this; } +#endif + + inline void Swap(VBestSquare &res) Q_DECL_NOTHROW + { std::swap(d, res.d); } void NewResult(const QSizeF &candidate, int i, int j, const QTransform &matrix, bool mirror, qreal position, BestFrom type); @@ -48,7 +61,7 @@ public: int GContourEdge() const; int DetailEdge() const; QTransform Matrix() const; - bool ValidResult() const; + bool IsValidResult() const; bool Mirror() const; BestFrom Type() const; qreal Position() const; @@ -56,71 +69,9 @@ public: bool IsSaveLength() const; private: - // All nedded information about best result - int resI; // Edge of global contour - int resJ; // Edge of detail - QTransform resMatrix; // Matrix for rotation and translation detail - QSizeF bestSize; - QSizeF sheetSize; - bool valideResult; - bool resMirror; - BestFrom type; - bool saveLength; - qreal position; + QSharedDataPointer d; }; -//--------------------------------------------------------------------------------------------------------------------- -inline QSizeF VBestSquare::BestSize() const -{ - return bestSize; -} - -//--------------------------------------------------------------------------------------------------------------------- -inline int VBestSquare::GContourEdge() const -{ - return resI; -} - -//--------------------------------------------------------------------------------------------------------------------- -inline int VBestSquare::DetailEdge() const -{ - return resJ; -} - -//--------------------------------------------------------------------------------------------------------------------- -inline QTransform VBestSquare::Matrix() const -{ - return resMatrix; -} - -//--------------------------------------------------------------------------------------------------------------------- -inline bool VBestSquare::ValidResult() const -{ - return valideResult; -} - -//--------------------------------------------------------------------------------------------------------------------- -inline bool VBestSquare::Mirror() const -{ - return resMirror; -} - -//--------------------------------------------------------------------------------------------------------------------- -inline BestFrom VBestSquare::Type() const -{ - return type; -} - -//--------------------------------------------------------------------------------------------------------------------- -inline qreal VBestSquare::Position() const -{ - return position; -} - -//--------------------------------------------------------------------------------------------------------------------- -inline bool VBestSquare::IsSaveLength() const -{ - return saveLength; -} +Q_DECLARE_TYPEINFO(VBestSquare, Q_MOVABLE_TYPE); #endif // VBESTSQUARE_H diff --git a/src/libs/vlayout/vbestsquare_p.h b/src/libs/vlayout/vbestsquare_p.h new file mode 100644 index 000000000..05842566c --- /dev/null +++ b/src/libs/vlayout/vbestsquare_p.h @@ -0,0 +1,81 @@ +/************************************************************************ + ** + ** @file vbestsquare_p.h + ** @author Roman Telezhynskyi + ** @date 26 3, 2019 + ** + ** @brief + ** @copyright + ** This source code is part of the Valentina project, a pattern making + ** program, whose allow create and modeling patterns of clothing. + ** Copyright (C) 2019 Valentina project + ** All Rights Reserved. + ** + ** Valentina is free software: you can redistribute it and/or modify + ** it under the terms of the GNU General Public License as published by + ** the Free Software Foundation, either version 3 of the License, or + ** (at your option) any later version. + ** + ** Valentina is distributed in the hope that it will be useful, + ** but WITHOUT ANY WARRANTY; without even the implied warranty of + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + ** GNU General Public License for more details. + ** + ** You should have received a copy of the GNU General Public License + ** along with Valentina. If not, see . + ** + *************************************************************************/ +#ifndef VBESTSQUARE_P_H +#define VBESTSQUARE_P_H + +#include +#include + +#include "vlayoutdef.h" +#include "../vmisc/diagnostic.h" + +QT_WARNING_PUSH +QT_WARNING_DISABLE_GCC("-Weffc++") +QT_WARNING_DISABLE_GCC("-Wnon-virtual-dtor") + +class VBestSquareData : public QSharedData +{ +public: + VBestSquareData(const QSizeF &sheetSize, bool saveLength) + : bestSize(QSizeF(sheetSize.width()+10, sheetSize.height()+10)), + sheetSize(sheetSize), + saveLength(saveLength) + {} + + VBestSquareData(const VBestSquareData &res) + : QSharedData(res), + resI(res.resI), + resJ(res.resJ), + resMatrix(res.resMatrix), + bestSize(res.bestSize), + sheetSize(res.sheetSize), + valideResult(res.valideResult), + resMirror(res.resMirror), + type(res.type), + saveLength(res.saveLength), + position(res.position) + {} + + ~VBestSquareData() {} + + int resI{0}; // Edge of global contour + int resJ{0}; // Edge of detail + QTransform resMatrix{}; // Matrix for rotation and translation detail + QSizeF bestSize; + QSizeF sheetSize; + bool valideResult{false}; + bool resMirror{false}; + BestFrom type{BestFrom::Rotation}; + bool saveLength; + qreal position{INT_MAX}; + +private: + VBestSquareData &operator=(const VBestSquareData &) Q_DECL_EQ_DELETE; +}; + +#endif // VBESTSQUARE_P_H diff --git a/src/libs/vlayout/vlayout.pri b/src/libs/vlayout/vlayout.pri index 892ebd7fa..d93cbfcf7 100644 --- a/src/libs/vlayout/vlayout.pri +++ b/src/libs/vlayout/vlayout.pri @@ -20,7 +20,8 @@ HEADERS += \ $$PWD/vlayoutpiece.h \ $$PWD/vlayoutpiece_p.h \ $$PWD/vlayoutpiecepath.h \ - $$PWD/vlayoutpiecepath_p.h + $$PWD/vlayoutpiecepath_p.h \ + $$PWD/vbestsquare_p.h SOURCES += \ $$PWD/vlayoutgenerator.cpp \ diff --git a/src/libs/vlayout/vlayoutpaper.cpp b/src/libs/vlayout/vlayoutpaper.cpp index fcef8b617..de22cba95 100644 --- a/src/libs/vlayout/vlayoutpaper.cpp +++ b/src/libs/vlayout/vlayoutpaper.cpp @@ -302,7 +302,7 @@ bool VLayoutPaper::AddToSheet(const VLayoutPiece &detail, std::atomic_bool &stop //--------------------------------------------------------------------------------------------------------------------- bool VLayoutPaper::SaveResult(const VBestSquare &bestResult, const VLayoutPiece &detail) { - if (bestResult.ValidResult()) + if (bestResult.IsValidResult()) { VLayoutPiece workDetail = detail; workDetail.SetMatrix(bestResult.Matrix());// Don't forget set matrix @@ -324,7 +324,7 @@ bool VLayoutPaper::SaveResult(const VBestSquare &bestResult, const VLayoutPiece #endif } - return bestResult.ValidResult(); // Do we have the best result? + return bestResult.IsValidResult(); // Do we have the best result? } //---------------------------------------------------------------------------------------------------------------------