Avoid potential negative array index access to cached text.
[LibreOffice.git] / sc / inc / interpretercontext.hxx
blobc43e9c0b6892feb82fbd2ab5029820519ac53c75
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/lang.h>
17 #include <svl/numformat.hxx>
18 #include "types.hxx"
20 namespace formula
22 class FormulaTypedDoubleToken;
25 #define TOKEN_CACHE_SIZE 8
27 class ScDocument;
28 class SvNumberFormatter;
29 struct ScLookupCacheMap;
30 class ScInterpreter;
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
36 SCCOL mCol;
37 SCROW mRow;
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;
53 std::mt19937 aRNG;
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();
66 return mpFormatter;
69 SvNumFormatType GetNumberFormatType(sal_uInt32 nFIndex) const;
71 sal_uInt32 GetFormatForLanguageIfBuiltIn(sal_uInt32 nFormat, LanguageType eLnge) const;
73 private:
74 friend class ScInterpreterContextPool;
75 void ResetTokens();
76 void SetDocAndFormatter(const ScDocument& rDoc, SvNumberFormatter* pFormatter);
77 void Cleanup();
78 void ClearLookupCache(const ScDocument* pDoc);
79 void initFormatTable();
80 SvNumberFormatter* mpFormatter;
82 // Some temp caches of the 4 most recent results from NumberFormatting
83 // lookups.
84 struct NFBuiltIn
86 sal_uInt64 nKey;
87 sal_uInt32 nFormat;
88 NFBuiltIn()
89 : nKey(SAL_MAX_UINT64)
90 , nFormat(SAL_MAX_UINT32)
94 // from format+lang to builtin format
95 mutable std::array<NFBuiltIn, 4> maNFBuiltInCache;
96 struct NFType
98 sal_uInt32 nKey;
99 SvNumFormatType eType;
100 NFType()
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;
119 size_t mnNextFree;
120 bool mbThreaded;
122 ScInterpreterContextPool(bool bThreaded)
123 : mnNextFree(0)
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.
153 void ReturnToPool();
155 public:
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;
164 public:
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;
176 #if !defined NDEBUG
177 size_t nContextIdx;
178 #endif
180 public:
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: */