LibreOffice Module sc (master)  1
interpretercontext.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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  */
9 
10 #ifndef INCLUDED_SC_INC_INTERPRETERCONTEXT_HXX
11 #define INCLUDED_SC_INC_INTERPRETERCONTEXT_HXX
12 
13 #include <vector>
14 #include <memory>
15 #include "types.hxx"
16 
17 namespace formula
18 {
19 class FormulaToken;
20 }
21 
22 #define TOKEN_CACHE_SIZE 8
23 
24 class ScDocument;
25 class SvNumberFormatter;
26 struct ScLookupCacheMap;
27 class ScInterpreter;
28 enum class SvNumFormatType : sal_Int16;
29 
30 // SetNumberFormat() is not thread-safe, so calls to it need to be delayed to the main thread.
32 {
35  sal_uInt32 mnNumberFormat;
36 };
37 
39 {
40  sal_uInt32 nIndex;
42  bool bIsValid : 1;
43 
45  : nIndex(0)
46  , eType(static_cast<SvNumFormatType>(0))
47  , bIsValid(false)
48  {
49  }
50 };
51 
53 
55 {
56  const ScDocument* mpDoc;
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;
65 
66  ScInterpreterContext(const ScDocument& rDoc, SvNumberFormatter* pFormatter);
67 
68  ScInterpreterContext() = delete;
69 
71 
73  {
74  if (mpFormatter == nullptr)
75  const_cast<ScInterpreterContext*>(this)->initFormatTable();
76  return mpFormatter;
77  }
78 
79  SvNumFormatType GetNumberFormatType(sal_uInt32 nFIndex) const;
80 
81 private:
83  void ResetTokens();
84  void SetDocAndFormatter(const ScDocument& rDoc, SvNumberFormatter* pFormatter);
85  void Cleanup();
86  void ClearLookupCache();
87  void initFormatTable();
90 };
91 
94 
96 {
99 
100  std::vector<std::unique_ptr<ScInterpreterContext>> maPool;
101  size_t mnNextFree;
103 
104  ScInterpreterContextPool(bool bThreaded)
105  : mnNextFree(0)
106  , mbThreaded(bThreaded)
107  {
108  }
109 
111 
114 
115  // API for threaded case
116 
117  // Ensures nNumThreads elements in pool.
118  void Init(size_t nNumThreads, const ScDocument& rDoc, SvNumberFormatter* pFormatter);
119 
120  // Returns ScInterpreterContext* for thread index nThreadIdx
122 
123  // API for non-threaded
124 
125  // Ensures there is one unused element in the pool.
126  void Init(const ScDocument& rDoc, SvNumberFormatter* pFormatter);
127 
128  // Returns ScInterpreterContext* for non-threaded use.
130 
131  // Common API for threaded/non-threaded
132 
133  // Cleans up the contexts prepared by call to immediately previous Init() and
134  // marks them all as unused.
135  void ReturnToPool();
136 
137 public:
138  // Only to be used to clear lookup cache in all pool elements
139  static void ClearLookupCaches();
140 };
141 
143 {
145 
146 public:
147  ScThreadedInterpreterContextGetterGuard(size_t nNumThreads, const ScDocument& rDoc,
148  SvNumberFormatter* pFormatter);
150 
151  // Returns ScInterpreterContext* for thread index nThreadIdx
153 };
154 
156 {
158 #if !defined NDEBUG
159  size_t nContextIdx;
160 #endif
161 
162 public:
165 
166  // Returns ScInterpreterContext* for non-threaded use.
168 };
169 
170 #endif // INCLUDED_SC_INC_INTERPRETERCONTEXT_HXX
171 
172 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
std::vector< DelayedSetNumberFormat > maDelayedSetNumberFormat
const ScDocument * mpDoc
static ScInterpreterContextPool aThreadedInterpreterPool
NFIndexAndFmtType maNFTypeCache
ScInterpreterContext * GetInterpreterContext() const
std::vector< std::unique_ptr< ScInterpreterContext > > maPool
ScThreadedInterpreterContextGetterGuard(size_t nNumThreads, const ScDocument &rDoc, SvNumberFormatter *pFormatter)
SvNumberFormatter * mpFormatter
void SetDocAndFormatter(const ScDocument &rDoc, SvNumberFormatter *pFormatter)
std::vector< sal_uInt32 > maConditions
static ScInterpreterContextPool aNonThreadedInterpreterPool
sal_Int16 SCCOL
Definition: types.hxx:22
SvNumberFormatter * GetFormatTable() const
std::unique_ptr< ScLookupCacheMap > mxScLookupCache
SvNumFormatType
ScInterpreter * pInterpreter
ScInterpreterContext()=delete
ScInterpreterContext * GetInterpreterContextForThreadIdx(size_t nThreadIdx) const
sal_Int32 SCROW
Definition: types.hxx:18
ScInterpreterContextGetterGuard(const ScDocument &rDoc, SvNumberFormatter *pFormatter)
std::vector< formula::FormulaToken * > maTokens
ScInterpreterContextPool & rPool
SvNumFormatType GetNumberFormatType(sal_uInt32 nFIndex) const
ScInterpreterContextPool(bool bThreaded)
ScInterpreterContext * GetInterpreterContext() const
void Init(size_t nNumThreads, const ScDocument &rDoc, SvNumberFormatter *pFormatter)
SvNumFormatType eType
ScInterpreterContext * GetInterpreterContextForThreadIdx(size_t nThreadIdx) const