bump product version to 4.1.6.2
[LibreOffice.git] / include / formula / token.hxx
blobc7fa0cd44ee324b80521b5b3a6272ed38f71d4a3
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 FORMULA_TOKEN_HXX
21 #define FORMULA_TOKEN_HXX
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 #include "formula/types.hxx"
32 namespace formula
35 enum StackVarEnum
37 svByte,
38 svDouble,
39 svString,
40 svSingleRef,
41 svDoubleRef,
42 svMatrix,
43 svIndex,
44 svJump,
45 svExternal, // Byte + String
46 svFAP, // FormulaAutoPilot only, ever exported
47 svJumpMatrix, // 2003-07-02
48 svRefList, // ocUnion result
49 svEmptyCell, // Result is an empty cell, e.g. in LOOKUP()
51 svMatrixCell, // Result is a matrix with bells and
52 // whistles as needed for _the_ matrix
53 // formula result.
55 svHybridCell, // A temporary condition of a formula
56 // cell during import, having a double
57 // and/or string result and a formula
58 // string to be compiled.
60 svHybridValueCell, // A temporary formula cell with an value
61 // and possibily a string representation
63 svExternalSingleRef,
64 svExternalDoubleRef,
65 svExternalName,
66 svSingleVectorRef,
67 svDoubleVectorRef,
68 svSubroutine, // A token with a subroutine token array.
69 svError, // error token
70 svMissing = 0x70, // 0 or ""
71 svSep, // separator, ocSep, ocOpen, ocClose
72 svUnknown // unknown StackType
75 #ifndef DBG_UTIL
76 // save memory since compilers tend to int an enum
77 typedef sal_uInt8 StackVar;
78 #else
79 // have enum names in debugger
80 typedef StackVarEnum StackVar;
81 #endif
83 class FormulaTokenArray;
85 class FORMULA_DLLPUBLIC FormulaToken : public IFormulaToken
87 OpCode eOp;
88 // not implemented, prevent usage
89 FormulaToken();
90 FormulaToken& operator=( const FormulaToken& );
91 protected:
93 const StackVar eType; // type of data
94 mutable sal_uInt16 nRefCnt; // reference count
96 public:
97 FormulaToken( StackVar eTypeP,OpCode e = ocPush ) :
98 eOp(e), eType( eTypeP ), nRefCnt(0) {}
99 FormulaToken( const FormulaToken& r ) : IFormulaToken(),
100 eOp(r.eOp), eType( r.eType ), nRefCnt(0) {}
102 virtual ~FormulaToken();
104 inline void Delete() { delete this; }
105 inline StackVar GetType() const { return eType; }
106 bool IsFunction() const; // pure functions, no operators
107 bool IsExternalRef() const;
108 sal_uInt8 GetParamCount() const;
109 inline void IncRef() const { nRefCnt++; }
110 inline void DecRef() const
112 if (!--nRefCnt)
113 const_cast<FormulaToken*>(this)->Delete();
115 inline sal_uInt16 GetRef() const { return nRefCnt; }
116 inline OpCode GetOpCode() const { return eOp; }
119 Dummy methods to avoid switches and casts where possible,
120 the real token classes have to overload the appropriate method[s].
121 The only methods valid anytime if not overloaded are:
123 - GetByte() since this represents the count of parameters to a function
124 which of course is 0 on non-functions. FormulaByteToken and ScExternal do
125 overload it.
127 - HasForceArray() since also this is only used for operators and
128 functions and is 0 for other tokens.
130 Any other non-overloaded method pops up an assertion.
133 virtual sal_uInt8 GetByte() const;
134 virtual void SetByte( sal_uInt8 n );
135 virtual bool HasForceArray() const;
136 virtual void SetForceArray( bool b );
137 virtual double GetDouble() const;
138 virtual double& GetDoubleAsReference();
139 virtual const String& GetString() const;
140 virtual sal_uInt16 GetIndex() const;
141 virtual void SetIndex( sal_uInt16 n );
142 virtual bool IsGlobal() const;
143 virtual void SetGlobal( bool b );
144 virtual short* GetJump() const;
145 virtual const String& GetExternal() const;
146 virtual FormulaToken* GetFAPOrigToken() const;
147 virtual sal_uInt16 GetError() const;
148 virtual void SetError( sal_uInt16 );
150 virtual FormulaToken* Clone() const { return new FormulaToken(*this); }
152 virtual bool Is3DRef() const; // reference with 3D flag set
153 virtual bool TextEqual( const formula::FormulaToken& rToken ) const;
154 virtual bool operator==( const FormulaToken& rToken ) const;
156 virtual bool isFunction() const
158 return IsFunction();
161 virtual sal_uInt32 getArgumentCount() const
163 return GetParamCount();
166 /** This is dirty and only the compiler should use it! */
167 struct PrivateAccess { friend class FormulaCompiler; private: PrivateAccess() { } };
168 inline void NewOpCode( OpCode e, const PrivateAccess& ) { eOp = e; }
170 static size_t GetStrLenBytes( xub_StrLen nLen )
171 { return nLen * sizeof(sal_Unicode); }
172 static size_t GetStrLenBytes( const String& rStr )
173 { return GetStrLenBytes( rStr.Len() ); }
176 inline void intrusive_ptr_add_ref(const FormulaToken* p)
178 p->IncRef();
181 inline void intrusive_ptr_release(const FormulaToken* p)
183 p->DecRef();
186 class FORMULA_DLLPUBLIC FormulaByteToken : public FormulaToken
188 private:
189 sal_uInt8 nByte;
190 bool bHasForceArray;
191 protected:
192 FormulaByteToken( OpCode e, sal_uInt8 n, StackVar v, bool b ) :
193 FormulaToken( v,e ), nByte( n ),
194 bHasForceArray( b ) {}
195 public:
196 FormulaByteToken( OpCode e, sal_uInt8 n, bool b ) :
197 FormulaToken( svByte,e ), nByte( n ),
198 bHasForceArray( b ) {}
199 FormulaByteToken( OpCode e, sal_uInt8 n ) :
200 FormulaToken( svByte,e ), nByte( n ),
201 bHasForceArray( false ) {}
202 FormulaByteToken( OpCode e ) :
203 FormulaToken( svByte,e ), nByte( 0 ),
204 bHasForceArray( false ) {}
205 FormulaByteToken( const FormulaByteToken& r ) :
206 FormulaToken( r ), nByte( r.nByte ),
207 bHasForceArray( r.bHasForceArray ) {}
209 virtual FormulaToken* Clone() const { return new FormulaByteToken(*this); }
210 virtual sal_uInt8 GetByte() const;
211 virtual void SetByte( sal_uInt8 n );
212 virtual bool HasForceArray() const;
213 virtual void SetForceArray( bool b );
214 virtual bool operator==( const FormulaToken& rToken ) const;
216 DECL_FIXEDMEMPOOL_NEWDEL_DLL( FormulaByteToken )
220 // A special token for the FormulaAutoPilot only. Keeps a reference pointer of
221 // the token of which it was created for comparison.
222 class FORMULA_DLLPUBLIC FormulaFAPToken : public FormulaByteToken
224 private:
225 FormulaTokenRef pOrigToken;
226 public:
227 FormulaFAPToken( OpCode e, sal_uInt8 n, FormulaToken* p ) :
228 FormulaByteToken( e, n, svFAP, false ),
229 pOrigToken( p ) {}
230 FormulaFAPToken( const FormulaFAPToken& r ) :
231 FormulaByteToken( r ), pOrigToken( r.pOrigToken ) {}
233 virtual FormulaToken* Clone() const { return new FormulaFAPToken(*this); }
234 virtual FormulaToken* GetFAPOrigToken() const;
235 virtual bool operator==( const FormulaToken& rToken ) const;
238 class FORMULA_DLLPUBLIC FormulaDoubleToken : public FormulaToken
240 private:
241 double fDouble;
242 public:
243 FormulaDoubleToken( double f ) :
244 FormulaToken( svDouble ), fDouble( f ) {}
245 FormulaDoubleToken( const FormulaDoubleToken& r ) :
246 FormulaToken( r ), fDouble( r.fDouble ) {}
248 virtual FormulaToken* Clone() const { return new FormulaDoubleToken(*this); }
249 virtual double GetDouble() const;
250 virtual double& GetDoubleAsReference();
251 virtual bool operator==( const FormulaToken& rToken ) const;
253 DECL_FIXEDMEMPOOL_NEWDEL_DLL( FormulaDoubleToken )
257 class FORMULA_DLLPUBLIC FormulaStringToken : public FormulaToken
259 private:
260 String aString;
261 public:
262 FormulaStringToken( const String& r ) :
263 FormulaToken( svString ), aString( r ) {}
264 FormulaStringToken( const FormulaStringToken& r ) :
265 FormulaToken( r ), aString( r.aString ) {}
267 virtual FormulaToken* Clone() const { return new FormulaStringToken(*this); }
268 virtual const String& GetString() const;
269 virtual bool operator==( const FormulaToken& rToken ) const;
271 DECL_FIXEDMEMPOOL_NEWDEL_DLL( FormulaStringToken )
275 /** Identical to FormulaStringToken, but with explicit OpCode instead of implicit
276 ocPush, and an optional sal_uInt8 for ocBad tokens. */
277 class FORMULA_DLLPUBLIC FormulaStringOpToken : public FormulaByteToken
279 private:
280 String aString;
281 public:
282 FormulaStringOpToken( OpCode e, const String& r ) :
283 FormulaByteToken( e, 0, svString, false ), aString( r ) {}
284 FormulaStringOpToken( const FormulaStringOpToken& r ) :
285 FormulaByteToken( r ), aString( r.aString ) {}
287 virtual FormulaToken* Clone() const { return new FormulaStringOpToken(*this); }
288 virtual const String& GetString() const;
289 virtual bool operator==( const FormulaToken& rToken ) const;
292 class FORMULA_DLLPUBLIC FormulaIndexToken : public FormulaToken
294 private:
295 sal_uInt16 nIndex;
296 bool mbGlobal;
297 public:
298 FormulaIndexToken( OpCode e, sal_uInt16 n, bool bGlobal = true ) :
299 FormulaToken( svIndex, e ), nIndex( n ), mbGlobal( bGlobal ) {}
300 FormulaIndexToken( const FormulaIndexToken& r ) :
301 FormulaToken( r ), nIndex( r.nIndex ), mbGlobal( r.mbGlobal ) {}
303 virtual FormulaToken* Clone() const { return new FormulaIndexToken(*this); }
304 virtual sal_uInt16 GetIndex() const;
305 virtual void SetIndex( sal_uInt16 n );
306 virtual bool IsGlobal() const;
307 virtual void SetGlobal( bool b );
308 virtual bool operator==( const FormulaToken& rToken ) const;
312 class FORMULA_DLLPUBLIC FormulaExternalToken : public FormulaToken
314 private:
315 String aExternal;
316 sal_uInt8 nByte;
317 public:
318 FormulaExternalToken( OpCode e, sal_uInt8 n, const String& r ) :
319 FormulaToken( svExternal, e ), aExternal( r ),
320 nByte( n ) {}
321 FormulaExternalToken( OpCode e, const String& r ) :
322 FormulaToken(svExternal, e ), aExternal( r ),
323 nByte( 0 ) {}
324 FormulaExternalToken( const FormulaExternalToken& r ) :
325 FormulaToken( r ), aExternal( r.aExternal ),
326 nByte( r.nByte ) {}
328 virtual FormulaToken* Clone() const { return new FormulaExternalToken(*this); }
329 virtual const String& GetExternal() const;
330 virtual sal_uInt8 GetByte() const;
331 virtual void SetByte( sal_uInt8 n );
332 virtual bool operator==( const FormulaToken& rToken ) const;
336 class FORMULA_DLLPUBLIC FormulaMissingToken : public FormulaToken
338 public:
339 FormulaMissingToken() :
340 FormulaToken( svMissing,ocMissing ) {}
341 FormulaMissingToken( const FormulaMissingToken& r ) :
342 FormulaToken( r ) {}
344 virtual FormulaToken* Clone() const { return new FormulaMissingToken(*this); }
345 virtual double GetDouble() const;
346 virtual const String& GetString() const;
347 virtual bool operator==( const FormulaToken& rToken ) const;
350 class FORMULA_DLLPUBLIC FormulaJumpToken : public FormulaToken
352 private:
353 short* pJump;
354 public:
355 FormulaJumpToken( OpCode e, short* p ) :
356 FormulaToken( formula::svJump , e)
358 pJump = new short[ p[0] + 1 ];
359 memcpy( pJump, p, (p[0] + 1) * sizeof(short) );
361 FormulaJumpToken( const FormulaJumpToken& r ) :
362 FormulaToken( r )
364 pJump = new short[ r.pJump[0] + 1 ];
365 memcpy( pJump, r.pJump, (r.pJump[0] + 1) * sizeof(short) );
367 virtual ~FormulaJumpToken();
368 virtual short* GetJump() const;
369 virtual bool operator==( const formula::FormulaToken& rToken ) const;
370 virtual FormulaToken* Clone() const { return new FormulaJumpToken(*this); }
374 class FORMULA_DLLPUBLIC FormulaSubroutineToken : public FormulaToken
376 public:
377 /** Takes ownership of pArray and deletes it upon destruction! */
378 FormulaSubroutineToken( const FormulaTokenArray* pArray ) :
379 FormulaToken( svSubroutine, ocCall ), mpArray( pArray) {}
380 FormulaSubroutineToken( const FormulaSubroutineToken& r );
381 virtual ~FormulaSubroutineToken();
382 virtual FormulaToken* Clone() const { return new FormulaSubroutineToken(*this); }
383 virtual bool operator==( const FormulaToken& rToken ) const;
385 private:
386 const FormulaTokenArray* mpArray;
390 class FORMULA_DLLPUBLIC FormulaUnknownToken : public FormulaToken
392 public:
393 FormulaUnknownToken( OpCode e ) :
394 FormulaToken( svUnknown, e ) {}
395 FormulaUnknownToken( const FormulaUnknownToken& r ) :
396 FormulaToken( r ) {}
398 virtual FormulaToken* Clone() const { return new FormulaUnknownToken(*this); }
399 virtual bool operator==( const FormulaToken& rToken ) const;
403 class FORMULA_DLLPUBLIC FormulaErrorToken : public FormulaToken
405 sal_uInt16 nError;
406 public:
407 FormulaErrorToken( sal_uInt16 nErr ) :
408 FormulaToken( svError ), nError( nErr) {}
409 FormulaErrorToken( const FormulaErrorToken& r ) :
410 FormulaToken( r ), nError( r.nError) {}
412 virtual FormulaToken* Clone() const { return new FormulaErrorToken(*this); }
413 virtual sal_uInt16 GetError() const;
414 virtual void SetError( sal_uInt16 nErr );
415 virtual bool operator==( const FormulaToken& rToken ) const;
418 // =============================================================================
419 } // formula
420 // =============================================================================
422 #endif
424 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */