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 mbEmpty
:1; // empty cell result
66 bool mbEmptyDisplayedAsString
:1; // only if mbEmpty
67 // If set it implies that the result is a simple double (in mfValue) and no error
68 bool mbValueCached
:1;
69 Multiline meMultiline
:2; // result is multiline
70 FormulaError mnError
; // error code
72 /** Reset mnError, mbEmpty and mbEmptyDisplayedAsString to their defaults
73 prior to assigning other types */
74 void ResetToDefaults();
76 /** If token is of formula::svError set error code and decrement RefCount.
77 If token is of formula::svEmptyCell set mbEmpty and mbEmptyAsString and
79 If token is of formula::svDouble set mfValue and decrement RefCount.
80 Else assign token to mpToken. NULL is valid => svUnknown.
81 Other member variables are set accordingly.
82 @precondition: Token MUST had been IncRef'ed prior to this call!
83 @precondition: An already existing different mpToken MUST had been
84 DecRef'ed prior to this call, p will be assigned to mpToken if not
86 ATTENTION! Token may get deleted in this call! */
87 void ResolveToken( const formula::FormulaToken
* p
);
90 /** Effectively type svUnknown. */
93 ScFormulaResult( const ScFormulaResult
& r
);
95 /** Same comments as for SetToken() apply! */
96 explicit ScFormulaResult( const formula::FormulaToken
* p
);
100 /** Well, guess what ... */
101 ScFormulaResult
& operator=( const ScFormulaResult
& r
);
103 /** Assignment as in operator=() but without return */
104 void Assign( const ScFormulaResult
& r
);
106 /** Sets a direct double if token type is formula::svDouble, or mbEmpty if
107 formula::svEmptyCell, else token. If p is NULL, that is set as well, effectively
108 resulting in GetType()==svUnknown. If the already existing result is
109 ScMatrixFormulaCellToken, the upper left is set to token.
111 ATTENTION! formula::FormulaToken had to be allocated using 'new' and if of type
112 formula::svDouble and no RefCount was set may not be used after this call
113 because it was deleted after decrement! */
114 void SetToken( const formula::FormulaToken
* p
);
116 /** May be NULL if SetToken() did so, also if type formula::svDouble or formula::svError! */
117 formula::FormulaConstTokenRef
GetToken() const;
119 /** Return upper left token if formula::svMatrixCell, else return GetToken().
120 May be NULL if SetToken() did so, also if type formula::svDouble or formula::svError! */
121 formula::FormulaConstTokenRef
GetCellResultToken() const;
123 /** Return type of result, including formula::svError, formula::svEmptyCell, formula::svDouble and
124 formula::svMatrixCell. */
125 formula::StackVar
GetType() const;
127 /** If type is formula::svMatrixCell return the type of upper left element, else
129 formula::StackVar
GetCellResultType() const;
131 /** If type is formula::svEmptyCell (including matrix upper left) and should be
132 displayed as empty string */
133 bool IsEmptyDisplayedAsString() const;
135 /** Test for cell result type formula::svDouble, including upper left if
136 formula::svMatrixCell. Also included is formula::svError for legacy, because previously
137 an error result was treated like a numeric value at some places in
138 ScFormulaCell. Also included is formula::svEmptyCell as a reference to an empty
139 cell usually is treated as numeric 0. Use GetCellResultType() for
141 bool IsValue() const;
143 bool IsValueNoError() const;
145 /** Determines whether or not the result is a string containing more than
147 bool IsMultiline() const;
149 bool GetErrorOrDouble( FormulaError
& rErr
, double& rVal
) const;
150 sc::FormulaResultValue
GetResult() const;
152 /** Get error code if set or GetCellResultType() is formula::svError or svUnknown,
154 FormulaError
GetResultError() const;
156 /** Set error code, don't touch token or double. */
157 void SetResultError( FormulaError nErr
);
159 /** Set direct double. Shouldn't be used externally except in
160 ScFormulaCell for rounded CalcAsShown or SetErrCode() or
161 SetResultDouble(), see there for condition. If
162 ScMatrixFormulaCellToken the token isn't replaced but upper
163 left result is modified instead, but only if it was of type
164 formula::svDouble before or not set at all.
166 void SetDouble( double f
);
168 /** Return value if type formula::svDouble or formula::svHybridCell or formula::svMatrixCell and upper
169 left formula::svDouble, else 0.0 */
170 double GetDouble() const;
172 /** Return string if type formula::svString or formula::svHybridCell or formula::svMatrixCell and
173 upper left formula::svString, else empty string. */
174 const svl::SharedString
& GetString() const;
176 /** Return matrix if type formula::svMatrixCell and ScMatrix present, else NULL. */
177 ScConstMatrixRef
GetMatrix() const;
179 /** Return formula string if type formula::svHybridCell, else empty string. */
180 OUString
GetHybridFormula() const;
182 /** Should only be used by import filters, best in the order
183 SetHybridDouble(), SetHybridString(), or only SetHybridFormula() for
184 formula string to be compiled later. */
185 void SetHybridDouble( double f
);
187 /** Should only be used by import filters, best in the order
188 SetHybridDouble(), SetHybridString()/SetHybridFormula(), or only
189 SetHybridFormula() for formula string to be compiled later. */
190 void SetHybridString( const svl::SharedString
& rStr
);
192 /** Should only be used by import filters, best in the order
193 SetHybridDouble(), SetHybridFormula(),
194 SetHybridEmptyDisplayedAsString() must be last. */
195 void SetHybridEmptyDisplayedAsString();
197 /** Should only be used by import filters, best in the order
198 SetHybridDouble(), SetHybridString()/SetHybridFormula(), or only
199 SetHybridFormula() for formula string to be compiled later. */
200 void SetHybridFormula( const OUString
& rFormula
);
202 void SetMatrix( SCCOL nCols
, SCROW nRows
, const ScConstMatrixRef
& pMat
, const formula::FormulaToken
* pUL
);
204 /** Get the const ScMatrixFormulaCellToken* if token is of that type, else
206 const ScMatrixFormulaCellToken
* GetMatrixFormulaCellToken() const;
208 /** Get the ScMatrixFormulaCellToken* if token is of that type, else NULL.
209 Shouldn't be used externally except by ScFormulaCell::SetMatColsRows(). */
210 ScMatrixFormulaCellToken
* GetMatrixFormulaCellTokenNonConst();
213 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */