Impress Remote 1.0.5, tag sdremote-1.0.5
[LibreOffice.git] / sc / inc / token.hxx
bloba54055561bae72b3b49e57630985881e5ae49a6c
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_TOKEN_HXX
21 #define SC_TOKEN_HXX
23 #include <memory>
24 #include <vector>
25 #include <boost/intrusive_ptr.hpp>
27 #include "formula/opcode.hxx"
28 #include "refdata.hxx"
29 #include "scmatrix.hxx"
30 #include <tools/mempool.hxx>
31 #include "scdllapi.h"
32 #include "formula/IFunctionDescription.hxx"
33 #include "formula/token.hxx"
36 class ScJumpMatrix;
37 class ScToken;
39 typedef ::std::vector< ScComplexRefData > ScRefList;
40 typedef ::boost::intrusive_ptr<ScToken> ScTokenRef;
42 class SC_DLLPUBLIC ScToken : public formula::FormulaToken
44 private:
45 // not implemented, prevent usage
46 ScToken();
47 ScToken& operator=( const ScToken& );
49 protected:
51 ScToken( formula::StackVar eTypeP,OpCode e = ocPush ) : formula::FormulaToken(eTypeP,e) {}
52 ScToken( const ScToken& r ): formula::FormulaToken(r) {}
54 public:
56 virtual ~ScToken();
58 /**
59 Dummy methods to avoid switches and casts where possible,
60 the real token classes have to overload the appropriate method[s].
61 The only methods valid anytime if not overloaded are:
63 - GetByte() since this represents the count of parameters to a function
64 which of course is 0 on non-functions. formula::FormulaByteToken and ScExternal do
65 overload it.
67 - HasForceArray() since also this is only used for operators and
68 functions and is 0 for other tokens.
70 Any other non-overloaded method pops up an assertion.
73 virtual const ScSingleRefData& GetSingleRef() const;
74 virtual ScSingleRefData& GetSingleRef();
75 virtual const ScComplexRefData& GetDoubleRef() const;
76 virtual ScComplexRefData& GetDoubleRef();
77 virtual const ScSingleRefData& GetSingleRef2() const;
78 virtual ScSingleRefData& GetSingleRef2();
79 virtual void CalcAbsIfRel( const ScAddress& );
80 virtual void CalcRelFromAbs( const ScAddress& );
81 virtual const ScMatrix* GetMatrix() const;
82 virtual ScMatrix* GetMatrix();
83 virtual ScJumpMatrix* GetJumpMatrix() const;
84 virtual const ScRefList* GetRefList() const;
85 virtual ScRefList* GetRefList();
87 virtual bool TextEqual( const formula::FormulaToken& rToken ) const;
88 virtual bool Is3DRef() const; // reference with 3D flag set
90 /** If rTok1 and rTok2 both are SingleRef or DoubleRef tokens, extend/merge
91 ranges as needed for ocRange.
92 @param rPos
93 The formula's position, used to calculate absolute positions from
94 relative references.
95 @param bReuseDoubleRef
96 If true, a DoubleRef token is reused if passed as rTok1 or rTok2,
97 else a new DoubleRef token is created and returned.
98 @return
99 A reused or new'ed ScDoubleRefToken, or a NULL TokenRef if rTok1 or
100 rTok2 are not of sv(Single|Double)Ref
102 static formula::FormulaTokenRef ExtendRangeReference( formula::FormulaToken & rTok1, formula::FormulaToken & rTok2, const ScAddress & rPos, bool bReuseDoubleRef );
105 inline void intrusive_ptr_add_ref(const ScToken* p)
107 p->IncRef();
110 inline void intrusive_ptr_release(const ScToken* p)
112 p->DecRef();
115 class ScSingleRefToken : public ScToken
117 private:
118 ScSingleRefData aSingleRef;
119 public:
120 ScSingleRefToken( const ScSingleRefData& r, OpCode e = ocPush ) :
121 ScToken( formula::svSingleRef, e ), aSingleRef( r ) {}
122 ScSingleRefToken( const ScSingleRefToken& r ) :
123 ScToken( r ), aSingleRef( r.aSingleRef ) {}
124 virtual const ScSingleRefData& GetSingleRef() const;
125 virtual ScSingleRefData& GetSingleRef();
126 virtual void CalcAbsIfRel( const ScAddress& );
127 virtual void CalcRelFromAbs( const ScAddress& );
128 virtual bool operator==( const formula::FormulaToken& rToken ) const;
129 virtual FormulaToken* Clone() const { return new ScSingleRefToken(*this); }
131 DECL_FIXEDMEMPOOL_NEWDEL( ScSingleRefToken );
134 class ScDoubleRefToken : public ScToken
136 private:
137 ScComplexRefData aDoubleRef;
138 public:
139 ScDoubleRefToken( const ScComplexRefData& r, OpCode e = ocPush ) :
140 ScToken( formula::svDoubleRef, e ), aDoubleRef( r ) {}
141 ScDoubleRefToken( const ScSingleRefData& r, OpCode e = ocPush ) :
142 ScToken( formula::svDoubleRef, e )
144 aDoubleRef.Ref1 = r;
145 aDoubleRef.Ref2 = r;
147 ScDoubleRefToken( const ScDoubleRefToken& r ) :
148 ScToken( r ), aDoubleRef( r.aDoubleRef ) {}
149 virtual const ScSingleRefData& GetSingleRef() const;
150 virtual ScSingleRefData& GetSingleRef();
151 virtual const ScComplexRefData& GetDoubleRef() const;
152 virtual ScComplexRefData& GetDoubleRef();
153 virtual const ScSingleRefData& GetSingleRef2() const;
154 virtual ScSingleRefData& GetSingleRef2();
155 virtual void CalcAbsIfRel( const ScAddress& );
156 virtual void CalcRelFromAbs( const ScAddress& );
157 virtual bool operator==( const formula::FormulaToken& rToken ) const;
158 virtual FormulaToken* Clone() const { return new ScDoubleRefToken(*this); }
160 DECL_FIXEDMEMPOOL_NEWDEL( ScDoubleRefToken );
163 class ScMatrixToken : public ScToken
165 private:
166 ScMatrixRef pMatrix;
167 public:
168 ScMatrixToken( ScMatrixRef p ) :
169 ScToken( formula::svMatrix ), pMatrix( p ) {}
170 ScMatrixToken( const ScMatrixToken& r ) :
171 ScToken( r ), pMatrix( r.pMatrix ) {}
172 virtual const ScMatrix* GetMatrix() const;
173 virtual ScMatrix* GetMatrix();
174 virtual bool operator==( const formula::FormulaToken& rToken ) const;
175 virtual FormulaToken* Clone() const { return new ScMatrixToken(*this); }
179 class ScExternalSingleRefToken : public ScToken
181 private:
182 sal_uInt16 mnFileId;
183 String maTabName;
184 ScSingleRefData maSingleRef;
186 ScExternalSingleRefToken(); // disabled
187 public:
188 ScExternalSingleRefToken( sal_uInt16 nFileId, const String& rTabName, const ScSingleRefData& r );
189 ScExternalSingleRefToken( const ScExternalSingleRefToken& r );
190 virtual ~ScExternalSingleRefToken();
192 virtual sal_uInt16 GetIndex() const;
193 virtual const String& GetString() const;
194 virtual const ScSingleRefData& GetSingleRef() const;
195 virtual ScSingleRefData& GetSingleRef();
196 virtual void CalcAbsIfRel( const ScAddress& );
197 virtual void CalcRelFromAbs( const ScAddress& );
198 virtual bool operator==( const formula::FormulaToken& rToken ) const;
199 virtual FormulaToken* Clone() const { return new ScExternalSingleRefToken(*this); }
203 class ScExternalDoubleRefToken : public ScToken
205 private:
206 sal_uInt16 mnFileId;
207 String maTabName; // name of the first sheet
208 ScComplexRefData maDoubleRef;
210 ScExternalDoubleRefToken(); // disabled
211 public:
212 ScExternalDoubleRefToken( sal_uInt16 nFileId, const String& rTabName, const ScComplexRefData& r );
213 ScExternalDoubleRefToken( const ScExternalDoubleRefToken& r );
214 virtual ~ScExternalDoubleRefToken();
216 virtual sal_uInt16 GetIndex() const;
217 virtual const String& GetString() const;
218 virtual const ScSingleRefData& GetSingleRef() const;
219 virtual ScSingleRefData& GetSingleRef();
220 virtual const ScSingleRefData& GetSingleRef2() const;
221 virtual ScSingleRefData& GetSingleRef2();
222 virtual const ScComplexRefData& GetDoubleRef() const;
223 virtual ScComplexRefData& GetDoubleRef();
224 virtual void CalcAbsIfRel( const ScAddress& );
225 virtual void CalcRelFromAbs( const ScAddress& );
226 virtual bool operator==( const formula::FormulaToken& rToken ) const;
227 virtual FormulaToken* Clone() const { return new ScExternalDoubleRefToken(*this); }
230 class ScExternalNameToken : public ScToken
232 private:
233 sal_uInt16 mnFileId;
234 String maName;
235 private:
236 ScExternalNameToken(); // disabled
237 public:
238 ScExternalNameToken( sal_uInt16 nFileId, const String& rName );
239 ScExternalNameToken( const ScExternalNameToken& r );
240 virtual ~ScExternalNameToken();
241 virtual sal_uInt16 GetIndex() const;
242 virtual const String& GetString() const;
243 virtual bool operator==( const formula::FormulaToken& rToken ) const;
244 virtual FormulaToken* Clone() const { return new ScExternalNameToken(*this); }
248 // Only created from within the interpreter, no conversion from ScRawToken,
249 // never added to ScTokenArray!
250 class ScJumpMatrixToken : public ScToken
252 private:
253 ScJumpMatrix* pJumpMatrix;
254 public:
255 ScJumpMatrixToken( ScJumpMatrix* p ) :
256 ScToken( formula::svJumpMatrix ), pJumpMatrix( p ) {}
257 ScJumpMatrixToken( const ScJumpMatrixToken& r ) :
258 ScToken( r ), pJumpMatrix( r.pJumpMatrix ) {}
259 virtual ~ScJumpMatrixToken();
260 virtual ScJumpMatrix* GetJumpMatrix() const;
261 virtual bool operator==( const formula::FormulaToken& rToken ) const;
262 virtual FormulaToken* Clone() const { return new ScJumpMatrixToken(*this); }
266 // Only created from within the interpreter, no conversion from ScRawToken,
267 // never added to ScTokenArray!
268 class ScRefListToken : public ScToken
270 private:
271 ScRefList aRefList;
272 public:
273 ScRefListToken() :
274 ScToken( formula::svRefList ) {}
275 ScRefListToken( const ScRefListToken & r ) :
276 ScToken( r ), aRefList( r.aRefList ) {}
277 virtual void CalcAbsIfRel( const ScAddress& );
278 virtual void CalcRelFromAbs( const ScAddress& );
279 virtual const ScRefList* GetRefList() const;
280 virtual ScRefList* GetRefList();
281 virtual bool operator==( const formula::FormulaToken& rToken ) const;
282 virtual FormulaToken* Clone() const { return new ScRefListToken(*this); }
286 class SC_DLLPUBLIC ScEmptyCellToken : public ScToken
288 bool bInherited :1;
289 bool bDisplayedAsString :1;
290 public:
291 explicit ScEmptyCellToken( bool bInheritedP, bool bDisplayAsString ) :
292 ScToken( formula::svEmptyCell ),
293 bInherited( bInheritedP ),
294 bDisplayedAsString( bDisplayAsString ) {}
295 ScEmptyCellToken( const ScEmptyCellToken& r ) :
296 ScToken( r ),
297 bInherited( r.bInherited ),
298 bDisplayedAsString( r.bDisplayedAsString ) {}
299 bool IsInherited() const { return bInherited; }
300 bool IsDisplayedAsString() const { return bDisplayedAsString; }
301 virtual double GetDouble() const;
302 virtual const String & GetString() const;
303 virtual bool operator==( const formula::FormulaToken& rToken ) const;
304 virtual FormulaToken* Clone() const { return new ScEmptyCellToken(*this); }
308 /** Transports the result from the interpreter to the formula cell. */
309 class SC_DLLPUBLIC ScMatrixCellResultToken : public ScToken
311 // No non-const access implemented, silence down unxsols4 complaining about
312 // the public GetMatrix() hiding the one from ScToken.
313 virtual ScMatrix* GetMatrix();
315 protected:
316 ScConstMatrixRef xMatrix;
317 formula::FormulaConstTokenRef xUpperLeft;
318 public:
319 ScMatrixCellResultToken( const ScConstMatrixRef& pMat, formula::FormulaToken* pUL ) :
320 ScToken( formula::svMatrixCell ),
321 xMatrix( pMat), xUpperLeft( pUL) {}
322 ScMatrixCellResultToken( const ScMatrixCellResultToken& r ) :
323 ScToken( r ), xMatrix( r.xMatrix ),
324 xUpperLeft( r.xUpperLeft ) {}
325 virtual double GetDouble() const;
326 virtual const String & GetString() const;
327 virtual const ScMatrix* GetMatrix() const;
328 virtual bool operator==( const formula::FormulaToken& rToken ) const;
329 virtual FormulaToken* Clone() const { return new ScMatrixCellResultToken(*this); }
330 formula::StackVar GetUpperLeftType() const
332 return xUpperLeft ?
333 xUpperLeft->GetType() :
334 static_cast<formula::StackVar>(formula::svUnknown);
336 inline formula::FormulaConstTokenRef GetUpperLeftToken() const { return xUpperLeft; }
337 void Assign( const ScMatrixCellResultToken & r )
339 xMatrix = r.xMatrix;
340 xUpperLeft = r.xUpperLeft;
345 /** Stores the matrix result at the formula cell, additionally the range the
346 matrix formula occupies. */
347 class SC_DLLPUBLIC ScMatrixFormulaCellToken : public ScMatrixCellResultToken
349 private:
350 SCROW nRows;
351 SCCOL nCols;
352 public:
353 ScMatrixFormulaCellToken( SCCOL nC, SCROW nR, const ScConstMatrixRef& pMat, formula::FormulaToken* pUL ) :
354 ScMatrixCellResultToken(pMat, pUL),
355 nRows(nR), nCols(nC) {}
357 ScMatrixFormulaCellToken( SCCOL nC, SCROW nR ) :
358 ScMatrixCellResultToken( NULL, NULL ),
359 nRows( nR ), nCols( nC ) {}
360 ScMatrixFormulaCellToken( const ScMatrixFormulaCellToken& r ) :
361 ScMatrixCellResultToken( r ),
362 nRows( r.nRows ), nCols( r.nCols )
364 // xUpperLeft is modifiable through
365 // SetUpperLeftDouble(), so clone it.
366 if (xUpperLeft)
367 xUpperLeft = xUpperLeft->Clone();
369 virtual bool operator==( const formula::FormulaToken& rToken ) const;
370 virtual FormulaToken* Clone() const { return new ScMatrixFormulaCellToken(*this); }
371 void SetMatColsRows( SCCOL nC, SCROW nR )
373 nRows = nR;
374 nCols = nC;
376 void GetMatColsRows( SCCOL & nC, SCROW & nR ) const
378 nR = nRows;
379 nC = nCols;
381 SCCOL GetMatCols() const { return nCols; }
382 SCROW GetMatRows() const { return nRows; }
384 /** Assign matrix result, keep matrix formula
385 dimension. */
386 void Assign( const ScMatrixCellResultToken & r )
388 ScMatrixCellResultToken::Assign( r);
391 /** Assign any result, keep matrix formula
392 dimension. If token is of type
393 ScMatrixCellResultToken uses the
394 appropriate Assign() call, other tokens
395 are assigned to xUpperLeft and xMatrix will
396 be assigned NULL. */
397 void Assign( const formula::FormulaToken & r );
399 /** Modify xUpperLeft if formula::svDouble, or create
400 new formula::FormulaDoubleToken if not set yet. Does
401 nothing if xUpperLeft is of different type! */
402 void SetUpperLeftDouble( double f);
404 /** Reset matrix and upper left, keep matrix
405 formula dimension. */
406 void ResetResult()
408 xMatrix = NULL;
409 xUpperLeft = NULL;
414 class SC_DLLPUBLIC ScHybridCellToken : public ScToken
416 private:
417 double mfDouble;
418 String maString;
419 OUString maFormula;
420 public:
421 ScHybridCellToken( double f,
422 const OUString & rStr,
423 const OUString & rFormula ) :
424 ScToken( formula::svHybridCell ),
425 mfDouble( f ), maString( rStr ),
426 maFormula( rFormula ) {}
428 const OUString& GetFormula() const { return maFormula; }
429 virtual double GetDouble() const;
430 virtual const String& GetString() const;
431 virtual bool operator==( const formula::FormulaToken& rToken ) const;
432 virtual FormulaToken* Clone() const { return new ScHybridCellToken(*this); }
435 class SC_DLLPUBLIC ScHybridValueCellToken : public ScToken
437 private:
438 double mfValue;
439 String maString;
440 public:
441 ScHybridValueCellToken (double f, const OUString& rStr ):
442 ScToken( formula::svHybridValueCell ),
443 mfValue( f ), maString( rStr ) {}
445 virtual double GetDouble() const { return mfValue; }
446 virtual const String & GetString() const { return maString; }
449 // Simplify argument passing to RefUpdate methods with ScSingleRefToken or
450 // ScDoubleRefToken
451 class SingleDoubleRefModifier
453 ScComplexRefData aDub;
454 ScSingleRefData* pS;
455 ScComplexRefData* pD;
457 // not implemented, prevent usage
458 SingleDoubleRefModifier( const SingleDoubleRefModifier& );
459 SingleDoubleRefModifier& operator=( const SingleDoubleRefModifier& );
461 public:
462 SingleDoubleRefModifier( ScToken& rT )
464 formula::StackVar eType = rT.GetType();
465 if ( eType == formula::svSingleRef || eType == formula::svExternalSingleRef )
467 pS = &rT.GetSingleRef();
468 aDub.Ref1 = aDub.Ref2 = *pS;
469 pD = &aDub;
471 else
473 pS = 0;
474 pD = &rT.GetDoubleRef();
477 SingleDoubleRefModifier( ScSingleRefData& rS )
479 pS = &rS;
480 aDub.Ref1 = aDub.Ref2 = *pS;
481 pD = &aDub;
483 ~SingleDoubleRefModifier()
485 if ( pS )
486 *pS = (*pD).Ref1;
488 inline ScComplexRefData& Ref() { return *pD; }
491 class SingleDoubleRefProvider
493 public:
495 const ScSingleRefData& Ref1;
496 const ScSingleRefData& Ref2;
498 SingleDoubleRefProvider( const ScToken& r )
499 : Ref1( r.GetSingleRef() ),
500 Ref2( (r.GetType() == formula::svDoubleRef ||
501 r.GetType() == formula::svExternalDoubleRef) ?
502 r.GetDoubleRef().Ref2 : Ref1 )
504 SingleDoubleRefProvider( const ScSingleRefData& r )
505 : Ref1( r ), Ref2( r )
507 SingleDoubleRefProvider( const ScComplexRefData& r )
508 : Ref1( r.Ref1 ), Ref2( r.Ref2 )
510 ~SingleDoubleRefProvider()
514 #endif
516 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */