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 .
20 #ifndef INCLUDED_SC_INC_FORMULARESULT_HXX
21 #define INCLUDED_SC_INC_FORMULARESULT_HXX
25 #include "calcmacros.hxx"
26 #include <svl/sharedstring.hxx>
27 #include <formula/token.hxx>
28 #include <formula/types.hxx>
30 class ScMatrixFormulaCellToken
;
34 struct FormulaResultValue
36 enum Type
{ Invalid
, Value
, String
, Error
};
41 svl::SharedString maString
;
45 FormulaResultValue( double fValue
);
46 FormulaResultValue( const svl::SharedString
& rStr
);
47 FormulaResultValue( FormulaError nErr
);
52 /** Store a variable formula cell result, balancing between runtime performance
53 and memory consumption. */
56 typedef unsigned char Multiline
;
57 static const Multiline MULTILINE_UNKNOWN
= 0;
58 static const Multiline MULTILINE_FALSE
= 1;
59 static const Multiline MULTILINE_TRUE
= 2;
61 // Clone token if the 16-bit only reference counter is nearing it's
62 // capacity during fill or copy&paste, leaving 4k for temporary passing
63 // around. (That should be enough for all times (TM) ;-)
64 static const sal_uInt16 MAX_TOKENREF_COUNT
= 0xf000;
65 static void IncrementTokenRef( const formula::FormulaToken
* & rp
)
69 if (rp
->GetRef() >= MAX_TOKENREF_COUNT
)
77 double mfValue
; // double result direct for performance and memory consumption
78 const formula::FormulaToken
* mpToken
; // if not, result token obtained from interpreter
80 FormulaError mnError
; // error code
81 bool mbToken
:1; // whether content of union is a token
82 bool mbEmpty
:1; // empty cell result
83 bool mbEmptyDisplayedAsString
:1; // only if mbEmpty
84 Multiline meMultiline
:2; // result is multiline
86 /** Reset mnError, mbEmpty and mbEmptyDisplayedAsString to their defaults
87 prior to assigning other types */
88 void ResetToDefaults();
90 /** If token is of formula::svError set error code and decrement RefCount.
91 If token is of formula::svEmptyCell set mbEmpty and mbEmptyAsString and
93 If token is of formula::svDouble set mfValue and decrement RefCount.
94 Else assign token to mpToken. NULL is valid => svUnknown.
95 Other member variables are set accordingly.
96 @precondition: Token MUST had been IncRef'ed prior to this call!
97 @precondition: An already existing different mpToken MUST had been
98 DecRef'ed prior to this call, p will be assigned to mpToken if not
100 ATTENTION! Token may get deleted in this call! */
101 void ResolveToken( const formula::FormulaToken
* p
);
104 /** Effectively type svUnknown. */
107 ScFormulaResult( const ScFormulaResult
& r
);
109 /** Same comments as for SetToken() apply! */
110 explicit ScFormulaResult( const formula::FormulaToken
* p
);
114 /** Well, guess what ... */
115 ScFormulaResult
& operator=( const ScFormulaResult
& r
);
117 /** Assignment as in operator=() but without return */
118 void Assign( const ScFormulaResult
& r
);
120 /** Sets a direct double if token type is formula::svDouble, or mbEmpty if
121 formula::svEmptyCell, else token. If p is NULL, that is set as well, effectively
122 resulting in GetType()==svUnknown. If the already existing result is
123 ScMatrixFormulaCellToken, the upper left is set to token.
125 ATTENTION! formula::FormulaToken had to be allocated using 'new' and if of type
126 formula::svDouble and no RefCount was set may not be used after this call
127 because it was deleted after decrement! */
128 void SetToken( const formula::FormulaToken
* p
);
130 /** May be NULL if SetToken() did so, also if type formula::svDouble or formula::svError! */
131 formula::FormulaConstTokenRef
GetToken() const;
133 /** Return upper left token if formula::svMatrixCell, else return GetToken().
134 May be NULL if SetToken() did so, also if type formula::svDouble or formula::svError! */
135 formula::FormulaConstTokenRef
GetCellResultToken() const;
137 /** Return type of result, including formula::svError, formula::svEmptyCell, formula::svDouble and
138 formula::svMatrixCell. */
139 formula::StackVar
GetType() const;
141 /** If type is formula::svMatrixCell return the type of upper left element, else
143 formula::StackVar
GetCellResultType() const;
145 /** If type is formula::svEmptyCell (including matrix upper left) and should be
146 displayed as empty string */
147 bool IsEmptyDisplayedAsString() const;
149 /** Test for cell result type formula::svDouble, including upper left if
150 formula::svMatrixCell. Also included is formula::svError for legacy, because previously
151 an error result was treated like a numeric value at some places in
152 ScFormulaCell. Also included is formula::svEmptyCell as a reference to an empty
153 cell usually is treated as numeric 0. Use GetCellResultType() for
155 bool IsValue() const;
157 bool IsValueNoError() const;
159 /** Determines whether or not the result is a string containing more than
161 bool IsMultiline() const;
163 bool GetErrorOrDouble( FormulaError
& rErr
, double& rVal
) const;
164 sc::FormulaResultValue
GetResult() const;
166 /** Get error code if set or GetCellResultType() is formula::svError or svUnknown,
168 FormulaError
GetResultError() const;
170 /** Set error code, don't touch token or double. */
171 void SetResultError( FormulaError nErr
);
173 /** Set direct double. Shouldn't be used externally except in
174 ScFormulaCell for rounded CalcAsShown or SetErrCode() or
175 SetResultDouble(), see there for condition. If
176 ScMatrixFormulaCellToken the token isn't replaced but upper
177 left result is modified instead, but only if it was of type
178 formula::svDouble before or not set at all.
180 SC_DLLPUBLIC
void SetDouble( double f
);
182 /** Return value if type formula::svDouble or formula::svHybridCell or formula::svMatrixCell and upper
183 left formula::svDouble, else 0.0 */
184 double GetDouble() const;
186 /** Return string if type formula::svString or formula::svHybridCell or formula::svMatrixCell and
187 upper left formula::svString, else empty string. */
188 svl::SharedString
GetString() const;
190 /** Return matrix if type formula::svMatrixCell and ScMatrix present, else NULL. */
191 ScConstMatrixRef
GetMatrix() const;
193 /** Return formula string if type formula::svHybridCell, else empty string. */
194 const OUString
& GetHybridFormula() const;
196 /** Should only be used by import filters, best in the order
197 SetHybridDouble(), SetHybridString(), or only SetHybridFormula() for
198 formula string to be compiled later. */
199 SC_DLLPUBLIC
void SetHybridDouble( double f
);
201 /** Should only be used by import filters, best in the order
202 SetHybridDouble(), SetHybridString()/SetHybridFormula(), or only
203 SetHybridFormula() for formula string to be compiled later. */
204 SC_DLLPUBLIC
void SetHybridString( const svl::SharedString
& rStr
);
206 /** Should only be used by import filters, best in the order
207 SetHybridDouble(), SetHybridFormula(),
208 SetHybridEmptyDisplayedAsString() must be last. */
209 SC_DLLPUBLIC
void SetHybridEmptyDisplayedAsString();
211 /** Should only be used by import filters, best in the order
212 SetHybridDouble(), SetHybridString()/SetHybridFormula(), or only
213 SetHybridFormula() for formula string to be compiled later. */
214 SC_DLLPUBLIC
void SetHybridFormula( const OUString
& rFormula
);
216 SC_DLLPUBLIC
void SetMatrix( SCCOL nCols
, SCROW nRows
, const ScConstMatrixRef
& pMat
, const formula::FormulaToken
* pUL
);
218 /** Get the const ScMatrixFormulaCellToken* if token is of that type, else
220 const ScMatrixFormulaCellToken
* GetMatrixFormulaCellToken() const;
222 /** Get the ScMatrixFormulaCellToken* if token is of that type, else NULL.
223 Shouldn't be used externally except by ScFormulaCell::SetMatColsRows(). */
224 ScMatrixFormulaCellToken
* GetMatrixFormulaCellTokenNonConst();
227 #endif // INCLUDED_SC_INC_FORMULARESULT_HXX
229 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */