Impress Remote 1.0.5, tag sdremote-1.0.5
[LibreOffice.git] / sc / inc / formularesult.hxx
blob673bcdbf84bcce009f531646f6f466489efbd6dc
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 #include <sal/log.hxx>
28 /** Store a variable formula cell result, balancing between runtime performance
29 and memory consumption. */
30 class ScFormulaResult
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;
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 static void IncrementTokenRef( const formula::FormulaToken* & rp )
43 if (rp)
45 if (rp->GetRef() >= MAX_TOKENREF_COUNT)
46 rp = rp->Clone();
47 rp->IncRef();
51 union
53 double mfValue; // double result direct for performance and memory consumption
54 const formula::FormulaToken* mpToken; // if not, result token obtained from interpreter
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
62 /** Reset mnError, mbEmpty and mbEmptyDisplayedAsString to their defaults
63 prior to assigning other types */
64 void ResetToDefaults();
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 );
79 public:
80 /** Effectively type svUnknown. */
81 ScFormulaResult();
83 ScFormulaResult( const ScFormulaResult & r );
85 /** Same comments as for SetToken() apply! */
86 explicit ScFormulaResult( const formula::FormulaToken* p );
88 ~ScFormulaResult();
90 /** Well, guess what ... */
91 ScFormulaResult& operator=( const ScFormulaResult & r );
93 /** Assignment as in operator=() but without return */
94 void Assign( const ScFormulaResult & r );
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.
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 );
106 /** May be NULL if SetToken() did so, also if type formula::svDouble or formula::svError! */
107 formula::FormulaConstTokenRef GetToken() const;
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;
113 /** Return type of result, including formula::svError, formula::svEmptyCell, formula::svDouble and
114 formula::svMatrixCell. */
115 formula::StackVar GetType() const;
117 /** If type is formula::svMatrixCell return the type of upper left element, else
118 GetType() */
119 formula::StackVar GetCellResultType() const;
121 /** If type is formula::svEmptyCell (including matrix upper left) and should be
122 displayed as empty string */
123 bool IsEmptyDisplayedAsString() const;
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;
133 /** Determines whether or not the result is a string containing more than
134 one paragraph */
135 bool IsMultiline() const;
137 /** Get error code if set or GetCellResultType() is formula::svError or svUnknown,
138 else 0. */
139 sal_uInt16 GetResultError() const;
141 /** Set error code, don't touch token or double. */
142 void SetResultError( sal_uInt16 nErr );
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.
151 SC_DLLPUBLIC void SetDouble( double f );
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;
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;
161 /** Return matrix if type formula::svMatrixCell and ScMatrix present, else NULL. */
162 ScConstMatrixRef GetMatrix() const;
164 /** Return formula string if type formula::svHybridCell, else empty string. */
165 const OUString& GetHybridFormula() const;
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 );
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 );
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 );
182 void SetHybridValueString( double nVal, const OUString& rStr );
184 SC_DLLPUBLIC void SetMatrix( SCCOL nCols, SCROW nRows, const ScConstMatrixRef& pMat, formula::FormulaToken* pUL );
186 /** Get the const ScMatrixFormulaCellToken* if token is of that type, else
187 NULL. */
188 const ScMatrixFormulaCellToken* GetMatrixFormulaCellToken() const;
190 /** Get the ScMatrixFormulaCellToken* if token is of that type, else NULL.
191 Shouldn't be used externally except by ScFormulaCell::SetMatColsRows(). */
192 ScMatrixFormulaCellToken* GetMatrixFormulaCellTokenNonConst();
195 #endif // SC_FORMULARESULT_HXX
197 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */