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

Jan-Marek Glogowski glogow at fbihome.de
Tue Mar 3 02:29:10 PST 2015


 sw/inc/doc.hxx                      |    4 +-
 sw/source/core/crsr/crstrvl.cxx     |   22 +++++------
 sw/source/core/doc/docdesc.cxx      |   70 +++++++++++++++++++++++++-----------
 sw/source/core/doc/docfmt.cxx       |    7 ++-
 sw/source/core/frmedt/fedesc.cxx    |   17 +++-----
 sw/source/core/layout/pagedesc.cxx  |    2 -
 sw/source/core/unocore/unosett.cxx  |   12 ------
 sw/source/core/unocore/unostyle.cxx |   14 ++-----
 sw/source/filter/html/htmlcss1.cxx  |   15 ++-----
 9 files changed, 84 insertions(+), 79 deletions(-)

New commits:
commit 82d573e8bf97193c032ca9631d33fa39bbd12638
Author: Jan-Marek Glogowski <glogow at fbihome.de>
Date:   Tue Jul 22 13:01:36 2014 +0200

    Drop local FindPageDesc implementations
    
    There are quite some places, which manually walk the vector of
    SwPageDesc to find the named item. Replace all of them and
    also drop the duplicated and unused GetPageDesc function.
    
    Additionally adds a ContainsPageDesc function, which checks
    the vector for the SwPageDesc pointer.
    
    Change-Id: I7ade35b78d8208dc00ee56a8f83c3ebadaebb8af
    Reviewed-on: https://gerrit.libreoffice.org/14508
    Reviewed-by: Caolán McNamara <caolanm at redhat.com>
    Tested-by: Caolán McNamara <caolanm at redhat.com>

diff --git a/sw/inc/doc.hxx b/sw/inc/doc.hxx
index b9b5551..d0b3fbc 100644
--- a/sw/inc/doc.hxx
+++ b/sw/inc/doc.hxx
@@ -948,6 +948,9 @@ public:
     const SwPageDesc& GetPageDesc( const sal_uInt16 i ) const { return maPageDescs[i]; }
     SwPageDesc& GetPageDesc( sal_uInt16 i ) { return maPageDescs[i]; }
     SwPageDesc* FindPageDesc(const OUString& rName, sal_uInt16* pPos = NULL);
+    SwPageDesc* FindPageDesc(const OUString& rName, sal_uInt16* pPos = NULL) const;
+    // Just searches the pointer in the maPageDescs vector!
+    bool        ContainsPageDesc(const SwPageDesc *pDesc, sal_uInt16* pPos = NULL);
 
     /** Copy the complete PageDesc - beyond document and "deep"!
      Optionally copying of PoolFmtId, -HlpId can be prevented. */
@@ -965,7 +968,6 @@ public:
         { CopyPageDescHeaderFooterImpl( false, rSrcFmt, rDestFmt ); }
 
     // For Reader
-    SwPageDesc * GetPageDesc( const OUString & rName );
     void ChgPageDesc( const OUString & rName, const SwPageDesc& );
     void ChgPageDesc( sal_uInt16 i, const SwPageDesc& );
     void DelPageDesc( const OUString & rName, bool bBroadcast = false);
