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: recursionhelper.hxx,v $
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 INCLUDED_RECURSIONHELPER_HXX
32 #define INCLUDED_RECURSIONHELPER_HXX
34 #include "formularesult.hxx"
38 #include <tools/solar.h>
42 struct ScFormulaRecursionEntry
46 ScFormulaResult aPreviousResult
;
47 ScFormulaRecursionEntry( ScFormulaCell
* p
, BOOL bR
,
48 const ScFormulaResult
& rRes
) :
49 pCell(p
), bOldRunning(bR
), aPreviousResult( rRes
)
54 typedef ::std::list
< ScFormulaRecursionEntry
> ScFormulaRecursionList
;
56 class ScRecursionHelper
58 typedef ::std::stack
< ScFormulaCell
* > ScRecursionInIterationStack
;
59 ScFormulaRecursionList aRecursionFormulas
;
60 ScFormulaRecursionList::iterator aInsertPos
;
61 ScFormulaRecursionList::iterator aLastIterationStart
;
62 ScRecursionInIterationStack aRecursionInIterationStack
;
63 USHORT nRecursionCount
;
65 bool bInRecursionReturn
;
67 bool bInIterationReturn
;
73 bInRecursionReturn
= bDoingRecursion
= bInIterationReturn
= false;
74 aInsertPos
= GetEnd();
79 aLastIterationStart
= GetEnd();
86 ScRecursionHelper() { Init(); }
87 USHORT
GetRecursionCount() const { return nRecursionCount
; }
88 void IncRecursionCount() { ++nRecursionCount
; }
89 void DecRecursionCount() { --nRecursionCount
; }
90 /// A pure recursion return, no iteration.
91 bool IsInRecursionReturn() const { return bInRecursionReturn
&&
92 !bInIterationReturn
; }
93 void SetInRecursionReturn( bool b
)
95 // Do not use IsInRecursionReturn() here, it decouples iteration.
96 if (b
&& !bInRecursionReturn
)
97 aInsertPos
= aRecursionFormulas
.begin();
98 bInRecursionReturn
= b
;
100 bool IsDoingRecursion() const { return bDoingRecursion
; }
101 void SetDoingRecursion( bool b
) { bDoingRecursion
= b
; }
102 void Insert( ScFormulaCell
* p
, BOOL bOldRunning
,
103 const ScFormulaResult
& rRes
)
105 aRecursionFormulas
.insert( aInsertPos
, ScFormulaRecursionEntry( p
,
108 ScFormulaRecursionList::iterator
GetStart()
110 return aRecursionFormulas
.begin();
112 ScFormulaRecursionList::iterator
GetEnd()
114 return aRecursionFormulas
.end();
116 bool IsInIterationReturn() const { return bInIterationReturn
; }
117 void SetInIterationReturn( bool b
)
119 // An iteration return is always coupled to a recursion return.
120 SetInRecursionReturn( b
);
121 bInIterationReturn
= b
;
123 bool IsDoingIteration() const { return nIteration
> 0; }
124 USHORT
GetIteration() const { return nIteration
; }
125 bool & GetConvergingReference() { return bConverging
; }
126 void StartIteration()
128 SetInIterationReturn( false);
131 aLastIterationStart
= GetIterationStart();
133 void ResumeIteration()
135 SetInIterationReturn( false);
136 aLastIterationStart
= GetIterationStart();
138 void IncIteration() { ++nIteration
; }
141 aRecursionFormulas
.erase( GetIterationStart(), GetIterationEnd());
144 ScFormulaRecursionList::iterator
GetLastIterationStart() { return aLastIterationStart
; }
145 ScFormulaRecursionList::iterator
GetIterationStart() { return GetStart(); }
146 ScFormulaRecursionList::iterator
GetIterationEnd() { return GetEnd(); }
147 /** Any return, recursion or iteration, iteration is always coupled with
149 bool IsInReturn() const { return bInRecursionReturn
; }
150 const ScFormulaRecursionList
& GetList() const { return aRecursionFormulas
; }
151 ScFormulaRecursionList
& GetList() { return aRecursionFormulas
; }
152 ScRecursionInIterationStack
& GetRecursionInIterationStack() { return aRecursionInIterationStack
; }
155 aRecursionFormulas
.clear();
156 while (!aRecursionInIterationStack
.empty())
157 aRecursionInIterationStack
.pop();
162 #endif // INCLUDED_RECURSIONHELPER_HXX