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. 31 struct DelayedSetNumberFormat 32 { 33 SCROW mCol; 34 SCROW mRow; 35 sal_uInt32 mnNumberFormat; 36 }; 37 38 struct NFIndexAndFmtType 39 { 40 sal_uInt32 nIndex; 41 SvNumFormatType eType : 16; 42 bool bIsValid : 1; 43 NFIndexAndFmtTypeNFIndexAndFmtType44 NFIndexAndFmtType() 45 : nIndex(0) 46 , eType(static_cast<SvNumFormatType>(0)) 47 , bIsValid(false) 48 { 49 } 50 }; 51 52 class ScInterpreterContextPool; 53 54 struct ScInterpreterContext 55 { 56 const ScDocument* mpDoc; 57 size_t mnTokenCachePos; 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; 64 ScInterpreter* pInterpreter; 65 ScInterpreterContextScInterpreterContext66 ScInterpreterContext(const ScDocument& rDoc, SvNumberFormatter* pFormatter) 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 78 ~ScInterpreterContext(); 79 GetFormatTableScInterpreterContext80 SvNumberFormatter* GetFormatTable() const 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: 90 friend class ScInterpreterContextPool; 91 void ResetTokens(); 92 void SetDocAndFormatter(const ScDocument& rDoc, SvNumberFormatter* pFormatter); 93 void Cleanup(); 94 void ClearLookupCache(); 95 void initFormatTable(); 96 SvNumberFormatter* mpFormatter; 97 mutable NFIndexAndFmtType maNFTypeCache; 98 }; 99 100 class ScThreadedInterpreterContextGetterGuard; 101 class ScInterpreterContextGetterGuard; 102 103 class ScInterpreterContextPool 104 { 105 friend class ScThreadedInterpreterContextGetterGuard; 106 friend class ScInterpreterContextGetterGuard; 107 108 std::vector<std::unique_ptr<ScInterpreterContext>> maPool; 109 size_t mnNextFree; 110 bool mbThreaded; 111 ScInterpreterContextPool(bool bThreaded)112 ScInterpreterContextPool(bool bThreaded) 113 : mnNextFree(0) 114 , mbThreaded(bThreaded) 115 { 116 } 117 ~ScInterpreterContextPool()118 ~ScInterpreterContextPool() {} 119 120 static ScInterpreterContextPool aThreadedInterpreterPool; 121 static ScInterpreterContextPool aNonThreadedInterpreterPool; 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 129 ScInterpreterContext* GetInterpreterContextForThreadIdx(size_t nThreadIdx) const; 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. 137 ScInterpreterContext* GetInterpreterContext() const; 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 150 class ScThreadedInterpreterContextGetterGuard 151 { 152 ScInterpreterContextPool& rPool; 153 154 public: 155 ScThreadedInterpreterContextGetterGuard(size_t nNumThreads, const ScDocument& rDoc, 156 SvNumberFormatter* pFormatter); 157 ~ScThreadedInterpreterContextGetterGuard(); 158 159 // Returns ScInterpreterContext* for thread index nThreadIdx 160 ScInterpreterContext* GetInterpreterContextForThreadIdx(size_t nThreadIdx) const; 161 }; 162 163 class ScInterpreterContextGetterGuard 164 { 165 ScInterpreterContextPool& rPool; 166 #if !defined NDEBUG 167 size_t nContextIdx; 168 #endif 169 170 public: 171 ScInterpreterContextGetterGuard(const ScDocument& rDoc, SvNumberFormatter* pFormatter); 172 ~ScInterpreterContextGetterGuard(); 173 174 // Returns ScInterpreterContext* for non-threaded use. 175 ScInterpreterContext* GetInterpreterContext() const; 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: */ 181