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
: sal_uInt8
{ Invalid
, Value
, String
, Error
};
39 svl::SharedString maString
;
44 FormulaResultValue( double fValue
);
45 FormulaResultValue( const svl::SharedString
& rStr
);
46 FormulaResultValue( FormulaError nErr
);
51 /** Store a variable formula cell result, balancing between runtime performance
52 and memory consumption. */
55 typedef unsigned char Multiline
;
56 static const Multiline MULTILINE_UNKNOWN
= 0;
57 static const Multiline MULTILINE_FALSE
= 1;
58 static const Multiline MULTILINE_TRUE
= 2;
60 // Clone token if the 16-bit only reference counter is nearing it's
61 // capacity during fill or copy&paste, leaving 4k for temporary passing
62 // around. (That should be enough for all times (TM) ;-)
63 static const sal_uInt16 MAX_TOKENREF_COUNT
= 0xf000;
64 static void IncrementTokenRef( const formula::FormulaToken
* & rp
)
68 if (rp
->GetRef() >= MAX_TOKENREF_COUNT
)
76 double mfValue
; // double result direct for performance and memory consumption
77 const formula::FormulaToken
* mpToken
; // if not, result token obtained from interpreter
79 bool mbToken
:1; // whether content of union is a token
80 bool mbEmpty
:1; // empty cell result
81 bool mbEmptyDisplayedAsString
:1; // only if mbEmpty
82 // If set it implies that the result is a simple double (in mfValue) and no error
83 bool mbValueCached
:1;
84 Multiline meMultiline
:2; // result is multiline
85 FormulaError mnError
; // error code
87 /** Reset mnError, mbEmpty and mbEmptyDisplayedAsString to their defaults
88 prior to assigning other types */
89 void ResetToDefaults();
91 /** If token is of formula::svError set error code and decrement RefCount.
92 If token is of formula::svEmptyCell set mbEmpty and mbEmptyAsString and
94 If token is of formula::svDouble set mfValue and decrement RefCount.
95 Else assign token to mpToken. NULL is valid => svUnknown.
96 Other member variables are set accordingly.
97 @precondition: Token MUST had been IncRef'ed prior to this call!
98 @precondition: An already existing different mpToken MUST had been
99 DecRef'ed prior to this call, p will be assigned to mpToken if not
101 ATTENTION! Token may get deleted in this call! */
102 void ResolveToken( const formula::FormulaToken
* p
);
105 /** Effectively type svUnknown. */
108 ScFormulaResult( const ScFormulaResult
& r
);
110 /** Same comments as for SetToken() apply! */
111 explicit ScFormulaResult( const formula::FormulaToken
* p
);
115 /** Well, guess what ... */
116 ScFormulaResult
& operator=( const ScFormulaResult
& r
);
118 /** Assignment as in operator=() but without return */
119 void Assign( const ScFormulaResult
& r
);
121 /** Sets a direct double if token type is formula::svDouble, or mbEmpty if
122 formula::svEmptyCell, else token. If p is NULL, that is set as well, effectively
123 resulting in GetType()==svUnknown. If the already existing result is
124 ScMatrixFormulaCellToken, the upper left is set to token.
126 ATTENTION! formula::FormulaToken had to be allocated using 'new' and if of type
127 formula::svDouble and no RefCount was set may not be used after this call
128 because it was deleted after decrement! */
129 void SetToken( const formula::FormulaToken
* p
);
131 /** May be NULL if SetToken() did so, also if type formula::svDouble or formula::svError! */
132 formula::FormulaConstTokenRef
GetToken() const;
134 /** Return upper left token if formula::svMatrixCell, else return GetToken().
135 May be NULL if SetToken() did so, also if type formula::svDouble or formula::svError! */
136 formula::FormulaConstTokenRef
GetCellResultToken() const;
138 /** Return type of result, including formula::svError, formula::svEmptyCell, formula::svDouble and
139 formula::svMatrixCell. */
140 formula::StackVar
GetType() const;
142 /** If type is formula::svMatrixCell return the type of upper left element, else
144 formula::StackVar
GetCellResultType() const;
146 /** If type is formula::svEmptyCell (including matrix upper left) and should be
147 displayed as empty string */
148 bool IsEmptyDisplayedAsString() const;
150 /** Test for cell result type formula::svDouble, including upper left if
151 formula::svMatrixCell. Also included is formula::svError for legacy, because previously
152 an error result was treated like a numeric value at some places in
153 ScFormulaCell. Also included is formula::svEmptyCell as a reference to an empty
154 cell usually is treated as numeric 0. Use GetCellResultType() for
156 bool IsValue() const;
158 bool IsValueNoError() const;
160 /** Determines whether or not the result is a string containing more than
162 bool IsMultiline() const;
164 bool GetErrorOrDouble( FormulaError
& rErr
, double& rVal
) const;
165 sc::FormulaResultValue
GetResult() const;
167 /** Get error code if set or GetCellResultType() is formula::svError or svUnknown,
169 FormulaError
GetResultError() const;
171 /** Set error code, don't touch token or double. */
172 void SetResultError( FormulaError nErr
);
174 /** Set direct double. Shouldn't be used externally except in
175 ScFormulaCell for rounded CalcAsShown or SetErrCode() or
176 SetResultDouble(), see there for condition. If
177 ScMatrixFormulaCellToken the token isn't replaced but upper
178 left result is modified instead, but only if it was of type
179 formula::svDouble before or not set at all.
181 void SetDouble( double f
);
183 /** Return value if type formula::svDouble or formula::svHybridCell or formula::svMatrixCell and upper
184 left formula::svDouble, else 0.0 */
185 double GetDouble() const;
187 /** Return string if type formula::svString or formula::svHybridCell or formula::svMatrixCell and
188 upper left formula::svString, else empty string. */
189 svl::SharedString
GetString() const;
191 /** Return matrix if type formula::svMatrixCell and ScMatrix present, else NULL. */
192 ScConstMatrixRef
GetMatrix() const;
194 /** Return formula string if type formula::svHybridCell, else empty string. */
195 const OUString
& GetHybridFormula() const;
197 /** Should only be used by import filters, best in the order
198 SetHybridDouble(), SetHybridString(), or only SetHybridFormula() for
199 formula string to be compiled later. */
200 void SetHybridDouble( double f
);
202 /** Should only be used by import filters, best in the order
203 SetHybridDouble(), SetHybridString()/SetHybridFormula(), or only
204 SetHybridFormula() for formula string to be compiled later. */
205 void SetHybridString( const svl::SharedString
& rStr
);
207 /** Should only be used by import filters, best in the order
208 SetHybridDouble(), SetHybridFormula(),
209 SetHybridEmptyDisplayedAsString() must be last. */
210 void SetHybridEmptyDisplayedAsString();
212 /** Should only be used by import filters, best in the order
213 SetHybridDouble(), SetHybridString()/SetHybridFormula(), or only
214 SetHybridFormula() for formula string to be compiled later. */
215 void SetHybridFormula( const OUString
& rFormula
);
217 void SetMatrix( SCCOL nCols
, SCROW nRows
, const ScConstMatrixRef
& pMat
, const formula::FormulaToken
* pUL
);
219 /** Get the const ScMatrixFormulaCellToken* if token is of that type, else
221 const ScMatrixFormulaCellToken
* GetMatrixFormulaCellToken() const;
223 /** Get the ScMatrixFormulaCellToken* if token is of that type, else NULL.
224 Shouldn't be used externally except by ScFormulaCell::SetMatColsRows(). */
225 ScMatrixFormulaCellToken
* GetMatrixFormulaCellTokenNonConst();
228 #endif // INCLUDED_SC_INC_FORMULARESULT_HXX
230 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */