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