| 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 | * This file incorporates work covered by the following license notice: |
| 10 | * |
| 11 | * Licensed to the Apache Software Foundation (ASF) under one or more |
| 12 | * contributor license agreements. See the NOTICE file distributed |
| 13 | * with this work for additional information regarding copyright |
| 14 | * ownership. The ASF licenses this file to you under the Apache |
| 15 | * License, Version 2.0 (the "License"); you may not use this file |
| 16 | * except in compliance with the License. You may obtain a copy of |
| 17 | * the License at http://www.apache.org/licenses/LICENSE-2.0 . |
| 18 | */ |
| 19 | |
| 20 | #pragma once |
| 21 | |
| 22 | #include "global.hxx" |
| 23 | #include "calcmacros.hxx" |
| 24 | #include <svl/sharedstring.hxx> |
| 25 | #include <formula/token.hxx> |
| 26 | #include <formula/types.hxx> |
| 27 | |
| 28 | class ScMatrixFormulaCellToken; |
| 29 | |
| 30 | namespace sc { |
| 31 | |
| 32 | struct FormulaResultValue |
| 33 | { |
| 34 | enum Type : sal_uInt8 { Invalid, Value, String, Error }; |
| 35 | |
| 36 | double mfValue; |
| 37 | svl::SharedString maString; |
| 38 | bool mbMultiLine = false; |
| 39 | Type meType; |
| 40 | FormulaError mnError; |
| 41 | |
| 42 | FormulaResultValue(); |
| 43 | FormulaResultValue( double fValue ); |
| 44 | FormulaResultValue( svl::SharedString aStr, bool bMultiLine ); |
| 45 | FormulaResultValue( FormulaError nErr ); |
| 46 | }; |
| 47 | |
| 48 | } |
| 49 | |
| 50 | /** Store a variable formula cell result, balancing between runtime performance |
| 51 | and memory consumption. */ |
| 52 | class ScFormulaResult |
| 53 | { |
| 54 | typedef unsigned char Multiline; |
| 55 | static const Multiline MULTILINE_UNKNOWN = 0; |
| 56 | static const Multiline MULTILINE_FALSE = 1; |
| 57 | static const Multiline MULTILINE_TRUE = 2; |
| 58 | |
| 59 | union |
| 60 | { |
| 61 | double mfValue; // double result direct for performance and memory consumption |
| 62 | const formula::FormulaToken* mpToken; // if not, result token obtained from interpreter |
| 63 | }; |
| 64 | bool mbToken :1; // whether content of union is a token |
| 65 | bool mbNoneRefCnt :1; // if token was added when using RefCntPolicy::None |
| 66 | bool mbEmpty :1; // empty cell result |
| 67 | bool mbEmptyDisplayedAsString :1; // only if mbEmpty |
| 68 | // If set it implies that the result is a simple double (in mfValue) and no error |
| 69 | bool mbValueCached :1; |
| 70 | Multiline meMultiline :2; // result is multiline |
| 71 | FormulaError mnError; // error code |
| 72 | |
| 73 | /** Reset mnError, mbEmpty and mbEmptyDisplayedAsString to their defaults |
| 74 | prior to assigning other types */ |
| 75 | void ResetToDefaults(); |
| 76 | |
| 77 | /** If token is of formula::svError set error code and decrement RefCount. |
| 78 | If token is of formula::svEmptyCell set mbEmpty and mbEmptyAsString and |
| 79 | decrement RefCount. |
| 80 | If token is of formula::svDouble set mfValue and decrement RefCount. |
| 81 | Else assign token to mpToken. NULL is valid => svUnknown. |
| 82 | Other member variables are set accordingly. |
| 83 | @precondition: Token MUST had been IncRef'ed prior to this call! |
| 84 | @precondition: An already existing different mpToken MUST had been |
| 85 | DecRef'ed prior to this call, p will be assigned to mpToken if not |
| 86 | resolved. |
| 87 | ATTENTION! Token may get deleted in this call! */ |
| 88 | void ResolveToken( const formula::FormulaToken * p ); |
| 89 | |
| 90 | public: |
| 91 | /** Effectively type svUnknown. */ |
| 92 | ScFormulaResult(); |
| 93 | |
| 94 | ScFormulaResult( const ScFormulaResult & r ); |
| 95 | |
| 96 | /** Same comments as for SetToken() apply! */ |
| 97 | explicit ScFormulaResult( const formula::FormulaToken* p ); |
| 98 | |
| 99 | ~ScFormulaResult(); |
| 100 | |
| 101 | /** Well, guess what ... */ |
| 102 | ScFormulaResult& operator=( const ScFormulaResult & r ); |
| 103 | |
| 104 | /** Assignment as in operator=() but without return */ |
| 105 | void Assign( const ScFormulaResult & r ); |
| 106 | |
| 107 | /** Sets a direct double if token type is formula::svDouble, or mbEmpty if |
| 108 | formula::svEmptyCell, else token. If p is NULL, that is set as well, effectively |
| 109 | resulting in GetType()==svUnknown. If the already existing result is |
| 110 | ScMatrixFormulaCellToken, the upper left is set to token. |
| 111 | |
| 112 | ATTENTION! formula::FormulaToken had to be allocated using 'new' and if of type |
| 113 | formula::svDouble and no RefCount was set may not be used after this call |
| 114 | because it was deleted after decrement! */ |
| 115 | void SetToken( const formula::FormulaToken* p ); |
| 116 | |
| 117 | /** May be NULL if SetToken() did so, also if type formula::svDouble or formula::svError! */ |
| 118 | formula::FormulaConstTokenRef GetToken() const; |
| 119 | |
| 120 | /** Return upper left token if formula::svMatrixCell, else return GetToken(). |
| 121 | May be NULL if SetToken() did so, also if type formula::svDouble or formula::svError! */ |
| 122 | formula::FormulaConstTokenRef GetCellResultToken() const; |
| 123 | |
| 124 | /** Return type of result, including formula::svError, formula::svEmptyCell, formula::svDouble and |
| 125 | formula::svMatrixCell. */ |
| 126 | formula::StackVar GetType() const; |
| 127 | |
| 128 | /** If type is formula::svMatrixCell return the type of upper left element, else |
| 129 | GetType() */ |
| 130 | formula::StackVar GetCellResultType() const; |
| 131 | |
| 132 | /** If type is formula::svEmptyCell (including matrix upper left) and should be |
| 133 | displayed as empty string */ |
| 134 | bool IsEmptyDisplayedAsString() const; |
| 135 | |
| 136 | /** Test for cell result type formula::svDouble, including upper left if |
| 137 | formula::svMatrixCell. Also included is formula::svError for legacy, because previously |
| 138 | an error result was treated like a numeric value at some places in |
| 139 | ScFormulaCell. Also included is formula::svEmptyCell as a reference to an empty |
| 140 | cell usually is treated as numeric 0. Use GetCellResultType() for |
| 141 | details instead. */ |
| 142 | bool IsValue() const; |
| 143 | |
| 144 | bool IsValueNoError() const; |
| 145 | |
| 146 | /** Determines whether or not the result is a string containing more than |
| 147 | one paragraph */ |
| 148 | bool IsMultiline() const; |
| 149 | |
| 150 | bool GetErrorOrDouble( FormulaError& rErr, double& rVal ) const; |
| 151 | sc::FormulaResultValue GetResult() const; |
| 152 | |
| 153 | /** Get error code if set or GetCellResultType() is formula::svError or svUnknown, |
| 154 | else 0. */ |
| 155 | FormulaError GetResultError() const; |
| 156 | |
| 157 | /** Set error code, don't touch token or double. */ |
| 158 | void SetResultError( FormulaError nErr ); |
| 159 | |
| 160 | /** Set direct double. Shouldn't be used externally except in |
| 161 | ScFormulaCell for rounded CalcAsShown or SetErrCode() or |
| 162 | SetResultDouble(), see there for condition. If |
| 163 | ScMatrixFormulaCellToken the token isn't replaced but upper |
| 164 | left result is modified instead, but only if it was of type |
| 165 | formula::svDouble before or not set at all. |
| 166 | */ |
| 167 | void SetDouble( double f ); |
| 168 | |
| 169 | /** Return value if type formula::svDouble or formula::svHybridCell or formula::svMatrixCell and upper |
| 170 | left formula::svDouble, else 0.0 */ |
| 171 | double GetDouble() const; |
| 172 | |
| 173 | /** Return string if type formula::svString or formula::svHybridCell or formula::svMatrixCell and |
| 174 | upper left formula::svString, else empty string. */ |
| 175 | const svl::SharedString & GetString() const; |
| 176 | |
| 177 | /** Return matrix if type formula::svMatrixCell and ScMatrix present, else NULL. */ |
| 178 | ScConstMatrixRef GetMatrix() const; |
| 179 | |
| 180 | /** Return formula string if type formula::svHybridCell, else empty string. */ |
| 181 | OUString GetHybridFormula() const; |
| 182 | |
| 183 | /** Should only be used by import filters, best in the order |
| 184 | SetHybridDouble(), SetHybridString(), or only SetHybridFormula() for |
| 185 | formula string to be compiled later. */ |
| 186 | void SetHybridDouble( double f ); |
| 187 | |
| 188 | /** Should only be used by import filters, best in the order |
| 189 | SetHybridDouble(), SetHybridString()/SetHybridFormula(), or only |
| 190 | SetHybridFormula() for formula string to be compiled later. */ |
| 191 | void SetHybridString( const svl::SharedString & rStr ); |
| 192 | |
| 193 | /** Should only be used by import filters, best in the order |
| 194 | SetHybridDouble(), SetHybridFormula(), |
| 195 | SetHybridEmptyDisplayedAsString() must be last. */ |
| 196 | void SetHybridEmptyDisplayedAsString(); |
| 197 | |
| 198 | /** Should only be used by import filters, best in the order |
| 199 | SetHybridDouble(), SetHybridString()/SetHybridFormula(), or only |
| 200 | SetHybridFormula() for formula string to be compiled later. */ |
| 201 | void SetHybridFormula( const OUString & rFormula ); |
| 202 | |
| 203 | void SetMatrix( SCCOL nCols, SCROW nRows, const ScConstMatrixRef& pMat, const formula::FormulaToken* pUL ); |
| 204 | |
| 205 | /** Get the const ScMatrixFormulaCellToken* if token is of that type, else |
| 206 | NULL. */ |
| 207 | const ScMatrixFormulaCellToken* GetMatrixFormulaCellToken() const; |
| 208 | |
| 209 | /** Get the ScMatrixFormulaCellToken* if token is of that type, else NULL. |
| 210 | Shouldn't be used externally except by ScFormulaCell::SetMatColsRows(). */ |
| 211 | ScMatrixFormulaCellToken* GetMatrixFormulaCellTokenNonConst(); |
| 212 | |
| 213 | void HandleStuffAfterParallelCalculation(); |
| 214 | }; |
| 215 | |
| 216 | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |