Files
libreoffice/sw/source/core/inc/swcache.hxx

287 lines
9.2 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
re-base on ALv2 code. Includes: Patches contributed by Oliver-Rainer Wittmann sw34bf06: #i117783# - Writer's implementation of XPagePrintable - apply print settings to new printing routines http://svn.apache.org/viewvc?view=revision&revision=1172115 sw34bf06: #o12311627# use <rtl_random> methods to create unique ids for list styles and list ids http://svn.apache.org/viewvc?view=revision&revision=1172112 sw34bf06 #i114725#,#i115828# - method <SwDoc::ClearDoc()> - clear list structures completely http://svn.apache.org/viewvc?view=revision&revision=1172122 i#118572 - remove ui string and help content regarding usage of Java Mail in Writer's Mail Merge as Java Mail is not used. http://svn.apache.org/viewvc?view=revision&revision=1197035 Patches contributed by Mathias Bauer cws mba34issues01: #i117718#: provide filter name in case storage of medium does not allow to detect one http://svn.apache.org/viewvc?view=revision&revision=1172350 cws mba34issues01: #i117721#: directly provide parameters retrieved from SfxMedium http://svn.apache.org/viewvc?view=revision&revision=1172353 gnumake4 work variously http://svn.apache.org/viewvc?view=revision&revision=1394707 http://svn.apache.org/viewvc?view=revision&revision=1394326 http://svn.apache.org/viewvc?view=revision&revision=1396797 http://svn.apache.org/viewvc?view=revision&revision=1397315 cws mba34issues01: #i117723#: convert assertion into trace http://svn.apache.org/viewvc?view=revision&revision=1172355 cws mba34issues01: #i117699#: keep layout alive until swdoc dies http://svn.apache.org/viewvc?view=revision&revision=1172362 cws mba34issues01: #i117943#: missing color attributes in RTF clipboard http://svn.apache.org/viewvc?view=revision&revision=1172363 Patch contributed by Henning Brinkmann imported patch i#103878 http://svn.apache.org/viewvc?view=revision&revision=1172109 Patches contributed by Michael Stahl sw34bf06: #i117955#: WW8 export: disable storing of section breaks in endnotes http://svn.apache.org/viewvc?view=revision&revision=1172119 Patch contributed by imacat Fixed the Asian language work count. http://svn.apache.org/viewvc?view=revision&revision=1241345 Patch contributed by Pedro Giffuni i#20878 - Add comment with BZ issue for reference. http://svn.apache.org/viewvc?view=revision&revision=1244517 Patch contributed by Andre Fischer Do not add targets for junit tests when junit is disabled. http://svn.apache.org/viewvc?view=revision&revision=1241508 add writerperfect dependency.
2011-03-31 10:05:04 +02:00
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#ifndef INCLUDED_SW_SOURCE_CORE_INC_SWCACHE_HXX
#define INCLUDED_SW_SOURCE_CORE_INC_SWCACHE_HXX
2000-09-18 23:08:29 +00:00
/*
* Es werden Pointer auf Objekte verwaltet. Diese werden in einem einfachen
* PtrArray verwaltet.
* Angelegt (new) werden die Objekte von Cache-Zugriffsklassen, zuerstoert
* werden die Objekte vom Cache.
*
* Auf die Objekte kann wahlweise per Index in das Array oder per Suche
* zugegriffen werden. Soll per Index zugegriffen werden, so obliegt die
* Verwaltung des Index dem Anwender des Cache.
*
* Fuer die verwalteten Cache-Objekte gibt es eine Basisklasse, von dieser
* sind spezifische Klassen abzuleiten.
* In der Basisklasse werden die Cache-Objekte eines Cache doppelt verkettet,
* das ermoeglich die Implementierung eines LRU-Algorithmus.
*
* Der LRU kann in der Cache-Basisklasse manipuliert werden, indem ein
* virtueller First-Pointer gesetzt wird. Dieser kann auf den echten ersten
* plus einem Ofst gesetzt werden. Dadurch kann man den Anfangsbereich des
* Cache sichern und so dafuer sorgen, dass man waehrend bestimmter
* Operationen nicht den Cache versaut. Beispiel: Der Idle-Handler sollte nicht
* den Cache fuer den sichtbaren Bereich vernichten.
*
* Der Cache kann in der Groesse erweitert und wieder verkleinert werden.
* Beispiel: Fuer jede neue Shell wird der Cache fuer FormatInfo vergrossert
* und beim Destruieren der Shell wieder verkleinert.
*
*/
2011-10-20 02:06:49 +01:00
#include <vector>
#include <rtl/ustring.hxx>
2000-09-18 23:08:29 +00:00
class SwCacheObj;
typedef std::vector<SwCacheObj*> SwCacheObjArr;
class SwCache
2000-09-18 23:08:29 +00:00
{
SwCacheObjArr m_aCacheObjects;
2011-10-20 02:06:49 +01:00
std::vector<sal_uInt16> aFreePositions; //Freie Positionen fuer das Insert wenn
2000-09-18 23:08:29 +00:00
//die Maximalgrenze nicht erreicht ist.
//Immer wenn ein Objekt ausgetragen wird,
//so wird seine Position hier eingetragen.
SwCacheObj *pRealFirst; //_immer_ der echte LRU-erste
SwCacheObj *pFirst; //der virtuelle erste.
SwCacheObj *pLast;
sal_uInt16 nCurMax; //Mehr werden nicht aufgenommen.
2000-09-18 23:08:29 +00:00
void DeleteObj( SwCacheObj *pObj );
#ifdef DBG_UTIL
OString m_aName;
long m_nAppend; /// number of entries appended
long m_nInsertFree; /// number of entries inserted on freed position
long m_nReplace; /// number of LRU replacements
long m_nGetSuccess;
long m_nGetFail;
long m_nToTop; /// number of reordering (LRU)
long m_nDelete; /// number of explicit deletes
long m_nGetSeek; /// number of gets without index
long m_nAverageSeekCnt; /// number of seeks for all gets without index
long m_nFlushCnt; /// number of flush calls
long m_nFlushedObjects;
long m_nIncreaseMax; /// number of cache size increases
long m_nDecreaseMax; /// number of cache size decreases
void Check();
2000-09-18 23:08:29 +00:00
#endif
public:
//nur sal_uInt8 hineinstecken!!!
#ifdef DBG_UTIL
SwCache( const sal_uInt16 nInitSize, const OString &rNm );
2000-09-18 23:08:29 +00:00
#else
2012-01-21 17:51:19 +01:00
SwCache( const sal_uInt16 nInitSize );
2000-09-18 23:08:29 +00:00
#endif
// the destructor will free all objects still in the vector
~SwCache();
2000-09-18 23:08:29 +00:00
void Flush( const sal_uInt8 nPercent = 100 );
2000-09-18 23:08:29 +00:00
//bToTop == sal_False -> Keine LRU-Umsortierung!
SwCacheObj *Get( const void *pOwner, const sal_Bool bToTop = sal_True );
SwCacheObj *Get( const void *pOwner, const sal_uInt16 nIndex,
const sal_Bool bToTop = sal_True );
2000-09-18 23:08:29 +00:00
void ToTop( SwCacheObj *pObj );
sal_Bool Insert( SwCacheObj *pNew );
2000-09-18 23:08:29 +00:00
void Delete( const void *pOwner );
// void Delete( const void *pOwner, const sal_uInt16 nIndex );
2000-09-18 23:08:29 +00:00
void SetLRUOfst( const sal_uInt16 nOfst ); //nOfst sagt wieviele unangetastet
2000-09-18 23:08:29 +00:00
//bleiben sollen.
void ResetLRUOfst() { pFirst = pRealFirst; }
inline void IncreaseMax( const sal_uInt16 nAdd );
inline void DecreaseMax( const sal_uInt16 nSub );
sal_uInt16 GetCurMax() const { return nCurMax; }
2000-09-18 23:08:29 +00:00
inline SwCacheObj *First() { return pRealFirst; }
inline SwCacheObj *Last() { return pLast; }
inline SwCacheObj *Next( SwCacheObj *pCacheObj);
inline SwCacheObj* operator[](sal_uInt16 nIndex) { return m_aCacheObjects[nIndex]; }
inline sal_uInt16 size() { return m_aCacheObjects.size(); }
2000-09-18 23:08:29 +00:00
};
//Cache-Manipulation auf die sichere Art.
class SwSaveSetLRUOfst
{
SwCache &rCache;
public:
SwSaveSetLRUOfst( SwCache &rC, const sal_uInt16 nOfst )
2000-09-18 23:08:29 +00:00
: rCache( rC ) { rCache.SetLRUOfst( nOfst ); }
~SwSaveSetLRUOfst() { rCache.ResetLRUOfst(); }
};
//Das allgemeine CacheObjekt. Anwender des Cache muessen eine Klasse vom
//CacheObjekt ableiten und dort die Nutzdaten unterbringen.
class SwCacheObj
{
friend class SwCache; //Der darf alles
SwCacheObj *pNext; //Fuer die LRU-Verkettung.
SwCacheObj *pPrev;
sal_uInt16 nCachePos; //Position im Cache-Array.
2000-09-18 23:08:29 +00:00
sal_uInt8 nLock;
2000-09-18 23:08:29 +00:00
inline SwCacheObj *GetNext() { return pNext; }
inline SwCacheObj *GetPrev() { return pPrev; }
inline void SetNext( SwCacheObj *pNew ) { pNext = pNew; }
inline void SetPrev( SwCacheObj *pNew ) { pPrev = pNew; }
inline void SetCachePos( const sal_uInt16 nNew ) { nCachePos = nNew; }
2000-09-18 23:08:29 +00:00
protected:
const void *pOwner;
inline void SetOwner( const void *pNew ) { pOwner = pNew; }
public:
SwCacheObj( const void *pOwner );
virtual ~SwCacheObj();
inline const void *GetOwner() const { return pOwner; }
inline sal_Bool IsOwner( const void *pNew ) const;
2000-09-18 23:08:29 +00:00
inline sal_uInt16 GetCachePos() const { return nCachePos; }
2000-09-18 23:08:29 +00:00
inline void Invalidate() { pOwner = 0; }
inline sal_Bool IsLocked() const { return 0 != nLock; }
2000-09-18 23:08:29 +00:00
#ifdef DBG_UTIL
2000-09-18 23:08:29 +00:00
void Lock();
void Unlock();
#else
inline void Lock() { ++nLock; }
inline void Unlock() { --nLock; }
2000-09-18 23:08:29 +00:00
#endif
SwCacheObj *Next() { return pNext; }
SwCacheObj *Prev() { return pPrev; }
};
//Zugriffsklasse fuer den Cache. Im CTor wird das CacheObjekt erzeugt.
//Wenn der Cache keines herausrueckt wird der Member zunaechst auf 0 gesetzt.
//Beim Get wird dann eines erzeugt und, falls moeglich, in den Cache
//eingetragen.
//Anwender der des Cache muessen eine Klasse vom Access ableiten um
//fuer Typsicherheit zu sorgen, die Basisklasse sollte fuer das Get aber immer
//gerufen werden, ein Abgeleitetes Get sollte nur der Typsicherheit dienen.
//Cache-Objekte werden stets gelockt solange die Instanz lebt.
class SwCacheAccess
{
SwCache &rCache;
void _Get();
protected:
SwCacheObj *pObj;
const void *pOwner; //Kann ggf. in NewObj benutzt werden.
virtual SwCacheObj *NewObj() = 0;
inline SwCacheObj *Get();
inline SwCacheAccess( SwCache &rCache, const void *pOwner, sal_Bool bSeek = sal_True );
inline SwCacheAccess( SwCache &rCache, const void *pOwner, const sal_uInt16 nIndex );
2000-09-18 23:08:29 +00:00
public:
virtual ~SwCacheAccess();
2000-09-18 23:08:29 +00:00
virtual sal_Bool IsAvailable() const;
2000-09-18 23:08:29 +00:00
//Abkuerzung fuer diejenigen, die wissen, das die Ableitung das IsAvailable
//nicht ueberladen haben.
sal_Bool IsAvail() const { return pObj != 0; }
2000-09-18 23:08:29 +00:00
};
inline void SwCache::IncreaseMax( const sal_uInt16 nAdd )
2000-09-18 23:08:29 +00:00
{
nCurMax = nCurMax + sal::static_int_cast< sal_uInt16 >(nAdd);
#ifdef DBG_UTIL
++m_nIncreaseMax;
2000-09-18 23:08:29 +00:00
#endif
}
inline void SwCache::DecreaseMax( const sal_uInt16 nSub )
2000-09-18 23:08:29 +00:00
{
if ( nCurMax > nSub )
nCurMax = nCurMax - sal::static_int_cast< sal_uInt16 >(nSub);
#ifdef DBG_UTIL
++m_nDecreaseMax;
2000-09-18 23:08:29 +00:00
#endif
}
inline sal_Bool SwCacheObj::IsOwner( const void *pNew ) const
2000-09-18 23:08:29 +00:00
{
return pOwner && pOwner == pNew;
}
inline SwCacheObj *SwCache::Next( SwCacheObj *pCacheObj)
{
if ( pCacheObj )
return pCacheObj->GetNext();
else
return NULL;
}
inline SwCacheAccess::SwCacheAccess( SwCache &rC, const void *pOwn, sal_Bool bSeek ) :
2000-09-18 23:08:29 +00:00
rCache( rC ),
pObj( 0 ),
pOwner( pOwn )
2000-09-18 23:08:29 +00:00
{
if ( bSeek && pOwner && 0 != (pObj = rCache.Get( pOwner )) )
pObj->Lock();
}
inline SwCacheAccess::SwCacheAccess( SwCache &rC, const void *pOwn,
const sal_uInt16 nIndex ) :
2000-09-18 23:08:29 +00:00
rCache( rC ),
pObj( 0 ),
pOwner( pOwn )
2000-09-18 23:08:29 +00:00
{
if ( pOwner && 0 != (pObj = rCache.Get( pOwner, nIndex )) )
pObj->Lock();
}
inline SwCacheObj *SwCacheAccess::Get()
{
if ( !pObj )
_Get();
return pObj;
}
#endif
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */