Version 7.5.1.1, tag libreoffice-7.5.1.1
[LibreOffice.git] / sc / inc / interpretercontext.hxx
blob39d528cb6ca9374b2399e2e8b9fa5b20ed940872
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 <vector>
13 #include <memory>
14 #include "types.hxx"
16 namespace formula
18 class FormulaToken;
21 #define TOKEN_CACHE_SIZE 8
23 class ScDocument;
24 class SvNumberFormatter;
25 struct ScLookupCacheMap;
26 class ScInterpreter;
27 enum class SvNumFormatType : sal_Int16;
29 // SetNumberFormat() is not thread-safe, so calls to it need to be delayed to the main thread.
30 struct DelayedSetNumberFormat
32 SCCOL mCol;
33 SCROW mRow;
34 sal_uInt32 mnNumberFormat;
37 struct NFIndexAndFmtType
39 sal_uInt32 nIndex;
40 SvNumFormatType eType : 16;
41 bool bIsValid : 1;
43 NFIndexAndFmtType()
44 : nIndex(0)
45 , eType(static_cast<SvNumFormatType>(0))
46 , bIsValid(false)
51 class ScInterpreterContextPool;
53 struct ScInterpreterContext
55 const ScDocument* mpDoc;
56 size_t mnTokenCachePos;
57 std::vector<formula::FormulaToken*> maTokens;
58 std::vector<DelayedSetNumberFormat> maDelayedSetNumberFormat;
59 std::unique_ptr<ScLookupCacheMap> mxScLookupCache; // cache for lookups like VLOOKUP and MATCH
60 // Allocation cache for "aConditions" array in ScInterpreter::IterateParameterIfs()
61 // This is populated/used only when formula-group threading is enabled.
62 std::vector<sal_uInt8> maConditions;
63 ScInterpreter* pInterpreter;
65 ScInterpreterContext(const ScDocument& rDoc, SvNumberFormatter* pFormatter);
67 ScInterpreterContext() = delete;
69 ~ScInterpreterContext();
71 SvNumberFormatter* GetFormatTable() const
73 if (mpFormatter == nullptr)
74 const_cast<ScInterpreterContext*>(this)->initFormatTable();
75 return mpFormatter;
78 SvNumFormatType GetNumberFormatType(sal_uInt32 nFIndex) const;
80 private:
81 friend class ScInterpreterContextPool;
82 void ResetTokens();
83 void SetDocAndFormatter(const ScDocument& rDoc, SvNumberFormatter* pFormatter);
84 void Cleanup();
85 void ClearLookupCache(const ScDocument* pDoc);
86 void initFormatTable();
87 SvNumberFormatter* mpFormatter;
88 mutable NFIndexAndFmtType maNFTypeCache;
91 class ScThreadedInterpreterContextGetterGuard;
92 class ScInterpreterContextGetterGuard;
94 class ScInterpreterContextPool
96 friend class ScThreadedInterpreterContextGetterGuard;
97 friend class ScInterpreterContextGetterGuard;
99 std::vector<std::unique_ptr<ScInterpreterContext>> maPool;
100 size_t mnNextFree;
101 bool mbThreaded;
103 ScInterpreterContextPool(bool bThreaded)
104 : mnNextFree(0)
105 , mbThreaded(bThreaded)
109 ~ScInterpreterContextPool() {}
111 static ScInterpreterContextPool aThreadedInterpreterPool;
112 static ScInterpreterContextPool aNonThreadedInterpreterPool;
114 // API for threaded case
116 // Ensures nNumThreads elements in pool.
117 void Init(size_t nNumThreads, const ScDocument& rDoc, SvNumberFormatter* pFormatter);
119 // Returns ScInterpreterContext* for thread index nThreadIdx
120 ScInterpreterContext* GetInterpreterContextForThreadIdx(size_t nThreadIdx) const;
122 // API for non-threaded
124 // Ensures there is one unused element in the pool.
125 void Init(const ScDocument& rDoc, SvNumberFormatter* pFormatter);
127 // Returns ScInterpreterContext* for non-threaded use.
128 ScInterpreterContext* GetInterpreterContext() const;
130 // Common API for threaded/non-threaded
132 // Cleans up the contexts prepared by call to immediately previous Init() and
133 // marks them all as unused.
134 void ReturnToPool();
136 public:
137 // Only to be used to clear lookup cache in all pool elements
138 static void ClearLookupCaches(const ScDocument* pDoc);
141 class ScThreadedInterpreterContextGetterGuard
143 ScInterpreterContextPool& rPool;
145 public:
146 ScThreadedInterpreterContextGetterGuard(size_t nNumThreads, const ScDocument& rDoc,
147 SvNumberFormatter* pFormatter);
148 ~ScThreadedInterpreterContextGetterGuard();
150 // Returns ScInterpreterContext* for thread index nThreadIdx
151 ScInterpreterContext* GetInterpreterContextForThreadIdx(size_t nThreadIdx) const;
154 class ScInterpreterContextGetterGuard
156 ScInterpreterContextPool& rPool;
157 #if !defined NDEBUG
158 size_t nContextIdx;
159 #endif
161 public:
162 ScInterpreterContextGetterGuard(const ScDocument& rDoc, SvNumberFormatter* pFormatter);
163 ~ScInterpreterContextGetterGuard();
165 // Returns ScInterpreterContext* for non-threaded use.
166 ScInterpreterContext* GetInterpreterContext() const;
169 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */