1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <interpretercontext.hxx>
21 #include <svl/numformat.hxx>
22 #include <svl/zforlist.hxx>
24 #include <document.hxx>
25 #include <comphelper/random.hxx>
26 #include <formula/token.hxx>
27 #include <lookupcache.hxx>
28 #include <rangecache.hxx>
31 ScInterpreterContextPool
ScInterpreterContextPool::aThreadedInterpreterPool(true);
32 ScInterpreterContextPool
ScInterpreterContextPool::aNonThreadedInterpreterPool(false);
34 ScInterpreterContext::ScInterpreterContext(const ScDocument
& rDoc
, SvNumberFormatter
* pFormatter
)
37 , maTokens(TOKEN_CACHE_SIZE
, nullptr)
38 // create a per-interpreter Random Number Generator, seeded from the global rng, so we don't have
39 // to lock a mutex to generate a random number
40 , aRNG(comphelper::rng::uniform_uint_distribution(0, std::numeric_limits
<sal_uInt32
>::max()))
41 , pInterpreter(nullptr)
42 , mpFormatter(pFormatter
)
46 mpFormatData
= nullptr;
50 prepFormatterForRoMode(pFormatter
);
53 ScInterpreterContext::~ScInterpreterContext() { ResetTokens(); }
55 void ScInterpreterContext::ResetTokens()
57 for (auto p
: maTokens
)
62 std::fill(maTokens
.begin(), maTokens
.end(), nullptr);
65 void ScInterpreterContext::SetDocAndFormatter(const ScDocument
& rDoc
, SvNumberFormatter
* pFormatter
)
69 mxScLookupCache
.reset();
72 if (mpFormatter
!= pFormatter
)
74 mpFormatter
= pFormatter
;
76 // formatter has changed
77 prepFormatterForRoMode(pFormatter
);
80 std::fill(maNFBuiltInCache
.begin(), maNFBuiltInCache
.end(), NFBuiltIn());
81 std::fill(maNFTypeCache
.begin(), maNFTypeCache
.end(), NFType());
85 void ScInterpreterContext::prepFormatterForRoMode(SvNumberFormatter
* pFormatter
)
87 pFormatter
->PrepForRoMode();
88 mpFormatData
= &pFormatter
->GetROFormatData();
89 mpNatNum
= &pFormatter
->GetNatNum();
90 mxLanguageData
.reset(new SvNFLanguageData(pFormatter
->GetROLanguageData()));
91 mxAuxFormatKeyMap
.reset(new SvNFFormatData::DefaultFormatKeysMap
);
92 maROPolicy
= SvNFEngine::GetROPolicy(*mpFormatData
, *mxAuxFormatKeyMap
);
95 void ScInterpreterContext::initFormatTable()
97 mpFormatter
= mpDoc
->GetFormatTable(); // will assert if not main thread
98 prepFormatterForRoMode(mpFormatter
);
101 void ScInterpreterContext::MergeDefaultFormatKeys(SvNumberFormatter
& rFormatter
) const
103 rFormatter
.MergeDefaultFormatKeys(*mxAuxFormatKeyMap
);
106 void ScInterpreterContext::Cleanup()
108 // Do not disturb mxScLookupCache.
109 maConditions
.clear();
110 maDelayedSetNumberFormat
.clear();
114 void ScInterpreterContext::ClearLookupCache(const ScDocument
* pDoc
)
118 mxScLookupCache
.reset();
119 mxLanguageData
.reset();
120 mxAuxFormatKeyMap
.reset();
121 mpFormatter
= nullptr;
122 mpFormatData
= nullptr;
127 SvNumFormatType
ScInterpreterContext::NFGetType(sal_uInt32 nFIndex
) const
129 if (!mpDoc
->IsThreadedGroupCalcInProgress())
130 return GetFormatTable()->GetType(nFIndex
);
132 auto aFind
= std::find_if(maNFTypeCache
.begin(), maNFTypeCache
.end(),
133 [nFIndex
](const NFType
& e
) { return e
.nKey
== nFIndex
; });
134 if (aFind
!= maNFTypeCache
.end())
137 SvNumFormatType eType
= mpFormatData
->GetType(nFIndex
);
139 std::move_backward(maNFTypeCache
.begin(),
140 std::next(maNFTypeCache
.begin(), maNFTypeCache
.size() - 1),
141 maNFTypeCache
.end());
142 maNFTypeCache
[0].nKey
= nFIndex
;
143 maNFTypeCache
[0].eType
= eType
;
148 const SvNumberformat
* ScInterpreterContext::NFGetFormatEntry(sal_uInt32 nKey
) const
150 if (!mpDoc
->IsThreadedGroupCalcInProgress())
151 return GetFormatTable()->GetEntry(nKey
);
152 return mpFormatData
->GetFormatEntry(nKey
);
155 bool ScInterpreterContext::NFIsTextFormat(sal_uInt32 nFIndex
) const
157 if (!mpDoc
->IsThreadedGroupCalcInProgress())
158 return GetFormatTable()->IsTextFormat(nFIndex
);
159 return mpFormatData
->IsTextFormat(nFIndex
);
162 const Date
& ScInterpreterContext::NFGetNullDate() const
164 if (!mpDoc
->IsThreadedGroupCalcInProgress())
165 return GetFormatTable()->GetNullDate();
166 return mxLanguageData
->GetNullDate();
169 sal_uInt32
ScInterpreterContext::NFGetTimeFormat(double fNumber
, LanguageType eLnge
,
170 bool bForceDuration
) const
172 if (!mpDoc
->IsThreadedGroupCalcInProgress())
173 return GetFormatTable()->GetTimeFormat(fNumber
, eLnge
, bForceDuration
);
174 return SvNFEngine::GetTimeFormat(*mxLanguageData
, *mpFormatData
, *mpNatNum
, maROPolicy
, fNumber
,
175 eLnge
, bForceDuration
);
178 sal_uInt32
ScInterpreterContext::NFGetFormatIndex(NfIndexTableOffset nTabOff
,
179 LanguageType eLnge
) const
181 if (!mpDoc
->IsThreadedGroupCalcInProgress())
182 return GetFormatTable()->GetFormatIndex(nTabOff
, eLnge
);
183 return SvNFEngine::GetFormatIndex(*mxLanguageData
, maROPolicy
, *mpNatNum
, nTabOff
, eLnge
);
185 OUString
ScInterpreterContext::NFGetFormatDecimalSep(sal_uInt32 nFormat
) const
187 if (!mpDoc
->IsThreadedGroupCalcInProgress())
188 return GetFormatTable()->GetFormatDecimalSep(nFormat
);
189 return SvNFEngine::GetFormatDecimalSep(*mxLanguageData
, *mpFormatData
, nFormat
);
192 sal_uInt16
ScInterpreterContext::NFGetFormatPrecision(sal_uInt32 nFormat
) const
194 if (!mpDoc
->IsThreadedGroupCalcInProgress())
195 return GetFormatTable()->GetFormatPrecision(nFormat
);
196 return SvNFEngine::GetFormatPrecision(*mxLanguageData
, *mpFormatData
, nFormat
);
199 sal_uInt32
ScInterpreterContext::NFGetFormatForLanguageIfBuiltIn(sal_uInt32 nFormat
,
200 LanguageType eLnge
) const
202 if (!mpDoc
->IsThreadedGroupCalcInProgress())
203 return GetFormatTable()->GetFormatForLanguageIfBuiltIn(nFormat
, eLnge
);
205 sal_uInt64 nKey
= (static_cast<sal_uInt64
>(nFormat
) << 32) | eLnge
.get();
207 auto aFind
= std::find_if(maNFBuiltInCache
.begin(), maNFBuiltInCache
.end(),
208 [nKey
](const NFBuiltIn
& e
) { return e
.nKey
== nKey
; });
209 if (aFind
!= maNFBuiltInCache
.end())
210 return aFind
->nFormat
;
212 nFormat
= SvNFEngine::GetFormatForLanguageIfBuiltIn(*mxLanguageData
, *mpNatNum
, maROPolicy
,
215 std::move_backward(maNFBuiltInCache
.begin(),
216 std::next(maNFBuiltInCache
.begin(), maNFBuiltInCache
.size() - 1),
217 maNFBuiltInCache
.end());
218 maNFBuiltInCache
[0].nKey
= nKey
;
219 maNFBuiltInCache
[0].nFormat
= nFormat
;
224 sal_uInt32
ScInterpreterContext::NFGetStandardFormat(SvNumFormatType eType
, LanguageType eLnge
)
226 if (!mpDoc
->IsThreadedGroupCalcInProgress())
227 return GetFormatTable()->GetStandardFormat(eType
, eLnge
);
228 return SvNFEngine::GetStandardFormat(*mxLanguageData
, *mpFormatData
, *mpNatNum
, maROPolicy
,
232 sal_uInt32
ScInterpreterContext::NFGetStandardFormat(sal_uInt32 nFIndex
, SvNumFormatType eType
,
235 if (!mpDoc
->IsThreadedGroupCalcInProgress())
236 return GetFormatTable()->GetStandardFormat(nFIndex
, eType
, eLnge
);
237 return SvNFEngine::GetStandardFormat(*mxLanguageData
, *mpFormatData
, *mpNatNum
, maROPolicy
,
238 nFIndex
, eType
, eLnge
);
241 OUString
ScInterpreterContext::NFGetInputLineString(const double& fOutNumber
, sal_uInt32 nFIndex
,
242 bool bFiltering
, bool bForceSystemLocale
) const
244 if (!mpDoc
->IsThreadedGroupCalcInProgress())
245 return GetFormatTable()->GetInputLineString(fOutNumber
, nFIndex
, bFiltering
,
247 return SvNFEngine::GetInputLineString(*mxLanguageData
, *mpFormatData
, *mpNatNum
, maROPolicy
,
248 fOutNumber
, nFIndex
, bFiltering
, bForceSystemLocale
);
250 void ScInterpreterContext::NFGetOutputString(const double& fOutNumber
, sal_uInt32 nFIndex
,
251 OUString
& sOutString
, const Color
** ppColor
,
252 bool bUseStarFormat
) const
254 if (!mpDoc
->IsThreadedGroupCalcInProgress())
255 return GetFormatTable()->GetOutputString(fOutNumber
, nFIndex
, sOutString
, ppColor
,
257 return SvNFEngine::GetOutputString(*mxLanguageData
, *mpFormatData
, *mpNatNum
, maROPolicy
,
258 fOutNumber
, nFIndex
, sOutString
, ppColor
, bUseStarFormat
);
261 void ScInterpreterContext::NFGetOutputString(const OUString
& sString
, sal_uInt32 nFIndex
,
262 OUString
& sOutString
, const Color
** ppColor
,
263 bool bUseStarFormat
) const
265 if (!mpDoc
->IsThreadedGroupCalcInProgress())
266 return GetFormatTable()->GetOutputString(sString
, nFIndex
, sOutString
, ppColor
,
268 return SvNFEngine::GetOutputString(*mxLanguageData
, *mpFormatData
, sString
, nFIndex
, sOutString
,
269 ppColor
, bUseStarFormat
);
272 sal_uInt32
ScInterpreterContext::NFGetStandardIndex(LanguageType eLnge
) const
274 if (!mpDoc
->IsThreadedGroupCalcInProgress())
275 return GetFormatTable()->GetStandardIndex(eLnge
);
276 return SvNFEngine::GetStandardIndex(*mxLanguageData
, *mpFormatData
, *mpNatNum
, maROPolicy
,
280 bool ScInterpreterContext::NFGetPreviewString(const OUString
& sFormatString
, double fPreviewNumber
,
281 OUString
& sOutString
, const Color
** ppColor
,
284 if (!mpDoc
->IsThreadedGroupCalcInProgress())
285 return GetFormatTable()->GetPreviewString(sFormatString
, fPreviewNumber
, sOutString
,
287 return SvNFEngine::GetPreviewString(*mxLanguageData
, *mpFormatData
, *mpNatNum
, maROPolicy
,
288 sFormatString
, fPreviewNumber
, sOutString
, ppColor
, eLnge
,
291 bool ScInterpreterContext::NFGetPreviewString(const OUString
& sFormatString
,
292 const OUString
& sPreviewString
, OUString
& sOutString
,
293 const Color
** ppColor
, LanguageType eLnge
)
295 if (!mpDoc
->IsThreadedGroupCalcInProgress())
296 return GetFormatTable()->GetPreviewString(sFormatString
, sPreviewString
, sOutString
,
298 return SvNFEngine::GetPreviewString(*mxLanguageData
, *mpFormatData
, *mpNatNum
, maROPolicy
,
299 sFormatString
, sPreviewString
, sOutString
, ppColor
, eLnge
);
302 bool ScInterpreterContext::NFGetPreviewStringGuess(const OUString
& sFormatString
,
303 double fPreviewNumber
, OUString
& sOutString
,
304 const Color
** ppColor
, LanguageType eLnge
)
306 if (!mpDoc
->IsThreadedGroupCalcInProgress())
307 return GetFormatTable()->GetPreviewStringGuess(sFormatString
, fPreviewNumber
, sOutString
,
309 return SvNFEngine::GetPreviewStringGuess(*mxLanguageData
, *mpFormatData
, *mpNatNum
, maROPolicy
,
310 sFormatString
, fPreviewNumber
, sOutString
, ppColor
,
314 OUString
ScInterpreterContext::NFGenerateFormat(sal_uInt32 nIndex
, LanguageType eLnge
,
315 bool bThousand
, bool bIsRed
, sal_uInt16 nPrecision
,
316 sal_uInt16 nLeadingCnt
)
318 if (!mpDoc
->IsThreadedGroupCalcInProgress())
319 return GetFormatTable()->GenerateFormat(nIndex
, eLnge
, bThousand
, bIsRed
, nPrecision
,
321 return SvNFEngine::GenerateFormat(*mxLanguageData
, *mpFormatData
, *mpNatNum
, maROPolicy
, nIndex
,
322 eLnge
, bThousand
, bIsRed
, nPrecision
, nLeadingCnt
);
324 OUString
ScInterpreterContext::NFGetCalcCellReturn(sal_uInt32 nFormat
) const
326 if (!mpDoc
->IsThreadedGroupCalcInProgress())
327 return GetFormatTable()->GetCalcCellReturn(nFormat
);
328 return mpFormatData
->GetCalcCellReturn(nFormat
);
331 sal_uInt16
ScInterpreterContext::NFExpandTwoDigitYear(sal_uInt16 nYear
) const
333 if (!mpDoc
->IsThreadedGroupCalcInProgress())
334 return GetFormatTable()->ExpandTwoDigitYear(nYear
);
335 return mxLanguageData
->ExpandTwoDigitYear(nYear
);
338 bool ScInterpreterContext::NFIsNumberFormat(const OUString
& sString
, sal_uInt32
& F_Index
,
339 double& fOutNumber
, SvNumInputOptions eInputOptions
)
341 if (!mpDoc
->IsThreadedGroupCalcInProgress())
342 return GetFormatTable()->IsNumberFormat(sString
, F_Index
, fOutNumber
, eInputOptions
);
343 return SvNFEngine::IsNumberFormat(*mxLanguageData
, *mpFormatData
, *mpNatNum
, maROPolicy
,
344 sString
, F_Index
, fOutNumber
, eInputOptions
);
347 /* ScInterpreterContextPool */
350 void ScInterpreterContextPool::Init(size_t nNumThreads
, const ScDocument
& rDoc
,
351 SvNumberFormatter
* pFormatter
)
354 size_t nOldSize
= maPool
.size();
355 maPool
.resize(nNumThreads
);
356 for (size_t nIdx
= 0; nIdx
< nNumThreads
; ++nIdx
)
358 if (nIdx
>= nOldSize
)
359 maPool
[nIdx
].reset(new ScInterpreterContext(rDoc
, pFormatter
));
361 maPool
[nIdx
]->SetDocAndFormatter(rDoc
, pFormatter
);
365 ScInterpreterContext
*
366 ScInterpreterContextPool::GetInterpreterContextForThreadIdx(size_t nThreadIdx
) const
369 assert(nThreadIdx
< maPool
.size());
370 return maPool
[nThreadIdx
].get();
373 // Non-Threaded version
374 void ScInterpreterContextPool::Init(const ScDocument
& rDoc
, SvNumberFormatter
* pFormatter
)
377 assert(mnNextFree
<= maPool
.size());
378 bool bCreateNew
= (maPool
.size() == mnNextFree
);
379 size_t nCurrIdx
= mnNextFree
;
382 maPool
.resize(maPool
.size() + 1);
383 maPool
[nCurrIdx
].reset(new ScInterpreterContext(rDoc
, pFormatter
));
386 maPool
[nCurrIdx
]->SetDocAndFormatter(rDoc
, pFormatter
);
391 ScInterpreterContext
* ScInterpreterContextPool::GetInterpreterContext() const
394 assert(mnNextFree
&& (mnNextFree
<= maPool
.size()));
395 return maPool
[mnNextFree
- 1].get();
398 void ScInterpreterContextPool::ReturnToPool()
402 for (size_t nIdx
= 0; nIdx
< maPool
.size(); ++nIdx
)
403 maPool
[nIdx
]->Cleanup();
407 assert(mnNextFree
&& (mnNextFree
<= maPool
.size()));
409 maPool
[mnNextFree
]->Cleanup();
414 void ScInterpreterContextPool::ClearLookupCaches(const ScDocument
* pDoc
)
416 for (auto& rPtr
: aThreadedInterpreterPool
.maPool
)
417 rPtr
->ClearLookupCache(pDoc
);
418 for (auto& rPtr
: aNonThreadedInterpreterPool
.maPool
)
419 rPtr
->ClearLookupCache(pDoc
);
423 void ScInterpreterContextPool::ModuleExiting()
425 for (auto& rPtr
: aThreadedInterpreterPool
.maPool
)
426 rPtr
->mxLanguageData
.reset();
427 for (auto& rPtr
: aNonThreadedInterpreterPool
.maPool
)
428 rPtr
->mxLanguageData
.reset();
431 /* ScThreadedInterpreterContextGetterGuard */
433 ScThreadedInterpreterContextGetterGuard::ScThreadedInterpreterContextGetterGuard(
434 size_t nNumThreads
, const ScDocument
& rDoc
, SvNumberFormatter
* pFormatter
)
435 : rPool(ScInterpreterContextPool::aThreadedInterpreterPool
)
437 rPool
.Init(nNumThreads
, rDoc
, pFormatter
);
440 ScThreadedInterpreterContextGetterGuard::~ScThreadedInterpreterContextGetterGuard()
442 rPool
.ReturnToPool();
445 ScInterpreterContext
*
446 ScThreadedInterpreterContextGetterGuard::GetInterpreterContextForThreadIdx(size_t nThreadIdx
) const
448 return rPool
.GetInterpreterContextForThreadIdx(nThreadIdx
);
451 /* ScInterpreterContextGetterGuard */
453 ScInterpreterContextGetterGuard::ScInterpreterContextGetterGuard(const ScDocument
& rDoc
,
454 SvNumberFormatter
* pFormatter
)
455 : rPool(ScInterpreterContextPool::aNonThreadedInterpreterPool
)
457 , nContextIdx(rPool
.mnNextFree
)
460 rPool
.Init(rDoc
, pFormatter
);
463 ScInterpreterContextGetterGuard::~ScInterpreterContextGetterGuard()
465 assert(nContextIdx
== (rPool
.mnNextFree
- 1));
466 rPool
.ReturnToPool();
469 ScInterpreterContext
* ScInterpreterContextGetterGuard::GetInterpreterContext() const
471 return rPool
.GetInterpreterContext();
474 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */