nss: upgrade to release 3.73
[LibreOffice.git] / sc / inc / interpretercontext.hxx
blob2e0ff91632d69a694f2e94cbce2303ca4439620e
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 #ifndef INCLUDED_SC_INC_INTERPRETERCONTEXT_HXX
11 #define INCLUDED_SC_INC_INTERPRETERCONTEXT_HXX
13 #include <vector>
14 #include <memory>
15 #include "types.hxx"
17 namespace formula
19 class FormulaToken;
22 #define TOKEN_CACHE_SIZE 8
24 class ScDocument;
25 class SvNumberFormatter;
26 struct ScLookupCacheMap;
27 class ScInterpreter;
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
33 SCCOL mCol;
34 SCROW mRow;
35 sal_uInt32 mnNumberFormat;
38 struct NFIndexAndFmtType
40 sal_uInt32 nIndex;
41 SvNumFormatType eType : 16;
42 bool bIsValid : 1;
44 NFIndexAndFmtType()
45 : nIndex(0)
46 , eType(static_cast<SvNumFormatType>(0))
47 , bIsValid(false)
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();
76 return mpFormatter;
79 SvNumFormatType GetNumberFormatType(sal_uInt32 nFIndex) const;
81 private:
82 friend class ScInterpreterContextPool;
83 void ResetTokens();
84 void SetDocAndFormatter(const ScDocument& rDoc, SvNumberFormatter* pFormatter);
85 void Cleanup();
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;
101 size_t mnNextFree;
102 bool mbThreaded;
104 ScInterpreterContextPool(bool bThreaded)
105 : mnNextFree(0)
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.
135 void ReturnToPool();
137 public:
138 // Only to be used to clear lookup cache in all pool elements
139 static void ClearLookupCaches();
142 class ScThreadedInterpreterContextGetterGuard
144 ScInterpreterContextPool& rPool;
146 public:
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;
158 #if !defined NDEBUG
159 size_t nContextIdx;
160 #endif
162 public:
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: */