From cc0c8e56436d35e726efe7f8d06cf403fcaf827e Mon Sep 17 00:00:00 2001 From: Kohei Yoshida Date: Thu, 15 Nov 2012 15:59:48 -0500 Subject: [PATCH] Store tree list entry items in ptr_vector & const correct-ness. Change-Id: I0e02b9ceb98f26a2b130ec978a992fcf889e718a --- dbaccess/source/ui/browser/unodatbr.cxx | 4 +- svtools/inc/svtools/treelistbox.hxx | 2 +- svtools/inc/svtools/treelistentry.hxx | 20 +++-- svtools/source/contnr/svimpbox.cxx | 2 +- svtools/source/contnr/svtabbx.cxx | 2 +- svtools/source/contnr/treelistentry.cxx | 114 ++++++++++++++---------- 6 files changed, 85 insertions(+), 59 deletions(-) diff --git a/dbaccess/source/ui/browser/unodatbr.cxx b/dbaccess/source/ui/browser/unodatbr.cxx index a0484e56a0b5..3c3279fdb4f4 100644 --- a/dbaccess/source/ui/browser/unodatbr.cxx +++ b/dbaccess/source/ui/browser/unodatbr.cxx @@ -3476,8 +3476,8 @@ IMPL_LINK( SbaTableQueryBrowser, OnTreeEntryCompare, const SvSortData*, _pSortDa return COMPARE_EQUAL; } - SvLBoxString* pLeftTextItem = static_cast(pLHS->GetFirstItem(SV_ITEM_ID_LBOXSTRING)); - SvLBoxString* pRightTextItem = static_cast(pRHS->GetFirstItem(SV_ITEM_ID_LBOXSTRING)); + const SvLBoxString* pLeftTextItem = static_cast(pLHS->GetFirstItem(SV_ITEM_ID_LBOXSTRING)); + const SvLBoxString* pRightTextItem = static_cast(pRHS->GetFirstItem(SV_ITEM_ID_LBOXSTRING)); OSL_ENSURE(pLeftTextItem && pRightTextItem, "SbaTableQueryBrowser::OnTreeEntryCompare: invalid text items!"); String sLeftText = pLeftTextItem->GetText(); diff --git a/svtools/inc/svtools/treelistbox.hxx b/svtools/inc/svtools/treelistbox.hxx index ce4bba1b170f..8ec235de4408 100644 --- a/svtools/inc/svtools/treelistbox.hxx +++ b/svtools/inc/svtools/treelistbox.hxx @@ -835,7 +835,7 @@ inline SvViewDataItem* SvTreeListBox::GetViewDataItem( SvTreeListEntry* pEntry, (SvViewDataEntry*)SvListView::GetViewData(pEntry); DBG_ASSERT(pEntryData,"Entry not in View"); DBG_ASSERT(pEntryData->pItemData,"No ItemData"); - sal_uInt16 nItemPos = ((SvTreeListEntry*)pEntry)->GetPos( pItem ); + sal_uInt16 nItemPos = pEntry->GetPos(pItem); return (pEntryData->pItemData+nItemPos); } diff --git a/svtools/inc/svtools/treelistentry.hxx b/svtools/inc/svtools/treelistentry.hxx index 37a0ba131d49..7eb015c7199b 100644 --- a/svtools/inc/svtools/treelistentry.hxx +++ b/svtools/inc/svtools/treelistentry.hxx @@ -32,7 +32,6 @@ #include "svtdllapi.h" #include "tools/solar.h" -#include #include // Flags, die am Model haengen @@ -58,11 +57,13 @@ class SVT_DLLPUBLIC SvTreeListEntry friend class SvListView; friend class SvTreeListBox; + typedef boost::ptr_vector ItemsType; + SvTreeListEntry* pParent; SvTreeListEntries maChildren; sal_uLong nAbsPos; sal_uLong nListPos; - std::vector aItems; + ItemsType maItems; void* pUserData; sal_uInt16 nEntryFlags; @@ -70,9 +71,10 @@ private: void ClearChildren(); void SetListPositions(); void InvalidateChildrensListPositions(); - void DeleteItems_Impl(); public: + static size_t ITEM_NOT_FOUND; + SvTreeListEntry(); SvTreeListEntry(const SvTreeListEntry& r); virtual ~SvTreeListEntry(); @@ -86,16 +88,18 @@ public: void Clone(SvTreeListEntry* pSource); - sal_uInt16 ItemCount() const; + size_t ItemCount() const; // DARF NUR GERUFEN WERDEN, WENN DER EINTRAG NOCH NICHT IM MODEL // EINGEFUEGT IST, DA SONST FUER DAS ITEM KEINE VIEW-ABHAENGIGEN // DATEN ALLOZIERT WERDEN! void AddItem( SvLBoxItem* pItem ); - void ReplaceItem( SvLBoxItem* pNewItem, sal_uInt16 nPos ); - SvLBoxItem* GetItem( sal_uInt16 nPos ) const; - SvLBoxItem* GetFirstItem( sal_uInt16 nId ) const; - sal_uInt16 GetPos( SvLBoxItem* pItem ) const; + void ReplaceItem( SvLBoxItem* pNewItem, size_t nPos ); + const SvLBoxItem* GetItem( size_t nPos ) const; + SvLBoxItem* GetItem( size_t nPos ); + const SvLBoxItem* GetFirstItem( sal_uInt16 nId ) const; + SvLBoxItem* GetFirstItem( sal_uInt16 nId ); + size_t GetPos( const SvLBoxItem* pItem ) const; void* GetUserData() const; void SetUserData( void* pPtr ); void EnableChildrenOnDemand( bool bEnable=true ); diff --git a/svtools/source/contnr/svimpbox.cxx b/svtools/source/contnr/svimpbox.cxx index 5f90b8fd7c7f..06145918f11d 100644 --- a/svtools/source/contnr/svimpbox.cxx +++ b/svtools/source/contnr/svimpbox.cxx @@ -229,7 +229,7 @@ void SvImpLBox::CalcCellFocusRect( SvTreeListEntry* pEntry, Rectangle& rRect ) SvLBoxItem* pItem = pCursor->GetItem( nCurTabPos ); rRect.Left() = pView->GetTab( pCursor, pItem )->GetPos(); } - if ( pCursor->ItemCount() > ( nCurTabPos + 1 ) ) + if (pCursor->ItemCount() > static_cast(nCurTabPos+1)) { SvLBoxItem* pNextItem = pCursor->GetItem( nCurTabPos + 1 ); long nRight = pView->GetTab( pCursor, pNextItem )->GetPos() - 1; diff --git a/svtools/source/contnr/svtabbx.cxx b/svtools/source/contnr/svtabbx.cxx index 8938104c8937..929c429cc00c 100644 --- a/svtools/source/contnr/svtabbx.cxx +++ b/svtools/source/contnr/svtabbx.cxx @@ -375,7 +375,7 @@ String SvTabListBox::GetCellText( sal_uLong nPos, sal_uInt16 nCol ) const SvTreeListEntry* pEntry = GetEntryOnPos( nPos ); DBG_ASSERT( pEntry, "SvTabListBox::GetCellText(): Invalid Entry" ); XubString aResult; - if ( pEntry && pEntry->ItemCount() > ( nCol + 1 ) ) + if (pEntry && pEntry->ItemCount() > static_cast(nCol+1)) { const SvLBoxItem* pStr = pEntry->GetItem( nCol + 1 ); if (pStr && pStr->GetType() == SV_ITEM_ID_LBOXSTRING) diff --git a/svtools/source/contnr/treelistentry.cxx b/svtools/source/contnr/treelistentry.cxx index b6a5977fe553..b98b24af9bf5 100644 --- a/svtools/source/contnr/treelistentry.cxx +++ b/svtools/source/contnr/treelistentry.cxx @@ -30,6 +30,10 @@ #include "svtools/treelist.hxx" #include "svtools/treelistbox.hxx" +#include + +size_t SvTreeListEntry::ITEM_NOT_FOUND = std::numeric_limits::max(); + void SvTreeListEntry::ClearChildren() { maChildren.clear(); @@ -55,18 +59,6 @@ void SvTreeListEntry::InvalidateChildrensListPositions() nListPos |= 0x80000000; } -void SvTreeListEntry::DeleteItems_Impl() -{ - sal_uInt16 nCount = aItems.size(); - while( nCount ) - { - nCount--; - SvLBoxItem* pItem = aItems[ nCount ]; - delete pItem; - } - aItems.clear(); -} - SvTreeListEntry::SvTreeListEntry() : pParent(NULL), nAbsPos(0), @@ -93,7 +85,7 @@ SvTreeListEntry::~SvTreeListEntry() #endif maChildren.clear(); - DeleteItems_Impl(); + maItems.clear(); } bool SvTreeListEntry::HasChildren() const @@ -132,29 +124,28 @@ void SvTreeListEntry::Clone(SvTreeListEntry* pSource) nAbsPos = pSource->nAbsPos; SvLBoxItem* pNewItem; - DeleteItems_Impl(); - sal_uInt16 nCount = ((SvTreeListEntry*)pSource)->ItemCount(); - sal_uInt16 nCurPos = 0; - while( nCurPos < nCount ) + maItems.clear(); + ItemsType::iterator it = pSource->maItems.begin(), itEnd = pSource->maItems.end(); + for (; it != itEnd; ++it) { - SvLBoxItem* pItem = ((SvTreeListEntry*)pSource)->GetItem( nCurPos ); + SvLBoxItem* pItem = &(*it); pNewItem = pItem->Create(); - pNewItem->Clone( pItem ); - AddItem( pNewItem ); - nCurPos++; + pNewItem->Clone(pItem); + maItems.push_back(pNewItem); } - pUserData = ((SvTreeListEntry*)pSource)->GetUserData(); - nEntryFlags = ((SvTreeListEntry*)pSource)->nEntryFlags; + + pUserData = pSource->GetUserData(); + nEntryFlags = pSource->nEntryFlags; } -sal_uInt16 SvTreeListEntry::ItemCount() const +size_t SvTreeListEntry::ItemCount() const { - return (sal_uInt16)aItems.size(); + return maItems.size(); } void SvTreeListEntry::AddItem( SvLBoxItem* pItem ) { - aItems.push_back( pItem ); + maItems.push_back( pItem ); } void SvTreeListEntry::EnableChildrenOnDemand( bool bEnable ) @@ -165,41 +156,72 @@ void SvTreeListEntry::EnableChildrenOnDemand( bool bEnable ) nEntryFlags &= (~SV_ENTRYFLAG_CHILDREN_ON_DEMAND); } -void SvTreeListEntry::ReplaceItem( SvLBoxItem* pNewItem, sal_uInt16 nPos ) +void SvTreeListEntry::ReplaceItem( SvLBoxItem* pNewItem, size_t nPos ) { DBG_ASSERT(pNewItem,"ReplaceItem:No Item"); - SvLBoxItem* pOld = GetItem( nPos ); - if ( pOld ) + if (nPos >= maItems.size()) { - aItems[ nPos ] = pNewItem; - delete pOld; + // Out of bound. Bail out. + delete pNewItem; + return; } + + maItems.erase(maItems.begin()+nPos); + maItems.insert(maItems.begin()+nPos, pNewItem); } -SvLBoxItem* SvTreeListEntry::GetItem( sal_uInt16 nPos ) const +const SvLBoxItem* SvTreeListEntry::GetItem( size_t nPos ) const { - return aItems[nPos]; + return &maItems[nPos]; } -SvLBoxItem* SvTreeListEntry::GetFirstItem( sal_uInt16 nId ) const +SvLBoxItem* SvTreeListEntry::GetItem( size_t nPos ) { - sal_uInt16 nCount = aItems.size(); - sal_uInt16 nCur = 0; - SvLBoxItem* pItem; - while( nCur < nCount ) + return &maItems[nPos]; +} + +namespace { + +class FindByType : std::unary_function +{ + sal_uInt16 mnId; +public: + FindByType(sal_uInt16 nId) : mnId(nId) {} + bool operator() (const SvLBoxItem& rItem) const { - pItem = GetItem( nCur ); - if (pItem->GetType() == nId) - return pItem; - nCur++; + return rItem.GetType() == mnId; } - return 0; +}; + +class FindByPointer : std::unary_function +{ + const SvLBoxItem* mpItem; +public: + FindByPointer(const SvLBoxItem* p) : mpItem(p) {} + bool operator() (const SvLBoxItem& rItem) const + { + return &rItem == mpItem; + } +}; + } -sal_uInt16 SvTreeListEntry::GetPos( SvLBoxItem* pItem ) const +const SvLBoxItem* SvTreeListEntry::GetFirstItem( sal_uInt16 nId ) const { - std::vector::const_iterator it = std::find( aItems.begin(), aItems.end(), pItem ); - return it == aItems.end() ? USHRT_MAX : it - aItems.begin(); + ItemsType::const_iterator it = std::find_if(maItems.begin(), maItems.end(), FindByType(nId)); + return it == maItems.end() ? NULL : &(*it); +} + +SvLBoxItem* SvTreeListEntry::GetFirstItem( sal_uInt16 nId ) +{ + ItemsType::iterator it = std::find_if(maItems.begin(), maItems.end(), FindByType(nId)); + return it == maItems.end() ? NULL : &(*it); +} + +size_t SvTreeListEntry::GetPos( const SvLBoxItem* pItem ) const +{ + ItemsType::const_iterator it = std::find_if(maItems.begin(), maItems.end(), FindByPointer(pItem)); + return it == maItems.end() ? ITEM_NOT_FOUND : std::distance(maItems.begin(), it); } void* SvTreeListEntry::GetUserData() const