LibreOffice Module sc (master)  1
interpretercontext.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 <interpretercontext.hxx>
21 #include <svl/numformat.hxx>
22 #include <svl/zforlist.hxx>
23 
24 #include <document.hxx>
25 #include <formula/token.hxx>
26 #include <lookupcache.hxx>
27 #include <algorithm>
28 
31 
33  : mpDoc(&rDoc)
34  , mnTokenCachePos(0)
35  , maTokens(TOKEN_CACHE_SIZE, nullptr)
36  , pInterpreter(nullptr)
37  , mpFormatter(pFormatter)
38 {
39 }
40 
42 {
43  ResetTokens();
44  mxScLookupCache.reset();
45 }
46 
48 {
49  for (auto p : maTokens)
50  if (p)
51  p->DecRef();
52 
53  mnTokenCachePos = 0;
54  std::fill(maTokens.begin(), maTokens.end(), nullptr);
55 }
56 
58 {
59  mpDoc = &rDoc;
60  mpFormatter = pFormatter;
61 }
62 
64 {
65  mpFormatter = mpDoc->GetFormatTable(); // will assert if not main thread
66 }
67 
69 {
70  // Do not disturb mScLookupCache
71  maConditions.clear();
73  ResetTokens();
74 }
75 
77 
79 {
81  {
82  return mpFormatter->GetType(nFIndex);
83  }
84 
85  if (maNFTypeCache.bIsValid && maNFTypeCache.nIndex == nFIndex)
86  {
87  return maNFTypeCache.eType;
88  }
89 
90  maNFTypeCache.nIndex = nFIndex;
92  maNFTypeCache.bIsValid = true;
93  return maNFTypeCache.eType;
94 }
95 
96 /* ScInterpreterContextPool */
97 
98 // Threaded version
99 void ScInterpreterContextPool::Init(size_t nNumThreads, const ScDocument& rDoc,
100  SvNumberFormatter* pFormatter)
101 {
102  assert(mbThreaded);
103  size_t nOldSize = maPool.size();
104  maPool.resize(nNumThreads);
105  for (size_t nIdx = 0; nIdx < nNumThreads; ++nIdx)
106  {
107  if (nIdx >= nOldSize)
108  maPool[nIdx].reset(new ScInterpreterContext(rDoc, pFormatter));
109  else
110  maPool[nIdx]->SetDocAndFormatter(rDoc, pFormatter);
111  }
112 }
113 
116 {
117  assert(mbThreaded);
118  assert(nThreadIdx < maPool.size());
119  return maPool[nThreadIdx].get();
120 }
121 
122 // Non-Threaded version
124 {
125  assert(!mbThreaded);
126  assert(mnNextFree <= maPool.size());
127  bool bCreateNew = (maPool.size() == mnNextFree);
128  size_t nCurrIdx = mnNextFree;
129  if (bCreateNew)
130  {
131  maPool.resize(maPool.size() + 1);
132  maPool[nCurrIdx].reset(new ScInterpreterContext(rDoc, pFormatter));
133  }
134  else
135  maPool[nCurrIdx]->SetDocAndFormatter(rDoc, pFormatter);
136 
137  ++mnNextFree;
138 }
139 
141 {
142  assert(!mbThreaded);
143  assert(mnNextFree && (mnNextFree <= maPool.size()));
144  return maPool[mnNextFree - 1].get();
145 }
146 
148 {
149  if (mbThreaded)
150  {
151  for (size_t nIdx = 0; nIdx < maPool.size(); ++nIdx)
152  maPool[nIdx]->Cleanup();
153  }
154  else
155  {
156  assert(mnNextFree && (mnNextFree <= maPool.size()));
157  --mnNextFree;
158  maPool[mnNextFree]->Cleanup();
159  }
160 }
161 
162 // static
164 {
165  for (auto& rPtr : aThreadedInterpreterPool.maPool)
166  rPtr->ClearLookupCache();
167  for (auto& rPtr : aNonThreadedInterpreterPool.maPool)
168  rPtr->ClearLookupCache();
169 }
170 
171 /* ScThreadedInterpreterContextGetterGuard */
172 
174  size_t nNumThreads, const ScDocument& rDoc, SvNumberFormatter* pFormatter)
175  : rPool(ScInterpreterContextPool::aThreadedInterpreterPool)
176 {
177  rPool.Init(nNumThreads, rDoc, pFormatter);
178 }
179 
181 {
183 }
184 
187 {
188  return rPool.GetInterpreterContextForThreadIdx(nThreadIdx);
189 }
190 
191 /* ScInterpreterContextGetterGuard */
192 
194  SvNumberFormatter* pFormatter)
195  : rPool(ScInterpreterContextPool::aNonThreadedInterpreterPool)
196 #if !defined NDEBUG
197  , nContextIdx(rPool.mnNextFree)
198 #endif
199 {
200  rPool.Init(rDoc, pFormatter);
201 }
202 
204 {
205  assert(nContextIdx == (rPool.mnNextFree - 1));
207 }
208 
210 {
211  return rPool.GetInterpreterContext();
212 }
213 
214 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define TOKEN_CACHE_SIZE
std::vector< DelayedSetNumberFormat > maDelayedSetNumberFormat
SvNumFormatType GetType(sal_uInt32 nFIndex) const
const ScDocument * mpDoc
static ScInterpreterContextPool aThreadedInterpreterPool
NFIndexAndFmtType maNFTypeCache
ScInterpreterContext * GetInterpreterContext() const
std::vector< std::unique_ptr< ScInterpreterContext > > maPool
ScThreadedInterpreterContextGetterGuard(size_t nNumThreads, const ScDocument &rDoc, SvNumberFormatter *pFormatter)
SvNumberFormatter * mpFormatter
void SetDocAndFormatter(const ScDocument &rDoc, SvNumberFormatter *pFormatter)
std::vector< sal_uInt32 > maConditions
SC_DLLPUBLIC SvNumberFormatter * GetFormatTable() const
Definition: documen2.cxx:441
static ScInterpreterContextPool aNonThreadedInterpreterPool
std::unique_ptr< ScLookupCacheMap > mxScLookupCache
SvNumFormatType
ScInterpreterContext()=delete
ScInterpreterContext * GetInterpreterContextForThreadIdx(size_t nThreadIdx) const
bool IsThreadedGroupCalcInProgress() const
Definition: document.hxx:617
ScInterpreterContextGetterGuard(const ScDocument &rDoc, SvNumberFormatter *pFormatter)
std::vector< formula::FormulaToken * > maTokens
if(aStr!=aBuf) UpdateName_Impl(m_xFollowLb.get()
ScInterpreterContextPool & rPool
SvNumFormatType GetNumberFormatType(sal_uInt32 nFIndex) const
void * p
ScInterpreterContext * GetInterpreterContext() const
void Init(size_t nNumThreads, const ScDocument &rDoc, SvNumberFormatter *pFormatter)
SvNumFormatType eType
ScInterpreterContext * GetInterpreterContextForThreadIdx(size_t nThreadIdx) const