[Libreoffice-commits] core.git: sw/inc sw/source

Noel Grandin noel.grandin at collabora.co.uk
Fri Mar 23 10:35:15 UTC 2018


 sw/inc/swcrsr.hxx                 |   10 ++-----
 sw/source/core/bastyp/swtypes.cxx |    1 
 sw/source/core/crsr/swcrsr.cxx    |   52 ++++++++++++++------------------------
 sw/source/core/crsr/trvlreg.cxx   |    4 +-
 4 files changed, 25 insertions(+), 42 deletions(-)

New commits:
commit b1119ed70bae01a862067c43722c55e7c39667f6
Author: Noel Grandin <noel.grandin at collabora.co.uk>
Date:   Thu Mar 22 15:21:14 2018 +0200

    loplugin:useuniqueptr in SwCursor
    
    storing a struct with two integer values in it, in a linked list, just
    seems inefficient
    
    Change-Id: I2913aa867cc978271d7778f06546d47fa6c57973
    Reviewed-on: https://gerrit.libreoffice.org/51761
    Tested-by: Jenkins <ci at libreoffice.org>
    Reviewed-by: Miklos Vajna <vmiklos at collabora.co.uk>

diff --git a/sw/inc/swcrsr.hxx b/sw/inc/swcrsr.hxx
index 1723f5e1bb0b..bebc7023f169 100644
--- a/sw/inc/swcrsr.hxx
+++ b/sw/inc/swcrsr.hxx
@@ -68,7 +68,7 @@ class SW_DLLPUBLIC SwCursor : public SwPaM
 {
     friend class SwCursorSaveState;
 
-    SwCursor_SavePos* m_pSavePos;
+    std::vector<SwCursor_SavePos> m_vSavePos; // the current entry is the last element
     long m_nRowSpanOffset;        // required for travelling in tabs with rowspans
     sal_uInt8 m_nCursorBidiLevel; // bidi level of the cursor
     bool m_bColumnSelection;      // true: cursor is aprt of a column selection
@@ -83,7 +83,7 @@ protected:
     void SaveState();
     void RestoreState();
 
-    const SwCursor_SavePos* GetSavePos() const { return m_pSavePos; }
+    const SwCursor_SavePos* GetSavePos() const { return m_vSavePos.empty() ? nullptr : &m_vSavePos.back(); }
 
     virtual const SwContentFrame* DoSetBidiLevelLeftRight(
         bool & io_rbLeft, bool bVisualAllowed, bool bInsertCursor);
@@ -242,15 +242,11 @@ struct SwCursor_SavePos final
 {
     sal_uLong nNode;
     sal_Int32 nContent;
-    SwCursor_SavePos* pNext;
 
     SwCursor_SavePos( const SwCursor& rCursor )
         : nNode( rCursor.GetPoint()->nNode.GetIndex() ),
-        nContent( rCursor.GetPoint()->nContent.GetIndex() ),
-        pNext( nullptr )
+          nContent( rCursor.GetPoint()->nContent.GetIndex() )
     {}
-
-    DECL_FIXEDMEMPOOL_NEWDEL( SwCursor_SavePos )
 };
 
 class SwTableCursor : public virtual SwCursor
diff --git a/sw/source/core/bastyp/swtypes.cxx b/sw/source/core/bastyp/swtypes.cxx
index eb6279c1c695..a2caa2705d67 100644
--- a/sw/source/core/bastyp/swtypes.cxx
+++ b/sw/source/core/bastyp/swtypes.cxx
@@ -76,7 +76,6 @@ IMPL_FIXEDMEMPOOL_NEWDEL( SwTextFrame )
 IMPL_FIXEDMEMPOOL_NEWDEL( SwTableFormat )
 IMPL_FIXEDMEMPOOL_NEWDEL( SwTableLineFormat )
 IMPL_FIXEDMEMPOOL_NEWDEL( SwTableBoxFormat )
-IMPL_FIXEDMEMPOOL_NEWDEL( SwCursor_SavePos )
 
 Size GetGraphicSizeTwip(const Graphic& rGraphic, vcl::RenderContext* pOutDev)
 {
diff --git a/sw/source/core/crsr/swcrsr.cxx b/sw/source/core/crsr/swcrsr.cxx
index 83c3d111cf35..029867f34b37 100644
--- a/sw/source/core/crsr/swcrsr.cxx
+++ b/sw/source/core/crsr/swcrsr.cxx
@@ -115,7 +115,6 @@ struct PercentHdl
 
 SwCursor::SwCursor( const SwPosition &rPos, SwPaM* pRing )
     : SwPaM( rPos, pRing )
-    , m_pSavePos(nullptr)
     , m_nRowSpanOffset(0)
     , m_nCursorBidiLevel(0)
     , m_bColumnSelection(false)
@@ -125,7 +124,6 @@ SwCursor::SwCursor( const SwPosition &rPos, SwPaM* pRing )
 // @@@ semantic: no copy ctor.
 SwCursor::SwCursor(SwCursor const& rCpy, SwPaM *const pRing)
     : SwPaM( rCpy, pRing )
-    , m_pSavePos(nullptr)
     , m_nRowSpanOffset(rCpy.m_nRowSpanOffset)
     , m_nCursorBidiLevel(rCpy.m_nCursorBidiLevel)
     , m_bColumnSelection(rCpy.m_bColumnSelection)
@@ -134,12 +132,6 @@ SwCursor::SwCursor(SwCursor const& rCpy, SwPaM *const pRing)
 
 SwCursor::~SwCursor()
 {
-    while( m_pSavePos )
-    {
-        SwCursor_SavePos* pNxt = m_pSavePos->pNext;
-        delete m_pSavePos;
-        m_pSavePos = pNxt;
-    }
 }
 
 SwCursor* SwCursor::Create( SwPaM* pRing ) const
@@ -166,18 +158,14 @@ bool SwCursor::IsSkipOverProtectSections() const
 // own SaveObjects if needed and validate them in the virtual check routines.
 void SwCursor::SaveState()
 {
-    SwCursor_SavePos* pNew = new SwCursor_SavePos( *this );
-    pNew->pNext = m_pSavePos;
-    m_pSavePos = pNew;
+    m_vSavePos.emplace_back( *this );
 }
 
 void SwCursor::RestoreState()
 {
-    if (m_pSavePos) // Robust
+    if (!m_vSavePos.empty()) // Robust
     {
-        SwCursor_SavePos* pDel = m_pSavePos;
-        m_pSavePos = m_pSavePos->pNext;
-        delete pDel;
+        m_vSavePos.pop_back();
     }
 }
 
@@ -236,7 +224,7 @@ bool SwCursor::IsSelOvr( SwCursorSelOverFlags eFlags )
         return true;
     }
 
-    if (m_pSavePos->nNode != GetPoint()->nNode.GetIndex() &&
+    if (m_vSavePos.back().nNode != GetPoint()->nNode.GetIndex() &&
         // (1997) in UI-ReadOnly everything is allowed
         ( !pDoc->GetDocShell() || !pDoc->GetDocShell()->IsReadOnlyUI() ))
     {
@@ -256,8 +244,8 @@ bool SwCursor::IsSelOvr( SwCursorSelOverFlags eFlags )
 
             // set cursor to new position:
             SwNodeIndex aIdx( rPtIdx );
-            sal_Int32 nContentPos = m_pSavePos->nContent;
-            bool bGoNxt = m_pSavePos->nNode < rPtIdx.GetIndex();
+            sal_Int32 nContentPos = m_vSavePos.back().nContent;
+            bool bGoNxt = m_vSavePos.back().nNode < rPtIdx.GetIndex();
             SwContentNode* pCNd = bGoNxt
                 ? rNds.GoNextSection( &rPtIdx, bSkipOverHiddenSections, bSkipOverProtectSections)
                 : SwNodes::GoPrevSection( &rPtIdx, bSkipOverHiddenSections, bSkipOverProtectSections);
@@ -273,7 +261,7 @@ bool SwCursor::IsSelOvr( SwCursorSelOverFlags eFlags )
                 ::CheckNodesRange( rPtIdx, aIdx, true );
             if( !bValidNodesRange )
             {
-                rPtIdx = m_pSavePos->nNode;
+                rPtIdx = m_vSavePos.back().nNode;
                 if( nullptr == ( pCNd = rPtIdx.GetNode().GetContentNode() ) )
                 {
                     bIsValidPos = false;
@@ -344,7 +332,7 @@ bool SwCursor::IsSelOvr( SwCursorSelOverFlags eFlags )
         {
             // skip to the next/prev valid paragraph with a layout
             SwNodeIndex& rPtIdx = GetPoint()->nNode;
-            bool bGoNxt = m_pSavePos->nNode < rPtIdx.GetIndex();
+            bool bGoNxt = m_vSavePos.back().nNode < rPtIdx.GetIndex();
             while( nullptr != ( pFrame = ( bGoNxt ? pFrame->GetNextContentFrame() : pFrame->GetPrevContentFrame() ))
                    && 0 == pFrame->getFrameArea().Height() )
                 ;
@@ -372,8 +360,8 @@ bool SwCursor::IsSelOvr( SwCursorSelOverFlags eFlags )
                 const sal_Int32 nTmpPos = bGoNxt ? 0 : pCNd->Len();
                 GetPoint()->nContent.Assign( pCNd, nTmpPos );
 
-                if (rPtIdx.GetIndex() == m_pSavePos->nNode
-                    && nTmpPos == m_pSavePos->nContent)
+                if (rPtIdx.GetIndex() == m_vSavePos.back().nNode
+                    && nTmpPos == m_vSavePos.back().nContent)
                 {
                     // new position equals saved one
                     // --> trigger restore of saved pos by setting <pFrame> to NULL - see below
@@ -434,11 +422,11 @@ bool SwCursor::IsSelOvr( SwCursorSelOverFlags eFlags )
         {
             const sal_uLong nRefNodeIdx =
                 ( SwCursorSelOverFlags::Toggle & eFlags )
-                ? m_pSavePos->nNode
+                ? m_vSavePos.back().nNode
                 : GetMark()->nNode.GetIndex();
             const sal_Int32 nRefContentIdx =
                 ( SwCursorSelOverFlags::Toggle & eFlags )
-                ? m_pSavePos->nContent
+                ? m_vSavePos.back().nContent
                 : GetMark()->nContent.GetIndex();
             const bool bIsForwardSelection =
                 nRefNodeIdx < GetPoint()->nNode.GetIndex()
@@ -484,7 +472,7 @@ bool SwCursor::IsSelOvr( SwCursorSelOverFlags eFlags )
     {
         bool bSelTop = GetPoint()->nNode.GetIndex() <
             ((SwCursorSelOverFlags::Toggle & eFlags)
-                 ? m_pSavePos->nNode : GetMark()->nNode.GetIndex());
+                 ? m_vSavePos.back().nNode : GetMark()->nNode.GetIndex());
 
         do { // loop for table after table
             sal_uLong nSEIdx = pPtNd->EndOfSectionIndex();
@@ -551,7 +539,7 @@ bool SwCursor::IsInProtectTable( bool bMove, bool bChgCursor )
         return false;
 
     // Current position == last save position?
-    if (m_pSavePos->nNode == GetPoint()->nNode.GetIndex())
+    if (m_vSavePos.back().nNode == GetPoint()->nNode.GetIndex())
         return false;
 
     // Check for covered cell:
@@ -586,7 +574,7 @@ bool SwCursor::IsInProtectTable( bool bMove, bool bChgCursor )
     }
 
     // We are in a protected table cell. Traverse top to bottom?
-    if (m_pSavePos->nNode < GetPoint()->nNode.GetIndex())
+    if (m_vSavePos.back().nNode < GetPoint()->nNode.GetIndex())
     {
         // search next valid box
         // if there is another StartNode after the EndNode of a cell then
@@ -2085,18 +2073,18 @@ void SwCursor::RestoreSavePos()
     // This method is not supposed to be used in cases when nodes may be
     // deleted; detect such cases, but do not crash (example: fdo#40831).
     sal_uLong uNodeCount = GetPoint()->nNode.GetNodes().Count();
-    OSL_ENSURE(!m_pSavePos || m_pSavePos->nNode < uNodeCount,
+    OSL_ENSURE(m_vSavePos.empty() || m_vSavePos.back().nNode < uNodeCount,
         "SwCursor::RestoreSavePos: invalid node: "
         "probably something was deleted; consider using SwUnoCursor instead");
-    if (m_pSavePos && m_pSavePos->nNode < uNodeCount)
+    if (!m_vSavePos.empty() && m_vSavePos.back().nNode < uNodeCount)
     {
-        GetPoint()->nNode = m_pSavePos->nNode;
+        GetPoint()->nNode = m_vSavePos.back().nNode;
 
         sal_Int32 nIdx = 0;
         if ( GetContentNode() )
         {
-            if (m_pSavePos->nContent <= GetContentNode()->Len())
-                nIdx = m_pSavePos->nContent;
+            if (m_vSavePos.back().nContent <= GetContentNode()->Len())
+                nIdx = m_vSavePos.back().nContent;
             else
             {
                 nIdx = GetContentNode()->Len();
diff --git a/sw/source/core/crsr/trvlreg.cxx b/sw/source/core/crsr/trvlreg.cxx
index 39993f7323dd..d5255047cd26 100644
--- a/sw/source/core/crsr/trvlreg.cxx
+++ b/sw/source/core/crsr/trvlreg.cxx
@@ -185,8 +185,8 @@ bool SwCursor::MoveRegion( SwWhichRegion fnWhichRegion, SwMoveFnCollection const
     return !dynamic_cast<SwTableCursor*>(this) &&
             (*fnWhichRegion)( *this, fnPosRegion, IsReadOnlyAvailable()  ) &&
             !IsSelOvr() &&
-            (GetPoint()->nNode.GetIndex() != m_pSavePos->nNode ||
-             GetPoint()->nContent.GetIndex() != m_pSavePos->nContent);
+            (GetPoint()->nNode.GetIndex() != m_vSavePos.back().nNode ||
+             GetPoint()->nContent.GetIndex() != m_vSavePos.back().nContent);
 }
 
 bool SwCursorShell::MoveRegion( SwWhichRegion fnWhichRegion, SwMoveFnCollection const & fnPosRegion )


More information about the Libreoffice-commits mailing list