Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #ifndef INCLUDED_SC_INC_FORMULARESULT_HXX
21 : #define INCLUDED_SC_INC_FORMULARESULT_HXX
22 :
23 : #include "token.hxx"
24 : #include "scdllapi.h"
25 :
26 : namespace sc {
27 :
28 1328 : struct FormulaResultValue
29 : {
30 : enum Type { Invalid, Value, String, Error };
31 :
32 : Type meType;
33 :
34 : double mfValue;
35 : svl::SharedString maString;
36 : sal_uInt16 mnError;
37 :
38 : FormulaResultValue();
39 : FormulaResultValue( double fValue );
40 : FormulaResultValue(const svl::SharedString& rStr );
41 : FormulaResultValue( sal_uInt16 nErr );
42 : };
43 :
44 : }
45 :
46 : /** Store a variable formula cell result, balancing between runtime performance
47 : and memory consumption. */
48 : class ScFormulaResult
49 : {
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;
54 :
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 24211 : static void IncrementTokenRef( const formula::FormulaToken* & rp )
60 : {
61 24211 : if (rp)
62 : {
63 21623 : if (rp->GetRef() >= MAX_TOKENREF_COUNT)
64 0 : rp = rp->Clone();
65 21623 : rp->IncRef();
66 : }
67 24211 : }
68 :
69 : union
70 : {
71 : double mfValue; // double result direct for performance and memory consumption
72 : const formula::FormulaToken* mpToken; // if not, result token obtained from interpreter
73 : };
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
79 :
80 : /** Reset mnError, mbEmpty and mbEmptyDisplayedAsString to their defaults
81 : prior to assigning other types */
82 : void ResetToDefaults();
83 :
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
86 : decrement RefCount.
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
93 : resolved.
94 : ATTENTION! Token may get deleted in this call! */
95 : void ResolveToken( const formula::FormulaToken * p );
96 :
97 : public:
98 : /** Effectively type svUnknown. */
99 : ScFormulaResult();
100 :
101 : ScFormulaResult( const ScFormulaResult & r );
102 :
103 : /** Same comments as for SetToken() apply! */
104 : explicit ScFormulaResult( const formula::FormulaToken* p );
105 :
106 : ~ScFormulaResult();
107 :
108 : /** Well, guess what ... */
109 : ScFormulaResult& operator=( const ScFormulaResult & r );
110 :
111 : /** Assignment as in operator=() but without return */
112 : void Assign( const ScFormulaResult & r );
113 :
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.
118 :
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 );
123 :
124 : /** May be NULL if SetToken() did so, also if type formula::svDouble or formula::svError! */
125 : formula::FormulaConstTokenRef GetToken() const;
126 :
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;
130 :
131 : /** Return type of result, including formula::svError, formula::svEmptyCell, formula::svDouble and
132 : formula::svMatrixCell. */
133 : formula::StackVar GetType() const;
134 :
135 : /** If type is formula::svMatrixCell return the type of upper left element, else
136 : GetType() */
137 : formula::StackVar GetCellResultType() const;
138 :
139 : /** If type is formula::svEmptyCell (including matrix upper left) and should be
140 : displayed as empty string */
141 : bool IsEmptyDisplayedAsString() const;
142 :
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
148 : details instead. */
149 : bool IsValue() const;
150 :
151 : bool IsValueNoError() const;
152 :
153 : /** Determines whether or not the result is a string containing more than
154 : one paragraph */
155 : bool IsMultiline() const;
156 :
157 : bool GetErrorOrDouble( sal_uInt16& rErr, double& rVal ) const;
158 : sc::FormulaResultValue GetResult() const;
159 :
160 : /** Get error code if set or GetCellResultType() is formula::svError or svUnknown,
161 : else 0. */
162 : sal_uInt16 GetResultError() const;
163 :
164 : /** Set error code, don't touch token or double. */
165 : void SetResultError( sal_uInt16 nErr );
166 :
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.
173 : */
174 : SC_DLLPUBLIC void SetDouble( double f );
175 :
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;
179 :
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;
183 :
184 : /** Return matrix if type formula::svMatrixCell and ScMatrix present, else NULL. */
185 : ScConstMatrixRef GetMatrix() const;
186 :
187 : /** Return formula string if type formula::svHybridCell, else empty string. */
188 : const OUString& GetHybridFormula() const;
189 :
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 );
194 :
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 );
199 :
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 );
204 :
205 : SC_DLLPUBLIC void SetMatrix( SCCOL nCols, SCROW nRows, const ScConstMatrixRef& pMat, formula::FormulaToken* pUL );
206 :
207 : /** Get the const ScMatrixFormulaCellToken* if token is of that type, else
208 : NULL. */
209 : const ScMatrixFormulaCellToken* GetMatrixFormulaCellToken() const;
210 :
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();
214 : };
215 :
216 : #endif // INCLUDED_SC_INC_FORMULARESULT_HXX
217 :
218 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|