diff --git a/sw/source/core/crsr/crstrvl.cxx b/sw/source/core/crsr/crstrvl.cxx
index 9993b50..e11fdd2 100644
--- a/sw/source/core/crsr/crstrvl.cxx
+++ b/sw/source/core/crsr/crstrvl.cxx
@@ -190,6 +190,7 @@ bool SwCrsrShell::SetCrsrInHdFt( sal_uInt16 nDescNo, bool bInHeader )
 {
     bool bRet = false;
     SwDoc *pMyDoc = GetDoc();
+    const SwPageDesc* pDesc = NULL;
 
     SET_CURR_SHELL( this );
 
@@ -197,31 +198,28 @@ bool SwCrsrShell::SetCrsrInHdFt( sal_uInt16 nDescNo, bool bInHeader )
     {
         // take the current one
         const SwPageFrm* pPage = GetCurrFrm()->FindPageFrm();
-        if( pPage )
-            for( sal_uInt16 i = 0; i < pMyDoc->GetPageDescCnt(); ++i )
-                if( pPage->GetPageDesc() == &pMyDoc->GetPageDesc( i ) )
-                {
-                    nDescNo = i;
-                    break;
-                }
+        if( pPage && pMyDoc->ContainsPageDesc(
+                pPage->GetPageDesc(), &nDescNo) )
+            pDesc = pPage->GetPageDesc();
     }
+    else
+        if (nDescNo < pMyDoc->GetPageDescCnt())
+            pDesc = &pMyDoc->GetPageDesc( nDescNo );
 
-    if( USHRT_MAX != nDescNo && nDescNo < pMyDoc->GetPageDescCnt() )
+    if( pDesc )
     {
         // check if the attribute exists
-        const SwPageDesc& rDesc = const_cast<const SwDoc *>(pMyDoc)
-            ->GetPageDesc( nDescNo );
         const SwFmtCntnt* pCnt = 0;
         if( bInHeader )
         {
             // mirrored pages? ignore for now
-            const SwFmtHeader& rHd = rDesc.GetMaster().GetHeader();
+            const SwFmtHeader& rHd = pDesc->GetMaster().GetHeader();
             if( rHd.GetHeaderFmt() )
                 pCnt = &rHd.GetHeaderFmt()->GetCntnt();
         }
         else
         {
-            const SwFmtFooter& rFt = rDesc.GetMaster().GetFooter();
+            const SwFmtFooter& rFt = pDesc->GetMaster().GetFooter();
             if( rFt.GetFooterFmt() )
                 pCnt = &rFt.GetFooterFmt()->GetCntnt();
         }
diff --git a/sw/source/core/doc/docdesc.cxx b/sw/source/core/doc/docdesc.cxx
index c489c62..0c29eb4 100644
--- a/sw/source/core/doc/docdesc.cxx
+++ b/sw/source/core/doc/docdesc.cxx
@@ -684,24 +684,6 @@ SwPageDesc* SwDoc::MakePageDesc(const OUString &rName, const SwPageDesc *pCpy,
     return pNew;
 }
 
-SwPageDesc* SwDoc::FindPageDesc(const OUString& rName, sal_uInt16* pPos)
-{
-    SwPageDesc* pRet = NULL;
-    if( pPos ) *pPos = USHRT_MAX;
-
-    for( SwPageDescs::size_type n = 0, nEnd = maPageDescs.size(); n < nEnd; ++n )
-    {
-        if( maPageDescs[ n ].HasName( rName ) )
-        {
-            pRet = &maPageDescs[ n ];
-            if( pPos )
-                *pPos = n;
-            break;
-        }
-    }
-    return pRet;
-}
-
 // We collect the GlobalNames of the servers at runtime, who don't want to be notified
 // about printer changes. Thereby saving loading a lot of objects (luckily all foreign
 // objects are mapped to one ID).
@@ -830,9 +812,57 @@ IMPL_LINK( SwDoc, DoUpdateModifiedOLE, Timer *, )
     return 0;
 }
 
-SwPageDesc * SwDoc::GetPageDesc( const OUString & rName )
+struct CompareSwPageDescName {
+    CompareSwPageDescName(const OUString &rName) : mName(rName) {}
+    bool operator () (const SwPageDesc& other) const { return other.GetName() == mName; }
+    const OUString &mName;
+};
+
+template <class UnaryPredicate>
+static SwPageDesc* lcl_FindPageDesc( SwPageDescs *pPageDescs,
+                                     sal_uInt16 *pPos, UnaryPredicate pred )
+{
+    SwPageDescs::iterator it = std::find_if(
+        pPageDescs->begin(), pPageDescs->end(), pred);
+    SwPageDesc* res = NULL;
+    if( it != pPageDescs->end() )
+    {
+        res = const_cast <SwPageDesc *>( &( *it ) );;
+        if( pPos )
+            *pPos = std::distance( pPageDescs->begin(), it );
+    }
+    else if( pPos )
+        *pPos = USHRT_MAX;
+    return res;
+}
+
+SwPageDesc* SwDoc::FindPageDesc( const OUString & rName, sal_uInt16* pPos )
+{
+    return lcl_FindPageDesc<CompareSwPageDescName>(
+        &maPageDescs, pPos, CompareSwPageDescName(rName) );
+}
+
+SwPageDesc* SwDoc::FindPageDesc( const OUString & rName, sal_uInt16* pPos ) const
+{
+    return lcl_FindPageDesc<CompareSwPageDescName>(
+        const_cast <SwPageDescs *>( &maPageDescs ), pPos,
+        CompareSwPageDescName(rName) );
+}
+
+struct CompareSwPageDescToPtr {
+    CompareSwPageDescToPtr(const SwPageDesc* ptr) : mPtr(ptr) {}
+    bool operator () (const SwPageDesc& other) const { return &other == mPtr; }
+    const SwPageDesc *mPtr;
+};
+
+bool SwDoc::ContainsPageDesc( const SwPageDesc *pDesc, sal_uInt16* pPos )
 {
-    return FindPageDesc(rName);
+    if (pDesc == NULL)
+        return false;
+    SwPageDesc *res = lcl_FindPageDesc<CompareSwPageDescToPtr>(
+        const_cast <SwPageDescs *>( &maPageDescs ), pPos,
+        CompareSwPageDescToPtr(pDesc) );
+    return res != NULL;
 }
 
 void SwDoc::DelPageDesc( const OUString & rName, bool bBroadcast )
diff --git a/sw/source/core/doc/docfmt.cxx b/sw/source/core/doc/docfmt.cxx
index 0d31901..cfa65b8 100644
--- a/sw/source/core/doc/docfmt.cxx
+++ b/sw/source/core/doc/docfmt.cxx
@@ -1419,12 +1419,13 @@ void SwDoc::CopyPageDesc( const SwPageDesc& rSrcDesc, SwPageDesc& rDstDesc,
 
     if( rSrcDesc.GetFollow() != &rSrcDesc )
     {
-        SwPageDesc* pFollow = FindPageDesc( rSrcDesc.GetFollow()->GetName() );
+        const SwPageDesc* pSrcFollow = rSrcDesc.GetFollow();
+        SwPageDesc* pFollow = FindPageDesc( pSrcFollow->GetName() );
         if( !pFollow )
         {
             // copy
-            pFollow = MakePageDesc(rSrcDesc.GetFollow()->GetName());
-            CopyPageDesc( *rSrcDesc.GetFollow(), *pFollow );
+            pFollow = MakePageDesc( pSrcFollow->GetName() );
+            CopyPageDesc( *pSrcFollow, *pFollow );
         }
         rDstDesc.SetFollow( pFollow );
         bNotifyLayout = true;
diff --git a/sw/source/core/frmedt/fedesc.cxx b/sw/source/core/frmedt/fedesc.cxx
index 9db4fee..67f786d 100644
--- a/sw/source/core/frmedt/fedesc.cxx
+++ b/sw/source/core/frmedt/fedesc.cxx
@@ -150,11 +150,9 @@ sal_uInt16 SwFEShell::GetMousePageDesc( const Point &rPt ) const
             while( pPage->GetNext() && rPt.Y() > pPage->Frm().Bottom() )
                 pPage = static_cast<const SwPageFrm*>( pPage->GetNext() );
             SwDoc *pMyDoc = GetDoc();
-            for ( sal_uInt16 i = 0; i < GetDoc()->GetPageDescCnt(); ++i )
-            {
-                if ( pPage->GetPageDesc() == &pMyDoc->GetPageDesc(i) )
-                    return i;
-            }
+            sal_uInt16 nPos;
+            if (pMyDoc->ContainsPageDesc( pPage->GetPageDesc(), &nPos ) )
+                return nPos;
         }
     }
     return 0;
@@ -168,12 +166,9 @@ sal_uInt16 SwFEShell::GetCurPageDesc( const bool bCalcFrm ) const
         const SwPageFrm *pPage = pFrm->FindPageFrm();
         if ( pPage )
         {
-            SwDoc *pMyDoc = GetDoc();
-            for ( sal_uInt16 i = 0; i < GetDoc()->GetPageDescCnt(); ++i )
-            {
-                if ( pPage->GetPageDesc() == &pMyDoc->GetPageDesc(i) )
-                    return i;
-            }
+            sal_uInt16 nPos;
+            if (GetDoc()->ContainsPageDesc( pPage->GetPageDesc(), &nPos ))
+                return nPos;
         }
     }
     return 0;
