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 SC_FORMULARESULT_HXX
21 : #define SC_FORMULARESULT_HXX
22 :
23 : #include "token.hxx"
24 : #include "scdllapi.h"
25 :
26 : #include <sal/log.hxx>
27 :
28 : /** Store a variable formula cell result, balancing between runtime performance
29 : and memory consumption. */
30 : class ScFormulaResult
31 : {
32 : typedef unsigned char Multiline;
33 : static const Multiline MULTILINE_UNKNOWN = 0;
34 : static const Multiline MULTILINE_FALSE = 1;
35 : static const Multiline MULTILINE_TRUE = 2;
36 :
37 : // Clone token if the 16-bit only reference counter is nearing it's
38 : // capacity during fill or copy&paste, leaving 4k for temporary passing
39 : // around. (That should be enough for all times (TM) ;-)
40 : static const sal_uInt16 MAX_TOKENREF_COUNT = 0xf000;
41 3385 : static void IncrementTokenRef( const formula::FormulaToken* & rp )
42 : {
43 3385 : if (rp)
44 : {
45 3155 : if (rp->GetRef() >= MAX_TOKENREF_COUNT)
46 0 : rp = rp->Clone();
47 3155 : rp->IncRef();
48 : }
49 3385 : }
50 :
51 : union
52 : {
53 : double mfValue; // double result direct for performance and memory consumption
54 : const formula::FormulaToken* mpToken; // if not, result token obtained from interpreter
55 : };
56 : sal_uInt16 mnError; // error code
57 : bool mbToken :1; // whether content of union is a token
58 : bool mbEmpty :1; // empty cell result
59 : bool mbEmptyDisplayedAsString :1; // only if mbEmpty
60 : Multiline meMultiline :2; // result is multiline
61 :
62 : /** Reset mnError, mbEmpty and mbEmptyDisplayedAsString to their defaults
63 : prior to assigning other types */
64 : void ResetToDefaults();
65 :
66 : /** If token is of formula::svError set error code and decrement RefCount.
67 : If token is of formula::svEmptyCell set mbEmpty and mbEmptyAsString and
68 : decrement RefCount.
69 : If token is of formula::svDouble set mfValue and decrement RefCount.
70 : Else assign token to mpToken. NULL is valid => svUnknown.
71 : Other member variables are set accordingly.
72 : @precondition: Token MUST had been IncRef'ed prior to this call!
73 : @precondition: An already existing different mpToken MUST had been
74 : DecRef'ed prior to this call, p will be assigned to mpToken if not
75 : resolved.
76 : ATTENTION! Token may get deleted in this call! */
77 : void ResolveToken( const formula::FormulaToken * p );
78 :
79 : public:
80 : /** Effectively type svUnknown. */
81 : ScFormulaResult();
82 :
83 : ScFormulaResult( const ScFormulaResult & r );
84 :
85 : /** Same comments as for SetToken() apply! */
86 : explicit ScFormulaResult( const formula::FormulaToken* p );
87 :
88 : ~ScFormulaResult();
89 :
90 : /** Well, guess what ... */
91 : ScFormulaResult& operator=( const ScFormulaResult & r );
92 :
93 : /** Assignment as in operator=() but without return */
94 : void Assign( const ScFormulaResult & r );
95 :
96 : /** Sets a direct double if token type is formula::svDouble, or mbEmpty if
97 : formula::svEmptyCell, else token. If p is NULL, that is set as well, effectively
98 : resulting in GetType()==svUnknown. If the already existing result is
99 : ScMatrixFormulaCellToken, the upper left ist set to token.
100 :
101 : ATTENTION! formula::FormulaToken had to be allocated using 'new' and if of type
102 : formula::svDouble and no RefCount was set may not be used after this call
103 : because it was deleted after decrement! */
104 : void SetToken( const formula::FormulaToken* p );
105 :
106 : /** May be NULL if SetToken() did so, also if type formula::svDouble or formula::svError! */
107 : formula::FormulaConstTokenRef GetToken() const;
108 :
109 : /** Return upper left token if formula::svMatrixCell, else return GetToken().
110 : May be NULL if SetToken() did so, also if type formula::svDouble or formula::svError! */
111 : formula::FormulaConstTokenRef GetCellResultToken() const;
112 :
113 : /** Return type of result, including formula::svError, formula::svEmptyCell, formula::svDouble and
114 : formula::svMatrixCell. */
115 : formula::StackVar GetType() const;
116 :
117 : /** If type is formula::svMatrixCell return the type of upper left element, else
118 : GetType() */
119 : formula::StackVar GetCellResultType() const;
120 :
121 : /** If type is formula::svEmptyCell (including matrix upper left) and should be
122 : displayed as empty string */
123 : bool IsEmptyDisplayedAsString() const;
124 :
125 : /** Test for cell result type formula::svDouble, including upper left if
126 : formula::svMatrixCell. Also included is formula::svError for legacy, because previously
127 : an error result was treated like a numeric value at some places in
128 : ScFormulaCell. Also included is formula::svEmptyCell as a reference to an empty
129 : cell usually is treated as numeric 0. Use GetCellResultType() for
130 : details instead. */
131 : bool IsValue() const;
132 :
133 : /** Determines whether or not the result is a string containing more than
134 : one paragraph */
135 : bool IsMultiline() const;
136 :
137 : /** Get error code if set or GetCellResultType() is formula::svError or svUnknown,
138 : else 0. */
139 : sal_uInt16 GetResultError() const;
140 :
141 : /** Set error code, don't touch token or double. */
142 : void SetResultError( sal_uInt16 nErr );
143 :
144 : /** Set direct double. Shouldn't be used externally except in
145 : ScFormulaCell for rounded CalcAsShown or SetErrCode() or
146 : SetResultDouble(), see there for condition. If
147 : ScMatrixFormulaCellToken the token isn't replaced but upper
148 : left result is modified instead, but only if it was of type
149 : formula::svDouble before or not set at all.
150 : */
151 : SC_DLLPUBLIC void SetDouble( double f );
152 :
153 : /** Return value if type formula::svDouble or formula::svHybridCell or formula::svMatrixCell and upper
154 : left formula::svDouble, else 0.0 */
155 : double GetDouble() const;
156 :
157 : /** Return string if type formula::svString or formula::svHybridCell or formula::svMatrixCell and
158 : upper left formula::svString, else empty string. */
159 : const String& GetString() const;
160 :
161 : /** Return matrix if type formula::svMatrixCell and ScMatrix present, else NULL. */
162 : ScConstMatrixRef GetMatrix() const;
163 :
164 : /** Return formula string if type formula::svHybridCell, else empty string. */
165 : const String& GetHybridFormula() const;
166 :
167 : /** Should only be used by import filters, best in the order
168 : SetHybridDouble(), SetHybridString(), or only SetHybridString() for
169 : formula string to be compiled later. */
170 : SC_DLLPUBLIC void SetHybridDouble( double f );
171 :
172 : /** Should only be used by import filters, best in the order
173 : SetHybridDouble(), SetHybridString()/SetHybridFormula(), or only
174 : SetHybridFormula() for formula string to be compiled later. */
175 : SC_DLLPUBLIC void SetHybridString( const rtl::OUString & rStr );
176 :
177 : /** Should only be used by import filters, best in the order
178 : SetHybridDouble(), SetHybridString()/SetHybridFormula(), or only
179 : SetHybridFormula() for formula string to be compiled later. */
180 : SC_DLLPUBLIC void SetHybridFormula( const String & rFormula );
181 :
182 : /** Get the const ScMatrixFormulaCellToken* if token is of that type, else
183 : NULL. */
184 : const ScMatrixFormulaCellToken* GetMatrixFormulaCellToken() const;
185 :
186 : /** Get the ScMatrixFormulaCellToken* if token is of that type, else NULL.
187 : Shouldn't be used externally except by ScFormulaCell::SetMatColsRows(). */
188 : ScMatrixFormulaCellToken* GetMatrixFormulaCellTokenNonConst();
189 : };
190 :
191 : #endif // SC_FORMULARESULT_HXX
192 :
193 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|