LCOV - code coverage report
Current view: top level - libreoffice/solver/unxlngi6.pro/inc/formula - token.hxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 70 106 66.0 %
Date: 2012-12-17 Functions: 49 73 67.1 %
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 FORMULA_TOKEN_HXX
      21             : #define FORMULA_TOKEN_HXX
      22             : 
      23             : #include <memory>
      24             : #include <string.h>
      25             : #include <vector>
      26             : #include "formula/opcode.hxx"
      27             : #include <tools/mempool.hxx>
      28             : #include "formula/IFunctionDescription.hxx"
      29             : #include "formula/formuladllapi.h"
      30             : 
      31             : #include <boost/intrusive_ptr.hpp>
      32             : 
      33             : namespace formula
      34             : {
      35             : 
      36             : enum StackVarEnum
      37             : {
      38             :     svByte,
      39             :     svDouble,
      40             :     svString,
      41             :     svSingleRef,
      42             :     svDoubleRef,
      43             :     svMatrix,
      44             :     svIndex,
      45             :     svJump,
      46             :     svExternal,                         // Byte + String
      47             :     svFAP,                              // FormulaAutoPilot only, ever exported
      48             :     svJumpMatrix,                       // 2003-07-02
      49             :     svRefList,                          // ocUnion result
      50             :     svEmptyCell,                        // Result is an empty cell, e.g. in LOOKUP()
      51             : 
      52             :     svMatrixCell,                       // Result is a matrix with bells and
      53             :                                         // whistles as needed for _the_ matrix
      54             :                                         // formula result.
      55             : 
      56             :     svHybridCell,                       // A temporary condition of a formula
      57             :                                         // cell during import, having a double
      58             :                                         // and/or string result and a formula
      59             :                                         // string to be compiled.
      60             : 
      61             :     svExternalSingleRef,
      62             :     svExternalDoubleRef,
      63             :     svExternalName,
      64             :     svSubroutine,                       // A token with a subroutine token array.
      65             :     svError,                            // error token
      66             :     svMissing = 0x70,                   // 0 or ""
      67             :     svSep,                              // separator, ocSep, ocOpen, ocClose
      68             :     svUnknown                           // unknown StackType
      69             : };
      70             : 
      71             : #ifndef DBG_UTIL
      72             : // save memory since compilers tend to int an enum
      73             : typedef sal_uInt8 StackVar;
      74             : #else
      75             : // have enum names in debugger
      76             : typedef StackVarEnum StackVar;
      77             : #endif
      78             : 
      79             : 
      80             : class FormulaToken;
      81             : typedef ::boost::intrusive_ptr<FormulaToken>        FormulaTokenRef;
      82             : typedef ::boost::intrusive_ptr<const FormulaToken>  FormulaConstTokenRef;
      83             : 
      84             : class FormulaTokenArray;
      85             : 
      86             : class FORMULA_DLLPUBLIC FormulaToken : public IFormulaToken
      87             : {
      88             :     OpCode                      eOp;
      89             :     // not implemented, prevent usage
      90             :             FormulaToken();
      91             :             FormulaToken&            operator=( const FormulaToken& );
      92             : protected:
      93             : 
      94             :             const StackVar      eType;          // type of data
      95             :             mutable sal_uInt16  nRefCnt;        // reference count
      96             : 
      97             : public:
      98       87364 :                                 FormulaToken( StackVar eTypeP,OpCode e = ocPush ) :
      99       87364 :                                     eOp(e), eType( eTypeP ), nRefCnt(0) {}
     100        2882 :                                 FormulaToken( const FormulaToken& r ) : IFormulaToken(),
     101        2882 :                                     eOp(r.eOp), eType( r.eType ), nRefCnt(0) {}
     102             : 
     103             :     virtual                     ~FormulaToken();
     104             : 
     105       87362 :     inline  void                Delete()                { delete this; }
     106      361764 :     inline  StackVar            GetType() const         { return eType; }
     107             :             bool                IsFunction() const; // pure functions, no operators
     108             :             bool                IsExternalRef() const;
     109             :             sal_uInt8           GetParamCount() const;
     110      343908 :     inline  void                IncRef() const          { nRefCnt++; }
     111      341964 :     inline  void                DecRef() const
     112             :                                     {
     113      341964 :                                         if (!--nRefCnt)
     114       87362 :                                             const_cast<FormulaToken*>(this)->Delete();
     115      341964 :                                     }
     116        6772 :     inline  sal_uInt16          GetRef() const          { return nRefCnt; }
     117     1299732 :     inline OpCode               GetOpCode() const       { return eOp; }
     118             : 
     119             :     /**
     120             :         Dummy methods to avoid switches and casts where possible,
     121             :         the real token classes have to overload the appropriate method[s].
     122             :         The only methods valid anytime if not overloaded are:
     123             : 
     124             :         - GetByte() since this represents the count of parameters to a function
     125             :           which of course is 0 on non-functions. FormulaByteToken and ScExternal do
     126             :           overload it.
     127             : 
     128             :         - HasForceArray() since also this is only used for operators and
     129             :           functions and is 0 for other tokens.
     130             : 
     131             :         Any other non-overloaded method pops up an assertion.
     132             :      */
     133             : 
     134             :     virtual sal_uInt8           GetByte() const;
     135             :     virtual void                SetByte( sal_uInt8 n );
     136             :     virtual bool                HasForceArray() const;
     137             :     virtual void                SetForceArray( bool b );
     138             :     virtual double              GetDouble() const;
     139             :     virtual double&             GetDoubleAsReference();
     140             :     virtual const String&       GetString() const;
     141             :     virtual sal_uInt16          GetIndex() const;
     142             :     virtual void                SetIndex( sal_uInt16 n );
     143             :     virtual bool                IsGlobal() const;
     144             :     virtual void                SetGlobal( bool b );
     145             :     virtual short*              GetJump() const;
     146             :     virtual const String&       GetExternal() const;
     147             :     virtual FormulaToken*       GetFAPOrigToken() const;
     148             :     virtual sal_uInt16          GetError() const;
     149             :     virtual void                SetError( sal_uInt16 );
     150             : 
     151         288 :     virtual FormulaToken*       Clone() const { return new FormulaToken(*this); }
     152             : 
     153             :     virtual bool                Is3DRef() const;    // reference with 3D flag set
     154             :     virtual bool                TextEqual( const formula::FormulaToken& rToken ) const;
     155             :     virtual bool                operator==( const FormulaToken& rToken ) const;
     156             : 
     157           0 :     virtual bool isFunction() const
     158             :     {
     159           0 :         return IsFunction();
     160             :     }
     161             : 
     162           0 :     virtual sal_uInt32 getArgumentCount() const
     163             :     {
     164           0 :         return GetParamCount();
     165             :     }
     166             : 
     167             :     /** This is dirty and only the compiler should use it! */
     168           0 :     struct PrivateAccess { friend class FormulaCompiler; private: PrivateAccess() { }  };
     169           0 :     inline  void                NewOpCode( OpCode e, const PrivateAccess&  ) { eOp = e; }
     170             : 
     171             :     static  size_t              GetStrLenBytes( xub_StrLen nLen )
     172             :                                     { return nLen * sizeof(sal_Unicode); }
     173             :     static  size_t              GetStrLenBytes( const String& rStr )
     174             :                                     { return GetStrLenBytes( rStr.Len() ); }
     175             : };
     176             : 
     177      131494 : inline void intrusive_ptr_add_ref(const FormulaToken* p)
     178             : {
     179      131494 :     p->IncRef();
     180      131494 : }
     181             : 
     182      131450 : inline void intrusive_ptr_release(const FormulaToken* p)
     183             : {
     184      131450 :     p->DecRef();
     185      131450 : }
     186             : 
     187       50796 : class FORMULA_DLLPUBLIC FormulaByteToken : public FormulaToken
     188             : {
     189             : private:
     190             :             sal_uInt8           nByte;
     191             :             bool                bHasForceArray;
     192             : protected:
     193         832 :                                 FormulaByteToken( OpCode e, sal_uInt8 n, StackVar v, bool b ) :
     194             :                                     FormulaToken( v,e ), nByte( n ),
     195         832 :                                     bHasForceArray( b ) {}
     196             : public:
     197       17642 :                                 FormulaByteToken( OpCode e, sal_uInt8 n, bool b ) :
     198             :                                     FormulaToken( svByte,e ), nByte( n ),
     199       17642 :                                     bHasForceArray( b ) {}
     200           4 :                                 FormulaByteToken( OpCode e, sal_uInt8 n ) :
     201             :                                     FormulaToken( svByte,e ), nByte( n ),
     202           4 :                                     bHasForceArray( false ) {}
     203        7968 :                                 FormulaByteToken( OpCode e ) :
     204             :                                     FormulaToken( svByte,e ), nByte( 0 ),
     205        7968 :                                     bHasForceArray( false ) {}
     206         390 :                                 FormulaByteToken( const FormulaByteToken& r ) :
     207             :                                     FormulaToken( r ), nByte( r.nByte ),
     208         390 :                                     bHasForceArray( r.bHasForceArray ) {}
     209             : 
     210         384 :     virtual FormulaToken*       Clone() const { return new FormulaByteToken(*this); }
     211             :     virtual sal_uInt8           GetByte() const;
     212             :     virtual void                SetByte( sal_uInt8 n );
     213             :     virtual bool                HasForceArray() const;
     214             :     virtual void                SetForceArray( bool b );
     215             :     virtual bool                operator==( const FormulaToken& rToken ) const;
     216             : 
     217             :     DECL_FIXEDMEMPOOL_NEWDEL_DLL( FormulaByteToken )
     218             : };
     219             : 
     220             : 
     221             : // A special token for the FormulaAutoPilot only. Keeps a reference pointer of
     222             : // the token of which it was created for comparison.
     223           0 : class FORMULA_DLLPUBLIC FormulaFAPToken : public FormulaByteToken
     224             : {
     225             : private:
     226             :             FormulaTokenRef     pOrigToken;
     227             : public:
     228           0 :                                 FormulaFAPToken( OpCode e, sal_uInt8 n, FormulaToken* p ) :
     229             :                                     FormulaByteToken( e, n, svFAP, false ),
     230           0 :                                     pOrigToken( p ) {}
     231           0 :                                 FormulaFAPToken( const FormulaFAPToken& r ) :
     232           0 :                                     FormulaByteToken( r ), pOrigToken( r.pOrigToken ) {}
     233             : 
     234           0 :     virtual FormulaToken*       Clone() const { return new FormulaFAPToken(*this); }
     235             :     virtual FormulaToken*       GetFAPOrigToken() const;
     236             :     virtual bool                operator==( const FormulaToken& rToken ) const;
     237             : };
     238             : 
     239       29816 : class FORMULA_DLLPUBLIC FormulaDoubleToken : public FormulaToken
     240             : {
     241             : private:
     242             :             double              fDouble;
     243             : public:
     244       14836 :                                 FormulaDoubleToken( double f ) :
     245       14836 :                                     FormulaToken( svDouble ), fDouble( f ) {}
     246         128 :                                 FormulaDoubleToken( const FormulaDoubleToken& r ) :
     247         128 :                                     FormulaToken( r ), fDouble( r.fDouble ) {}
     248             : 
     249         128 :     virtual FormulaToken*       Clone() const { return new FormulaDoubleToken(*this); }
     250             :     virtual double              GetDouble() const;
     251             :     virtual double&             GetDoubleAsReference();
     252             :     virtual bool                operator==( const FormulaToken& rToken ) const;
     253             : 
     254             :     DECL_FIXEDMEMPOOL_NEWDEL_DLL( FormulaDoubleToken )
     255             : };
     256             : 
     257             : 
     258       11260 : class FORMULA_DLLPUBLIC FormulaStringToken : public FormulaToken
     259             : {
     260             : private:
     261             :             String              aString;
     262             : public:
     263        5716 :                                 FormulaStringToken( const String& r ) :
     264        5716 :                                     FormulaToken( svString ), aString( r ) {}
     265          22 :                                 FormulaStringToken( const FormulaStringToken& r ) :
     266          22 :                                     FormulaToken( r ), aString( r.aString ) {}
     267             : 
     268          22 :     virtual FormulaToken*       Clone() const { return new FormulaStringToken(*this); }
     269             :     virtual const String&       GetString() const;
     270             :     virtual bool                operator==( const FormulaToken& rToken ) const;
     271             : 
     272             :     DECL_FIXEDMEMPOOL_NEWDEL_DLL( FormulaStringToken )
     273             : };
     274             : 
     275             : 
     276             : /** Identical to FormulaStringToken, but with explicit OpCode instead of implicit
     277             :     ocPush, and an optional sal_uInt8 for ocBad tokens. */
     278        1608 : class FORMULA_DLLPUBLIC FormulaStringOpToken : public FormulaByteToken
     279             : {
     280             : private:
     281             :             String              aString;
     282             : public:
     283         832 :                                 FormulaStringOpToken( OpCode e, const String& r ) :
     284         832 :                                     FormulaByteToken( e, 0, svString, false ), aString( r ) {}
     285           6 :                                 FormulaStringOpToken( const FormulaStringOpToken& r ) :
     286           6 :                                     FormulaByteToken( r ), aString( r.aString ) {}
     287             : 
     288           6 :     virtual FormulaToken*       Clone() const { return new FormulaStringOpToken(*this); }
     289             :     virtual const String&       GetString() const;
     290             :     virtual bool                operator==( const FormulaToken& rToken ) const;
     291             : };
     292             : 
     293         664 : class FORMULA_DLLPUBLIC FormulaIndexToken : public FormulaToken
     294             : {
     295             : private:
     296             :             sal_uInt16          nIndex;
     297             :             bool                mbGlobal;
     298             : public:
     299         308 :                                 FormulaIndexToken( OpCode e, sal_uInt16 n, bool bGlobal = true ) :
     300         308 :                                     FormulaToken(  svIndex, e ), nIndex( n ), mbGlobal( bGlobal ) {}
     301          42 :                                 FormulaIndexToken( const FormulaIndexToken& r ) :
     302          42 :                                     FormulaToken( r ), nIndex( r.nIndex ), mbGlobal( r.mbGlobal ) {}
     303             : 
     304          42 :     virtual FormulaToken*       Clone() const { return new FormulaIndexToken(*this); }
     305             :     virtual sal_uInt16          GetIndex() const;
     306             :     virtual void                SetIndex( sal_uInt16 n );
     307             :     virtual bool                IsGlobal() const;
     308             :     virtual void                SetGlobal( bool b );
     309             :     virtual bool                operator==( const FormulaToken& rToken ) const;
     310             : };
     311             : 
     312             : 
     313           4 : class FORMULA_DLLPUBLIC FormulaExternalToken : public FormulaToken
     314             : {
     315             : private:
     316             :             String              aExternal;
     317             :             sal_uInt8           nByte;
     318             : public:
     319           0 :                                 FormulaExternalToken( OpCode e, sal_uInt8 n, const String& r ) :
     320             :                                     FormulaToken( svExternal, e ), aExternal( r ),
     321           0 :                                     nByte( n ) {}
     322           2 :                                 FormulaExternalToken( OpCode e, const String& r ) :
     323             :                                     FormulaToken(svExternal, e ), aExternal( r ),
     324           2 :                                     nByte( 0 ) {}
     325           0 :                                 FormulaExternalToken( const FormulaExternalToken& r ) :
     326             :                                     FormulaToken( r ), aExternal( r.aExternal ),
     327           0 :                                     nByte( r.nByte ) {}
     328             : 
     329           0 :     virtual FormulaToken*       Clone() const { return new FormulaExternalToken(*this); }
     330             :     virtual const String&       GetExternal() const;
     331             :     virtual sal_uInt8           GetByte() const;
     332             :     virtual void                SetByte( sal_uInt8 n );
     333             :     virtual bool                operator==( const FormulaToken& rToken ) const;
     334             : };
     335             : 
     336             : 
     337          48 : class FORMULA_DLLPUBLIC FormulaMissingToken : public FormulaToken
     338             : {
     339             : public:
     340          24 :                                 FormulaMissingToken() :
     341          24 :                                     FormulaToken( svMissing,ocMissing ) {}
     342           0 :                                 FormulaMissingToken( const FormulaMissingToken& r ) :
     343           0 :                                     FormulaToken( r ) {}
     344             : 
     345           0 :     virtual FormulaToken*       Clone() const { return new FormulaMissingToken(*this); }
     346             :     virtual double              GetDouble() const;
     347             :     virtual const String&       GetString() const;
     348             :     virtual bool                operator==( const FormulaToken& rToken ) const;
     349             : };
     350             : 
     351             : class FORMULA_DLLPUBLIC FormulaJumpToken : public FormulaToken
     352             : {
     353             : private:
     354             :             short*              pJump;
     355             : public:
     356          40 :                                 FormulaJumpToken( OpCode e, short* p ) :
     357          40 :                                     FormulaToken( formula::svJump , e)
     358             :                                 {
     359          40 :                                     pJump = new short[ p[0] + 1 ];
     360          40 :                                     memcpy( pJump, p, (p[0] + 1) * sizeof(short) );
     361          40 :                                 }
     362           0 :                                 FormulaJumpToken( const FormulaJumpToken& r ) :
     363           0 :                                     FormulaToken( r )
     364             :                                 {
     365           0 :                                     pJump = new short[ r.pJump[0] + 1 ];
     366           0 :                                     memcpy( pJump, r.pJump, (r.pJump[0] + 1) * sizeof(short) );
     367           0 :                                 }
     368             :     virtual                     ~FormulaJumpToken();
     369             :     virtual short*              GetJump() const;
     370             :     virtual bool                operator==( const formula::FormulaToken& rToken ) const;
     371           0 :     virtual FormulaToken*       Clone() const { return new FormulaJumpToken(*this); }
     372             : };
     373             : 
     374             : 
     375             : class FORMULA_DLLPUBLIC FormulaSubroutineToken : public FormulaToken
     376             : {
     377             : public:
     378             :     /** Takes ownership of pArray and deletes it upon destruction! */
     379             :                                 FormulaSubroutineToken( const FormulaTokenArray* pArray ) :
     380             :                                     FormulaToken( svSubroutine, ocCall ), mpArray( pArray) {}
     381             :                                 FormulaSubroutineToken( const FormulaSubroutineToken& r );
     382             :     virtual                     ~FormulaSubroutineToken();
     383           0 :     virtual FormulaToken*       Clone() const { return new FormulaSubroutineToken(*this); }
     384             :     virtual bool                operator==( const FormulaToken& rToken ) const;
     385             : 
     386             : private:
     387             :     const FormulaTokenArray*    mpArray;
     388             : };
     389             : 
     390             : 
     391           0 : class FORMULA_DLLPUBLIC FormulaUnknownToken : public FormulaToken
     392             : {
     393             : public:
     394           0 :                                 FormulaUnknownToken( OpCode e ) :
     395           0 :                                     FormulaToken( svUnknown, e ) {}
     396           0 :                                 FormulaUnknownToken( const FormulaUnknownToken& r ) :
     397           0 :                                     FormulaToken( r ) {}
     398             : 
     399           0 :     virtual FormulaToken*       Clone() const { return new FormulaUnknownToken(*this); }
     400             :     virtual bool                operator==( const FormulaToken& rToken ) const;
     401             : };
     402             : 
     403             : 
     404         284 : class FORMULA_DLLPUBLIC FormulaErrorToken : public FormulaToken
     405             : {
     406             :             sal_uInt16          nError;
     407             : public:
     408         142 :                                 FormulaErrorToken( sal_uInt16 nErr ) :
     409         142 :                                     FormulaToken( svError ), nError( nErr) {}
     410           0 :                                 FormulaErrorToken( const FormulaErrorToken& r ) :
     411           0 :                                     FormulaToken( r ), nError( r.nError) {}
     412             : 
     413           0 :     virtual FormulaToken*       Clone() const { return new FormulaErrorToken(*this); }
     414             :     virtual sal_uInt16          GetError() const;
     415             :     virtual void                SetError( sal_uInt16 nErr );
     416             :     virtual bool                operator==( const FormulaToken& rToken ) const;
     417             : };
     418             : 
     419             : // =============================================================================
     420             : } // formula
     421             : // =============================================================================
     422             : 
     423             : #endif
     424             : 
     425             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10