tdf#104816 sw: if non-section content, let all sections hide.
[LibreOffice.git] / sc / inc / formularesult.hxx
blobd6e9293569ac83efee0ecd915610507c6af8bd28
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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
23 #include "scdllapi.h"
24 #include "global.hxx"
25 #include "calcmacros.hxx"
26 #include <svl/sharedstring.hxx>
27 #include <formula/token.hxx>
28 #include <formula/types.hxx>
30 class ScMatrixFormulaCellToken;
32 namespace sc {
34 struct FormulaResultValue
36 enum Type { Invalid, Value, String, Error };
38 Type const meType;
40 double mfValue;
41 svl::SharedString maString;
42 FormulaError mnError;
44 FormulaResultValue();
45 FormulaResultValue( double fValue );
46 FormulaResultValue( const svl::SharedString& rStr );
47 FormulaResultValue( FormulaError nErr );
52 /** Store a variable formula cell result, balancing between runtime performance
53 and memory consumption. */
54 class ScFormulaResult
56 typedef unsigned char Multiline;
57 static const Multiline MULTILINE_UNKNOWN = 0;
58 static const Multiline MULTILINE_FALSE = 1;
59 static const Multiline MULTILINE_TRUE = 2;
61 // Clone token if the 16-bit only reference counter is nearing it's
62 // capacity during fill or copy&paste, leaving 4k for temporary passing
63 // around. (That should be enough for all times (TM) ;-)
64 static const sal_uInt16 MAX_TOKENREF_COUNT = 0xf000;
65 static void IncrementTokenRef( const formula::FormulaToken* & rp )
67 if (rp)
69 if (rp->GetRef() >= MAX_TOKENREF_COUNT)
70 rp = rp->Clone();
71 rp->IncRef();
75 union
77 double mfValue; // double result direct for performance and memory consumption
78 const formula::FormulaToken* mpToken; // if not, result token obtained from interpreter
80 bool mbToken :1; // whether content of union is a token
81 bool mbEmpty :1; // empty cell result
82 bool mbEmptyDisplayedAsString :1; // only if mbEmpty
83 // If set it implies that the result is a simple double (in mfValue) and no error
84 bool mbValueCached :1;
85 Multiline meMultiline :2; // result is multiline
86 FormulaError mnError; // error code
88 /** Reset mnError, mbEmpty and mbEmptyDisplayedAsString to their defaults
89 prior to assigning other types */
90 void ResetToDefaults();
92 /** If token is of formula::svError set error code and decrement RefCount.
93 If token is of formula::svEmptyCell set mbEmpty and mbEmptyAsString and
94 decrement RefCount.
95 If token is of formula::svDouble set mfValue and decrement RefCount.
96 Else assign token to mpToken. NULL is valid => svUnknown.
97 Other member variables are set accordingly.
98 @precondition: Token MUST had been IncRef'ed prior to this call!
99 @precondition: An already existing different mpToken MUST had been
100 DecRef'ed prior to this call, p will be assigned to mpToken if not
101 resolved.
102 ATTENTION! Token may get deleted in this call! */
103 void ResolveToken( const formula::FormulaToken * p );
105 public:
106 /** Effectively type svUnknown. */
107 ScFormulaResult();
109 ScFormulaResult( const ScFormulaResult & r );
111 /** Same comments as for SetToken() apply! */
112 explicit ScFormulaResult( const formula::FormulaToken* p );
114 ~ScFormulaResult();
116 /** Well, guess what ... */
117 ScFormulaResult& operator=( const ScFormulaResult & r );
119 /** Assignment as in operator=() but without return */
120 void Assign( const ScFormulaResult & r );
122 /** Sets a direct double if token type is formula::svDouble, or mbEmpty if
123 formula::svEmptyCell, else token. If p is NULL, that is set as well, effectively
124 resulting in GetType()==svUnknown. If the already existing result is
125 ScMatrixFormulaCellToken, the upper left is set to token.
127 ATTENTION! formula::FormulaToken had to be allocated using 'new' and if of type
128 formula::svDouble and no RefCount was set may not be used after this call
129 because it was deleted after decrement! */
130 void SetToken( const formula::FormulaToken* p );
132 /** May be NULL if SetToken() did so, also if type formula::svDouble or formula::svError! */
133 formula::FormulaConstTokenRef GetToken() const;
135 /** Return upper left token if formula::svMatrixCell, else return GetToken().
136 May be NULL if SetToken() did so, also if type formula::svDouble or formula::svError! */
137 formula::FormulaConstTokenRef GetCellResultToken() const;
139 /** Return type of result, including formula::svError, formula::svEmptyCell, formula::svDouble and
140 formula::svMatrixCell. */
141 formula::StackVar GetType() const;
143 /** If type is formula::svMatrixCell return the type of upper left element, else
144 GetType() */
145 formula::StackVar GetCellResultType() const;
147 /** If type is formula::svEmptyCell (including matrix upper left) and should be
148 displayed as empty string */
149 bool IsEmptyDisplayedAsString() const;
151 /** Test for cell result type formula::svDouble, including upper left if
152 formula::svMatrixCell. Also included is formula::svError for legacy, because previously
153 an error result was treated like a numeric value at some places in
154 ScFormulaCell. Also included is formula::svEmptyCell as a reference to an empty
155 cell usually is treated as numeric 0. Use GetCellResultType() for
156 details instead. */
157 bool IsValue() const;
159 bool IsValueNoError() const;
161 /** Determines whether or not the result is a string containing more than
162 one paragraph */
163 bool IsMultiline() const;
165 bool GetErrorOrDouble( FormulaError& rErr, double& rVal ) const;
166 sc::FormulaResultValue GetResult() const;
168 /** Get error code if set or GetCellResultType() is formula::svError or svUnknown,
169 else 0. */
170 FormulaError GetResultError() const;
172 /** Set error code, don't touch token or double. */
173 void SetResultError( FormulaError nErr );
175 /** Set direct double. Shouldn't be used externally except in
176 ScFormulaCell for rounded CalcAsShown or SetErrCode() or
177 SetResultDouble(), see there for condition. If
178 ScMatrixFormulaCellToken the token isn't replaced but upper
179 left result is modified instead, but only if it was of type
180 formula::svDouble before or not set at all.
182 SC_DLLPUBLIC void SetDouble( double f );
184 /** Return value if type formula::svDouble or formula::svHybridCell or formula::svMatrixCell and upper
185 left formula::svDouble, else 0.0 */
186 double GetDouble() const;
188 /** Return string if type formula::svString or formula::svHybridCell or formula::svMatrixCell and
189 upper left formula::svString, else empty string. */
190 svl::SharedString GetString() const;
192 /** Return matrix if type formula::svMatrixCell and ScMatrix present, else NULL. */
193 ScConstMatrixRef GetMatrix() const;
195 /** Return formula string if type formula::svHybridCell, else empty string. */
196 const OUString& GetHybridFormula() const;
198 /** Should only be used by import filters, best in the order
199 SetHybridDouble(), SetHybridString(), or only SetHybridFormula() for
200 formula string to be compiled later. */
201 SC_DLLPUBLIC void SetHybridDouble( double f );
203 /** Should only be used by import filters, best in the order
204 SetHybridDouble(), SetHybridString()/SetHybridFormula(), or only
205 SetHybridFormula() for formula string to be compiled later. */
206 SC_DLLPUBLIC void SetHybridString( const svl::SharedString & rStr );
208 /** Should only be used by import filters, best in the order
209 SetHybridDouble(), SetHybridFormula(),
210 SetHybridEmptyDisplayedAsString() must be last. */
211 SC_DLLPUBLIC void SetHybridEmptyDisplayedAsString();
213 /** Should only be used by import filters, best in the order
214 SetHybridDouble(), SetHybridString()/SetHybridFormula(), or only
215 SetHybridFormula() for formula string to be compiled later. */
216 SC_DLLPUBLIC void SetHybridFormula( const OUString & rFormula );
218 SC_DLLPUBLIC void SetMatrix( SCCOL nCols, SCROW nRows, const ScConstMatrixRef& pMat, const formula::FormulaToken* pUL );
220 /** Get the const ScMatrixFormulaCellToken* if token is of that type, else
221 NULL. */
222 const ScMatrixFormulaCellToken* GetMatrixFormulaCellToken() const;
224 /** Get the ScMatrixFormulaCellToken* if token is of that type, else NULL.
225 Shouldn't be used externally except by ScFormulaCell::SetMatColsRows(). */
226 ScMatrixFormulaCellToken* GetMatrixFormulaCellTokenNonConst();
229 #endif // INCLUDED_SC_INC_FORMULARESULT_HXX
231 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */