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