phonebookengines_old/contactsmodel/tsrc/cfixedqueue.h
changeset 40 b46a585f6909
equal deleted inserted replaced
37:fd64c38c277d 40:b46a585f6909
       
     1 // Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // CFixedQueue Test module
       
    15 // 
       
    16 //
       
    17 
       
    18 
       
    19 #ifndef CFixedQueue_H
       
    20 #define CFixedQueue_H
       
    21 
       
    22 // INCLUDES
       
    23 #include <e32base.h>
       
    24 
       
    25 
       
    26 // CONSTANTS
       
    27 _LIT(KFixedQueuePanicText, "CFixedQueue<T>");
       
    28 
       
    29 
       
    30 // CLASS DECLARATION
       
    31 
       
    32 /**
       
    33  * Fixed size queue.
       
    34  * @param T type of objects in the queue. T must have accessible default 
       
    35  *          constructor and assignment operator.
       
    36  */
       
    37 template<class T>
       
    38 class CFixedQueue : public CBase
       
    39     {
       
    40     public:  // Constructors and destructor
       
    41         CFixedQueue();
       
    42 
       
    43         /**
       
    44          * Initializes a new fixed queue.
       
    45          * @param aSize maximum size of the queue.
       
    46          * @pre aSize > 0
       
    47          */
       
    48         void ConstructL(TInt aSize);
       
    49 
       
    50         /**
       
    51          * Destructor.
       
    52          */
       
    53         ~CFixedQueue();
       
    54 
       
    55     public:  // Interface
       
    56         /**
       
    57          * Returns the object at the head of this queue.
       
    58          * @pre !IsEmpty()
       
    59          */
       
    60         T& Head() const;
       
    61 
       
    62         /**
       
    63          * Pops the head off this queue. Does not deallocate memory.
       
    64          * @pre !IsEmpty()
       
    65          */
       
    66         void PopHead();
       
    67 
       
    68         /**
       
    69          * Returns true if this queue is empty
       
    70          */
       
    71         TBool IsEmpty() const;
       
    72 
       
    73         /**
       
    74          * Returns the number of objects in the queue.
       
    75          */
       
    76         TInt Count() const;
       
    77 
       
    78         /**
       
    79          * Pushes an object at the tail of the queue.
       
    80          * @return ETrue if succesful, EFalse if queue run out of space.
       
    81          */ 
       
    82         TBool Push(const T& aObj);
       
    83 
       
    84         /**
       
    85          * Empties this queue. Does not deallocate memory.
       
    86          */
       
    87         void Reset();
       
    88 
       
    89     private:  // Implementation
       
    90         enum TPanicCode
       
    91             {
       
    92             EPanicPreCond_ConstructL = 1,
       
    93             EPanicPreCond_Head,
       
    94             EPanicPreCond_PopHead,
       
    95             EPanicPreCond_Push,
       
    96             EPanicInvariant
       
    97             };
       
    98         static void Panic(TPanicCode aReason);
       
    99         __DECLARE_TEST;
       
   100 
       
   101     private:  // Data
       
   102         // Own: circural queue array
       
   103         T* iQueueArray;
       
   104         // Own: maximum size of queue
       
   105         TInt iQueueSize;
       
   106         // Own: number of objects currently in the queue
       
   107         TInt iCount;
       
   108         // Ref: head of queue
       
   109         T* iQueueHead;
       
   110         // Ref: tail of queue
       
   111         T* iQueueTail;
       
   112     };
       
   113 
       
   114 template<class T> inline
       
   115 void CFixedQueue<T>::Panic(TPanicCode aReason)
       
   116     {
       
   117     User::Panic(KFixedQueuePanicText,aReason);
       
   118     }
       
   119 
       
   120 template<class T> inline
       
   121 CFixedQueue<T>::CFixedQueue()
       
   122     {
       
   123     // CBase::operator new resets memebers
       
   124     __TEST_INVARIANT;
       
   125     }
       
   126 
       
   127 template<class T> inline
       
   128 void CFixedQueue<T>::ConstructL(TInt aSize)
       
   129     {
       
   130     __TEST_INVARIANT;
       
   131     __ASSERT_ALWAYS(!iQueueArray, Panic(EPanicPreCond_ConstructL));
       
   132     __ASSERT_ALWAYS(aSize > 0, Panic(EPanicPreCond_ConstructL));
       
   133     iQueueArray = new(ELeave) T[aSize];
       
   134     iQueueSize = aSize;
       
   135     iCount = 0;
       
   136     iQueueHead = iQueueTail = iQueueArray;
       
   137     __TEST_INVARIANT;
       
   138     }
       
   139 
       
   140 template<class T>
       
   141 CFixedQueue<T>::~CFixedQueue()
       
   142     {
       
   143     __TEST_INVARIANT;
       
   144     delete [] iQueueArray;
       
   145     }
       
   146 
       
   147 template<class T> inline
       
   148 T& CFixedQueue<T>::Head() const
       
   149     {
       
   150     __TEST_INVARIANT;
       
   151     __ASSERT_ALWAYS(iCount > 0, Panic(EPanicPreCond_Head));
       
   152     return *iQueueHead;
       
   153     }
       
   154 
       
   155 template<class T> inline
       
   156 void CFixedQueue<T>::PopHead()
       
   157     {
       
   158     __TEST_INVARIANT;
       
   159     __ASSERT_ALWAYS(iCount > 0, Panic(EPanicPreCond_PopHead));
       
   160     ++iQueueHead;
       
   161     if (iQueueHead == iQueueArray+iQueueSize)
       
   162         {
       
   163         iQueueHead = iQueueArray;
       
   164         }
       
   165     --iCount;
       
   166     }
       
   167 
       
   168 template<class T> inline
       
   169 TBool CFixedQueue<T>::IsEmpty() const
       
   170     {
       
   171     __TEST_INVARIANT;
       
   172     return (iCount==0);
       
   173     }
       
   174 
       
   175 template<class T> inline
       
   176 TInt CFixedQueue<T>::Count() const
       
   177     {
       
   178     __TEST_INVARIANT;
       
   179     return iCount;
       
   180     }
       
   181 
       
   182 template<class T> inline
       
   183 TBool CFixedQueue<T>::Push(const T& aObj)
       
   184     {
       
   185     __TEST_INVARIANT;
       
   186     if (iCount < iQueueSize)
       
   187         {
       
   188         *iQueueTail = aObj;
       
   189         ++iQueueTail;
       
   190         if (iQueueTail == iQueueArray+iQueueSize)
       
   191             {
       
   192             iQueueTail = iQueueArray;
       
   193             }
       
   194         ++iCount;
       
   195         __TEST_INVARIANT;
       
   196         return ETrue;
       
   197         }
       
   198     else
       
   199         {
       
   200         return EFalse;
       
   201         }
       
   202     }
       
   203 
       
   204 template<class T> inline
       
   205 void CFixedQueue<T>::Reset()
       
   206     {
       
   207     __TEST_INVARIANT;
       
   208     iQueueTail = iQueueHead;
       
   209     iCount = 0;
       
   210     __TEST_INVARIANT;
       
   211     }
       
   212 
       
   213 template<class T>
       
   214 void CFixedQueue<T>::__DbgTestInvariant() const
       
   215     {
       
   216     if (!iQueueArray)
       
   217         {
       
   218         // Not initialised
       
   219         __ASSERT_ALWAYS(iQueueSize==0 && iCount==0, Panic(EPanicInvariant));
       
   220         __ASSERT_ALWAYS(!iQueueHead && !iQueueTail, Panic(EPanicInvariant));
       
   221         }
       
   222     else
       
   223         {
       
   224         // Initialised
       
   225         __ASSERT_ALWAYS(iQueueSize > 0, Panic(EPanicInvariant));
       
   226         __ASSERT_ALWAYS(iCount >= 0 && iCount <= iQueueSize, Panic(EPanicInvariant));
       
   227         __ASSERT_ALWAYS((iQueueHead >= iQueueArray) && (iQueueHead < iQueueArray+iQueueSize), 
       
   228             Panic(EPanicInvariant));
       
   229         __ASSERT_ALWAYS((iQueueTail >= iQueueArray) && (iQueueTail < iQueueArray+iQueueSize), 
       
   230             Panic(EPanicInvariant));
       
   231         }
       
   232     }
       
   233 
       
   234 
       
   235 #endif
       
   236 
       
   237 // End of File
       
   238