[Libreoffice-commits] core.git: 2 commits - basctl/source

Noel Grandin noel.grandin at collabora.co.uk
Mon May 28 06:46:05 UTC 2018


 basctl/source/basicide/baside2.cxx    |    4 -
 basctl/source/basicide/baside2b.cxx   |   12 ++---
 basctl/source/basicide/breakpoint.cxx |   77 +++++++++++++++++-----------------
 basctl/source/basicide/breakpoint.hxx |   19 ++++----
 basctl/source/basicide/brkdlg.cxx     |   70 +++++++++++++-----------------
 basctl/source/basicide/brkdlg.hxx     |    6 +-
 basctl/source/basicide/moduldl2.cxx   |    9 +--
 basctl/source/basicide/moduldlg.hxx   |    2 
 8 files changed, 95 insertions(+), 104 deletions(-)

New commits:
commit 7fd67ca9361cc15732c505004ab4471bd2cf7f99
Author: Noel Grandin <noel.grandin at collabora.co.uk>
Date:   Thu May 17 15:32:38 2018 +0200

    loplugin:useuniqueptr in basctl::CheckBox
    
    Change-Id: Iec9a0f39b3853108bdb5218e7480a09b261198ec
    Reviewed-on: https://gerrit.libreoffice.org/54846
    Tested-by: Jenkins <ci at libreoffice.org>
    Reviewed-by: Noel Grandin <noel.grandin at collabora.co.uk>

