include/svx/EnhancedCustomShape2d.hxx | 10 - svx/qa/unit/customshapes.cxx | 12 + svx/source/customshapes/EnhancedCustomShape2d.cxx | 111 +++++++----------- svx/source/customshapes/EnhancedCustomShapeEngine.cxx | 4 4 files changed, 63 insertions(+), 74 deletions(-)
New commits: commit 3dcd7d8e412e2f05a213695eeb3ea2a296e9da73 Author: Noel <noel.gran...@collabora.co.uk> AuthorDate: Wed Feb 3 08:59:13 2021 +0200 Commit: Tünde Tóth <toth.tu...@nisz.hu> CommitDate: Thu Oct 21 10:07:04 2021 +0200 use more unique_ptr in EnhancedCustomShape2d and fix some leaks in the unit tests Change-Id: I8d324a24de11b14b4820e3cdd7f078d5877489e3 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/110288 Tested-by: Jenkins Reviewed-by: Noel Grandin <noel.gran...@collabora.co.uk> Reviewed-on: https://gerrit.libreoffice.org/c/core/+/123954 Tested-by: Tünde Tóth <toth.tu...@nisz.hu> Reviewed-by: Tünde Tóth <toth.tu...@nisz.hu> diff --git a/include/svx/EnhancedCustomShape2d.hxx b/include/svx/EnhancedCustomShape2d.hxx index 8976b2bdb106..a01e8411b9c7 100644 --- a/include/svx/EnhancedCustomShape2d.hxx +++ b/include/svx/EnhancedCustomShape2d.hxx @@ -22,6 +22,8 @@ #include <svx/msdffdef.hxx> #include <svx/sdasitm.hxx> +#include <svx/svdotext.hxx> +#include <tools/degree.hxx> #include <com/sun/star/awt/Size.hpp> #include <com/sun/star/uno/Sequence.h> #include <com/sun/star/beans/PropertyValues.hpp> @@ -141,11 +143,11 @@ class SVXCORE_DLLPUBLIC EnhancedCustomShape2d : public SfxItemSet SAL_DLLPRIVATE void CreateSubPath( sal_Int32& rSrcPt, sal_Int32& rSegmentInd, - std::vector< std::pair< SdrPathObj*, double> >& rObjectList, + std::vector< std::pair< SdrPathObjUniquePtr, double> >& rObjectList, bool bLineGeometryNeededOnly, bool bSortFilledObjectsToBack, sal_Int32 nIndex); - SAL_DLLPRIVATE SdrObject* CreatePathObj( bool bLineGeometryNeededOnly ); + SAL_DLLPRIVATE SdrObjectUniquePtr CreatePathObj( bool bLineGeometryNeededOnly ); SAL_DLLPRIVATE void ApplyShapeAttributes( const SdrCustomShapeGeometryItem& rItem ); SAL_DLLPRIVATE void SetPathSize( sal_Int32 nIndex = 0 ); @@ -185,8 +187,8 @@ class SVXCORE_DLLPUBLIC EnhancedCustomShape2d : public SfxItemSet SAL_DLLPRIVATE bool IsFlipHorz() const { return bFlipH; }; SAL_DLLPRIVATE sal_Int32 GetRotateAngle() const { return nRotateAngle; }; - SdrObject* CreateLineGeometry(); - SdrObject* CreateObject( bool bLineGeometryNeededOnly ); + SdrObjectUniquePtr CreateLineGeometry(); + SdrObjectUniquePtr CreateObject( bool bLineGeometryNeededOnly ); void ApplyGluePoints( SdrObject* pObj ); tools::Rectangle GetTextRect() const; const tools::Rectangle& GetLogicRect() const { return aLogicRect; } diff --git a/svx/qa/unit/customshapes.cxx b/svx/qa/unit/customshapes.cxx index db7808a16f2f..791588f1984d 100644 --- a/svx/qa/unit/customshapes.cxx +++ b/svx/qa/unit/customshapes.cxx @@ -383,7 +383,8 @@ CPPUNIT_TEST_FIXTURE(CustomshapesTest, testTdf121845_two_commands_U) SdrObjCustomShape& rSdrObjCustomShape( static_cast<SdrObjCustomShape&>(*GetSdrObjectFromXShape(xShape))); EnhancedCustomShape2d aCustomShape2d(rSdrObjCustomShape); - SdrPathObj* pPathObj = static_cast<SdrPathObj*>(aCustomShape2d.CreateLineGeometry()); + SdrPathObjUniquePtr pPathObj( + static_cast<SdrPathObj*>(aCustomShape2d.CreateLineGeometry().release())); CPPUNIT_ASSERT_MESSAGE("Could not convert to SdrPathObj", pPathObj); const basegfx::B2DPolyPolygon aPolyPolygon(pPathObj->GetPathPoly()); CPPUNIT_ASSERT_EQUAL_MESSAGE("count polygons", static_cast<sal_uInt32>(2), @@ -811,7 +812,8 @@ CPPUNIT_TEST_FIXTURE(CustomshapesTest, testTdf103474_commandT_CaseZeroHeight) SdrObjCustomShape& rSdrObjCustomShape( static_cast<SdrObjCustomShape&>(*GetSdrObjectFromXShape(xShape))); EnhancedCustomShape2d aCustomShape2d(rSdrObjCustomShape); - SdrPathObj* pPathObj = static_cast<SdrPathObj*>(aCustomShape2d.CreateLineGeometry()); + SdrPathObjUniquePtr pPathObj( + static_cast<SdrPathObj*>(aCustomShape2d.CreateLineGeometry().release())); CPPUNIT_ASSERT_MESSAGE("Could not convert to SdrPathObj", pPathObj); const basegfx::B2DPolyPolygon aPolyPolygon(pPathObj->GetPathPoly()); CPPUNIT_ASSERT_EQUAL_MESSAGE("count polygons", static_cast<sal_uInt32>(1), @@ -841,7 +843,8 @@ CPPUNIT_TEST_FIXTURE(CustomshapesTest, testTdf103474_commandG_CaseZeroHeight) SdrObjCustomShape& rSdrObjCustomShape( static_cast<SdrObjCustomShape&>(*GetSdrObjectFromXShape(xShape))); EnhancedCustomShape2d aCustomShape2d(rSdrObjCustomShape); - SdrPathObj* pPathObj = static_cast<SdrPathObj*>(aCustomShape2d.CreateLineGeometry()); + SdrPathObjUniquePtr pPathObj( + static_cast<SdrPathObj*>(aCustomShape2d.CreateLineGeometry().release())); CPPUNIT_ASSERT_MESSAGE("Could not convert to SdrPathObj", pPathObj); const basegfx::B2DPolyPolygon aPolyPolygon(pPathObj->GetPathPoly()); CPPUNIT_ASSERT_EQUAL_MESSAGE("count polygons", static_cast<sal_uInt32>(1), @@ -871,7 +874,8 @@ CPPUNIT_TEST_FIXTURE(CustomshapesTest, testTdf122323_largeSwingAngle) SdrObjCustomShape& rSdrObjCustomShape( static_cast<SdrObjCustomShape&>(*GetSdrObjectFromXShape(xShape))); EnhancedCustomShape2d aCustomShape2d(rSdrObjCustomShape); - SdrPathObj* pPathObj = static_cast<SdrPathObj*>(aCustomShape2d.CreateLineGeometry()); + SdrPathObjUniquePtr pPathObj( + static_cast<SdrPathObj*>(aCustomShape2d.CreateLineGeometry().release())); CPPUNIT_ASSERT_MESSAGE("Could not convert to SdrPathObj", pPathObj); const basegfx::B2DPolyPolygon aPolyPolygon(pPathObj->GetPathPoly()); const basegfx::B2DPolygon aPolygon(aPolyPolygon.getB2DPolygon(0)); diff --git a/svx/source/customshapes/EnhancedCustomShape2d.cxx b/svx/source/customshapes/EnhancedCustomShape2d.cxx index a509520ba08c..556e440e491c 100644 --- a/svx/source/customshapes/EnhancedCustomShape2d.cxx +++ b/svx/source/customshapes/EnhancedCustomShape2d.cxx @@ -2022,7 +2022,7 @@ static double lcl_getNormalizedAngleRad(const double fCircleAngleDeg) void EnhancedCustomShape2d::CreateSubPath( sal_Int32& rSrcPt, sal_Int32& rSegmentInd, - std::vector< std::pair< SdrPathObj*, double> >& rObjectList, + std::vector< std::pair< SdrPathObjUniquePtr, double> >& rObjectList, const bool bLineGeometryNeededOnly, const bool bSortFilledObjectsToBack, sal_Int32 nIndex) @@ -2581,15 +2581,15 @@ void EnhancedCustomShape2d::CreateSubPath( { basegfx::B2DPolyPolygon aClosedPolyPolygon(aNewB2DPolyPolygon); aClosedPolyPolygon.setClosed(true); - SdrPathObj* pFill = new SdrPathObj( + SdrPathObjUniquePtr pFill(new SdrPathObj( mrSdrObjCustomShape.getSdrModelFromSdrObject(), OBJ_POLY, - aClosedPolyPolygon); + aClosedPolyPolygon)); SfxItemSet aTempSet(*this); aTempSet.Put(makeSdrShadowItem(false)); aTempSet.Put(XLineStyleItem(drawing::LineStyle_NONE)); pFill->SetMergedItemSet(aTempSet); - rObjectList.push_back(std::pair< SdrPathObj*, double >(pFill, dBrightness)); + rObjectList.push_back(std::pair< SdrPathObjUniquePtr, double >(std::move(pFill), dBrightness)); } if(!bNoStroke) @@ -2598,39 +2598,39 @@ void EnhancedCustomShape2d::CreateSubPath( // the non-fill is defined by XFILL_NONE. Since SdrPathObj::ImpForceKind() needs // to correct the polygon (here: open it) using the type, the last edge may get lost. // Thus, use a type that fits the polygon - SdrPathObj* pStroke = new SdrPathObj( + SdrPathObjUniquePtr pStroke(new SdrPathObj( mrSdrObjCustomShape.getSdrModelFromSdrObject(), aNewB2DPolyPolygon.isClosed() ? OBJ_POLY : OBJ_PLIN, - aNewB2DPolyPolygon); + aNewB2DPolyPolygon)); SfxItemSet aTempSet(*this); aTempSet.Put(makeSdrShadowItem(false)); aTempSet.Put(XFillStyleItem(drawing::FillStyle_NONE)); pStroke->SetMergedItemSet(aTempSet); - rObjectList.push_back(std::pair< SdrPathObj*, double >(pStroke, dBrightness)); + rObjectList.push_back(std::pair< SdrPathObjUniquePtr, double >(std::move(pStroke), dBrightness)); } } else { - SdrPathObj* pObj = nullptr; + SdrPathObjUniquePtr pObj; SfxItemSet aTempSet(*this); aTempSet.Put(makeSdrShadowItem(false)); if(bNoFill) { // see comment above about OBJ_PLIN - pObj = new SdrPathObj( + pObj.reset(new SdrPathObj( mrSdrObjCustomShape.getSdrModelFromSdrObject(), aNewB2DPolyPolygon.isClosed() ? OBJ_POLY : OBJ_PLIN, - aNewB2DPolyPolygon); + aNewB2DPolyPolygon)); aTempSet.Put(XFillStyleItem(drawing::FillStyle_NONE)); } else { aNewB2DPolyPolygon.setClosed(true); - pObj = new SdrPathObj( + pObj.reset(new SdrPathObj( mrSdrObjCustomShape.getSdrModelFromSdrObject(), OBJ_POLY, - aNewB2DPolyPolygon); + aNewB2DPolyPolygon)); } if(bNoStroke) @@ -2639,14 +2639,14 @@ void EnhancedCustomShape2d::CreateSubPath( } pObj->SetMergedItemSet(aTempSet); - rObjectList.push_back(std::pair< SdrPathObj*, double >(pObj, dBrightness)); + rObjectList.push_back(std::pair< SdrPathObjUniquePtr, double >(std::move(pObj), dBrightness)); } } static void CorrectCalloutArrows( MSO_SPT eSpType, sal_uInt32 nLineObjectCount, - std::vector< std::pair< SdrPathObj*, double> >& vObjectList ) + std::vector< std::pair< SdrPathObjUniquePtr, double> >& vObjectList ) { bool bAccent = false; switch( eSpType ) @@ -2665,9 +2665,9 @@ static void CorrectCalloutArrows( { sal_uInt32 nLine = 0; - for ( const std::pair< SdrPathObj*, double >& rCandidate : vObjectList ) + for ( const std::pair< SdrPathObjUniquePtr, double >& rCandidate : vObjectList ) { - SdrPathObj* pObj(rCandidate.first); + SdrPathObj* pObj(rCandidate.first.get()); if(pObj->IsLine()) { @@ -2692,9 +2692,9 @@ static void CorrectCalloutArrows( { sal_uInt32 nLine = 0; - for ( const std::pair< SdrPathObj*, double >& rCandidate : vObjectList ) + for ( const std::pair< SdrPathObjUniquePtr, double >& rCandidate : vObjectList ) { - SdrPathObj* pObj(rCandidate.first); + SdrPathObj* pObj(rCandidate.first.get()); if(pObj->IsLine()) { @@ -2720,9 +2720,9 @@ static void CorrectCalloutArrows( { sal_uInt32 nLine = 0; - for ( const std::pair< SdrPathObj*, double >& rCandidate : vObjectList ) + for ( const std::pair< SdrPathObjUniquePtr, double >& rCandidate : vObjectList ) { - SdrPathObj* pObj(rCandidate.first); + SdrPathObj* pObj(rCandidate.first.get()); if(pObj->IsLine()) { @@ -2826,19 +2826,19 @@ void EnhancedCustomShape2d::AdaptObjColor( nColorIndex++; } -SdrObject* EnhancedCustomShape2d::CreatePathObj( bool bLineGeometryNeededOnly ) +SdrObjectUniquePtr EnhancedCustomShape2d::CreatePathObj( bool bLineGeometryNeededOnly ) { if ( !seqCoordinates.hasElements() ) { return nullptr; } - std::vector< std::pair< SdrPathObj*, double > > vObjectList; + std::vector< std::pair< SdrPathObjUniquePtr, double > > vObjectList; const bool bSortFilledObjectsToBack(SortFilledObjectsToBackByDefault(eSpType)); sal_Int32 nSubPathIndex(0); sal_Int32 nSrcPt(0); sal_Int32 nSegmentInd(0); - SdrObject* pRet(nullptr); + SdrObjectUniquePtr pRet; while( nSegmentInd <= seqSegments.getLength() ) { @@ -2859,28 +2859,20 @@ SdrObject* EnhancedCustomShape2d::CreatePathObj( bool bLineGeometryNeededOnly ) sal_uInt32 nColorIndex(0); // #i37011# remove invisible objects - std::vector< std::pair< SdrPathObj*, double> > vNewList; + std::vector< std::pair< SdrPathObjUniquePtr, double> > vNewList; - for ( const std::pair< SdrPathObj*, double >& rCandidate : vObjectList ) + for ( std::pair< SdrPathObjUniquePtr, double >& rCandidate : vObjectList ) { - SdrPathObj* pObj(rCandidate.first); + SdrPathObj* pObj(rCandidate.first.get()); const drawing::LineStyle eLineStyle(pObj->GetMergedItem(XATTR_LINESTYLE).GetValue()); const drawing::FillStyle eFillStyle(pObj->GetMergedItem(XATTR_FILLSTYLE).GetValue()); // #i40600# if bLineGeometryNeededOnly is set, linestyle does not matter - if(!bLineGeometryNeededOnly && (drawing::LineStyle_NONE == eLineStyle) && (drawing::FillStyle_NONE == eFillStyle)) - { - // always use SdrObject::Free(...) for SdrObjects (!) - SdrObject* pTemp(pObj); - SdrObject::Free(pTemp); - } - else - { - vNewList.push_back(rCandidate); - } + if(bLineGeometryNeededOnly || (drawing::LineStyle_NONE != eLineStyle) || (drawing::FillStyle_NONE != eFillStyle)) + vNewList.push_back(std::move(rCandidate)); } - vObjectList = vNewList; + vObjectList = std::move(vNewList); if(1 == vObjectList.size()) { @@ -2897,9 +2889,9 @@ SdrObject* EnhancedCustomShape2d::CreatePathObj( bool bLineGeometryNeededOnly ) sal_Int32 nLineObjectCount(0); // correct some values and collect content data - for ( const std::pair< SdrPathObj*, double >& rCandidate : vObjectList ) + for ( const std::pair< SdrPathObjUniquePtr, double >& rCandidate : vObjectList ) { - SdrPathObj* pObj(rCandidate.first); + SdrPathObj* pObj(rCandidate.first.get()); if(pObj->IsLine()) { @@ -2936,30 +2928,23 @@ SdrObject* EnhancedCustomShape2d::CreatePathObj( bool bLineGeometryNeededOnly ) // for some strange objects if(bSortFilledObjectsToBack) { - std::vector< std::pair< SdrPathObj*, double> > vTempList; + std::vector< std::pair< SdrPathObjUniquePtr, double> > vTempList; vTempList.reserve(vObjectList.size()); - for ( const std::pair< SdrPathObj*, double >& rCandidate : vObjectList ) + for ( std::pair< SdrPathObjUniquePtr, double >& rCandidate : vObjectList ) { - SdrPathObj* pObj(rCandidate.first); - + SdrPathObj* pObj(rCandidate.first.get()); if ( !pObj->IsLine() ) - { - vTempList.push_back(rCandidate); - } + vTempList.push_back(std::move(rCandidate)); } - for ( const std::pair< SdrPathObj*, double >& rCandidate : vObjectList ) + for ( std::pair< SdrPathObjUniquePtr, double >& rCandidate : vObjectList ) { - SdrPathObj* pObj(rCandidate.first); - - if ( pObj->IsLine() ) - { - vTempList.push_back(rCandidate); - } + if ( rCandidate.first ) + vTempList.push_back(std::move(rCandidate)); } - vObjectList = vTempList; + vObjectList = std::move(vTempList); } } } @@ -2970,18 +2955,16 @@ SdrObject* EnhancedCustomShape2d::CreatePathObj( bool bLineGeometryNeededOnly ) // copy remaining objects to pRet if(vObjectList.size() > 1) { - pRet = new SdrObjGroup(mrSdrObjCustomShape.getSdrModelFromSdrObject()); + pRet.reset(new SdrObjGroup(mrSdrObjCustomShape.getSdrModelFromSdrObject())); - for ( const std::pair< SdrPathObj*, double >& rCandidate : vObjectList ) + for ( std::pair< SdrPathObjUniquePtr, double >& rCandidate : vObjectList ) { - SdrPathObj* pObj(rCandidate.first); - - pRet->GetSubList()->NbcInsertObject(pObj); + pRet->GetSubList()->NbcInsertObject(rCandidate.first.release()); } } else if(1 == vObjectList.size()) { - pRet = vObjectList.begin()->first; + pRet.reset(vObjectList.begin()->first.release()); } if(pRet) @@ -2996,13 +2979,13 @@ SdrObject* EnhancedCustomShape2d::CreatePathObj( bool bLineGeometryNeededOnly ) return pRet; } -SdrObject* EnhancedCustomShape2d::CreateObject( bool bLineGeometryNeededOnly ) +SdrObjectUniquePtr EnhancedCustomShape2d::CreateObject( bool bLineGeometryNeededOnly ) { - SdrObject* pRet = nullptr; + SdrObjectUniquePtr pRet; if ( eSpType == mso_sptRectangle ) { - pRet = new SdrRectObj(mrSdrObjCustomShape.getSdrModelFromSdrObject(), aLogicRect); + pRet.reset(new SdrRectObj(mrSdrObjCustomShape.getSdrModelFromSdrObject(), aLogicRect)); pRet->SetMergedItemSet( *this ); } if ( !pRet ) @@ -3030,7 +3013,7 @@ void EnhancedCustomShape2d::ApplyGluePoints( SdrObject* pObj ) } } -SdrObject* EnhancedCustomShape2d::CreateLineGeometry() +SdrObjectUniquePtr EnhancedCustomShape2d::CreateLineGeometry() { return CreateObject( true ); } diff --git a/svx/source/customshapes/EnhancedCustomShapeEngine.cxx b/svx/source/customshapes/EnhancedCustomShapeEngine.cxx index 15b7cd41aa44..c8331fd0c13c 100644 --- a/svx/source/customshapes/EnhancedCustomShapeEngine.cxx +++ b/svx/source/customshapes/EnhancedCustomShapeEngine.cxx @@ -406,7 +406,7 @@ drawing::PolyPolygonBezierCoords SAL_CALL EnhancedCustomShapeEngine::getLineGeom static_cast< SdrObjCustomShape& >( *GetSdrObjectFromXShape(mxShape))); EnhancedCustomShape2d aCustomShape2d(rSdrObjCustomShape); - SdrObject* pObj = aCustomShape2d.CreateLineGeometry(); + SdrObjectUniquePtr pObj = aCustomShape2d.CreateLineGeometry(); if ( pObj ) { @@ -468,7 +468,7 @@ drawing::PolyPolygonBezierCoords SAL_CALL EnhancedCustomShapeEngine::getLineGeom if ( aPP.count() ) aPolyPolygon.append(aPP); } - SdrObject::Free( pObj ); + pObj.reset(); basegfx::utils::B2DPolyPolygonToUnoPolyPolygonBezierCoords( aPolyPolygon, aPolyPolygonBezierCoords ); }