Kaydet (Commit) 4055b68f authored tarafından Michael Stahl's avatar Michael Stahl

fix previous commit:

The SwOLELRUCache::InsertObj is wrong for the case when the vector is
empty; because "it != begin()" is false then (whereas "if (nPos)" is
true), the object would not be inserted.
Also do some more cleanup while at it.

Change-Id: I9107c8861c77f99b92654aac11fb4d96cccd4307
üst 8f501827
...@@ -68,10 +68,13 @@ using namespace utl; ...@@ -68,10 +68,13 @@ using namespace utl;
using namespace com::sun::star::uno; using namespace com::sun::star::uno;
using namespace com::sun::star; using namespace com::sun::star;
class SwOLELRUCache : private std::deque<SwOLEObj*>, private utl::ConfigItem class SwOLELRUCache
: private utl::ConfigItem
{ {
sal_uInt16 nLRU_InitSize; private:
sal_Bool bInUnload; typedef std::deque<SwOLEObj *> OleObjects_t;
OleObjects_t m_OleObjects;
sal_Int32 m_nLRU_InitSize;
uno::Sequence< rtl::OUString > GetPropertyNames(); uno::Sequence< rtl::OUString > GetPropertyNames();
public: public:
...@@ -82,16 +85,17 @@ public: ...@@ -82,16 +85,17 @@ public:
virtual void Commit(); virtual void Commit();
void Load(); void Load();
void SetInUnload( sal_Bool bFlag ) { bInUnload = bFlag; }
void InsertObj( SwOLEObj& rObj ); void InsertObj( SwOLEObj& rObj );
void RemoveObj( SwOLEObj& rObj ); void RemoveObj( SwOLEObj& rObj );
void RemovePtr( SwOLEObj* pObj ) void RemovePtr( SwOLEObj* pObj )
{ {
iterator it = std::find( begin(), end(), pObj ); OleObjects_t::iterator const it =
if( it != end() ) std::find(m_OleObjects.begin(), m_OleObjects.end(), pObj);
erase( it ); if (it != m_OleObjects.end())
{
m_OleObjects.erase(it);
}
} }
}; };
...@@ -897,10 +901,8 @@ String SwOLEObj::GetDescription() ...@@ -897,10 +901,8 @@ String SwOLEObj::GetDescription()
SwOLELRUCache::SwOLELRUCache() SwOLELRUCache::SwOLELRUCache()
: std::deque<SwOLEObj*>(), : utl::ConfigItem(OUString("Office.Common/Cache"))
utl::ConfigItem(OUString(RTL_CONSTASCII_USTRINGPARAM("Office.Common/Cache"))), , m_nLRU_InitSize( 20 )
nLRU_InitSize( 20 ),
bInUnload( sal_False )
{ {
EnableNotification( GetPropertyNames() ); EnableNotification( GetPropertyNames() );
Load(); Load();
...@@ -935,16 +937,16 @@ void SwOLELRUCache::Load() ...@@ -935,16 +937,16 @@ void SwOLELRUCache::Load()
*pValues >>= nVal; *pValues >>= nVal;
{ {
if( nVal < nLRU_InitSize ) if (nVal < m_nLRU_InitSize)
{ {
// size of cache has been changed // size of cache has been changed
sal_uInt16 nCount = size(); sal_Int32 nCount = m_OleObjects.size();
sal_uInt16 nPos = nCount; sal_Int32 nPos = nCount;
// try to remove the last entries until new maximum size is reached // try to remove the last entries until new maximum size is reached
while( nCount > nVal ) while( nCount > nVal )
{ {
SwOLEObj* pObj = operator[]( --nPos ); SwOLEObj *const pObj = m_OleObjects[ --nPos ];
if ( pObj->UnloadObject() ) if ( pObj->UnloadObject() )
nCount--; nCount--;
if ( !nPos ) if ( !nPos )
...@@ -953,28 +955,32 @@ void SwOLELRUCache::Load() ...@@ -953,28 +955,32 @@ void SwOLELRUCache::Load()
} }
} }
nLRU_InitSize = (sal_uInt16)nVal; m_nLRU_InitSize = nVal;
} }
} }
void SwOLELRUCache::InsertObj( SwOLEObj& rObj ) void SwOLELRUCache::InsertObj( SwOLEObj& rObj )
{ {
SwOLEObj* pObj = &rObj; SwOLEObj* pObj = &rObj;
iterator it = std::find( begin(), end(), pObj ); OleObjects_t::iterator it =
if( it != begin() ) std::find(m_OleObjects.begin(), m_OleObjects.end(), pObj);
if (it != m_OleObjects.end() && it != m_OleObjects.begin())
{ {
// object is currently not the first in cache // object in cache but is currently not the first in cache
if( it != end() ) m_OleObjects.erase(it);
erase( it ); it = m_OleObjects.end();
}
push_front( pObj ); if (it == m_OleObjects.end())
{
m_OleObjects.push_front( pObj );
// try to remove objects if necessary (of course not the freshly inserted one at nPos=0) // try to remove objects if necessary
sal_uInt16 nCount = size(); // (of course not the freshly inserted one at nPos=0)
sal_uInt16 nPos = nCount-1; sal_Int32 nCount = m_OleObjects.size();
while( nPos && nCount > nLRU_InitSize ) sal_Int32 nPos = nCount-1;
while (nPos && nCount > m_nLRU_InitSize)
{ {
pObj = operator[]( nPos-- ); pObj = m_OleObjects[ nPos-- ];
if ( pObj->UnloadObject() ) if ( pObj->UnloadObject() )
nCount--; nCount--;
} }
...@@ -983,11 +989,16 @@ void SwOLELRUCache::InsertObj( SwOLEObj& rObj ) ...@@ -983,11 +989,16 @@ void SwOLELRUCache::InsertObj( SwOLEObj& rObj )
void SwOLELRUCache::RemoveObj( SwOLEObj& rObj ) void SwOLELRUCache::RemoveObj( SwOLEObj& rObj )
{ {
iterator it = std::find( begin(), end(), &rObj ); OleObjects_t::iterator const it =
if ( it != end() ) std::find(m_OleObjects.begin(), m_OleObjects.end(), &rObj);
erase( it ); if (it != m_OleObjects.end())
if( empty() ) {
m_OleObjects.erase(it);
}
if (m_OleObjects.empty())
{
DELETEZ( pOLELRU_Cache ); DELETEZ( pOLELRU_Cache );
}
} }
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment