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