idlehomescreen/inc/xnnodebreadthfirstiterator.h
changeset 0 f72a12da539e
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, breadth first
       
    15 *
       
    16 */
       
    17 
       
    18 #ifndef XNNODEBREADTHFIRSTITERATOR_H
       
    19 #define XNNODEBREADTHFIRSTITERATOR_H
       
    20 
       
    21 #include <e32base.h>
       
    22 #include <e32cmn.h>
       
    23 #include "xnchilditerator.h"
       
    24 
       
    25 // Constants
       
    26 /** 
       
    27  * Queue granularity value
       
    28  */
       
    29 const TInt KMemoryAloc = 128;
       
    30 
       
    31 template< class T > class CXnNodeBreadthFirstIterator : public CBase,
       
    32     public MXnChildIterator
       
    33     {
       
    34 public:
       
    35 
       
    36     /**
       
    37     * Two-phased constructor.
       
    38     * @param aStartNode Object to iterate over
       
    39     */
       
    40     static CXnNodeBreadthFirstIterator< T >* NewL( T& aStartNode );
       
    41 
       
    42     /**
       
    43     * Destructor.
       
    44     */
       
    45     virtual ~CXnNodeBreadthFirstIterator();
       
    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 protected:
       
    72 
       
    73     /**
       
    74     * C++ default constructor. 
       
    75     */
       
    76     CXnNodeBreadthFirstIterator( T* aStartNode );
       
    77 
       
    78     /**
       
    79     * 2nd phase constructor. 
       
    80     */
       
    81     void ConstructL();
       
    82 
       
    83 private:   
       
    84     // Queue
       
    85     RPointerArray< T > iQueue;
       
    86     // Object to iterate over
       
    87     T* iStart;   
       
    88     // Current object
       
    89     T* iCurrent;
       
    90     };
       
    91 
       
    92 
       
    93 // -----------------------------------------------------------------------------
       
    94 // CXnNodeBreadthFirstIterator< T >::NewL()
       
    95 // -----------------------------------------------------------------------------
       
    96 //
       
    97 template< class T > CXnNodeBreadthFirstIterator< T >* 
       
    98     CXnNodeBreadthFirstIterator< T >::NewL( T& aStartNode )
       
    99     {
       
   100     CXnNodeBreadthFirstIterator< T >* p = 
       
   101         new ( ELeave )CXnNodeBreadthFirstIterator< T >( &aStartNode );
       
   102     CleanupStack::PushL( p );
       
   103     p->ConstructL();
       
   104     CleanupStack::Pop();
       
   105     return p;
       
   106     }
       
   107 
       
   108 // -----------------------------------------------------------------------------
       
   109 // CXnNodeBreadthFirstIterator< T >::CXnNodeBreadthFirstIterator()
       
   110 // C++ default constructor
       
   111 // -----------------------------------------------------------------------------
       
   112 //
       
   113 template< class T >
       
   114     CXnNodeBreadthFirstIterator < T >::CXnNodeBreadthFirstIterator(
       
   115     T* aStartNode ): iStart( aStartNode ), iCurrent( aStartNode ),
       
   116     iQueue( KMemoryAloc )
       
   117     {
       
   118     }
       
   119 
       
   120 // -----------------------------------------------------------------------------
       
   121 //  CXnNodeBreadthFirstIterator< T >::~CXnNodeBreadthFirstIterator()
       
   122 //  C++ default destructor.     
       
   123 // -----------------------------------------------------------------------------
       
   124 //     
       
   125 template< class T >
       
   126     CXnNodeBreadthFirstIterator< T >::~CXnNodeBreadthFirstIterator()
       
   127     {
       
   128     iQueue.Close();
       
   129     }
       
   130 
       
   131 // -----------------------------------------------------------------------------
       
   132 // CXnNodeBreadthFirstIterator< T >::ConstructL()
       
   133 // Symbian 2nd phase constructor can leave.
       
   134 // -----------------------------------------------------------------------------
       
   135 //
       
   136 template< class T > void CXnNodeBreadthFirstIterator< T >::ConstructL()
       
   137     {
       
   138     iQueue.AppendL( iStart );
       
   139     }
       
   140 
       
   141 // -----------------------------------------------------------------------------
       
   142 // CXnNodeBreadthFirstIterator< T >::NextL()
       
   143 // -----------------------------------------------------------------------------
       
   144 //
       
   145 template< class T > T* CXnNodeBreadthFirstIterator< T >::NextL()
       
   146     {
       
   147     if( iQueue.Count() )
       
   148         {
       
   149         // Dequeue
       
   150         iCurrent = iQueue[0];
       
   151         iQueue.Remove(0);
       
   152 
       
   153         RPointerArray< T >& currentChildNodes = iCurrent->Children();
       
   154         TInt currentChildCount( currentChildNodes.Count() );
       
   155 
       
   156         // Enqueue
       
   157         for( TInt i = 0; i < currentChildCount; ++i )
       
   158             {
       
   159             iQueue.AppendL( currentChildNodes[i] );
       
   160             }
       
   161         }
       
   162     else
       
   163         {
       
   164         iCurrent = NULL;
       
   165         iQueue.Reset();
       
   166         iQueue.AppendL( iStart );
       
   167         }
       
   168     return iCurrent;
       
   169     }
       
   170 
       
   171 // -----------------------------------------------------------------------------
       
   172 // CXnNodeBreadthFirstIterator< T >::Value()
       
   173 // -----------------------------------------------------------------------------
       
   174 //
       
   175 template< class T > T* CXnNodeBreadthFirstIterator< T >::Value()
       
   176     {
       
   177     return iCurrent;
       
   178     }
       
   179 
       
   180 // -----------------------------------------------------------------------------
       
   181 // CXnNodeBreadthFirstIterator< T >::PreviousL()
       
   182 // -----------------------------------------------------------------------------
       
   183 //
       
   184 template< class T > T* CXnNodeBreadthFirstIterator< T >::PreviousL()
       
   185     {
       
   186     return NULL;
       
   187     }
       
   188 
       
   189 // -----------------------------------------------------------------------------
       
   190 // CXnNodeBreadthFirstIterator< T >::Index()
       
   191 // -----------------------------------------------------------------------------
       
   192 //
       
   193 template< class T > TUint CXnNodeBreadthFirstIterator< T >::Index() const
       
   194     {
       
   195     return 0;
       
   196     }
       
   197 
       
   198 #endif    //CXNNODEBREADTHFIRSTITERATOR_H
       
   199 
       
   200 // End of File