diff options
author | Noel Grandin <noel.grandin@collabora.co.uk> | 2019-01-09 12:52:54 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2019-01-10 08:16:27 +0100 |
commit | b8f497a06d585dbae3adadb4d177fe84fdb1b5fa (patch) | |
tree | 81a49d027648531906191c52d6fc7be026dff182 | |
parent | 50887c9010515fe9fe61b3067f502cd28333773a (diff) |
use unique_ptr in SwCache
Change-Id: I2b961380dcb5eb26ce517f7b56e5c32f5e6429e4
Reviewed-on: https://gerrit.libreoffice.org/66011
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
-rw-r--r-- | sw/source/core/bastyp/swcache.cxx | 23 | ||||
-rw-r--r-- | sw/source/core/inc/swcache.hxx | 6 |
2 files changed, 12 insertions, 17 deletions
diff --git a/sw/source/core/bastyp/swcache.cxx b/sw/source/core/bastyp/swcache.cxx index 2693578c8b21..6abbbb51faea 100644 --- a/sw/source/core/bastyp/swcache.cxx +++ b/sw/source/core/bastyp/swcache.cxx @@ -125,9 +125,6 @@ SwCache::~SwCache() << "; number of Cache reductions: " << m_nDecreaseMax); Check(); #endif - - for(const auto& rpObj : m_aCacheObjects) - delete rpObj; } void SwCache::IncreaseMax( const sal_uInt16 nAdd ) @@ -163,8 +160,7 @@ void SwCache::Flush() pTmp = pObj; pObj = pTmp->GetNext(); m_aFreePositions.push_back( pTmp->GetCachePos() ); - m_aCacheObjects[pTmp->GetCachePos()] = nullptr; - delete pTmp; + m_aCacheObjects[pTmp->GetCachePos()].reset(); // deletes pTmp INCREMENT( m_nFlushedObjects ); } } @@ -236,7 +232,7 @@ SwCacheObj *SwCache::Get( const void *pOwner, const sal_uInt16 nIndex, const bool bToTop ) { SwCacheObj *pRet; - if ( nullptr != (pRet = (nIndex < m_aCacheObjects.size()) ? m_aCacheObjects[ nIndex ] : nullptr) ) + if ( nullptr != (pRet = (nIndex < m_aCacheObjects.size()) ? m_aCacheObjects[ nIndex ].get() : nullptr) ) { if ( !pRet->IsOwner( pOwner ) ) pRet = nullptr; @@ -300,8 +296,7 @@ void SwCache::DeleteObj( SwCacheObj *pObj ) pObj->GetNext()->SetPrev( pObj->GetPrev() ); m_aFreePositions.push_back( pObj->GetCachePos() ); - m_aCacheObjects[pObj->GetCachePos()] = nullptr; - delete pObj; + m_aCacheObjects[pObj->GetCachePos()] = nullptr; // deletes pObj CHECK; if ( m_aCacheObjects.size() > m_nCurMax && @@ -312,9 +307,10 @@ void SwCache::DeleteObj( SwCacheObj *pObj ) // these might not find them afterwards for ( size_t i = 0; i < m_aCacheObjects.size(); ++i ) { - SwCacheObj *pTmpObj = m_aCacheObjects[i]; + SwCacheObj *pTmpObj = m_aCacheObjects[i].get(); if ( !pTmpObj ) - { m_aCacheObjects.erase( m_aCacheObjects.begin() + i ); + { + m_aCacheObjects.erase( m_aCacheObjects.begin() + i ); --i; } else @@ -346,7 +342,7 @@ bool SwCache::Insert( SwCacheObj *pNew ) // there is still space; insert directly INCREMENT( m_nAppend ); nPos = m_aCacheObjects.size(); - m_aCacheObjects.push_back(pNew); + m_aCacheObjects.emplace_back(pNew); } else if ( !m_aFreePositions.empty() ) { @@ -354,7 +350,7 @@ bool SwCache::Insert( SwCacheObj *pNew ) INCREMENT( m_nInsertFree ); const sal_uInt16 nFreePos = m_aFreePositions.size() - 1; nPos = m_aFreePositions[ nFreePos ]; - m_aCacheObjects[nPos] = pNew; + m_aCacheObjects[nPos].reset(pNew); m_aFreePositions.erase( m_aFreePositions.begin() + nFreePos ); } else @@ -403,8 +399,7 @@ bool SwCache::Insert( SwCacheObj *pNew ) { pObj->GetNext()->SetPrev( pObj->GetPrev() ); } - delete pObj; - m_aCacheObjects[nPos] = pNew; + m_aCacheObjects[nPos].reset(pNew); } pNew->SetCachePos( nPos ); diff --git a/sw/source/core/inc/swcache.hxx b/sw/source/core/inc/swcache.hxx index 2744b9f2f318..aa0e4188d608 100644 --- a/sw/source/core/inc/swcache.hxx +++ b/sw/source/core/inc/swcache.hxx @@ -43,16 +43,16 @@ * when destroying them. */ +#include <memory> #include <vector> #include <rtl/ustring.hxx> class SwCacheObj; -typedef std::vector<SwCacheObj*> SwCacheObjArr; class SwCache { - SwCacheObjArr m_aCacheObjects; + std::vector<std::unique_ptr<SwCacheObj>> m_aCacheObjects; std::vector<sal_uInt16> m_aFreePositions; /// Free positions for the Insert if the maximum has not been reached /// Every time an object is deregistered, its position is added here SwCacheObj *m_pRealFirst; /// _ALWAYS_ the real first LRU @@ -112,7 +112,7 @@ public: sal_uInt16 GetCurMax() const { return m_nCurMax; } SwCacheObj *First() { return m_pRealFirst; } static inline SwCacheObj *Next( SwCacheObj *pCacheObj); - SwCacheObj* operator[](sal_uInt16 nIndex) { return m_aCacheObjects[nIndex]; } + SwCacheObj* operator[](sal_uInt16 nIndex) { return m_aCacheObjects[nIndex].get(); } sal_uInt16 size() { return m_aCacheObjects.size(); } }; |