[PATCH] Convert SmFontPickList from SfxPtrArr to std::vector

Noel Grandin (via Code Review) gerrit at gerrit.libreoffice.org
Mon Apr 15 05:59:21 PDT 2013


Hi,

I have submitted a patch for review:

    https://gerrit.libreoffice.org/3398

To pull it, you can do:

    git pull ssh://gerrit.libreoffice.org:29418/core refs/changes/98/3398/1

Convert SmFontPickList from SfxPtrArr to std::vector

- removing SmPickList in the process, since it's only used
  as a base class for SmFontPickList
- and remove dynamic allocation, since we're always making our
  own copy, just store the data inline in the vector

Change-Id: Idedff240456788c473ac49bdaa3f6d27a217e3d6
---
M starmath/inc/utility.hxx
M starmath/source/utility.cxx
2 files changed, 71 insertions(+), 197 deletions(-)



diff --git a/starmath/inc/utility.hxx b/starmath/inc/utility.hxx
index 0f4a748..01a41cd 100644
--- a/starmath/inc/utility.hxx
+++ b/starmath/inc/utility.hxx
@@ -19,12 +19,12 @@
 #ifndef UTILITY_HXX
 #define UTILITY_HXX
 
-#include <sfx2/minarray.hxx>
 #include <vcl/font.hxx>
 #include <vcl/fixed.hxx>
 #include <vcl/combobox.hxx>
 #include <vcl/lstbox.hxx>
 #include <tools/fract.hxx>
+#include <deque>
 
 
 inline long SmPtsTo100th_mm(long nNumPts)
@@ -114,114 +114,38 @@
 
 ////////////////////////////////////////////////////////////
 //
-// SmPickList
-//
-
-class SmPickList : public SfxPtrArr
-{
-protected:
-    sal_uInt16  nSize;
-
-    virtual void   *CreateItem(const void *pItem) = 0;
-    virtual void    DestroyItem(void *pItem) = 0;
-
-    virtual bool    CompareItem(const void *pFirstItem, const void *pSecondItem) const = 0;
-
-    virtual OUString GetStringItem(void *pItem) = 0;
-
-    void       *GetPtr(sal_uInt16 nPos) const { return SfxPtrArr::GetObject(nPos); }
-    void      *&GetPtr(sal_uInt16 nPos) { return SfxPtrArr::GetObject(nPos); }
-    void        InsertPtr(sal_uInt16 nPos, void *pItem) { SfxPtrArr::Insert(nPos, pItem); }
-    void        RemovePtr(sal_uInt16 nPos, sal_uInt16 nCount = 1) { SfxPtrArr::Remove(nPos, nCount); }
-
-public:
-    SmPickList(sal_uInt16 nInitSize = 0, sal_uInt16 nMaxSize = 5);
-    virtual ~SmPickList();
-
-    SmPickList&   operator = (const SmPickList& rList);
-
-    void       *Get(sal_uInt16 nPos = 0) const { return GetPtr(nPos); }
-    using   SfxPtrArr::Insert;
-    void        Insert(const void* pItem);
-    void        Update(const void* pItem, const void *pNewItem);
-    using   SfxPtrArr::Remove;
-    void        Remove(const void* pItem);
-
-    using   SfxPtrArr::operator [];
-    void       *operator [] (sal_uInt16 nPos) const { return GetPtr(nPos); }
-
-    sal_uInt16      GetSize() const { return nSize; }
-    sal_uInt16      Count() const { return SfxPtrArr::Count(); }
-
-    void        Clear();
-};
-
-
-////////////////////////////////////////////////////////////
-//
 // SmFontPickList
 //
 
 class SmFontDialog;
 
