LibreOffice Module sw (master)  1
txtcache.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include "txtcache.hxx"
21 #include <txtfrm.hxx>
22 #include "porlay.hxx"
23 
24 #include <sfx2/viewsh.hxx>
25 #include <view.hxx>
26 
27 SwTextLine::SwTextLine( SwTextFrame const *pFrame, std::unique_ptr<SwParaPortion> pNew ) :
28  SwCacheObj( static_cast<void const *>(pFrame) ),
29  pLine( std::move(pNew) )
30 {
31 }
32 
34 {
35 }
36 
38 {
39  // note: SwTextFrame lives longer than its SwTextLine, see ~SwTextFrame
40  assert(m_pOwner);
41  const_cast<SwTextFrame *>(static_cast<SwTextFrame const *>(m_pOwner))->SetCacheIdx(GetCachePos());
42 }
43 
45 {
46  return new SwTextLine( static_cast<SwTextFrame const *>(m_pOwner) );
47 }
48 
50 {
51  SwTextLine *pRet;
52  if ( m_pObj )
53  pRet = static_cast<SwTextLine*>(m_pObj);
54  else
55  {
56  pRet = static_cast<SwTextLine*>(Get(false));
57  const_cast<SwTextFrame *>(static_cast<SwTextFrame const *>(m_pOwner))->SetCacheIdx( pRet->GetCachePos() );
58  }
59  if ( !pRet->GetPara() )
60  pRet->SetPara( new SwParaPortion, true/*bDelete*/ );
61  return pRet->GetPara();
62 }
63 
65  SwCacheAccess( *SwTextFrame::GetTextCache(), pOwn, pOwn->GetCacheIdx() )
66 {
67 }
68 
70 {
71  return m_pObj && static_cast<SwTextLine*>(m_pObj)->GetPara();
72 }
73 
75 {
76  SwTextLine *pTextLine = static_cast<SwTextLine*>(SwTextFrame::GetTextCache()->
77  Get( this, GetCacheIdx(), false ));
78  if ( pTextLine )
79  {
80  if ( pTextLine->GetPara() )
81  return true;
82  }
83  else
84  const_cast<SwTextFrame*>(this)->mnCacheIndex = USHRT_MAX;
85 
86  return false;
87 }
88 
90 {
91  if ( GetCacheIdx() != USHRT_MAX )
92  {
93  SwTextLine *pLine = static_cast<SwTextLine*>(SwTextFrame::GetTextCache()->
94  Get( this, GetCacheIdx(), false ));
95  if ( pLine )
96  return pLine->GetPara();
97  else
99  }
100  return nullptr;
101 }
102 
104 {
105  OSL_ENSURE( !IsLocked(), "+SwTextFrame::ClearPara: this is locked." );
106  if ( !IsLocked() && GetCacheIdx() != USHRT_MAX )
107  {
108  SwTextLine *pTextLine = static_cast<SwTextLine*>(SwTextFrame::GetTextCache()->
109  Get( this, GetCacheIdx(), false ));
110  if ( pTextLine )
111  {
112  pTextLine->SetPara( nullptr, true/*bDelete*/ );
113  }
114  else
116  }
117 }
118 
120 {
121  if (GetCacheIdx() != USHRT_MAX)
122  {
123  s_pTextCache->Delete(this, GetCacheIdx());
125  }
126 }
127 
128 void SwTextFrame::SetPara( SwParaPortion *pNew, bool bDelete )
129 {
130  if ( GetCacheIdx() != USHRT_MAX )
131  {
132  // Only change the information, the CacheObj stays there
133  SwTextLine *pTextLine = static_cast<SwTextLine*>(SwTextFrame::GetTextCache()->
134  Get( this, GetCacheIdx(), false ));
135  if ( pTextLine )
136  {
137  pTextLine->SetPara( pNew, bDelete );
138  }
139  else
140  {
141  OSL_ENSURE( !pNew, "+SetPara: Losing SwParaPortion" );
143  }
144  }
145  else if ( pNew )
146  { // Insert a new one
147  SwTextLine *pTextLine = new SwTextLine( this, std::unique_ptr<SwParaPortion>(pNew) );
148  if (SwTextFrame::GetTextCache()->Insert(pTextLine, false))
149  mnCacheIndex = pTextLine->GetCachePos();
150  else
151  {
152  OSL_FAIL( "+SetPara: InsertCache failed." );
153  }
154  }
155 }
156 
167 {
168  sal_uInt16 nVisibleShells(0);
169  for (auto pView = SfxViewShell::GetFirst(true, checkSfxViewShell<SwView>);
170  pView != nullptr;
171  pView = SfxViewShell::GetNext(*pView, true, checkSfxViewShell<SwView>))
172  {
173  ++nVisibleShells;
174  }
175 
176  sal_uInt16 const nPreserved(100 * nVisibleShells);
177  SwCache & rCache(*SwTextFrame::GetTextCache());
178  if (rCache.GetCurMax() < nPreserved + 250)
179  {
180  rCache.IncreaseMax(nPreserved + 250 - rCache.GetCurMax());
181  }
182  rCache.SetLRUOfst(nPreserved);
183 }
184 
186 {
188 }
189 
190 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Represents the visualization of a paragraph.
Definition: txtfrm.hxx:149
sal_uInt16 GetCachePos() const
Definition: swcache.hxx:170
void SetLRUOfst(const sal_uInt16 nOfst)
Definition: swcache.cxx:446
SwCacheObj * m_pObj
Definition: swcache.hxx:201
sal_uInt16 GetCurMax() const
Definition: swcache.hxx:113
SwParaPortion * GetPara()
Definition: txtcache.cxx:49
SwParaPortion * GetPara()
Definition: txtcache.cxx:89
SwParaPortion * GetPara()
Definition: txtcache.hxx:38
const void * m_pOwner
Definition: swcache.hxx:202
Collection of SwLineLayout instances, represents the paragraph text in Writer layout.
Definition: porlay.hxx:229
void SetPara(SwParaPortion *pNew, bool bDelete=true)
Definition: txtcache.cxx:128
void SetPara(SwParaPortion *pNew, bool bDelete)
Definition: txtcache.hxx:41
static SwCache * GetTextCache()
Definition: txtfrm.hxx:597
SwCacheObj * Get(bool isDuplicateOwnerAllowed)
Definition: swcache.hxx:252
void ResetLRUOfst()
nOfst determines how many are not to be touched
Definition: swcache.hxx:109
const void * m_pOwner
Definition: swcache.hxx:160
static SfxViewShell * GetNext(const SfxViewShell &rPrev, bool bOnlyVisible=true, const std::function< bool(const SfxViewShell *)> &isViewShell=nullptr)
sal_uInt16 GetCacheIdx() const
Definition: txtfrm.hxx:602
sal_uInt16 mnCacheIndex
Definition: txtfrm.hxx:191
SwTextLineAccess(const SwTextFrame *pOwner)
Definition: txtcache.cxx:64
virtual void UpdateCachePos() override
Definition: txtcache.cxx:37
void IncreaseMax(const sal_uInt16 nAdd)
Definition: swcache.cxx:130
void RemoveFromCache()
Removes this frame completely from the Cache.
Definition: txtcache.cxx:119
SwSaveSetLRUOfst()
Prevent the SwParaPortions of the visible paragraphs from being deleted; they would just be recreated...
Definition: txtcache.cxx:166
virtual ~SwTextLine() override
Definition: txtcache.cxx:33
bool IsLocked() const
Definition: txtfrm.hxx:505
static SfxViewShell * GetFirst(bool bOnlyVisible=true, const std::function< bool(const SfxViewShell *)> &isViewShell=nullptr)
virtual SwCacheObj * NewObj() override
Can be use in NewObj.
Definition: txtcache.cxx:44
SwTextLine(SwTextFrame const *pFrame, std::unique_ptr< SwParaPortion > pNew=nullptr)
Definition: txtcache.cxx:27
void Delete(const void *pOwner, sal_uInt16 nIndex)
void ClearPara()
Removes the Line information from the Cache but retains the entry itself.
Definition: txtcache.cxx:103
The Cache object base class Users of the Cache must derrive a class from the SwCacheObj and store the...
Definition: swcache.hxx:133
const o3tl::enumarray< SvxAdjust, unsigned short > aSvxToUnoAdjust USHRT_MAX
Definition: unosett.cxx:261
SVX_DLLPUBLIC MSO_SPT Get(const OUString &)
void SetCacheIdx(const sal_uInt16 nNew)
Definition: txtfrm.hxx:603
Access class for the Cache.
Definition: swcache.hxx:194
static SwCache * s_pTextCache
SwLineLayout cache: the lines are not actually owned by the SwTextFrame but by this SwCache...
Definition: txtfrm.hxx:162
bool HasPara_() const
Definition: txtcache.cxx:74
bool IsAvailable() const
Definition: txtcache.cxx:69
typedef void(CALLTYPE *GetFuncDataPtr)(sal_uInt16 &nNo