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  ScLookupCacheMap* mScLookupCache; // 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 
67  : mpDoc(&rDoc)
68  , mnTokenCachePos(0)
69  , maTokens(TOKEN_CACHE_SIZE, nullptr)
70  , mScLookupCache(nullptr)
71  , pInterpreter(nullptr)
72  , mpFormatter(pFormatter)
73  {
74  }
75 
76  ScInterpreterContext() = delete;
77 
79 
81  {
82  if (mpFormatter == nullptr)
83  const_cast<ScInterpreterContext*>(this)->initFormatTable();
84  return mpFormatter;
85  }
86 
87  SvNumFormatType GetNumberFormatType(sal_uInt32 nFIndex) const;
88 
89 private:
91  void ResetTokens();
92  void SetDocAndFormatter(const ScDocument& rDoc, SvNumberFormatter* pFormatter);
93  void Cleanup();
94  void ClearLookupCache();
95  void initFormatTable();
98 };
99 
102 
104 {
107 
108  std::vector<std::unique_ptr<ScInterpreterContext>> maPool;
109  size_t mnNextFree;
111 
112  ScInterpreterContextPool(bool bThreaded)
113  : mnNextFree(0)
114  , mbThreaded(bThreaded)
115  {
116  }
117 
119 
122 
123  // API for threaded case
124 
125  // Ensures nNumThreads elements in pool.
126  void Init(size_t nNumThreads, const ScDocument& rDoc, SvNumberFormatter* pFormatter);
127 
128  // Returns ScInterpreterContext* for thread index nThreadIdx
130 
131  // API for non-threaded
132 
133  // Ensures there is one unused element in the pool.
134  void Init(const ScDocument& rDoc, SvNumberFormatter* pFormatter);
135 
136  // Returns ScInterpreterContext* for non-threaded use.
138 
139  // Common API for threaded/non-threaded
140 
141  // Cleans up the contexts prepared by call to immediately previous Init() and
142  // marks them all as unused.
143  void ReturnToPool();
144 
145 public:
146  // Only to be used to clear lookup cache in all pool elements
147  static void ClearLookupCaches();
148 };
149 
151 {
153 
154 public:
155  ScThreadedInterpreterContextGetterGuard(size_t nNumThreads, const ScDocument& rDoc,
156  SvNumberFormatter* pFormatter);
158 
159  // Returns ScInterpreterContext* for thread index nThreadIdx
161 };
162 
164 {
166 #if !defined NDEBUG
167  size_t nContextIdx;
168 #endif
169 
170 public:
173 
174  // Returns ScInterpreterContext* for non-threaded use.
176 };
177 
178 #endif // INCLUDED_SC_INC_INTERPRETERCONTEXT_HXX
179 
180 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
#define TOKEN_CACHE_SIZE
std::vector< DelayedSetNumberFormat > maDelayedSetNumberFormat
const ScDocument * mpDoc
static ScInterpreterContextPool aThreadedInterpreterPool
ScInterpreterContext(const ScDocument &rDoc, SvNumberFormatter *pFormatter)
NFIndexAndFmtType maNFTypeCache
ScInterpreterContext * GetInterpreterContext() const
std::vector< std::unique_ptr< ScInterpreterContext > > maPool
ScLookupCacheMap * mScLookupCache
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
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