Update ooo320-m1
[ooovba.git] / autodoc / source / parser / inc / semantic / callf.hxx
blob774c72db4d2b8f98c15d5b0754d5549fdcd547c2
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: callf.hxx,v $
10 * $Revision: 1.6 $
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 ADC_CPP_CALLF_HXX
32 #define ADC_CPP_CALLF_HXX
34 // USED SERVICES
39 /** This represents a function to be called, if a specific kind of token
40 arrives in the semantic parser.
42 @descr This class is only to be used as member of PeStatus<PE>.
43 @template PE
44 The owning ParseEnvironment.
45 @see PeStatus, ParseEnvironment
47 template <class PE>
48 class CallFunction
50 public:
51 typedef void (PE::*F_Tok)(const char *);
53 CallFunction(
54 F_Tok i_f2Call,
55 INT16 i_nTokType );
57 F_Tok GetF() const;
58 INT16 TokType() const;
60 private:
61 // DATA
62 F_Tok f2Call;
63 INT16 nTokType;
67 /** One state within a ParseEnvironment.
69 @template PE
70 The owning ParseEnvironment.
72 template <class PE>
73 class PeStatus
75 public:
76 typedef typename CallFunction<PE>::F_Tok F_Tok;
78 PeStatus(
79 PE & i_rMyPE,
80 uintt i_nSize,
81 F_Tok * i_pFuncArray,
82 INT16 * i_pTokTypeArray,
83 F_Tok i_pDefault );
84 virtual ~PeStatus();
86 virtual void Call_Handler(
87 INT16 i_nTokTypeId,
88 const char * i_sTokenText ) const;
90 private:
91 bool CheckForCall(
92 uintt i_nPos,
93 INT16 i_nTokTypeId,
94 const char * i_sTokenText ) const;
96 PE * pMyPE;
97 std::vector< CallFunction<PE> >
98 aBranches;
99 F_Tok fDefault;
103 template <class PE>
104 class PeStatusArray
106 public:
107 typedef typename PE::E_State State;
109 PeStatusArray();
110 void InsertState(
111 State i_ePosition,
112 DYN PeStatus<PE> & let_drState );
113 ~PeStatusArray();
115 const PeStatus<PE> &
116 operator[](
117 State i_ePosition ) const;
119 void SetCur(
120 State i_eCurState );
121 const PeStatus<PE> &
122 Cur() const;
124 private:
125 DYN PeStatus<PE> * aStati[PE::size_of_states];
126 State eState;
131 // IMPLEMENTATION
134 // CallFunction
136 template <class PE>
137 CallFunction<PE>::CallFunction( F_Tok i_f2Call,
138 INT16 i_nTokType )
139 : f2Call(i_f2Call),
140 nTokType(i_nTokType)
144 template <class PE>
145 inline typename CallFunction<PE>::F_Tok
146 CallFunction<PE>::GetF() const
148 return f2Call;
151 template <class PE>
152 inline INT16
153 CallFunction<PE>::TokType() const
155 return nTokType;
160 // PeStatus
162 template <class PE>
163 PeStatus<PE>::PeStatus( PE & i_rMyPE,
164 uintt i_nSize,
165 F_Tok * i_pFuncArray,
166 INT16 * i_pTokTypeArray,
167 F_Tok i_fDefault )
168 : pMyPE(&i_rMyPE),
169 fDefault(i_fDefault)
171 aBranches.reserve(i_nSize);
172 for ( uintt i = 0; i < i_nSize; ++i )
174 // csv_assert(i > 0 ? i_pTokTypeArray[i] > i_pTokTypeArray[i-1] : true);
175 aBranches.push_back( CallFunction<PE>( i_pFuncArray[i], i_pTokTypeArray[i]) );
176 } // end for
179 template <class PE>
180 PeStatus<PE>::~PeStatus()
185 template <class PE>
186 void
187 PeStatus<PE>::Call_Handler( INT16 i_nTokTypeId,
188 const char * i_sTokenText ) const
190 uintt nSize = aBranches.size();
191 uintt nPos = nSize / 2;
193 if ( i_nTokTypeId < aBranches[nPos].TokType() )
195 for ( --nPos; intt(nPos) >= 0; --nPos )
197 if (CheckForCall(nPos, i_nTokTypeId, i_sTokenText))
198 return;
201 else
203 for ( ; nPos < nSize; ++nPos )
205 if (CheckForCall(nPos, i_nTokTypeId, i_sTokenText))
206 return;
210 (pMyPE->*fDefault)(i_sTokenText);
213 template <class PE>
214 bool
215 PeStatus<PE>::CheckForCall( uintt i_nPos,
216 INT16 i_nTokTypeId,
217 const char * i_sTokenText ) const
219 if ( aBranches[i_nPos].TokType() == i_nTokTypeId )
221 (pMyPE->*aBranches[i_nPos].GetF())(i_sTokenText);
222 return true;
224 return false;
227 // PeStatusArray
229 template <class PE>
230 PeStatusArray<PE>::PeStatusArray()
231 : eState(PE::size_of_states)
233 memset(aStati, 0, sizeof aStati);
236 template <class PE>
237 void
238 PeStatusArray<PE>::InsertState( State i_ePosition,
239 DYN PeStatus<PE> & let_drState )
241 csv_assert(aStati[i_ePosition] == 0);
242 aStati[i_ePosition] = &let_drState;
245 template <class PE>
246 PeStatusArray<PE>::~PeStatusArray()
248 int i_max = PE::size_of_states;
249 for (int i = 0; i < i_max; i++)
251 delete aStati[i];
252 } // end for
255 template <class PE>
256 inline const PeStatus<PE> &
257 PeStatusArray<PE>::operator[]( State i_ePosition ) const
259 csv_assert( uintt(i_ePosition) < PE::size_of_states );
260 csv_assert( aStati[i_ePosition] != 0 );
261 return *aStati[i_ePosition];
264 template <class PE>
265 inline void
266 PeStatusArray<PE>::SetCur( State i_eCurState )
268 eState = i_eCurState;
272 template <class PE>
273 const PeStatus<PE> &
274 PeStatusArray<PE>::Cur() const
276 return (*this)[eState];
279 #define SEMPARSE_CREATE_STATUS(penv, state, default_function) \
280 pStati->InsertState( state, \
281 *new PeStatus<penv>( \
282 *this, \
283 sizeof( stateT_##state ) / sizeof (INT16), \
284 stateF_##state, \
285 stateT_##state, \
286 &penv::default_function ) )
289 #endif