diff --git a/basctl/source/basicide/moduldl2.cxx b/basctl/source/basicide/moduldl2.cxx
index 020e2f4488cf..b446588a6b93 100644
--- a/basctl/source/basicide/moduldl2.cxx
+++ b/basctl/source/basicide/moduldl2.cxx
@@ -166,8 +166,7 @@ CheckBox::~CheckBox()
 
 void CheckBox::dispose()
 {
-    delete pCheckButton;
-    pCheckButton = nullptr;
+    pCheckButton.reset();
 
     // delete user data
     SvTreeListEntry* pEntry = First();
@@ -182,10 +181,10 @@ void CheckBox::dispose()
 
 void CheckBox::Init()
 {
-    pCheckButton = new SvLBoxButtonData(this);
+    pCheckButton.reset(new SvLBoxButtonData(this));
 
     if (eMode == ObjectMode::Library)
-        EnableCheckButton( pCheckButton );
+        EnableCheckButton( pCheckButton.get() );
     else
         EnableCheckButton( nullptr );
 
@@ -197,7 +196,7 @@ void CheckBox::SetMode (ObjectMode e)
     eMode = e;
 
     if (eMode == ObjectMode::Library)
-        EnableCheckButton( pCheckButton );
+        EnableCheckButton( pCheckButton.get() );
     else
         EnableCheckButton( nullptr );
 }
diff --git a/basctl/source/basicide/moduldlg.hxx b/basctl/source/basicide/moduldlg.hxx
index adb1e661cd0a..cbd8b5c171f5 100644
--- a/basctl/source/basicide/moduldlg.hxx
+++ b/basctl/source/basicide/moduldlg.hxx
@@ -111,7 +111,7 @@ class CheckBox : public SvTabListBox
 {
 private:
     ObjectMode          eMode;
-    SvLBoxButtonData*   pCheckButton;
+    std::unique_ptr<SvLBoxButtonData> pCheckButton;
     ScriptDocument      m_aDocument;
     void                Init();
 
commit 4b5699bff586dd923123fbfd949c9bf03e491ed7
Author: Noel Grandin <noel.grandin at collabora.co.uk>
Date:   Thu May 17 15:31:21 2018 +0200

    loplugin:useuniqueptr in BreakPointList
    
    and simplify the surrounding code
    
    Change-Id: Ie4b24008a1c7d30d43ba562d14ddc14d1a4569b6
    Reviewed-on: https://gerrit.libreoffice.org/54845
    Tested-by: Jenkins <ci at libreoffice.org>
    Reviewed-by: Noel Grandin <noel.grandin at collabora.co.uk>

diff --git a/basctl/source/basicide/baside2.cxx b/basctl/source/basicide/baside2.cxx
index d85eac5a1b39..217075194460 100644
--- a/basctl/source/basicide/baside2.cxx
+++ b/basctl/source/basicide/baside2.cxx
@@ -510,13 +510,13 @@ void ModulWindow::ToggleBreakPoint( sal_uLong nLine )
         if ( pBrk ) // remove
         {
             m_xModule->ClearBP( static_cast<sal_uInt16>(nLine) );
-            delete GetBreakPoints().remove( pBrk );
+            GetBreakPoints().remove( pBrk );
         }
         else // create one
         {
             if ( m_xModule->SetBP( static_cast<sal_uInt16>(nLine)) )
             {
-                GetBreakPoints().InsertSorted( new BreakPoint( nLine ) );
+                GetBreakPoints().InsertSorted( BreakPoint( nLine ) );
                 if ( StarBASIC::IsRunning() )
                 {
                     for ( sal_uInt16 nMethod = 0; nMethod < m_xModule->GetMethods()->Count(); nMethod++ )
diff --git a/basctl/source/basicide/baside2b.cxx b/basctl/source/basicide/baside2b.cxx
index 27da2dc9d828..6b97f30409e7 100644
--- a/basctl/source/basicide/baside2b.cxx
+++ b/basctl/source/basicide/baside2b.cxx
@@ -1358,8 +1358,8 @@ void BreakPointWindow::Paint(vcl::RenderContext& rRenderContext, const tools::Re
 
     for (size_t i = 0, n = GetBreakPoints().size(); i < n; ++i)
     {
-        BreakPoint& rBrk = *GetBreakPoints().at(i);
-        size_t const nLine = rBrk.nLine - 1;
+        BreakPoint& rBrk = GetBreakPoints().at(i);
+        sal_uInt16 const nLine = rBrk.nLine - 1;
         size_t const nY = nLine*nLineHeight - nCurYOffset;
         rRenderContext.DrawImage(Point(0, nY) + aBmpOff, aBrk[rBrk.bEnabled]);
     }
@@ -1419,11 +1419,11 @@ BreakPoint* BreakPointWindow::FindBreakPoint( const Point& rMousePos )
 
     for ( size_t i = 0, n = GetBreakPoints().size(); i < n ; ++i )
     {
-        BreakPoint* pBrk = GetBreakPoints().at( i );
-        size_t nLine = pBrk->nLine-1;
+        BreakPoint& rBrk = GetBreakPoints().at( i );
+        sal_uInt16 nLine = rBrk.nLine-1;
         size_t nY = nLine*nLineHeight;
         if ( ( nYPos > nY ) && ( nYPos < ( nY + nLineHeight ) ) )
-            return pBrk;
+            return &rBrk;
     }
     return nullptr;
 }
@@ -1469,7 +1469,7 @@ void BreakPointWindow::Command( const CommandEvent& rCEvt )
             else if (sCommand == "properties")
             {
                 ScopedVclPtrInstance<BreakPointDialog> aBrkDlg(this, GetBreakPoints());
-                aBrkDlg->SetCurrentBreakPoint( pBrk );
+                aBrkDlg->SetCurrentBreakPoint( *pBrk );
                 aBrkDlg->Execute();
                 Invalidate();
             }
diff --git a/basctl/source/basicide/breakpoint.cxx b/basctl/source/basicide/breakpoint.cxx
index abb47b1f1afa..cc396f666b69 100644
--- a/basctl/source/basicide/breakpoint.cxx
+++ b/basctl/source/basicide/breakpoint.cxx
@@ -32,87 +32,83 @@ BreakPointList::BreakPointList()
 BreakPointList::BreakPointList(BreakPointList const & rList)
 {
     for (size_t i = 0; i < rList.size(); ++i)
-        maBreakPoints.push_back( new BreakPoint(*rList.at( i ) ) );
+        maBreakPoints.push_back( BreakPoint(rList.at( i )) );
 }
 
 BreakPointList::~BreakPointList()
 {
-    reset();
 }
 
 void BreakPointList::reset()
 {
-    for (BreakPoint* pBreakPoint : maBreakPoints)
-        delete pBreakPoint;
     maBreakPoints.clear();
 }
 
 void BreakPointList::transfer(BreakPointList & rList)
 {
-    maBreakPoints.swap(rList.maBreakPoints);
-    rList.reset();
+    maBreakPoints = std::move(rList.maBreakPoints);
 }
 
-void BreakPointList::InsertSorted(BreakPoint* pNewBrk)
+void BreakPointList::InsertSorted(BreakPoint aNewBrk)
 {
-    for ( std::vector< BreakPoint* >::iterator i = maBreakPoints.begin(); i != maBreakPoints.end(); ++i )
+    for ( auto it = maBreakPoints.begin(); it != maBreakPoints.end(); ++it )
     {
-        if ( pNewBrk->nLine <= (*i)->nLine )
+        if ( aNewBrk.nLine <= it->nLine )
         {
-            DBG_ASSERT( (*i)->nLine != pNewBrk->nLine, "BreakPoint exists already!" );
-            maBreakPoints.insert( i, pNewBrk );
+            DBG_ASSERT( it->nLine != aNewBrk.nLine, "BreakPoint exists already!" );
+            maBreakPoints.insert( it, aNewBrk );
             return;
         }
     }
     // no insert position found => LIST_APPEND
-    maBreakPoints.push_back( pNewBrk );
+    maBreakPoints.push_back( aNewBrk );
 }
 
 void BreakPointList::SetBreakPointsInBasic(SbModule* pModule)
 {
     pModule->ClearAllBP();
 
-    for (BreakPoint* pBrk : maBreakPoints)
+    for (BreakPoint& rBrk : maBreakPoints)
     {
-        if ( pBrk->bEnabled )
-            pModule->SetBP( static_cast<sal_uInt16>(pBrk->nLine) );
+        if ( rBrk.bEnabled )
+            pModule->SetBP( rBrk.nLine );
     }
 }
 
-BreakPoint* BreakPointList::FindBreakPoint(size_t nLine)
+BreakPoint* BreakPointList::FindBreakPoint(sal_uInt16 nLine)
 {
-    for (BreakPoint* pBrk : maBreakPoints)
+    for (BreakPoint& rBrk : maBreakPoints)
     {
-        if ( pBrk->nLine == nLine )
-            return pBrk;
+        if ( rBrk.nLine == nLine )
+            return &rBrk;
     }
     return nullptr;
 }
 
-void BreakPointList::AdjustBreakPoints(size_t nLine, bool bInserted)
+void BreakPointList::AdjustBreakPoints(sal_uInt16 nLine, bool bInserted)
 {
     for ( size_t i = 0; i < maBreakPoints.size(); )
     {
-        BreakPoint* pBrk = maBreakPoints[ i ];
+        BreakPoint& rBrk = maBreakPoints[ i ];
         bool bDelBrk = false;
-        if ( pBrk->nLine == nLine )
+        if ( rBrk.nLine == nLine )
         {
             if ( bInserted )
-                pBrk->nLine++;
+                rBrk.nLine++;
             else
                 bDelBrk = true;
         }
-        else if ( pBrk->nLine > nLine )
+        else if ( rBrk.nLine > nLine )
         {
             if ( bInserted )
-                pBrk->nLine++;
+                rBrk.nLine++;
             else
-                pBrk->nLine--;
+                rBrk.nLine--;
         }
 
         if ( bDelBrk )
         {
-            delete remove( pBrk );
+            maBreakPoints.erase(maBreakPoints.begin() + i);
         }
         else
         {
@@ -123,23 +119,28 @@ void BreakPointList::AdjustBreakPoints(size_t nLine, bool bInserted)
 
 void BreakPointList::ResetHitCount()
 {
-    for (BreakPoint* pBrk : maBreakPoints)
+    for (BreakPoint& rBrk : maBreakPoints)
     {
-        pBrk->nHitCount = 0;
+        rBrk.nHitCount = 0;
     }
 }
 
-BreakPoint* BreakPointList::remove(BreakPoint* ptr)
+void BreakPointList::remove(BreakPoint* ptr)
 {
-    for ( std::vector< BreakPoint* >::iterator i = maBreakPoints.begin(); i != maBreakPoints.end(); ++i )
+    for ( auto i = maBreakPoints.begin(); i != maBreakPoints.end(); ++i )
     {
-        if ( ptr == *i )
+        if ( ptr == &(*i) )
         {
             maBreakPoints.erase( i );
-            return ptr;
+            return;
         }
     }
-    return nullptr;
+    return;
+}
+
+void BreakPointList::remove(size_t idx)
+{
+    maBreakPoints.erase( maBreakPoints.begin() + idx );
 }
 
 size_t BreakPointList::size() const
@@ -147,14 +148,14 @@ size_t BreakPointList::size() const
     return maBreakPoints.size();
 }
 
-BreakPoint* BreakPointList::at(size_t i)
+BreakPoint& BreakPointList::at(size_t i)
 {
-    return i < maBreakPoints.size() ? maBreakPoints[ i ] : nullptr;
+    return maBreakPoints[ i ];
 }
 
-const BreakPoint* BreakPointList::at(size_t i) const
+const BreakPoint& BreakPointList::at(size_t i) const
 {
-    return i < maBreakPoints.size() ? maBreakPoints[ i ] : nullptr;
+    return maBreakPoints[ i ];
 }
 
 } // namespace basctl
diff --git a/basctl/source/basicide/breakpoint.hxx b/basctl/source/basicide/breakpoint.hxx
index 5e666e5cdac9..7b2d824e89e0 100644
--- a/basctl/source/basicide/breakpoint.hxx
+++ b/basctl/source/basicide/breakpoint.hxx
@@ -33,11 +33,11 @@ namespace basctl
 struct BreakPoint
 {
     bool bEnabled;
-    size_t nLine;
+    sal_uInt16 nLine;
     size_t nStopAfter;
     size_t nHitCount;
 
-    explicit BreakPoint(size_t nL)
+    explicit BreakPoint(sal_uInt16 nL)
         : bEnabled(true)
         , nLine(nL)
         , nStopAfter(0)
@@ -50,7 +50,7 @@ class BreakPointList
 {
 private:
     void operator =(BreakPointList) = delete;
-    std::vector<BreakPoint*> maBreakPoints;
+    std::vector<BreakPoint> maBreakPoints;
 
 public:
     BreakPointList();
@@ -63,16 +63,17 @@ public:
 
     void transfer(BreakPointList & rList);
 
-    void InsertSorted(BreakPoint* pBrk);
-    BreakPoint* FindBreakPoint(size_t nLine);
-    void AdjustBreakPoints(size_t nLine, bool bInserted);
+    void InsertSorted(BreakPoint pBrk);
+    BreakPoint* FindBreakPoint(sal_uInt16 nLine);
+    void AdjustBreakPoints(sal_uInt16 nLine, bool bInserted);
     void SetBreakPointsInBasic(SbModule* pModule);
     void ResetHitCount();
 
     size_t size() const;
-    BreakPoint* at(size_t i);
-    const BreakPoint* at(size_t i) const;
-    BreakPoint* remove(BreakPoint* ptr);
+    BreakPoint& at(size_t i);
+    const BreakPoint& at(size_t i) const;
+    void remove(BreakPoint* ptr);
+    void remove(size_t i);
 };
 
 } // namespace basctl
diff --git a/basctl/source/basicide/brkdlg.cxx b/basctl/source/basicide/brkdlg.cxx
index f661b114264c..3563e5734118 100644
--- a/basctl/source/basicide/brkdlg.cxx
+++ b/basctl/source/basicide/brkdlg.cxx
@@ -78,8 +78,8 @@ BreakPointDialog::BreakPointDialog( vcl::Window* pParent, BreakPointList& rBrkPn
     m_pComboBox->SetUpdateMode(false);
     for ( size_t i = 0, n = m_aModifiedBreakPointList.size(); i < n; ++i )
     {
-        BreakPoint* pBrk = m_aModifiedBreakPointList.at( i );
-        OUString aEntryStr( "# " + OUString::number(pBrk->nLine) );
+        BreakPoint& rBrk = m_aModifiedBreakPointList.at( i );
+        OUString aEntryStr( "# " + OUString::number(rBrk.nLine) );
         m_pComboBox->InsertEntry( aEntryStr );
     }
     m_pComboBox->SetUpdateMode(true);
@@ -100,7 +100,8 @@ BreakPointDialog::BreakPointDialog( vcl::Window* pParent, BreakPointList& rBrkPn
     m_pNumericField->SetModifyHdl( LINK( this, BreakPointDialog, EditModifyHdl ) );
 
     m_pComboBox->SetText( m_pComboBox->GetEntry( 0 ) );
-    UpdateFields( m_aModifiedBreakPointList.at( 0 ) );
+    if (m_aModifiedBreakPointList.size())
+        UpdateFields( m_aModifiedBreakPointList.at( 0 ) );
 
     CheckButtons();
 }
@@ -121,11 +122,11 @@ void BreakPointDialog::dispose()
     ModalDialog::dispose();
 }
 
-void BreakPointDialog::SetCurrentBreakPoint( BreakPoint const * pBrk )
+void BreakPointDialog::SetCurrentBreakPoint( BreakPoint const & rBrk )
 {
-    OUString aStr( "# " + OUString::number(pBrk->nLine) );
+    OUString aStr( "# " + OUString::number(rBrk.nLine) );
     m_pComboBox->SetText( aStr );
-    UpdateFields( pBrk );
+    UpdateFields( rBrk );
 }
 
 void BreakPointDialog::CheckButtons()
@@ -152,9 +153,8 @@ void BreakPointDialog::CheckButtons()
 IMPL_LINK( BreakPointDialog, CheckBoxHdl, Button *, pButton, void )
 {
     ::CheckBox * pChkBx = static_cast<::CheckBox*>(pButton);
-    BreakPoint* pBrk = GetSelectedBreakPoint();
-    if ( pBrk )
-        pBrk->bEnabled = pChkBx->IsChecked();
+    BreakPoint& rBrk = GetSelectedBreakPoint();
+    rBrk.bEnabled = pChkBx->IsChecked();
 }
 
 IMPL_LINK( BreakPointDialog, ComboBoxHighlightHdl, ComboBox&, rBox, void )
@@ -164,9 +164,8 @@ IMPL_LINK( BreakPointDialog, ComboBoxHighlightHdl, ComboBox&, rBox, void )
     m_pDelButton->Enable();
 
     sal_Int32 nEntry = rBox.GetEntryPos( rBox.GetText() );
-    BreakPoint* pBrk = m_aModifiedBreakPointList.at( nEntry );
-    DBG_ASSERT( pBrk, "No matching break point to list?" );
-    UpdateFields( pBrk );
+    BreakPoint& rBrk = m_aModifiedBreakPointList.at( nEntry );
+    UpdateFields( rBrk );
 }
 
 
@@ -176,9 +175,8 @@ IMPL_LINK( BreakPointDialog, EditModifyHdl, Edit&, rEdit, void )
         CheckButtons();
     else if (&rEdit == m_pNumericField)
     {
-        BreakPoint* pBrk = GetSelectedBreakPoint();
-        if ( pBrk )
-            pBrk->nStopAfter = rEdit.GetText().toInt32();
+        BreakPoint& rBrk = GetSelectedBreakPoint();
+        rBrk.nStopAfter = rEdit.GetText().toInt32();
     }
 }
 
@@ -198,11 +196,11 @@ IMPL_LINK( BreakPointDialog, ButtonHdl, Button *, pButton, void )
         bool bValid = lcl_ParseText( aText, nLine );
         if ( bValid )
         {
-            BreakPoint* pBrk = new BreakPoint( nLine );
-            pBrk->bEnabled = m_pCheckBox->IsChecked();
-            pBrk->nStopAfter = static_cast<size_t>(m_pNumericField->GetValue());
-            m_aModifiedBreakPointList.InsertSorted( pBrk );
-            OUString aEntryStr( "# " + OUString::number(pBrk->nLine) );
+            BreakPoint aBrk( nLine );
+            aBrk.bEnabled = m_pCheckBox->IsChecked();
+            aBrk.nStopAfter = static_cast<size_t>(m_pNumericField->GetValue());
+            m_aModifiedBreakPointList.InsertSorted( aBrk );
+            OUString aEntryStr( "# " + OUString::number(aBrk.nLine) );
             m_pComboBox->InsertEntry( aEntryStr );
             if (SfxDispatcher* pDispatcher = GetDispatcher())
                 pDispatcher->Execute( SID_BASICIDE_BRKPNTSCHANGED );
@@ -217,37 +215,29 @@ IMPL_LINK( BreakPointDialog, ButtonHdl, Button *, pButton, void )
     else if (pButton == m_pDelButton)
     {
         sal_Int32 nEntry = m_pComboBox->GetEntryPos( m_pComboBox->GetText() );
-        BreakPoint* pBrk = m_aModifiedBreakPointList.at( nEntry );
-        if ( pBrk )
-        {
-            delete m_aModifiedBreakPointList.remove( pBrk );
-            m_pComboBox->RemoveEntryAt(nEntry);
-            if ( nEntry && nEntry >= m_pComboBox->GetEntryCount() )
-                nEntry--;
-            m_pComboBox->SetText( m_pComboBox->GetEntry( nEntry ) );
-            if (SfxDispatcher* pDispatcher = GetDispatcher())
-                pDispatcher->Execute( SID_BASICIDE_BRKPNTSCHANGED );
-        }
+        m_aModifiedBreakPointList.remove( nEntry );
+        m_pComboBox->RemoveEntryAt(nEntry);
+        if ( nEntry && nEntry >= m_pComboBox->GetEntryCount() )
+            nEntry--;
+        m_pComboBox->SetText( m_pComboBox->GetEntry( nEntry ) );
+        if (SfxDispatcher* pDispatcher = GetDispatcher())
+            pDispatcher->Execute( SID_BASICIDE_BRKPNTSCHANGED );
         CheckButtons();
     }
 }
 
 
-void BreakPointDialog::UpdateFields( BreakPoint const * pBrk )
+void BreakPointDialog::UpdateFields( BreakPoint const & rBrk )
 {
-    if ( pBrk )
-    {
-        m_pCheckBox->Check( pBrk->bEnabled );
-        m_pNumericField->SetValue( pBrk->nStopAfter );
-    }
+    m_pCheckBox->Check( rBrk.bEnabled );
+    m_pNumericField->SetValue( rBrk.nStopAfter );
 }
 
 
-BreakPoint* BreakPointDialog::GetSelectedBreakPoint()
+BreakPoint& BreakPointDialog::GetSelectedBreakPoint()
 {
     size_t nEntry = m_pComboBox->GetEntryPos( m_pComboBox->GetText() );
-    BreakPoint* pBrk = m_aModifiedBreakPointList.at( nEntry );
-    return pBrk;
+    return m_aModifiedBreakPointList.at( nEntry );
 }
 
 } // namespace basctl
diff --git a/basctl/source/basicide/brkdlg.hxx b/basctl/source/basicide/brkdlg.hxx
index 6b3b42035f4e..ab797a20699c 100644
--- a/basctl/source/basicide/brkdlg.hxx
+++ b/basctl/source/basicide/brkdlg.hxx
@@ -45,8 +45,8 @@ class BreakPointDialog final : public ModalDialog
     DECL_LINK( ComboBoxHighlightHdl, ComboBox&, void );
     DECL_LINK( EditModifyHdl, Edit&, void );
     DECL_LINK( ButtonHdl, Button*, void );
-    void            UpdateFields( BreakPoint const * pBrk );
-    BreakPoint*     GetSelectedBreakPoint();
+    void            UpdateFields( BreakPoint const & rBrk );
+    BreakPoint&     GetSelectedBreakPoint();
 
 
 public:
@@ -54,7 +54,7 @@ public:
     virtual ~BreakPointDialog() override;
     virtual void dispose() override;
 
-    void    SetCurrentBreakPoint( BreakPoint const * pBrk );
+    void    SetCurrentBreakPoint( BreakPoint const & rBrk );
 };
 
 } // namespace basctl


More information about the Libreoffice-commits mailing list