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/.
10 #ifndef INCLUDED_SC_INC_INTERPRETERCONTEXT_HXX
11 #define INCLUDED_SC_INC_INTERPRETERCONTEXT_HXX
22 #define TOKEN_CACHE_SIZE 8
25 class SvNumberFormatter
;
26 struct ScLookupCacheMap
;
28 enum class SvNumFormatType
: sal_Int16
;
30 // SetNumberFormat() is not thread-safe, so calls to it need to be delayed to the main thread.
31 struct DelayedSetNumberFormat
35 sal_uInt32 mnNumberFormat
;
38 struct NFIndexAndFmtType
41 SvNumFormatType eType
: 16;
46 , eType(static_cast<SvNumFormatType
>(0))
52 class ScInterpreterContextPool
;
54 struct ScInterpreterContext
56 const ScDocument
* mpDoc
;
57 size_t mnTokenCachePos
;
58 std::vector
<formula::FormulaToken
*> maTokens
;
59 std::vector
<DelayedSetNumberFormat
> maDelayedSetNumberFormat
;
60 std::unique_ptr
<ScLookupCacheMap
> mxScLookupCache
; // cache for lookups like VLOOKUP and MATCH
61 // Allocation cache for "aConditions" array in ScInterpreter::IterateParameterIfs()
62 // This is populated/used only when formula-group threading is enabled.
63 std::vector
<sal_uInt32
> maConditions
;
64 ScInterpreter
* pInterpreter
;
66 ScInterpreterContext(const ScDocument
& rDoc
, SvNumberFormatter
* pFormatter
);
68 ScInterpreterContext() = delete;
70 ~ScInterpreterContext();
72 SvNumberFormatter
* GetFormatTable() const
74 if (mpFormatter
== nullptr)
75 const_cast<ScInterpreterContext
*>(this)->initFormatTable();
79 SvNumFormatType
GetNumberFormatType(sal_uInt32 nFIndex
) const;
82 friend class ScInterpreterContextPool
;
84 void SetDocAndFormatter(const ScDocument
& rDoc
, SvNumberFormatter
* pFormatter
);
86 void ClearLookupCache();
87 void initFormatTable();
88 SvNumberFormatter
* mpFormatter
;
89 mutable NFIndexAndFmtType maNFTypeCache
;
92 class ScThreadedInterpreterContextGetterGuard
;
93 class ScInterpreterContextGetterGuard
;
95 class ScInterpreterContextPool
97 friend class ScThreadedInterpreterContextGetterGuard
;
98 friend class ScInterpreterContextGetterGuard
;
100 std::vector
<std::unique_ptr
<ScInterpreterContext
>> maPool
;
104 ScInterpreterContextPool(bool bThreaded
)
106 , mbThreaded(bThreaded
)
110 ~ScInterpreterContextPool() {}
112 static ScInterpreterContextPool aThreadedInterpreterPool
;
113 static ScInterpreterContextPool aNonThreadedInterpreterPool
;
115 // API for threaded case
117 // Ensures nNumThreads elements in pool.
118 void Init(size_t nNumThreads
, const ScDocument
& rDoc
, SvNumberFormatter
* pFormatter
);
120 // Returns ScInterpreterContext* for thread index nThreadIdx
121 ScInterpreterContext
* GetInterpreterContextForThreadIdx(size_t nThreadIdx
) const;
123 // API for non-threaded
125 // Ensures there is one unused element in the pool.
126 void Init(const ScDocument
& rDoc
, SvNumberFormatter
* pFormatter
);
128 // Returns ScInterpreterContext* for non-threaded use.
129 ScInterpreterContext
* GetInterpreterContext() const;
131 // Common API for threaded/non-threaded
133 // Cleans up the contexts prepared by call to immediately previous Init() and
134 // marks them all as unused.
138 // Only to be used to clear lookup cache in all pool elements
139 static void ClearLookupCaches();
142 class ScThreadedInterpreterContextGetterGuard
144 ScInterpreterContextPool
& rPool
;
147 ScThreadedInterpreterContextGetterGuard(size_t nNumThreads
, const ScDocument
& rDoc
,
148 SvNumberFormatter
* pFormatter
);
149 ~ScThreadedInterpreterContextGetterGuard();
151 // Returns ScInterpreterContext* for thread index nThreadIdx
152 ScInterpreterContext
* GetInterpreterContextForThreadIdx(size_t nThreadIdx
) const;
155 class ScInterpreterContextGetterGuard
157 ScInterpreterContextPool
& rPool
;
163 ScInterpreterContextGetterGuard(const ScDocument
& rDoc
, SvNumberFormatter
* pFormatter
);
164 ~ScInterpreterContextGetterGuard();
166 // Returns ScInterpreterContext* for non-threaded use.
167 ScInterpreterContext
* GetInterpreterContext() const;
170 #endif // INCLUDED_SC_INC_INTERPRETERCONTEXT_HXX
172 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */