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 <rangecache.hxx>
28 #include <algorithm>
29 
32 
34  : mpDoc(&rDoc)
35  , mnTokenCachePos(0)
36  , maTokens(TOKEN_CACHE_SIZE, nullptr)
37  , pInterpreter(nullptr)
38  , mpFormatter(pFormatter)
39 {
40 }
41 
43 
45 {
46  for (auto p : maTokens)
47  if (p)
48  p->DecRef();
49 
50  mnTokenCachePos = 0;
51  std::fill(maTokens.begin(), maTokens.end(), nullptr);
52 }
53 
55 {
56  mpDoc = &rDoc;
57  mpFormatter = pFormatter;
58 }
59 
61 {
62  mpFormatter = mpDoc->GetFormatTable(); // will assert if not main thread
63 }
64 
66 {
67  // Do not disturb mxScLookupCache.
68  maConditions.clear();
70  ResetTokens();
71 }
72 
74 
76 {
78  {
79  return mpFormatter->GetType(nFIndex);
80  }
81 
82  if (maNFTypeCache.bIsValid && maNFTypeCache.nIndex == nFIndex)
83  {
84  return maNFTypeCache.eType;
85  }
86 
87  maNFTypeCache.nIndex = nFIndex;
89  maNFTypeCache.bIsValid = true;
90  return maNFTypeCache.eType;
91 }
92 
93 /* ScInterpreterContextPool */
94 
95 // Threaded version
96 void ScInterpreterContextPool::Init(size_t nNumThreads, const ScDocument& rDoc,
97  SvNumberFormatter* pFormatter)
98 {
99  assert(mbThreaded);
100  size_t nOldSize = maPool.size();
101  maPool.resize(nNumThreads);
102  for (size_t nIdx = 0; nIdx < nNumThreads; ++nIdx)
103  {
104  if (nIdx >= nOldSize)
105  maPool[nIdx].reset(new ScInterpreterContext(rDoc, pFormatter));
106  else
107  maPool[nIdx]->SetDocAndFormatter(rDoc, pFormatter);
108  }
109 }
110 
113 {
114  assert(mbThreaded);
115  assert(nThreadIdx < maPool.size());
116  return maPool[nThreadIdx].get();
117 }
118 
119 // Non-Threaded version
121 {
122  assert(!mbThreaded);
123  assert(mnNextFree <= maPool.size());
124  bool bCreateNew = (maPool.size() == mnNextFree);
125  size_t nCurrIdx = mnNextFree;
126  if (bCreateNew)
127  {
128  maPool.resize(maPool.size() + 1);
129  maPool[nCurrIdx].reset(new ScInterpreterContext(rDoc, pFormatter));
130  }
131  else
132  maPool[nCurrIdx]->SetDocAndFormatter(rDoc, pFormatter);
133 
134  ++mnNextFree;
135 }
136 
138 {
139  assert(!mbThreaded);
140  assert(mnNextFree && (mnNextFree <= maPool.size()));
141  return maPool[mnNextFree - 1].get();
142 }
143 
145 {
146  if (mbThreaded)
147  {
148  for (size_t nIdx = 0; nIdx < maPool.size(); ++nIdx)
149  maPool[nIdx]->Cleanup();
150  }
151  else
152  {
153  assert(mnNextFree && (mnNextFree <= maPool.size()));
154  --mnNextFree;
155  maPool[mnNextFree]->Cleanup();
156  }
157 }
158 
159 // static
161 {
162  for (auto& rPtr : aThreadedInterpreterPool.maPool)
163  rPtr->ClearLookupCache();
164  for (auto& rPtr : aNonThreadedInterpreterPool.maPool)
165  rPtr->ClearLookupCache();
166 }
167 
168 /* ScThreadedInterpreterContextGetterGuard */
169 
171  size_t nNumThreads, const ScDocument& rDoc, SvNumberFormatter* pFormatter)
172  : rPool(ScInterpreterContextPool::aThreadedInterpreterPool)
173 {
174  rPool.Init(nNumThreads, rDoc, pFormatter);
175 }
176 
178 {
180 }
181 
184 {
185  return rPool.GetInterpreterContextForThreadIdx(nThreadIdx);
186 }
187 
188 /* ScInterpreterContextGetterGuard */
189 
191  SvNumberFormatter* pFormatter)
192  : rPool(ScInterpreterContextPool::aNonThreadedInterpreterPool)
193 #if !defined NDEBUG
194  , nContextIdx(rPool.mnNextFree)
195 #endif
196 {
197  rPool.Init(rDoc, pFormatter);
198 }
199 
201 {
202  assert(nContextIdx == (rPool.mnNextFree - 1));
204 }
205 
207 {
208  return rPool.GetInterpreterContext();
209 }
210 
211 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::vector< sal_uInt8 > maConditions
#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)
SC_DLLPUBLIC SvNumberFormatter * GetFormatTable() const
Definition: documen2.cxx:460
static ScInterpreterContextPool aNonThreadedInterpreterPool
std::unique_ptr< ScLookupCacheMap > mxScLookupCache
SvNumFormatType
ScInterpreterContext()=delete
ScInterpreterContext * GetInterpreterContextForThreadIdx(size_t nThreadIdx) const
bool IsThreadedGroupCalcInProgress() const
Definition: document.hxx:629
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