Fix build
[LibreOffice.git] / sc / inc / interpretercontext.hxx
blob4f66f29e31b62366d80f880f00749a4ba7413002
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
10 #pragma once
12 #include <array>
13 #include <memory>
14 #include <random>
15 #include <vector>
16 #include <i18nlangtag/mslangid.hxx>
17 #include <svl/numformat.hxx>
18 #include "types.hxx"
20 namespace formula
22 class FormulaTypedDoubleToken;
25 #define TOKEN_CACHE_SIZE 8
27 class Color;
28 class ScDocument;
29 struct ScLookupCacheMap;
30 class ScInterpreter;
32 // SetNumberFormat() is not thread-safe, so calls to it need to be delayed to the main thread.
33 struct DelayedSetNumberFormat
35 SCCOL mCol;
36 SCROW mRow;
37 sal_uInt32 mnNumberFormat;
40 class ScInterpreterContextPool;
42 struct ScInterpreterContext
44 const ScDocument* mpDoc;
45 size_t mnTokenCachePos;
46 std::vector<formula::FormulaTypedDoubleToken*> maTokens;
47 std::vector<DelayedSetNumberFormat> maDelayedSetNumberFormat;
48 std::unique_ptr<ScLookupCacheMap> mxScLookupCache; // cache for lookups like VLOOKUP and MATCH
49 // Allocation cache for "aConditions" array in ScInterpreter::IterateParameterIfs()
50 // This is populated/used only when formula-group threading is enabled.
51 std::vector<sal_uInt8> maConditions;
52 std::mt19937 aRNG;
53 ScInterpreter* pInterpreter;
55 ScInterpreterContext(const ScDocument& rDoc, SvNumberFormatter* pFormatter);
57 ScInterpreterContext() = delete;
59 ~ScInterpreterContext();
61 SvNumberFormatter* GetFormatTable() const
63 if (mpFormatter == nullptr)
64 const_cast<ScInterpreterContext*>(this)->initFormatTable();
65 return mpFormatter;
68 SvNumFormatType NFGetType(sal_uInt32 nFIndex) const;
69 const SvNumberformat* NFGetFormatEntry(sal_uInt32 nKey) const;
70 sal_uInt32 NFGetFormatIndex(NfIndexTableOffset, LanguageType eLnge = LANGUAGE_DONTKNOW) const;
71 bool NFIsTextFormat(sal_uInt32 nFIndex) const;
72 sal_uInt32 NFGetTimeFormat(double fNumber, LanguageType eLnge, bool bForceDuration) const;
73 const Date& NFGetNullDate() const;
74 OUString NFGetFormatDecimalSep(sal_uInt32 nFormat) const;
75 sal_uInt16 NFGetFormatPrecision(sal_uInt32 nFormat) const;
77 sal_uInt32 NFGetFormatForLanguageIfBuiltIn(sal_uInt32 nFormat, LanguageType eLnge) const;
79 bool NFIsNumberFormat(const OUString& sString, sal_uInt32& F_Index, double& fOutNumber,
80 SvNumInputOptions eInputOptions = SvNumInputOptions::NONE);
82 OUString NFGetInputLineString(const double& fOutNumber, sal_uInt32 nFIndex,
83 bool bFiltering = false, bool bForceSystemLocale = false) const;
85 void NFGetOutputString(const double& fOutNumber, sal_uInt32 nFIndex, OUString& sOutString,
86 const Color** ppColor, bool bUseStarFormat = false) const;
88 void NFGetOutputString(const OUString& sString, sal_uInt32 nFIndex, OUString& sOutString,
89 const Color** ppColor, bool bUseStarFormat = false) const;
91 sal_uInt32 NFGetStandardFormat(SvNumFormatType eType, LanguageType eLnge = LANGUAGE_DONTKNOW);
92 sal_uInt32 NFGetStandardFormat(sal_uInt32 nFIndex, SvNumFormatType eType, LanguageType eLnge);
94 bool NFGetPreviewString(const OUString& sFormatString, double fPreviewNumber,
95 OUString& sOutString, const Color** ppColor, LanguageType eLnge);
96 bool NFGetPreviewString(const OUString& sFormatString, const OUString& sPreviewString,
97 OUString& sOutString, const Color** ppColor,
98 LanguageType eLnge = LANGUAGE_DONTKNOW);
99 bool NFGetPreviewStringGuess(const OUString& sFormatString, double fPreviewNumber,
100 OUString& sOutString, const Color** ppColor,
101 LanguageType eLnge = LANGUAGE_DONTKNOW);
103 sal_uInt32 NFGetStandardIndex(LanguageType eLnge = LANGUAGE_DONTKNOW) const;
105 OUString NFGenerateFormat(sal_uInt32 nIndex, LanguageType eLnge = LANGUAGE_DONTKNOW,
106 bool bThousand = false, bool IsRed = false, sal_uInt16 nPrecision = 0,
107 sal_uInt16 nLeadingCnt = 1);
109 sal_uInt16 NFExpandTwoDigitYear(sal_uInt16 nYear) const;
111 OUString NFGetCalcCellReturn(sal_uInt32 nFormat) const;
113 void MergeDefaultFormatKeys(SvNumberFormatter& rFormatter) const;
115 private:
116 friend class ScInterpreterContextPool;
117 void ResetTokens();
118 void SetDocAndFormatter(const ScDocument& rDoc, SvNumberFormatter* pFormatter);
119 void Cleanup();
120 void ClearLookupCache(const ScDocument* pDoc);
121 void initFormatTable();
122 void prepFormatterForRoMode(SvNumberFormatter* pFormatter);
124 // During threaded calculation, where we don't need to add to the number
125 // format data, we can access the numbering data with a RO unlocked view of
126 // the NumberFormat's data and a throw-away object for currently used language
127 // This is essentially an exploded view of mpFormatter
128 std::unique_ptr<SvNFLanguageData> mxLanguageData;
129 // FormatData can be driven read-only, but may want to cache some data,
130 // in RO Mode we can cache per thread to mxAuxFormatKeyMap, and
131 // discard or merge after threaded calculation is over
132 std::unique_ptr<SvNFFormatData::DefaultFormatKeysMap> mxAuxFormatKeyMap;
134 const SvNFFormatData* mpFormatData;
135 const NativeNumberWrapper* mpNatNum;
136 SvNFEngine::Accessor maROPolicy;
138 // Some temp caches of the 4 most recent results from NumberFormatting
139 // lookups.
140 struct NFBuiltIn
142 sal_uInt64 nKey;
143 sal_uInt32 nFormat;
144 NFBuiltIn()
145 : nKey(SAL_MAX_UINT64)
146 , nFormat(SAL_MAX_UINT32)
150 // from format+lang to builtin format
151 mutable std::array<NFBuiltIn, 4> maNFBuiltInCache;
152 struct NFType
154 sal_uInt32 nKey;
155 SvNumFormatType eType;
156 NFType()
157 : nKey(SAL_MAX_UINT32)
158 , eType(SvNumFormatType::ALL)
162 // from format index to type
163 mutable std::array<NFType, 4> maNFTypeCache;
165 // Formatter used when non-nthreaded calculation
166 SvNumberFormatter* mpFormatter;
169 class ScThreadedInterpreterContextGetterGuard;
170 class ScInterpreterContextGetterGuard;
172 class ScInterpreterContextPool
174 friend class ScThreadedInterpreterContextGetterGuard;
175 friend class ScInterpreterContextGetterGuard;
177 std::vector<std::unique_ptr<ScInterpreterContext>> maPool;
178 size_t mnNextFree;
179 bool mbThreaded;
181 ScInterpreterContextPool(bool bThreaded)
182 : mnNextFree(0)
183 , mbThreaded(bThreaded)
187 ~ScInterpreterContextPool() {}
189 static ScInterpreterContextPool aThreadedInterpreterPool;
190 static ScInterpreterContextPool aNonThreadedInterpreterPool;
192 // API for threaded case
194 // Ensures nNumThreads elements in pool.
195 void Init(size_t nNumThreads, const ScDocument& rDoc, SvNumberFormatter* pFormatter);
197 // Returns ScInterpreterContext* for thread index nThreadIdx
198 ScInterpreterContext* GetInterpreterContextForThreadIdx(size_t nThreadIdx) const;
200 // API for non-threaded
202 // Ensures there is one unused element in the pool.
203 void Init(const ScDocument& rDoc, SvNumberFormatter* pFormatter);
205 // Returns ScInterpreterContext* for non-threaded use.
206 ScInterpreterContext* GetInterpreterContext() const;
208 // Common API for threaded/non-threaded
210 // Cleans up the contexts prepared by call to immediately previous Init() and
211 // marks them all as unused.
212 void ReturnToPool();
214 public:
215 // Only to be used to clear lookup cache in all pool elements
216 static void ClearLookupCaches(const ScDocument* pDoc);
217 // Called from ScModule dtor, drop all resources
218 static void ModuleExiting();
221 class ScThreadedInterpreterContextGetterGuard
223 ScInterpreterContextPool& rPool;
225 public:
226 ScThreadedInterpreterContextGetterGuard(size_t nNumThreads, const ScDocument& rDoc,
227 SvNumberFormatter* pFormatter);
228 ~ScThreadedInterpreterContextGetterGuard();
230 // Returns ScInterpreterContext* for thread index nThreadIdx
231 ScInterpreterContext* GetInterpreterContextForThreadIdx(size_t nThreadIdx) const;
234 class ScInterpreterContextGetterGuard
236 ScInterpreterContextPool& rPool;
237 #if !defined NDEBUG
238 size_t nContextIdx;
239 #endif
241 public:
242 ScInterpreterContextGetterGuard(const ScDocument& rDoc, SvNumberFormatter* pFormatter);
243 ~ScInterpreterContextGetterGuard();
245 // Returns ScInterpreterContext* for non-threaded use.
246 ScInterpreterContext* GetInterpreterContext() const;
249 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */