[Libreoffice-commits] core.git: include/svx svx/qa svx/source

Noel (via logerrit) logerrit at kemper.freedesktop.org
Wed Feb 3 13:27:34 UTC 2021


 include/svx/EnhancedCustomShape2d.hxx                 |    9 -
 svx/qa/unit/customshapes.cxx                          |   12 +
 svx/source/customshapes/EnhancedCustomShape2d.cxx     |  111 +++++++-----------
 svx/source/customshapes/EnhancedCustomShapeEngine.cxx |    4 
 4 files changed, 62 insertions(+), 74 deletions(-)

New commits:
commit 57f5f399c6047fa6c484a3b969c84aca3a723fab
Author:     Noel <noel.grandin at collabora.co.uk>
AuthorDate: Wed Feb 3 08:59:13 2021 +0200
Commit:     Noel Grandin <noel.grandin at collabora.co.uk>
CommitDate: Wed Feb 3 14:26:34 2021 +0100

    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.grandin at collabora.co.uk>

diff --git a/include/svx/EnhancedCustomShape2d.hxx b/include/svx/EnhancedCustomShape2d.hxx
index 67b0d06a7d64..ba0d4a6314bf 100644
--- a/include/svx/EnhancedCustomShape2d.hxx
+++ b/include/svx/EnhancedCustomShape2d.hxx
@@ -22,6 +22,7 @@
 
 #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>
@@ -142,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 );
@@ -186,8 +187,8 @@ class SVXCORE_DLLPUBLIC EnhancedCustomShape2d : public SfxItemSet
         SAL_DLLPRIVATE bool     IsFlipHorz() const { return bFlipH; };
         SAL_DLLPRIVATE Degree100 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 d0d4ac3d3e6d..271a7aad6f19 100644
--- a/svx/qa/unit/customshapes.cxx
+++ b/svx/qa/unit/customshapes.cxx
@@ -349,7 +349,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),
@@ -777,7 +778,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),
@@ -807,7 +809,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),
@@ -837,7 +840,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 57d622eb6acc..32c458df7848 100644
--- a/svx/source/customshapes/EnhancedCustomShape2d.cxx
+++ b/svx/source/customshapes/EnhancedCustomShape2d.cxx
@@ -2020,7 +2020,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)
@@ -2579,15 +2579,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)
@@ -2596,39 +2596,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)
@@ -2637,14 +2637,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 )
@@ -2663,9 +2663,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())
                 {
@@ -2690,9 +2690,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())
                 {
@@ -2718,9 +2718,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())
                 {
@@ -2824,19 +2824,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() )
     {
@@ -2857,28 +2857,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())
         {
@@ -2895,9 +2887,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())
                 {
@@ -2934,30 +2926,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);
             }
         }
     }
@@ -2968,18 +2953,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)
@@ -2994,13 +2977,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 )
@@ -3028,7 +3011,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 d4635c4e0ddd..8e12ad83672d 100644
--- a/svx/source/customshapes/EnhancedCustomShapeEngine.cxx
+++ b/svx/source/customshapes/EnhancedCustomShapeEngine.cxx
@@ -402,7 +402,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 )
         {
@@ -461,7 +461,7 @@ drawing::PolyPolygonBezierCoords SAL_CALL EnhancedCustomShapeEngine::getLineGeom
                 if ( aPP.count() )
                     aPolyPolygon.append(aPP);
             }
-            SdrObject::Free( pObj );
+            pObj.reset();
             basegfx::utils::B2DPolyPolygonToUnoPolyPolygonBezierCoords( aPolyPolygon,
                                                                   aPolyPolygonBezierCoords );
         }


More information about the Libreoffice-commits mailing list