diff --git a/sw/source/core/layout/pagedesc.cxx b/sw/source/core/layout/pagedesc.cxx
index 6b79713..0d05037 100644
--- a/sw/source/core/layout/pagedesc.cxx
+++ b/sw/source/core/layout/pagedesc.cxx
@@ -452,7 +452,7 @@ SwPageDescExt::operator SwPageDesc() const
 {
     SwPageDesc aResult(aPageDesc);
 
-    SwPageDesc * pPageDesc = pDoc->GetPageDesc(sFollow);
+    SwPageDesc * pPageDesc = pDoc->FindPageDesc(sFollow);
 
     if ( 0 != pPageDesc )
         aResult.SetFollow(pPageDesc);
diff --git a/sw/source/core/unocore/unosett.cxx b/sw/source/core/unocore/unosett.cxx
index 1a17f3d..a565dce 100644
--- a/sw/source/core/unocore/unosett.cxx
+++ b/sw/source/core/unocore/unosett.cxx
@@ -227,21 +227,11 @@ static SwTxtFmtColl* lcl_GetParaStyle(SwDoc* pDoc, const uno::Any& aValue)
 
 static SwPageDesc* lcl_GetPageDesc(SwDoc* pDoc, const uno::Any& aValue)
 {
-    SwPageDesc* pRet = 0;
-    const sal_uInt16 nCount = pDoc->GetPageDescCnt();
     OUString uTmp;
     aValue >>= uTmp;
     OUString sPageDesc;
     SwStyleNameMapper::FillUIName(uTmp, sPageDesc, nsSwGetPoolIdFromName::GET_POOLID_PAGEDESC, true );
-    for( sal_uInt16 i = 0; i < nCount; i++)
-    {
-        const SwPageDesc& rDesc = pDoc->GetPageDesc( i );
-        if(rDesc.GetName() == sPageDesc)
-        {
-            pRet = (SwPageDesc*)&rDesc;
-            break;
-        }
-    }
+    SwPageDesc* pRet = pDoc->FindPageDesc( sPageDesc );
     if(!pRet)
     {
         const sal_uInt16 nId = SwStyleNameMapper::GetPoolIdFromUIName(sPageDesc, nsSwGetPoolIdFromName::GET_POOLID_PAGEDESC);
diff --git a/sw/source/core/unocore/unostyle.cxx b/sw/source/core/unocore/unostyle.cxx
index e101eb9..886c371 100644
--- a/sw/source/core/unocore/unostyle.cxx
+++ b/sw/source/core/unocore/unostyle.cxx
@@ -1626,16 +1626,10 @@ const SwPageDesc& SwStyleBase_Impl::GetOldPageDesc()
 {
     if(!mpOldPageDesc)
     {
-        const sal_uInt16 nPDescCount = mrDoc.GetPageDescCnt();
-        for(sal_uInt16 i = 0; i < nPDescCount; ++i)
-        {
-            const SwPageDesc& rDesc = mrDoc.GetPageDesc( i );
-            if(rDesc.GetName() == mrStyleName)
-            {
-                mpOldPageDesc = & rDesc;
-                break;
-            }
-        }
+        SwPageDesc *pd = mrDoc.FindPageDesc( mrStyleName );
+        if( pd )
+            mpOldPageDesc = pd;
+
         if(!mpOldPageDesc)
         {
             for(sal_uInt16 i = RC_POOLPAGEDESC_BEGIN; i <= STR_POOLPAGE_LANDSCAPE; ++i)
diff --git a/sw/source/filter/html/htmlcss1.cxx b/sw/source/filter/html/htmlcss1.cxx
index 1254401..f026033 100644
--- a/sw/source/filter/html/htmlcss1.cxx
+++ b/sw/source/filter/html/htmlcss1.cxx
@@ -89,16 +89,11 @@ static struct SwCSS1ItemIds
 void SwCSS1Parser::ChgPageDesc( const SwPageDesc *pPageDesc,
                                 const SwPageDesc& rNewPageDesc )
 {
-    sal_uInt16 nPageDescs = pDoc->GetPageDescCnt();
-    sal_uInt16 i;
-    for( i=0; i<nPageDescs; i++ )
-        if( pPageDesc == &pDoc->GetPageDesc(i) )
-        {
-            pDoc->ChgPageDesc( i, rNewPageDesc );
-            return;
-        }
-
-    OSL_ENSURE( i<nPageDescs, "Seitenvorlage nicht gefunden" );
+    sal_uInt16 pos;
+    bool found = pDoc->ContainsPageDesc( pPageDesc, &pos );
+    OSL_ENSURE( found, "Seitenvorlage nicht gefunden" );
+    if (found)
+        pDoc->ChgPageDesc( pos, rNewPageDesc );
 }
 
 SwCSS1Parser::SwCSS1Parser( SwDoc *pD, sal_uInt32 aFHeights[7], const OUString& rBaseURL, bool bNewDoc ) :


More information about the Libreoffice-commits mailing list