1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
23 #include "calcmacros.hxx"
24 #include <svl/sharedstring.hxx>
25 #include <formula/token.hxx>
26 #include <formula/types.hxx>
28 class ScMatrixFormulaCellToken
;
32 struct FormulaResultValue
34 enum Type
: sal_uInt8
{ Invalid
, Value
, String
, Error
};
37 svl::SharedString maString
;
38 bool mbMultiLine
= false;
43 FormulaResultValue( double fValue
);
44 FormulaResultValue( svl::SharedString aStr
, bool bMultiLine
);
45 FormulaResultValue( FormulaError nErr
);
50 /** Store a variable formula cell result, balancing between runtime performance
51 and memory consumption. */
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;
61 double mfValue
; // double result direct for performance and memory consumption
62 const formula::FormulaToken
* mpToken
; // if not, result token obtained from interpreter
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
73 /** Reset mnError, mbEmpty and mbEmptyDisplayedAsString to their defaults
74 prior to assigning other types */
75 void ResetToDefaults();
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
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
87 ATTENTION! Token may get deleted in this call! */
88 void ResolveToken( const formula::FormulaToken
* p
);
91 /** Effectively type svUnknown. */
94 ScFormulaResult( const ScFormulaResult
& r
);
96 /** Same comments as for SetToken() apply! */
97 explicit ScFormulaResult( const formula::FormulaToken
* p
);
101 /** Well, guess what ... */
102 ScFormulaResult
& operator=( const ScFormulaResult
& r
);
104 /** Assignment as in operator=() but without return */
105 void Assign( const ScFormulaResult
& r
);
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.
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
);
117 /** May be NULL if SetToken() did so, also if type formula::svDouble or formula::svError! */
118 formula::FormulaConstTokenRef
GetToken() const;
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;
124 /** Return type of result, including formula::svError, formula::svEmptyCell, formula::svDouble and
125 formula::svMatrixCell. */
126 formula::StackVar
GetType() const;
128 /** If type is formula::svMatrixCell return the type of upper left element, else
130 formula::StackVar
GetCellResultType() const;
132 /** If type is formula::svEmptyCell (including matrix upper left) and should be
133 displayed as empty string */
134 bool IsEmptyDisplayedAsString() const;
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
142 bool IsValue() const;
144 bool IsValueNoError() const;
146 /** Determines whether or not the result is a string containing more than
148 bool IsMultiline() const;
150 bool GetErrorOrDouble( FormulaError
& rErr
, double& rVal
) const;
151 sc::FormulaResultValue
GetResult() const;
153 /** Get error code if set or GetCellResultType() is formula::svError or svUnknown,
155 FormulaError
GetResultError() const;
157 /** Set error code, don't touch token or double. */
158 void SetResultError( FormulaError nErr
);
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.
167 void SetDouble( double f
);
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;
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;
177 /** Return matrix if type formula::svMatrixCell and ScMatrix present, else NULL. */
178 ScConstMatrixRef
GetMatrix() const;
180 /** Return formula string if type formula::svHybridCell, else empty string. */
181 OUString
GetHybridFormula() const;
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
);
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
);
193 /** Should only be used by import filters, best in the order
194 SetHybridDouble(), SetHybridFormula(),
195 SetHybridEmptyDisplayedAsString() must be last. */
196 void SetHybridEmptyDisplayedAsString();
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
);
203 void SetMatrix( SCCOL nCols
, SCROW nRows
, const ScConstMatrixRef
& pMat
, const formula::FormulaToken
* pUL
);
205 /** Get the const ScMatrixFormulaCellToken* if token is of that type, else
207 const ScMatrixFormulaCellToken
* GetMatrixFormulaCellToken() const;
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();
213 void HandleStuffAfterParallelCalculation();
216 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */