LCOV - code coverage report
Current view: top level - sc/inc - token.hxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 63 86 73.3 %
Date: 2014-11-03 Functions: 43 59 72.9 %
Legend: Lines: hit not hit

          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 INCLUDED_SC_INC_TOKEN_HXX
      21             : #define INCLUDED_SC_INC_TOKEN_HXX
      22             : 
      23             : #include <vector>
      24             : #include <boost/intrusive_ptr.hpp>
      25             : 
      26             : #include <formula/opcode.hxx>
      27             : #include "refdata.hxx"
      28             : #include <tools/mempool.hxx>
      29             : #include "scdllapi.h"
      30             : #include <formula/IFunctionDescription.hxx>
      31             : #include <formula/token.hxx>
      32             : #include "scmatrix.hxx"
      33             : #include "calcmacros.hxx"
      34             : 
      35             : // Matrix token constants.
      36             : #define MATRIX_TOKEN_HAS_RANGE 1
      37             : 
      38             : namespace sc {
      39             : 
      40             : struct RangeMatrix;
      41             : 
      42             : }
      43             : 
      44             : class ScJumpMatrix;
      45             : 
      46             : typedef ::std::vector< ScComplexRefData > ScRefList;
      47             : 
      48             : #if DEBUG_FORMULA_COMPILER
      49             : void DumpToken(formula::FormulaToken const & rToken);
      50             : #endif
      51             : 
      52             : /** If rTok1 and rTok2 both are SingleRef or DoubleRef tokens, extend/merge
      53             :     ranges as needed for ocRange.
      54             :     @param rPos
      55             :         The formula's position, used to calculate absolute positions from
      56             :         relative references.
      57             :     @param bReuseDoubleRef
      58             :         If true, a DoubleRef token is reused if passed as rTok1 or rTok2,
      59             :         else a new DoubleRef token is created and returned.
      60             :     @return
      61             :         A reused or new'ed ScDoubleRefToken, or a NULL TokenRef if rTok1 or
      62             :         rTok2 are not of sv(Single|Double)Ref
      63             : */
      64             : formula::FormulaTokenRef extendRangeReference( formula::FormulaToken & rTok1, formula::FormulaToken & rTok2, const ScAddress & rPos, bool bReuseDoubleRef );
      65             : 
      66      118688 : class ScSingleRefToken : public formula::FormulaToken
      67             : {
      68             : private:
      69             :             ScSingleRefData       aSingleRef;
      70             : public:
      71       31084 :                                 ScSingleRefToken( const ScSingleRefData& r, OpCode e = ocPush ) :
      72       31084 :                                     FormulaToken( formula::svSingleRef, e ), aSingleRef( r ) {}
      73       28330 :                                 ScSingleRefToken( const ScSingleRefToken& r ) :
      74       28330 :                                     FormulaToken( r ), aSingleRef( r.aSingleRef ) {}
      75             :     virtual const ScSingleRefData*    GetSingleRef() const SAL_OVERRIDE;
      76             :     virtual ScSingleRefData*      GetSingleRef() SAL_OVERRIDE;
      77             :     virtual bool                TextEqual( const formula::FormulaToken& rToken ) const SAL_OVERRIDE;
      78             :     virtual bool                operator==( const formula::FormulaToken& rToken ) const SAL_OVERRIDE;
      79       28330 :     virtual FormulaToken*       Clone() const SAL_OVERRIDE { return new ScSingleRefToken(*this); }
      80             : 
      81      118758 :     DECL_FIXEDMEMPOOL_NEWDEL( ScSingleRefToken );
      82             : };
      83             : 
      84       49808 : class ScDoubleRefToken : public formula::FormulaToken
      85             : {
      86             : private:
      87             :             ScComplexRefData        aDoubleRef;
      88             : public:
      89       20498 :                                 ScDoubleRefToken( const ScComplexRefData& r, OpCode e = ocPush  ) :
      90       20498 :                                     FormulaToken( formula::svDoubleRef, e ), aDoubleRef( r ) {}
      91             :                                 ScDoubleRefToken( const ScSingleRefData& r, OpCode e = ocPush  ) :
      92             :                                     FormulaToken( formula::svDoubleRef, e )
      93             :                                 {
      94             :                                     aDoubleRef.Ref1 = r;
      95             :                                     aDoubleRef.Ref2 = r;
      96             :                                 }
      97        4406 :                                 ScDoubleRefToken( const ScDoubleRefToken& r ) :
      98        4406 :                                     FormulaToken( r ), aDoubleRef( r.aDoubleRef ) {}
      99             :     virtual const ScSingleRefData*    GetSingleRef() const SAL_OVERRIDE;
     100             :     virtual ScSingleRefData*      GetSingleRef() SAL_OVERRIDE;
     101             :     virtual const ScComplexRefData* GetDoubleRef() const SAL_OVERRIDE;
     102             :     virtual ScComplexRefData*       GetDoubleRef() SAL_OVERRIDE;
     103             :     virtual const ScSingleRefData*    GetSingleRef2() const SAL_OVERRIDE;
     104             :     virtual ScSingleRefData*      GetSingleRef2() SAL_OVERRIDE;
     105             :     virtual bool                TextEqual( const formula::FormulaToken& rToken ) const SAL_OVERRIDE;
     106             :     virtual bool                operator==( const formula::FormulaToken& rToken ) const SAL_OVERRIDE;
     107        4406 :     virtual FormulaToken*       Clone() const SAL_OVERRIDE { return new ScDoubleRefToken(*this); }
     108             : 
     109       49808 :     DECL_FIXEDMEMPOOL_NEWDEL( ScDoubleRefToken );
     110             : };
     111             : 
     112         812 : class ScMatrixToken : public formula::FormulaToken
     113             : {
     114             : private:
     115             :             ScMatrixRef         pMatrix;
     116             : public:
     117             :     ScMatrixToken( const ScMatrixRef& p );
     118             :     ScMatrixToken( const ScMatrixToken& r );
     119             : 
     120             :     virtual const ScMatrix*     GetMatrix() const SAL_OVERRIDE;
     121             :     virtual ScMatrix*           GetMatrix() SAL_OVERRIDE;
     122             :     virtual bool                operator==( const formula::FormulaToken& rToken ) const SAL_OVERRIDE;
     123          34 :     virtual FormulaToken*       Clone() const SAL_OVERRIDE { return new ScMatrixToken(*this); }
     124             : };
     125             : 
     126             : /**
     127             :  * Token storing matrix that represents values in sheet range. It stores
     128             :  * both the values in matrix form, and the range address the matrix
     129             :  * represents.
     130             :  */
     131           0 : class ScMatrixRangeToken : public formula::FormulaToken
     132             : {
     133             :     ScMatrixRef mpMatrix;
     134             :     ScComplexRefData maRef;
     135             : public:
     136             :     ScMatrixRangeToken( const ScMatrixRef& p, const ScComplexRefData& rRef );
     137             :     ScMatrixRangeToken( const sc::RangeMatrix& rMat );
     138             :     ScMatrixRangeToken( const ScMatrixRangeToken& r );
     139             : 
     140             :     virtual sal_uInt8 GetByte() const SAL_OVERRIDE;
     141             :     virtual const ScMatrix* GetMatrix() const SAL_OVERRIDE;
     142             :     virtual ScMatrix* GetMatrix() SAL_OVERRIDE;
     143             :     virtual const ScComplexRefData* GetDoubleRef() const SAL_OVERRIDE;
     144             :     virtual ScComplexRefData* GetDoubleRef() SAL_OVERRIDE;
     145             :     virtual bool operator==( const formula::FormulaToken& rToken ) const SAL_OVERRIDE;
     146             :     virtual FormulaToken* Clone() const SAL_OVERRIDE;
     147             : };
     148             : 
     149             : class ScExternalSingleRefToken : public formula::FormulaToken
     150             : {
     151             :     sal_uInt16                  mnFileId;
     152             :     svl::SharedString           maTabName;
     153             :     ScSingleRefData             maSingleRef;
     154             : 
     155             :     ScExternalSingleRefToken(); // disabled
     156             : public:
     157             :     ScExternalSingleRefToken( sal_uInt16 nFileId, const svl::SharedString& rTabName, const ScSingleRefData& r );
     158             :     ScExternalSingleRefToken( const ScExternalSingleRefToken& r );
     159             :     virtual ~ScExternalSingleRefToken();
     160             : 
     161             :     virtual sal_uInt16                  GetIndex() const SAL_OVERRIDE;
     162             :     virtual svl::SharedString GetString() const SAL_OVERRIDE;
     163             :     virtual const ScSingleRefData*  GetSingleRef() const SAL_OVERRIDE;
     164             :     virtual ScSingleRefData*          GetSingleRef() SAL_OVERRIDE;
     165             :     virtual bool                operator==( const formula::FormulaToken& rToken ) const SAL_OVERRIDE;
     166         156 :     virtual FormulaToken*       Clone() const SAL_OVERRIDE { return new ScExternalSingleRefToken(*this); }
     167             : };
     168             : 
     169             : class ScExternalDoubleRefToken : public formula::FormulaToken
     170             : {
     171             :     sal_uInt16                  mnFileId;
     172             :     svl::SharedString           maTabName;  // name of the first sheet
     173             :     ScComplexRefData            maDoubleRef;
     174             : 
     175             :     ScExternalDoubleRefToken(); // disabled
     176             : public:
     177             :     ScExternalDoubleRefToken( sal_uInt16 nFileId, const svl::SharedString& rTabName, const ScComplexRefData& r );
     178             :     ScExternalDoubleRefToken( const ScExternalDoubleRefToken& r );
     179             :     virtual ~ScExternalDoubleRefToken();
     180             : 
     181             :     virtual sal_uInt16                 GetIndex() const SAL_OVERRIDE;
     182             :     virtual svl::SharedString GetString() const SAL_OVERRIDE;
     183             :     virtual const ScSingleRefData* GetSingleRef() const SAL_OVERRIDE;
     184             :     virtual ScSingleRefData*       GetSingleRef() SAL_OVERRIDE;
     185             :     virtual const ScSingleRefData* GetSingleRef2() const SAL_OVERRIDE;
     186             :     virtual ScSingleRefData*       GetSingleRef2() SAL_OVERRIDE;
     187             :     virtual const ScComplexRefData*    GetDoubleRef() const SAL_OVERRIDE;
     188             :     virtual ScComplexRefData*      GetDoubleRef() SAL_OVERRIDE;
     189             :     virtual bool                operator==( const formula::FormulaToken& rToken ) const SAL_OVERRIDE;
     190           2 :     virtual FormulaToken*       Clone() const SAL_OVERRIDE { return new ScExternalDoubleRefToken(*this); }
     191             : };
     192             : 
     193             : class ScExternalNameToken : public formula::FormulaToken
     194             : {
     195             :     sal_uInt16                  mnFileId;
     196             :     svl::SharedString           maName;
     197             : 
     198             :     ScExternalNameToken(); // disabled
     199             : public:
     200             :     ScExternalNameToken( sal_uInt16 nFileId, const svl::SharedString& rName );
     201             :     ScExternalNameToken( const ScExternalNameToken& r );
     202             :     virtual ~ScExternalNameToken();
     203             : 
     204             :     virtual sal_uInt16              GetIndex() const SAL_OVERRIDE;
     205             :     virtual svl::SharedString GetString() const SAL_OVERRIDE;
     206             :     virtual bool                operator==( const formula::FormulaToken& rToken ) const SAL_OVERRIDE;
     207           0 :     virtual FormulaToken*       Clone() const SAL_OVERRIDE { return new ScExternalNameToken(*this); }
     208             : };
     209             : 
     210             : // Only created from within the interpreter, no conversion from ScRawToken,
     211             : // never added to ScTokenArray!
     212             : class ScJumpMatrixToken : public formula::FormulaToken
     213             : {
     214             : private:
     215             :             ScJumpMatrix*       pJumpMatrix;
     216             : public:
     217           6 :                                 ScJumpMatrixToken( ScJumpMatrix* p ) :
     218           6 :                                     FormulaToken( formula::svJumpMatrix ), pJumpMatrix( p ) {}
     219           0 :                                 ScJumpMatrixToken( const ScJumpMatrixToken& r ) :
     220           0 :                                     FormulaToken( r ), pJumpMatrix( r.pJumpMatrix ) {}
     221             :     virtual                     ~ScJumpMatrixToken();
     222             :     virtual ScJumpMatrix*       GetJumpMatrix() const SAL_OVERRIDE;
     223             :     virtual bool                operator==( const formula::FormulaToken& rToken ) const SAL_OVERRIDE;
     224           0 :     virtual FormulaToken*       Clone() const SAL_OVERRIDE { return new ScJumpMatrixToken(*this); }
     225             : };
     226             : 
     227             : // Only created from within the interpreter, no conversion from ScRawToken,
     228             : // never added to ScTokenArray!
     229           0 : class ScRefListToken : public formula::FormulaToken
     230             : {
     231             : private:
     232             :             ScRefList           aRefList;
     233             : public:
     234           0 :                                 ScRefListToken() :
     235           0 :                                     FormulaToken( formula::svRefList ) {}
     236           0 :                                 ScRefListToken( const ScRefListToken & r ) :
     237           0 :                                     FormulaToken( r ), aRefList( r.aRefList ) {}
     238             :     virtual const ScRefList*    GetRefList() const SAL_OVERRIDE;
     239             :     virtual       ScRefList*    GetRefList() SAL_OVERRIDE;
     240             :     virtual bool                operator==( const formula::FormulaToken& rToken ) const SAL_OVERRIDE;
     241           0 :     virtual FormulaToken*       Clone() const SAL_OVERRIDE { return new ScRefListToken(*this); }
     242             : };
     243             : 
     244        2776 : class SC_DLLPUBLIC ScEmptyCellToken : public formula::FormulaToken
     245             : {
     246             :             bool                bInherited          :1;
     247             :             bool                bDisplayedAsString  :1;
     248             : public:
     249        1384 :     explicit                    ScEmptyCellToken( bool bInheritedP, bool bDisplayAsString ) :
     250             :                                     FormulaToken( formula::svEmptyCell ),
     251             :                                     bInherited( bInheritedP ),
     252        1384 :                                     bDisplayedAsString( bDisplayAsString ) {}
     253           4 :                                 ScEmptyCellToken( const ScEmptyCellToken& r ) :
     254             :                                     FormulaToken( r ),
     255             :                                     bInherited( r.bInherited ),
     256           4 :                                     bDisplayedAsString( r.bDisplayedAsString ) {}
     257           0 :             bool                IsInherited() const { return bInherited; }
     258        1372 :             bool                IsDisplayedAsString() const { return bDisplayedAsString; }
     259             :     virtual double              GetDouble() const SAL_OVERRIDE;
     260             :     virtual svl::SharedString GetString() const SAL_OVERRIDE;
     261             :     virtual bool                operator==( const formula::FormulaToken& rToken ) const SAL_OVERRIDE;
     262           4 :     virtual FormulaToken*       Clone() const SAL_OVERRIDE { return new ScEmptyCellToken(*this); }
     263             : };
     264             : 
     265             : /**  Transports the result from the interpreter to the formula cell. */
     266         262 : class SC_DLLPUBLIC ScMatrixCellResultToken : public formula::FormulaToken
     267             : {
     268             :     // No non-const access implemented, silence down unxsols4 complaining about
     269             :     // the public GetMatrix() hiding the one from FormulaToken.
     270             :     virtual ScMatrix*           GetMatrix() SAL_OVERRIDE;
     271             : 
     272             : protected:
     273             :     ScConstMatrixRef xMatrix;
     274             :     formula::FormulaConstTokenRef     xUpperLeft;
     275             : public:
     276             :     ScMatrixCellResultToken( const ScConstMatrixRef& pMat, formula::FormulaToken* pUL );
     277             :     ScMatrixCellResultToken( const ScMatrixCellResultToken& r );
     278             :     virtual double              GetDouble() const SAL_OVERRIDE;
     279             :     virtual svl::SharedString GetString() const SAL_OVERRIDE;
     280             :     virtual const ScMatrix*     GetMatrix() const SAL_OVERRIDE;
     281             :     virtual bool                operator==( const formula::FormulaToken& rToken ) const SAL_OVERRIDE;
     282             :     virtual FormulaToken*       Clone() const SAL_OVERRIDE;
     283        1850 :     formula::StackVar           GetUpperLeftType() const
     284             :                                     {
     285             :                                         return xUpperLeft ?
     286        1746 :                                             xUpperLeft->GetType() :
     287        3596 :                                             static_cast<formula::StackVar>(formula::svUnknown);
     288             :                                     }
     289         576 :     inline formula::FormulaConstTokenRef     GetUpperLeftToken() const   { return xUpperLeft; }
     290             :     void Assign( const ScMatrixCellResultToken & r );
     291             : };
     292             : 
     293             : /** Stores the matrix result at the formula cell, additionally the range the
     294             :     matrix formula occupies. */
     295         524 : class SC_DLLPUBLIC ScMatrixFormulaCellToken : public ScMatrixCellResultToken
     296             : {
     297             : private:
     298             :             SCROW               nRows;
     299             :             SCCOL               nCols;
     300             : public:
     301             :     ScMatrixFormulaCellToken( SCCOL nC, SCROW nR, const ScConstMatrixRef& pMat, formula::FormulaToken* pUL );
     302             :     ScMatrixFormulaCellToken( SCCOL nC, SCROW nR );
     303             :     ScMatrixFormulaCellToken( const ScMatrixFormulaCellToken& r );
     304             : 
     305             :     virtual bool                operator==( const formula::FormulaToken& rToken ) const SAL_OVERRIDE;
     306           0 :     virtual FormulaToken*       Clone() const SAL_OVERRIDE { return new ScMatrixFormulaCellToken(*this); }
     307          48 :             void                SetMatColsRows( SCCOL nC, SCROW nR )
     308             :                                     {
     309          48 :                                         nRows = nR;
     310          48 :                                         nCols = nC;
     311          48 :                                     }
     312          62 :             void                GetMatColsRows( SCCOL & nC, SCROW & nR ) const
     313             :                                     {
     314          62 :                                         nR = nRows;
     315          62 :                                         nC = nCols;
     316          62 :                                     }
     317          48 :             SCCOL               GetMatCols() const  { return nCols; }
     318          48 :             SCROW               GetMatRows() const  { return nRows; }
     319             : 
     320             :     /** Assign matrix result, keep matrix formula
     321             :         dimension. */
     322             :     void Assign( const ScMatrixCellResultToken & r );
     323             : 
     324             :                                 /** Assign any result, keep matrix formula
     325             :                                     dimension. If token is of type
     326             :                                     ScMatrixCellResultToken uses the
     327             :                                     appropriate Assign() call, other tokens
     328             :                                     are assigned to xUpperLeft and xMatrix will
     329             :                                     be assigned NULL. */
     330             :             void                Assign( const formula::FormulaToken & r );
     331             : 
     332             :                                 /** Modify xUpperLeft if formula::svDouble, or create
     333             :                                     new formula::FormulaDoubleToken if not set yet. Does
     334             :                                     nothing if xUpperLeft is of different type! */
     335             :             void                SetUpperLeftDouble( double f);
     336             : 
     337             :     /** Reset matrix and upper left, keep matrix
     338             :         formula dimension. */
     339             :     void ResetResult();
     340             : };
     341             : 
     342         488 : class SC_DLLPUBLIC ScHybridCellToken : public formula::FormulaToken
     343             : {
     344             : private:
     345             :     double mfDouble;
     346             :     svl::SharedString maString;
     347             :     OUString maFormula;
     348             : public:
     349             :     ScHybridCellToken(
     350             :         double f, const svl::SharedString & rStr, const OUString & rFormula );
     351             : 
     352          10 :     const OUString& GetFormula() const  { return maFormula; }
     353             :     virtual double GetDouble() const SAL_OVERRIDE;
     354             : 
     355             :     virtual svl::SharedString GetString() const SAL_OVERRIDE;
     356             :     virtual bool operator==( const formula::FormulaToken& rToken ) const SAL_OVERRIDE;
     357           0 :     virtual FormulaToken* Clone() const SAL_OVERRIDE { return new ScHybridCellToken(*this); }
     358             : };
     359             : 
     360             : // Simplify argument passing to RefUpdate methods with ScSingleRefToken or
     361             : // ScDoubleRefToken
     362             : class SingleDoubleRefModifier
     363             : {
     364             :     ScComplexRefData    aDub;
     365             :     ScSingleRefData*  pS;
     366             :     ScComplexRefData*   pD;
     367             : 
     368             :                 // not implemented, prevent usage
     369             :                 SingleDoubleRefModifier( const SingleDoubleRefModifier& );
     370             :         SingleDoubleRefModifier& operator=( const SingleDoubleRefModifier& );
     371             : 
     372             : public:
     373           0 :         SingleDoubleRefModifier( formula::FormulaToken& rT )
     374             :                     {
     375           0 :                         formula::StackVar eType = rT.GetType();
     376           0 :                         if ( eType == formula::svSingleRef || eType == formula::svExternalSingleRef )
     377             :                         {
     378           0 :                             pS = rT.GetSingleRef();
     379           0 :                             aDub.Ref1 = aDub.Ref2 = *pS;
     380           0 :                             pD = &aDub;
     381             :                         }
     382             :                         else
     383             :                         {
     384           0 :                             pS = 0;
     385           0 :                             pD = rT.GetDoubleRef();
     386             :                             // aDub intentionally not initialized, unnecessary
     387             :                             // because unused.
     388             :                         }
     389           0 :                     }
     390        1166 :                 SingleDoubleRefModifier( ScSingleRefData& rS )
     391             :                     {
     392        1166 :                         pS = &rS;
     393        1166 :                         aDub.Ref1 = aDub.Ref2 = *pS;
     394        1166 :                         pD = &aDub;
     395        1166 :                     }
     396        1166 :                 ~SingleDoubleRefModifier()
     397             :                     {
     398        1166 :                         if ( pS )
     399        1166 :                             *pS = (*pD).Ref1;
     400        1166 :                     }
     401        1166 :     inline  ScComplexRefData& Ref() { return *pD; }
     402             : };
     403             : 
     404             : class SingleDoubleRefProvider
     405             : {
     406             : public:
     407             : 
     408             :     const ScSingleRefData&    Ref1;
     409             :     const ScSingleRefData&    Ref2;
     410             : 
     411         336 :                 SingleDoubleRefProvider( const formula::FormulaToken& r )
     412         336 :                         : Ref1( *r.GetSingleRef() ),
     413         648 :                         Ref2( (r.GetType() == formula::svDoubleRef ||
     414         312 :                                     r.GetType() == formula::svExternalDoubleRef) ?
     415         696 :                                 r.GetDoubleRef()->Ref2 : Ref1 )
     416         336 :                     {}
     417             :                 SingleDoubleRefProvider( const ScSingleRefData& r )
     418             :                         : Ref1( r ), Ref2( r )
     419             :                     {}
     420             :                 SingleDoubleRefProvider( const ScComplexRefData& r )
     421             :                         : Ref1( r.Ref1 ), Ref2( r.Ref2 )
     422             :                     {}
     423         336 :                 ~SingleDoubleRefProvider()
     424         336 :                     {}
     425             : };
     426             : 
     427             : #endif
     428             : 
     429             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10