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/zforlist.hxx>
22 
23 #include <document.hxx>
24 #include <formula/token.hxx>
25 #include <lookupcache.hxx>
26 #include <algorithm>
27 
30 
32  : mpDoc(&rDoc)
33  , mnTokenCachePos(0)
34  , maTokens(TOKEN_CACHE_SIZE, nullptr)
35  , pInterpreter(nullptr)
36  , mpFormatter(pFormatter)
37 {
38 }
39 
41 {
42  ResetTokens();
43  mxScLookupCache.reset();
44 }
45 
47 {
48  for (auto p : maTokens)
49  if (p)
50  p->DecRef();
51 
52  mnTokenCachePos = 0;
53  std::fill(maTokens.begin(), maTokens.end(), nullptr);
54 }
55 
57 {
58  mpDoc = &rDoc;
59  mpFormatter = pFormatter;
60 }
61 
63 {
64  mpFormatter = mpDoc->GetFormatTable(); // will assert if not main thread
65 }
66 
68 {
69  // Do not disturb mScLookupCache
70  maConditions.clear();
72  ResetTokens();
73 }
74 
76 
78 {
80  {
81  return mpFormatter->GetType(nFIndex);
82  }
83 
84  if (maNFTypeCache.bIsValid && maNFTypeCache.nIndex == nFIndex)
85  {
86  return maNFTypeCache.eType;
87  }
88 
89  maNFTypeCache.nIndex = nFIndex;
91  maNFTypeCache.bIsValid = true;
92  return maNFTypeCache.eType;
93 }
94 
95 /* ScInterpreterContextPool */
96 
97 // Threaded version
98 void ScInterpreterContextPool::Init(size_t nNumThreads, const ScDocument& rDoc,
99  SvNumberFormatter* pFormatter)
100 {
102  size_t nOldSize = maPool.size();
103  maPool.resize(nNumThreads);
104  for (size_t nIdx = 0; nIdx < nNumThreads; ++nIdx)
105  {
106  if (nIdx >= nOldSize)
107  maPool[nIdx].reset(new ScInterpreterContext(rDoc, pFormatter));
108  else
109  maPool[nIdx]->SetDocAndFormatter(rDoc, pFormatter);
110  }
111 }
112 
115 {
117  assert(nThreadIdx < maPool.size());
118  return maPool[nThreadIdx].get();
119 }
120 
121 // Non-Threaded version
123 {
124  assert(!mbThreaded);
125  assert(mnNextFree <= maPool.size());
126  bool bCreateNew = (maPool.size() == mnNextFree);
127  size_t nCurrIdx = mnNextFree;
128  if (bCreateNew)
129  {
130  maPool.resize(maPool.size() + 1);
131  maPool[nCurrIdx].reset(new ScInterpreterContext(rDoc, pFormatter));
132  }
133  else
134  maPool[nCurrIdx]->SetDocAndFormatter(rDoc, pFormatter);
135 
136  ++mnNextFree;
137 }
138 
140 {
141  assert(!mbThreaded);
142  assert(mnNextFree && (mnNextFree <= maPool.size()));
143  return maPool[mnNextFree - 1].get();
144 }
145 
147 {
148  if (mbThreaded)
149  {
150  for (size_t nIdx = 0; nIdx < maPool.size(); ++nIdx)
151  maPool[nIdx]->Cleanup();
152  }
153  else
154  {
155  assert(mnNextFree && (mnNextFree <= maPool.size()));
156  --mnNextFree;
157  maPool[mnNextFree]->Cleanup();
158  }
159 }
160 
161 // static
163 {
164  for (auto& rPtr : aThreadedInterpreterPool.maPool)
165  rPtr->ClearLookupCache();
166  for (auto& rPtr : aNonThreadedInterpreterPool.maPool)
167  rPtr->ClearLookupCache();
168 }
169 
170 /* ScThreadedInterpreterContextGetterGuard */
171 
173  size_t nNumThreads, const ScDocument& rDoc, SvNumberFormatter* pFormatter)
174  : rPool(ScInterpreterContextPool::aThreadedInterpreterPool)
175 {
176  rPool.Init(nNumThreads, rDoc, pFormatter);
177 }
178 
180 {
182 }
183 
186 {
187  return rPool.GetInterpreterContextForThreadIdx(nThreadIdx);
188 }
189 
190 /* ScInterpreterContextGetterGuard */
191 
193  SvNumberFormatter* pFormatter)
194  : rPool(ScInterpreterContextPool::aNonThreadedInterpreterPool)
195 #if !defined NDEBUG
196  , nContextIdx(rPool.mnNextFree)
197 #endif
198 {
199  rPool.Init(rDoc, pFormatter);
200 }
201 
203 {
206 }
207 
209 {
210  return rPool.GetInterpreterContext();
211 }
212 
213 /* 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
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
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:440
static ScInterpreterContextPool aNonThreadedInterpreterPool
std::unique_ptr< ScLookupCacheMap > mxScLookupCache
SvNumFormatType
ScInterpreterContext()=delete
ScInterpreterContext * GetInterpreterContextForThreadIdx(size_t nThreadIdx) const
bool IsThreadedGroupCalcInProgress() const
Definition: document.hxx:616
ScInterpreterContextGetterGuard(const ScDocument &rDoc, SvNumberFormatter *pFormatter)
std::vector< formula::FormulaToken * > maTokens
ScInterpreterContextPool & rPool
SvNumFormatType GetNumberFormatType(sal_uInt32 nFIndex) const
void * p
if(!pCandidateA->getEnd().equal(pCandidateB->getStart()))
ScInterpreterContext * GetInterpreterContext() const
void Init(size_t nNumThreads, const ScDocument &rDoc, SvNumberFormatter *pFormatter)
SvNumFormatType eType
ScInterpreterContext * GetInterpreterContextForThreadIdx(size_t nThreadIdx) const