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/.
16 #include <i18nlangtag/mslangid.hxx>
17 #include <svl/numformat.hxx>
22 class FormulaTypedDoubleToken
;
25 #define TOKEN_CACHE_SIZE 8
29 struct ScLookupCacheMap
;
32 // SetNumberFormat() is not thread-safe, so calls to it need to be delayed to the main thread.
33 struct DelayedSetNumberFormat
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
;
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();
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;
116 friend class ScInterpreterContextPool
;
118 void SetDocAndFormatter(const ScDocument
& rDoc
, SvNumberFormatter
* pFormatter
);
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
145 : nKey(SAL_MAX_UINT64
)
146 , nFormat(SAL_MAX_UINT32
)
150 // from format+lang to builtin format
151 mutable std::array
<NFBuiltIn
, 4> maNFBuiltInCache
;
155 SvNumFormatType eType
;
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
;
181 ScInterpreterContextPool(bool bThreaded
)
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.
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
;
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
;
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: */