Update ooo320-m1
[ooovba.git] / svx / source / inc / treevisitor.hxx
blobf846f55d5053dc70d6a7371545b7085ac92785ac
1 /*************************************************************************
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * Copyright 2008 by Sun Microsystems, Inc.
6 * OpenOffice.org - a multi-platform office productivity suite
8 * $RCSfile: code,v $
10 * $Revision: 1.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.
28 ************************************************************************/
30 #ifndef SVX_TREE_VISITOR_HXX
31 #define SVX_TREE_VISITOR_HXX
33 #include <stack>
35 template< class ELEMENT, class NODEINFO, class PROCESSOR >
36 class TreeVisitor
38 public:
39 TreeVisitor( NODEINFO _nodeInfo )
40 :m_visitedRoot( false )
41 ,m_root()
42 ,m_current()
43 ,m_nodeInfo( _nodeInfo )
47 void process( const ELEMENT& _root, PROCESSOR& _processor )
49 m_root = _root;
50 m_visitedRoot = false;
52 while ( do_step() )
53 _processor.process( m_current );
56 private:
57 bool do_step();
59 private:
60 bool m_visitedRoot;
61 ELEMENT m_root;
62 ELEMENT m_current;
63 const NODEINFO m_nodeInfo;
65 ::std::stack< size_t > m_pathToCurrent;
66 ::std::stack< ELEMENT > m_currentAncestors;
69 template< class ELEMENT, class NODEINFO, class PROCESSOR >
70 bool TreeVisitor< ELEMENT, NODEINFO, PROCESSOR >::do_step()
72 if ( !m_visitedRoot )
74 m_current = m_root;
75 m_visitedRoot = true;
76 return true;
79 // can we step down from the current node?
80 size_t childCount = m_nodeInfo.childCount( m_current );
81 if ( childCount )
83 m_currentAncestors.push( m_current );
84 m_current = m_nodeInfo.getChild( m_current, 0 );
85 m_pathToCurrent.push( 0 );
86 return true;
89 // is there a right sibling of the current node?
90 while ( !m_pathToCurrent.empty() )
92 const ELEMENT& currentParent = m_currentAncestors.top();
93 childCount = m_nodeInfo.childCount( currentParent );
95 size_t currentChildPos = m_pathToCurrent.top();
96 if ( ++currentChildPos < childCount )
98 // yes there is
99 m_pathToCurrent.top() = currentChildPos;
100 m_current = m_nodeInfo.getChild( currentParent, currentChildPos );
101 return true;
104 // no there isn't => step up
105 m_currentAncestors.pop();
106 m_pathToCurrent.pop();
109 return false;
112 #endif // SVX_TREE_VISITOR_HXX