idlehomescreen/inc/xndepthfirsttreeiterator.h
changeset 0 f72a12da539e
child 5 c743ef5928ba
equal deleted inserted replaced
-1:000000000000 0:f72a12da539e
       
     1 /*
       
     2 * Copyright (c) 2002-2004 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Iterates over a tree, depth first
       
    15 *
       
    16 */
       
    17 
       
    18 #ifndef XNDEPTHFIRSTTREEITERATOR_H
       
    19 #define XNDEPTHFIRSTTREEITERATOR_H
       
    20 
       
    21 //  INCLUDES
       
    22 #include <e32base.h>
       
    23 #include "xnchilditerator.h"
       
    24 
       
    25 // Constants
       
    26 /** 
       
    27  * Queue granularity value
       
    28  */
       
    29 const TInt KMemoryAlocS = 64;
       
    30 
       
    31 
       
    32 template < class T >
       
    33     class CXnDepthFirstTreeIterator: public CBase, public MXnChildIterator
       
    34     {
       
    35 public:
       
    36     /**
       
    37     * Two-phased constructor.
       
    38     * @param aStartNode Object to iterate over
       
    39     */
       
    40     static CXnDepthFirstTreeIterator< T >* NewL( T& aStartNode );
       
    41 
       
    42     /**
       
    43     * Destructor.
       
    44     */
       
    45     virtual ~CXnDepthFirstTreeIterator();
       
    46 
       
    47     /**
       
    48     * Get the next iterator value.
       
    49     * @return Next object or NULL if no more values. 
       
    50     */
       
    51     T* NextL();
       
    52 
       
    53     /**
       
    54     * Get the current iterator value.
       
    55     * @return Current object 
       
    56     */  
       
    57     T* Value();
       
    58 
       
    59     /**
       
    60     * Get the previous iterator value. Obsolete!
       
    61     * @return NULL 
       
    62     */
       
    63     T* PreviousL();
       
    64 
       
    65     /**
       
    66     * Get the current iterator index. Obsolete!
       
    67     * @return value 0
       
    68     */
       
    69     TUint Index() const;
       
    70 
       
    71     /**
       
    72     * Get the current iterator level in the tree.
       
    73     * @return current level
       
    74     */
       
    75     TUint Level() const;
       
    76 
       
    77 protected:
       
    78 
       
    79     CXnDepthFirstTreeIterator( T* aStartNode );
       
    80 
       
    81     void ConstructL();
       
    82 
       
    83 private:
       
    84     //Stack
       
    85     RPointerArray< T > iStack;
       
    86     //Object to iterate over
       
    87     T* iStart;
       
    88     // Current object
       
    89     T* iCurrent;
       
    90 
       
    91     };
       
    92 
       
    93 // -----------------------------------------------------------------------------
       
    94 // CXnDepthFirstTreeIterator< T >::NewL()
       
    95 // -----------------------------------------------------------------------------
       
    96 //
       
    97 template< class T > CXnDepthFirstTreeIterator< T >*
       
    98     CXnDepthFirstTreeIterator<T>::NewL( T& aStartNode )
       
    99     {
       
   100     CXnDepthFirstTreeIterator<T>* self = 
       
   101         new ( ELeave )CXnDepthFirstTreeIterator< T >( &aStartNode );
       
   102     CleanupStack::PushL( self );
       
   103     self->ConstructL();
       
   104     CleanupStack::Pop();
       
   105     return self;
       
   106     }
       
   107 
       
   108 // -----------------------------------------------------------------------------
       
   109 // CXnDepthFirstTreeIterator< T >::CXnDepthFirstTreeIterator()
       
   110 // C++ default constructor
       
   111 // -----------------------------------------------------------------------------
       
   112 //
       
   113 template< class T > CXnDepthFirstTreeIterator< T >::CXnDepthFirstTreeIterator(
       
   114     T* aStartNode ) : iStart( aStartNode ), iStack( KMemoryAlocS )
       
   115     {
       
   116 
       
   117     }
       
   118 
       
   119 // -----------------------------------------------------------------------------
       
   120 //  CXnDepthFirstTreeIterator< T >::~CXnDepthFirstTreeIterator()
       
   121 //  C++ default destructor.  
       
   122 // -----------------------------------------------------------------------------
       
   123 //  
       
   124 template< class T >
       
   125     CXnDepthFirstTreeIterator< T >::~CXnDepthFirstTreeIterator()
       
   126     {
       
   127     iStack.Close();
       
   128     }
       
   129 
       
   130 // -----------------------------------------------------------------------------
       
   131 // CXnDepthFirstTreeIterator< T >::ConstructL()
       
   132 // Symbian 2nd phase constructor can leave.
       
   133 // -----------------------------------------------------------------------------
       
   134 //
       
   135 template< class T > void CXnDepthFirstTreeIterator< T >::ConstructL()
       
   136     {
       
   137     iStack.AppendL( iStart );
       
   138     }
       
   139 
       
   140 // -----------------------------------------------------------------------------
       
   141 // CXnDepthFirstTreeIterator< T >::NextL()
       
   142 // -----------------------------------------------------------------------------
       
   143 //
       
   144 template< class T > T* CXnDepthFirstTreeIterator< T >::NextL()
       
   145     {
       
   146     if( iStack.Count() )
       
   147         {
       
   148         // Pop
       
   149         TInt topIndex( iStack.Count() - 1 );
       
   150         iCurrent = iStack[topIndex];
       
   151         iStack.Remove( topIndex );
       
   152         
       
   153         RPointerArray< T >& currentChildNodes( iCurrent->Children() );
       
   154         TInt currentChildCount( currentChildNodes.Count() );
       
   155        
       
   156         // Push left most child to top
       
   157         for( TInt i = currentChildCount - 1; i >= 0; --i )
       
   158             {
       
   159             iStack.AppendL( currentChildNodes[i] );
       
   160             }
       
   161         }
       
   162     else
       
   163         {
       
   164         iCurrent = NULL;
       
   165         iStack.Reset();
       
   166         iStack.AppendL( iStart );
       
   167         }
       
   168     return iCurrent;
       
   169     }
       
   170 
       
   171 // -----------------------------------------------------------------------------
       
   172 // CXnDepthFirstTreeIterator< T >::Value()
       
   173 // -----------------------------------------------------------------------------
       
   174 //
       
   175 template< class T > T* CXnDepthFirstTreeIterator< T >::Value()
       
   176     {
       
   177     return iCurrent;
       
   178     }
       
   179 
       
   180 // -----------------------------------------------------------------------------
       
   181 // CXnDepthFirstTreeIterator< T >::PreviousL()
       
   182 // -----------------------------------------------------------------------------
       
   183 //
       
   184 template< class T > T* CXnDepthFirstTreeIterator< T >::PreviousL()
       
   185     {
       
   186     return NULL;
       
   187     }
       
   188 
       
   189 // -----------------------------------------------------------------------------
       
   190 // CXnDepthFirstTreeIterator< T >::Index()
       
   191 // -----------------------------------------------------------------------------
       
   192 //
       
   193 template< class T > TUint CXnDepthFirstTreeIterator< T >::Index() const
       
   194     {
       
   195     return 0;
       
   196     }
       
   197 
       
   198 // -----------------------------------------------------------------------------
       
   199 // CXnDepthFirstTreeIterator< T >::Level()
       
   200 // Get the current iterator level in the tree.
       
   201 // -----------------------------------------------------------------------------
       
   202 template< class T > TUint CXnDepthFirstTreeIterator< T >::Level() const
       
   203     {
       
   204     return iStack.Count();
       
   205     }
       
   206 
       
   207 
       
   208 #endif    // CXNDEPTHFIRSTTREEITERATOR_H
       
   209 
       
   210 // End of File