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

Noel Grandin (via logerrit) logerrit at kemper.freedesktop.org
Tue Jun 22 19:43:50 UTC 2021


 sw/inc/doc.hxx                   |    6 ++----
 sw/inc/docary.hxx                |   12 ++++++++++--
 sw/inc/frameformats.hxx          |    6 ++++++
 sw/source/core/attr/format.cxx   |   19 +++++++++++++++++++
 sw/source/core/doc/docfmt.cxx    |   30 +++++++-----------------------
 sw/source/core/docnode/ndtbl.cxx |    2 +-
 sw/source/core/undo/unattr.cxx   |    6 +++---
 7 files changed, 48 insertions(+), 33 deletions(-)

New commits:
commit b2331179fc508fd6bc37355e5c3c5a5ee54557c4
Author:     Noel Grandin <noel.grandin at collabora.co.uk>
AuthorDate: Tue Jun 22 19:39:19 2021 +0200
Commit:     Noel Grandin <noel.grandin at collabora.co.uk>
CommitDate: Tue Jun 22 21:43:09 2021 +0200

    Move SwDoc::FindFormatByName to SwFormatsBase
    
    in preparation for adding optimised implementations of it for subclasses
    
    Change-Id: I6d8ff54864d2d3c605a1cd0b4da2c6136e2a21cb
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/117672
    Tested-by: Jenkins
    Reviewed-by: Noel Grandin <noel.grandin at collabora.co.uk>

diff --git a/sw/inc/doc.hxx b/sw/inc/doc.hxx
index c94e5f04c5f3..b5edfcf39566 100644
--- a/sw/inc/doc.hxx
+++ b/sw/inc/doc.hxx
@@ -758,8 +758,6 @@ public:
     // Remove all language dependencies from all existing formats
     void RemoveAllFormatLanguageDependencies();
 
-    static SwFormat* FindFormatByName(const SwFormatsBase& rFormatArr, std::u16string_view rName);
-
     SwFrameFormat  *MakeFrameFormat(const OUString &rFormatName, SwFrameFormat *pDerivedFrom,
                           bool bBroadcast = false, bool bAuto = true);
     void       DelFrameFormat( SwFrameFormat *pFormat, bool bBroadcast = false );
@@ -770,7 +768,7 @@ public:
     void       DelCharFormat(size_t nFormat, bool bBroadcast = false);
     void       DelCharFormat(SwCharFormat const * pFormat, bool bBroadcast = false);
     SwCharFormat* FindCharFormatByName( std::u16string_view rName ) const
-        {   return static_cast<SwCharFormat*>(FindFormatByName( *mpCharFormatTable, rName )); }
+        {   return mpCharFormatTable->FindFormatByName(rName); }
 
     // Formatcollections (styles)
     // TXT
@@ -798,7 +796,7 @@ public:
                        const bool bResetListAttrs = false,
                        SwRootFrame const* pLayout = nullptr);
     SwTextFormatColl* FindTextFormatCollByName( std::u16string_view rName ) const
-        {   return static_cast<SwTextFormatColl*>(FindFormatByName( *mpTextFormatCollTable, rName )); }
+        {   return mpTextFormatCollTable->FindFormatByName(rName); }
 
     void ChkCondColls();
 
diff --git a/sw/inc/docary.hxx b/sw/inc/docary.hxx
index 9b4a6e80ea26..09c0dc3636d8 100644
--- a/sw/inc/docary.hxx
+++ b/sw/inc/docary.hxx
@@ -19,6 +19,7 @@
 #ifndef INCLUDED_SW_INC_DOCARY_HXX
 #define INCLUDED_SW_INC_DOCARY_HXX
 
+#include <map>
 #include <vector>
 #include <type_traits>
 #include <o3tl/sorted_vector.hxx>
@@ -40,12 +41,15 @@ struct SwPosition;
 enum class RedlineType : sal_uInt16;
 
 /** provides some methods for generic operations on lists that contain SwFormat* subclasses. */
-class SwFormatsBase
+class SW_DLLPUBLIC SwFormatsBase
 {
 public:
     virtual size_t GetFormatCount() const = 0;
     virtual SwFormat* GetFormat(size_t idx) const = 0;
-    virtual ~SwFormatsBase() {};
+    virtual ~SwFormatsBase();
+
+    // default linear search implementation, some subclasses will override with a more efficient search
+    virtual SwFormat* FindFormatByName(std::u16string_view rName) const;
 
     SwFormatsBase() = default;
     SwFormatsBase(SwFormatsBase const &) = default;
@@ -164,6 +168,10 @@ public:
         Value p = dynamic_cast<Value>(const_cast<SwFormat*>(pFormat));
         return p != nullptr && SwVectorModifyBase<Value>::IsAlive(p);
     }