-class SmFontPickList : public SmPickList
+class SmFontPickList
 {
 protected:
-    virtual void   *CreateItem(const void *pItem);
-    virtual void    DestroyItem(void *pItem);
+    sal_uInt16 nMaxItems;
+    std::deque<Font> aFontVec;
 
-    virtual bool    CompareItem(const void *pFirstItem, const void *pSecondItem) const;
-
-    virtual OUString GetStringItem(void *pItem);
+    bool     CompareItem(const Font & rFirstFont, const Font & rSecondFont) const;
+    OUString GetStringItem(const Font &rItem);
 
 public:
-    SmFontPickList()
-        : SmPickList(0, 5) {}
-    SmFontPickList(sal_uInt16 nInitSize, sal_uInt16 nMaxSize)
-        : SmPickList(nInitSize, nMaxSize) {}
-    SmFontPickList(const SmPickList& rOrig )
-        : SmPickList(rOrig) {}
+    SmFontPickList(sal_uInt16 nMax = 5) : nMaxItems(nMax) {}
     virtual ~SmFontPickList() { Clear(); }
 
-    using   SfxPtrArr::Insert;
     virtual void    Insert(const Font &rFont);
-    using   SmPickList::Update;
     virtual void    Update(const Font &rFont, const Font &rNewFont);
-    using   SfxPtrArr::Remove;
     virtual void    Remove(const Font &rFont);
 
-    using   SmPickList::Contains;
-    inline bool     Contains(const Font &rFont) const;
-    inline Font     Get(sal_uInt16 nPos = 0) const;
+    void     Clear();
+    bool     Contains(const Font &rFont) const;
+    Font     Get(sal_uInt16 nPos = 0) const;
 
-    inline SmFontPickList&  operator = (const SmFontPickList& rList);
-    using   SfxPtrArr::operator [];
-    inline Font             operator [] (sal_uInt16 nPos) const;
+    SmFontPickList&  operator = (const SmFontPickList& rList);
+    Font             operator [] (sal_uInt16 nPos) const;
 
     void            ReadFrom(const SmFontDialog& rDialog);
     void            WriteTo(SmFontDialog& rDialog) const;
 };
-
-inline SmFontPickList& SmFontPickList::operator = (const SmFontPickList& rList)
-{
-    *(SmPickList *)this = *(SmPickList *)&rList; return *this;
-}
-
-inline Font SmFontPickList::operator [] (sal_uInt16 nPos) const
-{
-    return *((Font *)SmPickList::operator[](nPos));
-}
-
-inline Font SmFontPickList::Get(sal_uInt16 nPos) const
-{
-    return nPos < Count() ? *((Font *)SmPickList::Get(nPos)) : Font();
-}
-
-inline bool SmFontPickList::Contains(const Font &rFont) const
-{
-    return SmPickList::Contains((void *)&rFont);
-}
-
 
 ////////////////////////////////////////////////////////////
 //
@@ -234,15 +158,13 @@
     DECL_LINK(SelectHdl, ListBox *);
 
 public:
-    SmFontPickListBox(Window* pParent, const ResId& rResId, sal_uInt16 nMax = 4);
+    SmFontPickListBox(Window* pParent, const ResId& rResId);
 
     SmFontPickListBox& operator = (const SmFontPickList& rList);
 
-    using   SfxPtrArr::Insert;
     virtual void    Insert(const Font &rFont);
     using   Window::Update;
     virtual void    Update(const Font &rFont, const Font &rNewFont);
-    using   SfxPtrArr::Remove;
     virtual void    Remove(const Font &rFont);
 };
 
diff --git a/starmath/source/utility.cxx b/starmath/source/utility.cxx
index cde1b35..1d450ec 100644
--- a/starmath/source/utility.cxx
+++ b/starmath/source/utility.cxx
@@ -50,122 +50,58 @@
 
 /**************************************************************************/
 
-SmPickList::SmPickList(sal_uInt16 nInitSize, sal_uInt16 nMaxSize) :
-    SfxPtrArr((sal_uInt8) nInitSize, 1)
+void SmFontPickList::Clear()
 {
-    nSize = nMaxSize;
+    aFontVec.clear();
 }
 
-
-SmPickList::~SmPickList()
+SmFontPickList& SmFontPickList::operator = (const SmFontPickList& rList)
 {
     Clear();
-}
-
-
-SmPickList& SmPickList::operator=(const SmPickList& rList)
-{
-    sal_uInt16  nPos;
-
-    Clear();
-    nSize = rList.nSize;
-    for (nPos = 0; nPos < rList.Count(); nPos++)
-        InsertPtr(nPos, CreateItem(rList.Get(nPos)));
+    nMaxItems = rList.nMaxItems;
+    for (sal_uInt16 nPos = 0; nPos < rList.aFontVec.size(); nPos++)
+        aFontVec.push_back( rList.aFontVec[nPos] );
 
     return *this;
 }
 
-
-void SmPickList::Insert(const void *pItem)
+Font SmFontPickList::operator [] (sal_uInt16 nPos) const
 {
-    Remove(pItem);
-    InsertPtr(0, CreateItem(pItem));
+    return aFontVec[nPos];
+}
 
-    if (Count() > nSize)
-    {
-        DestroyItem(GetPtr(nSize));
-        RemovePtr(nSize, 1);
-    }
+Font SmFontPickList::Get(sal_uInt16 nPos) const
+{
+    return nPos < aFontVec.size() ? aFontVec[nPos] : Font();
+}
+
+bool SmFontPickList::Contains(const Font &rFont) const
+{
+    return std::find( aFontVec.begin(), aFontVec.end(), rFont ) != aFontVec.end();
 }
 
 
-void SmPickList::Update(const void *pItem, const void *pNewItem)
-{
-    sal_uInt16  nPos;
 
-    for (nPos = 0; nPos < Count(); nPos++)
-        if (CompareItem(GetPtr(nPos), pItem))
-        {
-            DestroyItem(GetPtr(nPos));
-            GetPtr(nPos) = CreateItem(pNewItem);
-            break;
-        }
+
+bool SmFontPickList::CompareItem(const Font & rFirstFont, const Font & rSecondFont) const
+{
+  return rFirstFont.GetName() == rSecondFont.GetName() &&
+        rFirstFont.GetFamily()  == rSecondFont.GetFamily()  &&
+        rFirstFont.GetCharSet() == rSecondFont.GetCharSet() &&
+        rFirstFont.GetWeight()  == rSecondFont.GetWeight()  &&
+        rFirstFont.GetItalic()  == rSecondFont.GetItalic();
 }
 
-void SmPickList::Remove(const void *pItem)
+OUString SmFontPickList::GetStringItem(const Font &rFont)
 {
-    sal_uInt16  nPos;
+    OUStringBuffer aString(rFont.GetName());
 
-    for (nPos = 0; nPos < Count(); nPos++)
-        if (CompareItem(GetPtr(nPos), pItem))
-        {
-            DestroyItem(GetPtr(nPos));
-            RemovePtr(nPos, 1);
-            break;
-        }
-}
-
-void SmPickList::Clear()
-{
-    sal_uInt16  nPos;
-
-    for (nPos = 0; nPos < Count(); nPos++)
-        DestroyItem(GetPtr(nPos));
-
-    RemovePtr(0, Count());
-}
-
-
-/**************************************************************************/
-
-void * SmFontPickList::CreateItem(const void *pItem)
-{
-    return new Font(*((Font *) pItem));
-}
-
-void SmFontPickList::DestroyItem(void *pItem)
-{
-    delete (Font *)pItem;
-}
-
-bool SmFontPickList::CompareItem(const void *pFirstItem, const void *pSecondItem) const
-{
-    Font    *pFirstFont, *pSecondFont;
-
-    pFirstFont  = (Font *)pFirstItem;
-    pSecondFont = (Font *)pSecondItem;
-
-    if (pFirstFont->GetName() == pSecondFont->GetName())
-        if ((pFirstFont->GetFamily()  == pSecondFont->GetFamily())  &&
-            (pFirstFont->GetCharSet() == pSecondFont->GetCharSet()) &&
-            (pFirstFont->GetWeight()  == pSecondFont->GetWeight())  &&
-            (pFirstFont->GetItalic()  == pSecondFont->GetItalic()))
-            return (true);
-
-    return false;
-}
-
-OUString SmFontPickList::GetStringItem(void *pItem)
-{
-    Font *pFont = (Font *)pItem;
-    OUStringBuffer aString(pFont->GetName());
-
-    if (IsItalic( *pFont ))
+    if (IsItalic( rFont ))
     {
         aString.append(", ");
         aString.append(SM_RESSTR(RID_FONTITALIC));
     }
-    if (IsBold( *pFont ))
+    if (IsBold( rFont ))
     {
         aString.append(", ");
         aString.append(SM_RESSTR(RID_FONTBOLD));
@@ -176,17 +112,33 @@
 
 void SmFontPickList::Insert(const Font &rFont)
 {
-    SmPickList::Insert((void *)&rFont);
+    Remove(rFont);
+    aFontVec.push_front( rFont );
+
+    if (aFontVec.size() > nMaxItems)
+    {
+        aFontVec.pop_back();
+    }
 }
 
 void SmFontPickList::Update(const Font &rFont, const Font &rNewFont)
 {
-    SmPickList::Update((void *)&rFont, (void *)&rNewFont);
+    for (sal_uInt16 nPos = 0; nPos < aFontVec.size(); nPos++)
+        if (CompareItem( aFontVec[nPos], rFont ))
+        {
+            aFontVec[nPos] = rNewFont;
+            break;
+        }
 }
 
 void SmFontPickList::Remove(const Font &rFont)
 {
-    SmPickList::Remove((void *)&rFont);
+    for (sal_uInt16 nPos = 0; nPos < aFontVec.size(); nPos++)
+        if (CompareItem( aFontVec[nPos], rFont))
+        {
+            aFontVec.erase( aFontVec.begin() + nPos );
+            break;
+        }
 }
 
 
@@ -225,8 +177,8 @@
 }
 
 
-SmFontPickListBox::SmFontPickListBox(Window* pParent, const ResId& rResId, sal_uInt16 nMax) :
-    SmFontPickList(nMax, nMax),
+SmFontPickListBox::SmFontPickListBox(Window* pParent, const ResId& rResId) :
+    SmFontPickList(4),
     ListBox(pParent, rResId)
 {
     SetSelectHdl(LINK(this, SmFontPickListBox, SelectHdl));
@@ -239,11 +191,11 @@
 
     *(SmFontPickList *)this = rList;
 
-    for (nPos = 0; nPos < Count(); nPos++)
-        InsertEntry(GetStringItem(GetPtr(nPos)), nPos);
+    for (nPos = 0; nPos < aFontVec.size(); nPos++)
+        InsertEntry(GetStringItem(aFontVec[nPos]), nPos);
 
-    if (Count() > 0)
-        SelectEntry(GetStringItem(GetPtr(0)));
+    if (aFontVec.size() > 0)
+        SelectEntry(GetStringItem(aFontVec.front()));
 
     return *this;
 }
@@ -252,11 +204,11 @@
 {
     SmFontPickList::Insert(rFont);
 
-    RemoveEntry(GetStringItem(GetPtr(0)));
-    InsertEntry(GetStringItem(GetPtr(0)), 0);
-    SelectEntry(GetStringItem(GetPtr(0)));
+    RemoveEntry(GetStringItem(aFontVec.front()));
+    InsertEntry(GetStringItem(aFontVec.front()), 0);
+    SelectEntry(GetStringItem(aFontVec.front()));
 
-    while (GetEntryCount() > nSize)
+    while (GetEntryCount() > nMaxItems)
         RemoveEntry(GetEntryCount() - 1);
 
     return;

-- 
To view, visit https://gerrit.libreoffice.org/3398
To unsubscribe, visit https://gerrit.libreoffice.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Idedff240456788c473ac49bdaa3f6d27a217e3d6
Gerrit-PatchSet: 1
Gerrit-Project: core
Gerrit-Branch: master
Gerrit-Owner: Noel Grandin <noelgrandin at gmail.com>



More information about the LibreOffice mailing list