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 #pragma once
11 
12 #include <vector>
13 #include <memory>
14 #include "types.hxx"
15 
16 namespace formula
17 {
18 class FormulaToken;
19 }
20 
21 #define TOKEN_CACHE_SIZE 8
22 
23 class ScDocument;
24 class SvNumberFormatter;
25 struct ScLookupCacheMap;
26 class ScInterpreter;
27 enum class SvNumFormatType : sal_Int16;
28 
29 // SetNumberFormat() is not thread-safe, so calls to it need to be delayed to the main thread.
31 {
34  sal_uInt32 mnNumberFormat;
35 };
36 
38 {
39  sal_uInt32 nIndex;
41  bool bIsValid : 1;
42 
44  : nIndex(0)
45  , eType(static_cast<SvNumFormatType>(0))
46  , bIsValid(false)
47  {
48  }
49 };
50 
52 
54 {
55  const ScDocument* mpDoc;
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_uInt32> maConditions;
64 
65  ScInterpreterContext(const ScDocument& rDoc, SvNumberFormatter* pFormatter);
66 
67  ScInterpreterContext() = delete;
68 
70 
72  {
73  if (mpFormatter == nullptr)
74  const_cast<ScInterpreterContext*>(this)->initFormatTable();
75  return mpFormatter;
76  }
77 
78  SvNumFormatType GetNumberFormatType(sal_uInt32 nFIndex) const;
79 
80 private:
82  void ResetTokens();
83  void SetDocAndFormatter(const ScDocument& rDoc, SvNumberFormatter* pFormatter);
84  void Cleanup();
85  void ClearLookupCache();
86  void initFormatTable();
89 };
90 
93 
95 {
98 
99  std::vector<std::unique_ptr<ScInterpreterContext>> maPool;
100  size_t mnNextFree;
102 
103  ScInterpreterContextPool(bool bThreaded)
104  : mnNextFree(0)
105  , mbThreaded(bThreaded)
106  {
107  }
108 
110 
113 
114  // API for threaded case
115 
116  // Ensures nNumThreads elements in pool.
117  void Init(size_t nNumThreads, const ScDocument& rDoc, SvNumberFormatter* pFormatter);
118 
119  // Returns ScInterpreterContext* for thread index nThreadIdx
121 
122  // API for non-threaded
123 
124  // Ensures there is one unused element in the pool.
125  void Init(const ScDocument& rDoc, SvNumberFormatter* pFormatter);
126 
127  // Returns ScInterpreterContext* for non-threaded use.
129 
130  // Common API for threaded/non-threaded
131 
132  // Cleans up the contexts prepared by call to immediately previous Init() and
133  // marks them all as unused.
134  void ReturnToPool();
135 
136 public:
137  // Only to be used to clear lookup cache in all pool elements
138  static void ClearLookupCaches();
139 };
140 
142 {
144 
145 public:
146  ScThreadedInterpreterContextGetterGuard(size_t nNumThreads, const ScDocument& rDoc,
147  SvNumberFormatter* pFormatter);
149 
150  // Returns ScInterpreterContext* for thread index nThreadIdx
152 };
153 
155 {
157 #if !defined NDEBUG
158  size_t nContextIdx;
159 #endif
160 
161 public:
164 
165  // Returns ScInterpreterContext* for non-threaded use.
167 };
168 
169 /* 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:21
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:17
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