+
+    // Override return type to reduce casting
+    virtual Value FindFormatByName(std::u16string_view rName) const override
+    { return static_cast<Value>(SwFormatsBase::FindFormatByName(rName)); }
 };
 
 class SwGrfFormatColls final : public SwFormatsModifyBase<SwGrfFormatColl*>
diff --git a/sw/inc/frameformats.hxx b/sw/inc/frameformats.hxx
index 8ad02559a79b..491adf9d3f70 100644
--- a/sw/inc/frameformats.hxx
+++ b/sw/inc/frameformats.hxx
@@ -112,6 +112,12 @@ public:
 
     bool newDefault(const value_type& x);
     void newDefault(const_iterator const& position);
+
+    // Override return type to reduce casting
+    virtual SwFrameFormat* FindFormatByName(std::u16string_view rName) const override
+    {
+        return static_cast<SwFrameFormat*>(SwFormatsBase::FindFormatByName(rName));
+    }
 };
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sw/source/core/attr/format.cxx b/sw/source/core/attr/format.cxx
index 7efb53cd7e45..676adf89fcf7 100644
--- a/sw/source/core/attr/format.cxx
+++ b/sw/source/core/attr/format.cxx
@@ -765,4 +765,23 @@ void SwFormat::RemoveAllUnos()
     SwPtrMsgPoolItem aMsgHint(RES_REMOVE_UNO_OBJECT, this);
     SwClientNotify(*this, sw::LegacyModifyHint(&aMsgHint, &aMsgHint));
 }
+
+SwFormatsBase::~SwFormatsBase()
+{}
+
+SwFormat* SwFormatsBase::FindFormatByName( std::u16string_view rName ) const
+{
+    SwFormat* pFnd = nullptr;
+    for( size_t n = 0; n < GetFormatCount(); ++n )
+    {
+        // Does the Doc already contain the template?
+        if( GetFormat(n)->HasName( rName ) )
+        {
+            pFnd = GetFormat(n);
+            break;
+        }
+    }
+    return pFnd;
+}
+
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sw/source/core/doc/docfmt.cxx b/sw/source/core/doc/docfmt.cxx
index f3c4b71653ac..c45e268c26fc 100644
--- a/sw/source/core/doc/docfmt.cxx
+++ b/sw/source/core/doc/docfmt.cxx
@@ -733,7 +733,7 @@ void SwDoc::DelTableFrameFormat( SwTableFormat *pFormat )
 
 SwFrameFormat* SwDoc::FindFrameFormatByName( std::u16string_view rName ) const
 {
-    return static_cast<SwFrameFormat*>(FindFormatByName( static_cast<SwFormatsBase&>(*mpFrameFormatTable), rName ));
+    return mpFrameFormatTable->FindFormatByName( rName );
 }
 
 /// Create the formats
@@ -1231,7 +1231,7 @@ SwTextFormatColl* SwDoc::CopyTextColl( const SwTextFormatColl& rColl )
 /// copy the graphic nodes
 SwGrfFormatColl* SwDoc::CopyGrfColl( const SwGrfFormatColl& rColl )
 {
-    SwGrfFormatColl* pNewColl = static_cast<SwGrfFormatColl*>(FindFormatByName( static_cast<SwFormatsBase const &>(*mpGrfFormatCollTable), rColl.GetName() ));
+    SwGrfFormatColl* pNewColl = mpGrfFormatCollTable->FindFormatByName( rColl.GetName() );
     if( pNewColl )
         return pNewColl;
 
@@ -1269,7 +1269,7 @@ void SwDoc::CopyFormatArr( const SwFormatsBase& rSourceArr,
         if( pSrc->IsDefault() || pSrc->IsAuto() )
             continue;
 
-        if( nullptr == FindFormatByName( rDestArr, pSrc->GetName() ) )
+        if( nullptr == rDestArr.FindFormatByName( pSrc->GetName() ) )
         {
             if( RES_CONDTXTFMTCOLL == pSrc->Which() )
                 MakeCondTextFormatColl( pSrc->GetName(), static_cast<SwTextFormatColl*>(&rDfltFormat) );
@@ -1286,7 +1286,7 @@ void SwDoc::CopyFormatArr( const SwFormatsBase& rSourceArr,
         if( pSrc->IsDefault() || pSrc->IsAuto() )
             continue;
 
-        pDest = FindFormatByName( rDestArr, pSrc->GetName() );
+        pDest = rDestArr.FindFormatByName( pSrc->GetName() );
         pDest->SetAuto(false);
         pDest->DelDiffs( *pSrc );
 
@@ -1320,7 +1320,7 @@ void SwDoc::CopyFormatArr( const SwFormatsBase& rSourceArr,
         pDest->SetPoolHlpFileId( UCHAR_MAX );
 
         if( pSrc->DerivedFrom() )
-            pDest->SetDerivedFrom( FindFormatByName( rDestArr,
+            pDest->SetDerivedFrom( rDestArr.FindFormatByName(
                                         pSrc->DerivedFrom()->GetName() ) );
         if( RES_TXTFMTCOLL == pSrc->Which() ||
             RES_CONDTXTFMTCOLL == pSrc->Which() )
@@ -1328,8 +1328,8 @@ void SwDoc::CopyFormatArr( const SwFormatsBase& rSourceArr,
             SwTextFormatColl* pSrcColl = static_cast<SwTextFormatColl*>(pSrc),
                         * pDstColl = static_cast<SwTextFormatColl*>(pDest);
             if( &pSrcColl->GetNextTextFormatColl() != pSrcColl )
-                pDstColl->SetNextTextFormatColl( *static_cast<SwTextFormatColl*>(FindFormatByName(
-                    rDestArr, pSrcColl->GetNextTextFormatColl().GetName() ) ) );
+                pDstColl->SetNextTextFormatColl(
+                    *static_cast<SwTextFormatColl*>(rDestArr.FindFormatByName( pSrcColl->GetNextTextFormatColl().GetName() )) );
 
             if(pSrcColl->IsAssignedToListLevelOfOutlineStyle())
                 pDstColl->AssignToListLevelOfOutlineStyle(pSrcColl->GetAssignedOutlineStyleLevel());
@@ -1626,22 +1626,6 @@ void SwDoc::ReplaceStyles( const SwDoc& rSource, bool bIncludePageStyles )
     getIDocumentState().SetModified();
 }
 
-SwFormat* SwDoc::FindFormatByName( const SwFormatsBase& rFormatArr,
-                                   std::u16string_view rName )
-{
-    SwFormat* pFnd = nullptr;
-    for( size_t n = 0; n < rFormatArr.GetFormatCount(); ++n )
-    {
-        // Does the Doc already contain the template?
-        if( rFormatArr.GetFormat(n)->HasName( rName ) )
-        {
-            pFnd = rFormatArr.GetFormat(n);
-            break;
-        }
-    }
-    return pFnd;
-}
-
 void SwDoc::MoveLeftMargin(const SwPaM& rPam, bool bRight, bool bModulus,
         SwRootFrame const*const pLayout)
 {
diff --git a/sw/source/core/docnode/ndtbl.cxx b/sw/source/core/docnode/ndtbl.cxx
index a17ce9b3e041..654cae173fe4 100644
--- a/sw/source/core/docnode/ndtbl.cxx
+++ b/sw/source/core/docnode/ndtbl.cxx
@@ -3949,7 +3949,7 @@ SwTableFormat* SwDoc::FindTableFormatByName( std::u16string_view rName, bool bAl
 {
     const SwFormat* pRet = nullptr;
     if( bAll )
-        pRet = FindFormatByName( *mpTableFrameFormatTable, rName );
+        pRet = mpTableFrameFormatTable->FindFormatByName( rName );
     else
     {
         // Only the ones set in the Doc
diff --git a/sw/source/core/undo/unattr.cxx b/sw/source/core/undo/unattr.cxx
index 160a983b1c00..a4c1aaec54d9 100644
--- a/sw/source/core/undo/unattr.cxx
+++ b/sw/source/core/undo/unattr.cxx
@@ -209,7 +209,7 @@ SwFormat* SwUndoFormatAttr::GetFormat( const SwDoc& rDoc )
         return rDoc.FindTextFormatCollByName(m_sFormatName);
 
     case RES_GRFFMTCOLL:
-        return SwDoc::FindFormatByName(*rDoc.GetGrfFormatColls(), m_sFormatName);
+        return rDoc.GetGrfFormatColls()->FindFormatByName(m_sFormatName);
 
     case RES_CHRFMT:
         return rDoc.FindCharFormatByName(m_sFormatName);
@@ -244,10 +244,10 @@ SwFormat* SwUndoFormatAttr::GetFormat( const SwDoc& rDoc )
     case RES_DRAWFRMFMT:
     case RES_FLYFRMFMT:
         {
-            SwFormat * pFormat = SwDoc::FindFormatByName(*rDoc.GetSpzFrameFormats(), m_sFormatName);
+            SwFormat * pFormat = rDoc.GetSpzFrameFormats()->FindFormatByName(m_sFormatName);
             if (pFormat)
                 return pFormat;
-            pFormat = SwDoc::FindFormatByName(*rDoc.GetFrameFormats(), m_sFormatName);
+            pFormat = rDoc.GetFrameFormats()->FindFormatByName(m_sFormatName);
             if (pFormat)
                 return pFormat;
         }


More information about the Libreoffice-commits mailing list