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
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
51 std::fill(maTokens.begin(), maTokens.end(), nullptr);
52}
53
55{
56 if (mpDoc != &rDoc)
57 {
58 mxScLookupCache.reset();
59 mpDoc = &rDoc;
60 }
61 mpFormatter = pFormatter;
62}
63
65{
66 mpFormatter = mpDoc->GetFormatTable(); // will assert if not main thread
67}
68
70{
71 // Do not disturb mxScLookupCache.
72 maConditions.clear();
75}
76
78{
79 if (pDoc == mpDoc)
80 mxScLookupCache.reset();
81}
82
84{
86 {
87 return mpFormatter->GetType(nFIndex);
88 }
89
91 {
92 return maNFTypeCache.eType;
93 }
94
95 maNFTypeCache.nIndex = nFIndex;
98 return maNFTypeCache.eType;
99}
100
101/* ScInterpreterContextPool */
102
103// Threaded version
104void ScInterpreterContextPool::Init(size_t nNumThreads, const ScDocument& rDoc,
105 SvNumberFormatter* pFormatter)
106{
107 assert(mbThreaded);
108 size_t nOldSize = maPool.size();
109 maPool.resize(nNumThreads);
110 for (size_t nIdx = 0; nIdx < nNumThreads; ++nIdx)
111 {
112 if (nIdx >= nOldSize)
113 maPool[nIdx].reset(new ScInterpreterContext(rDoc, pFormatter));
114 else
115 maPool[nIdx]->SetDocAndFormatter(rDoc, pFormatter);
116 }
117}
118
121{
122 assert(mbThreaded);
123 assert(nThreadIdx < maPool.size());
124 return maPool[nThreadIdx].get();
125}
126
127// Non-Threaded version
129{
130 assert(!mbThreaded);
131 assert(mnNextFree <= maPool.size());
132 bool bCreateNew = (maPool.size() == mnNextFree);
133 size_t nCurrIdx = mnNextFree;
134 if (bCreateNew)
135 {
136 maPool.resize(maPool.size() + 1);
137 maPool[nCurrIdx].reset(new ScInterpreterContext(rDoc, pFormatter));
138 }
139 else
140 maPool[nCurrIdx]->SetDocAndFormatter(rDoc, pFormatter);
141
142 ++mnNextFree;
143}
144
146{
147 assert(!mbThreaded);
148 assert(mnNextFree && (mnNextFree <= maPool.size()));
149 return maPool[mnNextFree - 1].get();
150}
151
153{
154 if (mbThreaded)
155 {
156 for (size_t nIdx = 0; nIdx < maPool.size(); ++nIdx)
157 maPool[nIdx]->Cleanup();
158 }
159 else
160 {
161 assert(mnNextFree && (mnNextFree <= maPool.size()));
162 --mnNextFree;
163 maPool[mnNextFree]->Cleanup();
164 }
165}
166
167// static
169{
170 for (auto& rPtr : aThreadedInterpreterPool.maPool)
171 rPtr->ClearLookupCache(pDoc);
172 for (auto& rPtr : aNonThreadedInterpreterPool.maPool)
173 rPtr->ClearLookupCache(pDoc);
174}
175
176/* ScThreadedInterpreterContextGetterGuard */
177
179 size_t nNumThreads, const ScDocument& rDoc, SvNumberFormatter* pFormatter)
180 : rPool(ScInterpreterContextPool::aThreadedInterpreterPool)
181{
182 rPool.Init(nNumThreads, rDoc, pFormatter);
183}
184
186{
188}
189
192{
193 return rPool.GetInterpreterContextForThreadIdx(nThreadIdx);
194}
195
196/* ScInterpreterContextGetterGuard */
197
199 SvNumberFormatter* pFormatter)
200 : rPool(ScInterpreterContextPool::aNonThreadedInterpreterPool)
201#if !defined NDEBUG
202 , nContextIdx(rPool.mnNextFree)
203#endif
204{
205 rPool.Init(rDoc, pFormatter);
206}
207
209{
210 assert(nContextIdx == (rPool.mnNextFree - 1));
212}
213
215{
217}
218
219/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool IsThreadedGroupCalcInProgress() const
Definition: document.hxx:630
SC_DLLPUBLIC SvNumberFormatter * GetFormatTable() const
Definition: documen2.cxx:467
ScInterpreterContextPool & rPool
ScInterpreterContext * GetInterpreterContext() const
ScInterpreterContextGetterGuard(const ScDocument &rDoc, SvNumberFormatter *pFormatter)
void Init(size_t nNumThreads, const ScDocument &rDoc, SvNumberFormatter *pFormatter)
static ScInterpreterContextPool aNonThreadedInterpreterPool
static ScInterpreterContextPool aThreadedInterpreterPool
std::vector< std::unique_ptr< ScInterpreterContext > > maPool
ScInterpreterContext * GetInterpreterContext() const
static void ClearLookupCaches(const ScDocument *pDoc)
ScInterpreterContext * GetInterpreterContextForThreadIdx(size_t nThreadIdx) const
ScInterpreterContext * GetInterpreterContextForThreadIdx(size_t nThreadIdx) const
ScThreadedInterpreterContextGetterGuard(size_t nNumThreads, const ScDocument &rDoc, SvNumberFormatter *pFormatter)
SvNumFormatType GetType(sal_uInt32 nFIndex) const
#define TOKEN_CACHE_SIZE
void * p
if(aStr !=aBuf) UpdateName_Impl(m_xFollowLb.get()
SvNumFormatType eType
ScInterpreterContext()=delete
void SetDocAndFormatter(const ScDocument &rDoc, SvNumberFormatter *pFormatter)
const ScDocument * mpDoc
std::unique_ptr< ScLookupCacheMap > mxScLookupCache
void ClearLookupCache(const ScDocument *pDoc)
std::vector< formula::FormulaToken * > maTokens
SvNumFormatType GetNumberFormatType(sal_uInt32 nFIndex) const
std::vector< sal_uInt8 > maConditions
std::vector< DelayedSetNumberFormat > maDelayedSetNumberFormat
NFIndexAndFmtType maNFTypeCache
SvNumberFormatter * mpFormatter
SvNumFormatType