1*3334a7e6SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*3334a7e6SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*3334a7e6SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*3334a7e6SAndrew Rist * distributed with this work for additional information 6*3334a7e6SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*3334a7e6SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*3334a7e6SAndrew Rist * "License"); you may not use this file except in compliance 9*3334a7e6SAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*3334a7e6SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*3334a7e6SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*3334a7e6SAndrew Rist * software distributed under the License is distributed on an 15*3334a7e6SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*3334a7e6SAndrew Rist * KIND, either express or implied. See the License for the 17*3334a7e6SAndrew Rist * specific language governing permissions and limitations 18*3334a7e6SAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*3334a7e6SAndrew Rist *************************************************************/ 21*3334a7e6SAndrew Rist 22*3334a7e6SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir #ifndef SVX_TREE_VISITOR_HXX 25cdf0e10cSrcweir #define SVX_TREE_VISITOR_HXX 26cdf0e10cSrcweir 27cdf0e10cSrcweir #include <stack> 28cdf0e10cSrcweir 29cdf0e10cSrcweir template< class ELEMENT, class NODEINFO, class PROCESSOR > 30cdf0e10cSrcweir class TreeVisitor 31cdf0e10cSrcweir { 32cdf0e10cSrcweir public: 33cdf0e10cSrcweir TreeVisitor( NODEINFO _nodeInfo ) 34cdf0e10cSrcweir :m_visitedRoot( false ) 35cdf0e10cSrcweir ,m_root() 36cdf0e10cSrcweir ,m_current() 37cdf0e10cSrcweir ,m_nodeInfo( _nodeInfo ) 38cdf0e10cSrcweir { 39cdf0e10cSrcweir } 40cdf0e10cSrcweir 41cdf0e10cSrcweir void process( const ELEMENT& _root, PROCESSOR& _processor ) 42cdf0e10cSrcweir { 43cdf0e10cSrcweir m_root = _root; 44cdf0e10cSrcweir m_visitedRoot = false; 45cdf0e10cSrcweir 46cdf0e10cSrcweir while ( do_step() ) 47cdf0e10cSrcweir _processor.process( m_current ); 48cdf0e10cSrcweir } 49cdf0e10cSrcweir 50cdf0e10cSrcweir private: 51cdf0e10cSrcweir bool do_step(); 52cdf0e10cSrcweir 53cdf0e10cSrcweir private: 54cdf0e10cSrcweir bool m_visitedRoot; 55cdf0e10cSrcweir ELEMENT m_root; 56cdf0e10cSrcweir ELEMENT m_current; 57cdf0e10cSrcweir const NODEINFO m_nodeInfo; 58cdf0e10cSrcweir 59cdf0e10cSrcweir ::std::stack< size_t > m_pathToCurrent; 60cdf0e10cSrcweir ::std::stack< ELEMENT > m_currentAncestors; 61cdf0e10cSrcweir }; 62cdf0e10cSrcweir 63cdf0e10cSrcweir template< class ELEMENT, class NODEINFO, class PROCESSOR > 64cdf0e10cSrcweir bool TreeVisitor< ELEMENT, NODEINFO, PROCESSOR >::do_step() 65cdf0e10cSrcweir { 66cdf0e10cSrcweir if ( !m_visitedRoot ) 67cdf0e10cSrcweir { 68cdf0e10cSrcweir m_current = m_root; 69cdf0e10cSrcweir m_visitedRoot = true; 70cdf0e10cSrcweir return true; 71cdf0e10cSrcweir } 72cdf0e10cSrcweir 73cdf0e10cSrcweir // can we step down from the current node? 74cdf0e10cSrcweir size_t childCount = m_nodeInfo.childCount( m_current ); 75cdf0e10cSrcweir if ( childCount ) 76cdf0e10cSrcweir { 77cdf0e10cSrcweir m_currentAncestors.push( m_current ); 78cdf0e10cSrcweir m_current = m_nodeInfo.getChild( m_current, 0 ); 79cdf0e10cSrcweir m_pathToCurrent.push( 0 ); 80cdf0e10cSrcweir return true; 81cdf0e10cSrcweir } 82cdf0e10cSrcweir 83cdf0e10cSrcweir // is there a right sibling of the current node? 84cdf0e10cSrcweir while ( !m_pathToCurrent.empty() ) 85cdf0e10cSrcweir { 86cdf0e10cSrcweir const ELEMENT& currentParent = m_currentAncestors.top(); 87cdf0e10cSrcweir childCount = m_nodeInfo.childCount( currentParent ); 88cdf0e10cSrcweir 89cdf0e10cSrcweir size_t currentChildPos = m_pathToCurrent.top(); 90cdf0e10cSrcweir if ( ++currentChildPos < childCount ) 91cdf0e10cSrcweir { 92cdf0e10cSrcweir // yes there is 93cdf0e10cSrcweir m_pathToCurrent.top() = currentChildPos; 94cdf0e10cSrcweir m_current = m_nodeInfo.getChild( currentParent, currentChildPos ); 95cdf0e10cSrcweir return true; 96cdf0e10cSrcweir } 97cdf0e10cSrcweir 98cdf0e10cSrcweir // no there isn't => step up 99cdf0e10cSrcweir m_currentAncestors.pop(); 100cdf0e10cSrcweir m_pathToCurrent.pop(); 101cdf0e10cSrcweir } 102cdf0e10cSrcweir 103cdf0e10cSrcweir return false; 104cdf0e10cSrcweir } 105cdf0e10cSrcweir 106cdf0e10cSrcweir #endif // SVX_TREE_VISITOR_HXX 107