Update ooo320-m1
[ooovba.git] / sc / source / filter / inc / tokstack.hxx
blobe11c2807de99c492b4129d53f9183c0eddfbedee
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: tokstack.hxx,v $
10 * $Revision: 1.14.32.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef SC_TOKSTACK_HXX
32 #define SC_TOKSTACK_HXX
34 #include <string.h>
35 #include <tools/debug.hxx>
36 #include "compiler.hxx"
37 #include "tokenarray.hxx"
39 #include <vector>
41 typedef OpCode DefTokenId;
42 // in PRODUCT version: ambiguity between OpCode (being USHORT) and UINT16
43 // Unfortunately a typedef is just a dumb alias and not a real type ...
44 //typedef UINT16 TokenId;
45 struct TokenId
47 UINT16 nId;
49 TokenId() : nId( 0 ) {}
50 TokenId( UINT16 n ) : nId( n ) {}
51 TokenId( const TokenId& r ) : nId( r.nId ) {}
52 inline TokenId& operator =( const TokenId& r ) { nId = r.nId; return *this; }
53 inline TokenId& operator =( UINT16 n ) { nId = n; return *this; }
54 inline operator UINT16&() { return nId; }
55 inline operator const UINT16&() const { return nId; }
56 inline BOOL operator <( UINT16 n ) const { return nId < n; }
57 inline BOOL operator >( UINT16 n ) const { return nId > n; }
58 inline BOOL operator <=( UINT16 n ) const { return nId <= n; }
59 inline BOOL operator >=( UINT16 n ) const { return nId >= n; }
60 inline BOOL operator ==( UINT16 n ) const { return nId == n; }
61 inline BOOL operator !=( UINT16 n ) const { return nId != n; }
65 //------------------------------------------------------------------------
66 struct ScComplexRefData;
67 class TokenStack;
68 class ScToken;
71 enum E_TYPE
73 T_Id, // Id-Folge
74 T_Str, // String
75 T_D, // Double
76 T_Err, // Error code
77 T_RefC, // Cell Reference
78 T_RefA, // Area Reference
79 T_RN, // Range Name
80 T_Ext, // irgendwas Unbekanntes mit Funktionsnamen
81 T_Nlf, // token for natural language formula
82 T_Matrix, // token for inline arrays
83 T_ExtName, // token for external names
84 T_ExtRefC,
85 T_ExtRefA,
86 T_Error // fuer Abfrage im Fehlerfall
92 class TokenPool
94 // !ACHTUNG!: externe Id-Basis ist 1, interne 0!
95 // Ausgabe Id = 0 -> Fehlerfall
96 private:
97 String** ppP_Str; // Pool fuer Strings
98 UINT16 nP_Str; // ...mit Groesse
99 UINT16 nP_StrAkt; // ...und Schreibmarke
101 double* pP_Dbl; // Pool fuer Doubles
102 UINT16 nP_Dbl;
103 UINT16 nP_DblAkt;
105 USHORT* pP_Err; // Pool for error codes
106 UINT16 nP_Err;
107 UINT16 nP_ErrAkt;
109 ScSingleRefData** ppP_RefTr; // Pool fuer Referenzen
110 UINT16 nP_RefTr;
111 UINT16 nP_RefTrAkt;
113 UINT16* pP_Id; // Pool fuer Id-Folgen
114 UINT16 nP_Id;
115 UINT16 nP_IdAkt;
116 UINT16 nP_IdLast; // letzter Folgen-Beginn
118 struct EXTCONT
120 DefTokenId eId;
121 String aText;
122 EXTCONT( const DefTokenId e, const String& r ) :
123 eId( e ), aText( r ){}
125 EXTCONT** ppP_Ext;
126 UINT16 nP_Ext;
127 UINT16 nP_ExtAkt;
129 struct NLFCONT
131 ScSingleRefData aRef;
132 NLFCONT( const ScSingleRefData& r ) : aRef( r ) {}
134 NLFCONT** ppP_Nlf;
135 UINT16 nP_Nlf;
136 UINT16 nP_NlfAkt;
138 ScMatrix** ppP_Matrix; // Pool fuer Matricies
139 UINT16 nP_Matrix;
140 UINT16 nP_MatrixAkt;
142 /** for storage of external names */
143 struct ExtName
145 sal_uInt16 mnFileId;
146 String maName;
148 ::std::vector<ExtName> maExtNames;
150 /** for storage of external cell references */
151 struct ExtCellRef
153 sal_uInt16 mnFileId;
154 String maTabName;
155 ScSingleRefData maRef;
157 ::std::vector<ExtCellRef> maExtCellRefs;
159 /** for storage of external area references */
160 struct ExtAreaRef
162 sal_uInt16 mnFileId;
163 String maTabName;
164 ScComplexRefData maRef;
166 ::std::vector<ExtAreaRef> maExtAreaRefs;
168 UINT16* pElement; // Array mit Indizes fuer Elemente
169 E_TYPE* pType; // ...mit Typ-Info
170 UINT16* pSize; // ...mit Laengenangabe (Anz. UINT16)
171 UINT16 nElement;
172 UINT16 nElementAkt;
174 static const UINT16 nScTokenOff;// Offset fuer SC-Token
175 #ifdef DBG_UTIL
176 UINT16 nRek; // Rekursionszaehler
177 #endif
178 ScTokenArray* pScToken; // Tokenbastler
180 void GrowString( void );
181 void GrowDouble( void );
182 //UNUSED2009-05 void GrowError( void );
183 void GrowTripel( void );
184 void GrowId( void );
185 void GrowElement( void );
186 void GrowExt( void );
187 void GrowNlf( void );
188 void GrowMatrix( void );
189 void GetElement( const UINT16 nId );
190 void GetElementRek( const UINT16 nId );
191 public:
192 TokenPool( void );
193 ~TokenPool();
194 inline TokenPool& operator <<( const TokenId nId );
195 inline TokenPool& operator <<( const DefTokenId eId );
196 inline TokenPool& operator <<( TokenStack& rStack );
197 void operator >>( TokenId& rId );
198 inline void operator >>( TokenStack& rStack );
199 inline const TokenId Store( void );
200 const TokenId Store( const double& rDouble );
201 //UNUSED2008-05 const TokenId StoreError( USHORT nError );
203 // nur fuer Range-Names
204 const TokenId Store( const UINT16 nIndex );
205 inline const TokenId Store( const INT16 nWert );
206 const TokenId Store( const String& rString );
207 const TokenId Store( const ScSingleRefData& rTr );
208 const TokenId Store( const ScComplexRefData& rTr );
210 const TokenId Store( const DefTokenId eId, const String& rName );
211 // 4 externals (e.g. AddIns, Makros...)
212 const TokenId StoreNlf( const ScSingleRefData& rTr );
213 const TokenId StoreMatrix( SCSIZE nC, SCSIZE nR );
214 const TokenId StoreExtName( sal_uInt16 nFileId, const String& rName );
215 const TokenId StoreExtRef( sal_uInt16 nFileId, const String& rTabName, const ScSingleRefData& rRef );
216 const TokenId StoreExtRef( sal_uInt16 nFileId, const String& rTabName, const ScComplexRefData& rRef );
218 inline const TokenId LastId( void ) const;
219 inline const ScTokenArray* operator []( const TokenId nId );
220 void Reset( void );
221 inline E_TYPE GetType( const TokenId& nId ) const;
222 BOOL IsSingleOp( const TokenId& nId, const DefTokenId eId ) const;
223 const String* GetExternal( const TokenId& nId ) const;
224 //UNUSED2008-05 const String* GetString( const TokenId& nId ) const;
225 ScMatrix* GetMatrix( unsigned int n ) const;
231 class TokenStack
232 // Stack fuer Token-Ids: Id 0 sollte reserviert bleiben als
233 // fehlerhafte Id, da z.B. Get() im Fehlerfall 0 liefert
235 private:
236 TokenId* pStack; // Stack als Array
237 UINT16 nPos; // Schreibmarke
238 UINT16 nSize; // Erster Index ausserhalb des Stacks
239 public:
240 TokenStack( UINT16 nNewSize = 1024 );
241 ~TokenStack();
242 inline TokenStack& operator <<( const TokenId nNewId );
243 inline void operator >>( TokenId &rId );
245 inline void Reset( void );
247 inline bool HasMoreTokens() const { return nPos > 0; }
248 inline const TokenId Get( void );
254 inline const TokenId TokenStack::Get( void )
256 DBG_ASSERT( nPos > 0,
257 "*TokenStack::Get(): Leer ist leer, ist leer, ist leer, ist..." );
259 TokenId nRet;
261 if( nPos == 0 )
262 nRet = 0;
263 else
265 nPos--;
266 nRet = pStack[ nPos ];
269 return nRet;
273 inline TokenStack &TokenStack::operator <<( const TokenId nNewId )
274 {// Element auf Stack
275 DBG_ASSERT( nPos < nSize, "*TokenStack::<<(): Stackueberlauf" );
276 if( nPos < nSize )
278 pStack[ nPos ] = nNewId;
279 nPos++;
282 return *this;
286 inline void TokenStack::operator >>( TokenId& rId )
287 {// Element von Stack
288 DBG_ASSERT( nPos > 0,
289 "*TokenStack::>>(): Leer ist leer, ist leer, ist leer, ..." );
290 if( nPos > 0 )
292 nPos--;
293 rId = pStack[ nPos ];
298 inline void TokenStack::Reset( void )
300 nPos = 0;
306 inline TokenPool& TokenPool::operator <<( const TokenId nId )
308 // POST: nId's werden hintereinander im Pool unter einer neuen Id
309 // abgelegt. Vorgang wird mit >> oder Store() abgeschlossen
310 // nId -> ( UINT16 ) nId - 1;
311 DBG_ASSERT( ( UINT16 ) nId < nScTokenOff,
312 "-TokenPool::operator <<: TokenId im DefToken-Bereich!" );
314 if( nP_IdAkt >= nP_Id )
315 GrowId();
317 pP_Id[ nP_IdAkt ] = ( ( UINT16 ) nId ) - 1;
318 nP_IdAkt++;
320 return *this;
324 inline TokenPool& TokenPool::operator <<( const DefTokenId eId )
326 DBG_ASSERT( ( UINT32 ) eId + nScTokenOff < 0xFFFF,
327 "-TokenPool::operator<<: enmum zu gross!" );
329 if( nP_IdAkt >= nP_Id )
330 GrowId();
332 pP_Id[ nP_IdAkt ] = ( ( UINT16 ) eId ) + nScTokenOff;
333 nP_IdAkt++;
335 return *this;
339 inline TokenPool& TokenPool::operator <<( TokenStack& rStack )
341 if( nP_IdAkt >= nP_Id )
342 GrowId();
344 pP_Id[ nP_IdAkt ] = ( ( UINT16 ) rStack.Get() ) - 1;
345 nP_IdAkt++;
347 return *this;
351 inline void TokenPool::operator >>( TokenStack& rStack )
353 TokenId nId;
354 *this >> nId;
355 rStack << nId;
359 inline const TokenId TokenPool::Store( void )
361 TokenId nId;
362 *this >> nId;
363 return nId;
367 inline const TokenId TokenPool::Store( const INT16 nWert )
369 return Store( ( double ) nWert );
373 inline const TokenId TokenPool::LastId( void ) const
375 return ( TokenId ) nElementAkt; // stimmt, da Ausgabe mit Offset 1!
379 const inline ScTokenArray* TokenPool::operator []( const TokenId nId )
381 pScToken->Clear();
383 if( nId )
384 {//...nur wenn nId > 0!
385 #ifdef DBG_UTIL
386 nRek = 0;
387 #endif
388 GetElement( ( UINT16 ) nId - 1 );
391 return pScToken;
395 inline E_TYPE TokenPool::GetType( const TokenId& rId ) const
397 E_TYPE nRet;
399 UINT16 nId = (UINT16) rId - 1;
401 if( nId < nElementAkt )
402 nRet = pType[ nId ] ;
403 else
404 nRet = T_Error;
406 return nRet;
410 #endif