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/lang.h>
17 #include <svl/numformat.hxx>
22 class FormulaTypedDoubleToken
;
25 #define TOKEN_CACHE_SIZE 8
28 class SvNumberFormatter
;
29 struct ScLookupCacheMap
;
31 enum class SvNumFormatType
: sal_Int16
;
33 // SetNumberFormat() is not thread-safe, so calls to it need to be delayed to the main thread.
34 struct DelayedSetNumberFormat
38 sal_uInt32 mnNumberFormat
;
41 class ScInterpreterContextPool
;
43 struct ScInterpreterContext
45 const ScDocument
* mpDoc
;
46 size_t mnTokenCachePos
;
47 std::vector
<formula::FormulaTypedDoubleToken
*> maTokens
;
48 std::vector
<DelayedSetNumberFormat
> maDelayedSetNumberFormat
;
49 std::unique_ptr
<ScLookupCacheMap
> mxScLookupCache
; // cache for lookups like VLOOKUP and MATCH
50 // Allocation cache for "aConditions" array in ScInterpreter::IterateParameterIfs()
51 // This is populated/used only when formula-group threading is enabled.
52 std::vector
<sal_uInt8
> maConditions
;
54 ScInterpreter
* pInterpreter
;
56 ScInterpreterContext(const ScDocument
& rDoc
, SvNumberFormatter
* pFormatter
);
58 ScInterpreterContext() = delete;
60 ~ScInterpreterContext();
62 SvNumberFormatter
* GetFormatTable() const
64 if (mpFormatter
== nullptr)
65 const_cast<ScInterpreterContext
*>(this)->initFormatTable();
69 SvNumFormatType
GetNumberFormatType(sal_uInt32 nFIndex
) const;
71 sal_uInt32
GetFormatForLanguageIfBuiltIn(sal_uInt32 nFormat
, LanguageType eLnge
) const;
74 friend class ScInterpreterContextPool
;
76 void SetDocAndFormatter(const ScDocument
& rDoc
, SvNumberFormatter
* pFormatter
);
78 void ClearLookupCache(const ScDocument
* pDoc
);
79 void initFormatTable();
80 SvNumberFormatter
* mpFormatter
;
82 // Some temp caches of the 4 most recent results from NumberFormatting
89 : nKey(SAL_MAX_UINT64
)
90 , nFormat(SAL_MAX_UINT32
)
94 // from format+lang to builtin format
95 mutable std::array
<NFBuiltIn
, 4> maNFBuiltInCache
;
99 SvNumFormatType eType
;
101 : nKey(SAL_MAX_UINT32
)
102 , eType(SvNumFormatType::ALL
)
106 // from format index to type
107 mutable std::array
<NFType
, 4> maNFTypeCache
;
110 class ScThreadedInterpreterContextGetterGuard
;
111 class ScInterpreterContextGetterGuard
;
113 class ScInterpreterContextPool
115 friend class ScThreadedInterpreterContextGetterGuard
;
116 friend class ScInterpreterContextGetterGuard
;
118 std::vector
<std::unique_ptr
<ScInterpreterContext
>> maPool
;
122 ScInterpreterContextPool(bool bThreaded
)
124 , mbThreaded(bThreaded
)
128 ~ScInterpreterContextPool() {}
130 static ScInterpreterContextPool aThreadedInterpreterPool
;
131 static ScInterpreterContextPool aNonThreadedInterpreterPool
;
133 // API for threaded case
135 // Ensures nNumThreads elements in pool.
136 void Init(size_t nNumThreads
, const ScDocument
& rDoc
, SvNumberFormatter
* pFormatter
);
138 // Returns ScInterpreterContext* for thread index nThreadIdx
139 ScInterpreterContext
* GetInterpreterContextForThreadIdx(size_t nThreadIdx
) const;
141 // API for non-threaded
143 // Ensures there is one unused element in the pool.
144 void Init(const ScDocument
& rDoc
, SvNumberFormatter
* pFormatter
);
146 // Returns ScInterpreterContext* for non-threaded use.
147 ScInterpreterContext
* GetInterpreterContext() const;
149 // Common API for threaded/non-threaded
151 // Cleans up the contexts prepared by call to immediately previous Init() and
152 // marks them all as unused.
156 // Only to be used to clear lookup cache in all pool elements
157 static void ClearLookupCaches(const ScDocument
* pDoc
);
160 class ScThreadedInterpreterContextGetterGuard
162 ScInterpreterContextPool
& rPool
;
165 ScThreadedInterpreterContextGetterGuard(size_t nNumThreads
, const ScDocument
& rDoc
,
166 SvNumberFormatter
* pFormatter
);
167 ~ScThreadedInterpreterContextGetterGuard();
169 // Returns ScInterpreterContext* for thread index nThreadIdx
170 ScInterpreterContext
* GetInterpreterContextForThreadIdx(size_t nThreadIdx
) const;
173 class ScInterpreterContextGetterGuard
175 ScInterpreterContextPool
& rPool
;
181 ScInterpreterContextGetterGuard(const ScDocument
& rDoc
, SvNumberFormatter
* pFormatter
);
182 ~ScInterpreterContextGetterGuard();
184 // Returns ScInterpreterContext* for non-threaded use.
185 ScInterpreterContext
* GetInterpreterContext() const;
188 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */