commondrm/drmutility/src/DrmQueue.inl
changeset 0 95b198f216e5
equal deleted inserted replaced
-1:000000000000 0:95b198f216e5
       
     1 /*
       
     2 * Copyright (c) 2006 - 2007 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:  DRM Queue
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // ============================ MEMBER FUNCTIONS ===============================
       
    20 
       
    21 // -----------------------------------------------------------------------------
       
    22 // CDrmQueue
       
    23 // Constructor
       
    24 // -----------------------------------------------------------------------------
       
    25 //
       
    26 template< class T > inline DRM::CDrmQueue< T >::CDrmQueue() : CBase()
       
    27     {
       
    28     }
       
    29 
       
    30 // -----------------------------------------------------------------------------
       
    31 // ~CDrmQueue
       
    32 // Destructor
       
    33 // -----------------------------------------------------------------------------
       
    34 //
       
    35 template< class T > inline DRM::CDrmQueue< T >::~CDrmQueue()
       
    36     {
       
    37     T* payload = PopFront();
       
    38 
       
    39     // Empty the queue:
       
    40     while( payload )
       
    41         {
       
    42         delete payload;
       
    43         payload = PopFront();
       
    44         }
       
    45     iSemaphore.Close();
       
    46     }
       
    47 
       
    48 // -----------------------------------------------------------------------------
       
    49 // CDrmQueue::ConstructL()
       
    50 // (other items were commented in a header).
       
    51 // -----------------------------------------------------------------------------
       
    52 //
       
    53 template< class T > inline void DRM::CDrmQueue< T >::ConstructL()
       
    54     {
       
    55     User::LeaveIfError( iSemaphore.CreateLocal(1) );
       
    56     }
       
    57 
       
    58 
       
    59 // -----------------------------------------------------------------------------
       
    60 // CDrmQueue::NewLC
       
    61 // (other items were commented in a header).
       
    62 // -----------------------------------------------------------------------------
       
    63 //
       
    64 template< class T > inline DRM::CDrmQueue< T >* DRM::CDrmQueue< T >::NewLC()
       
    65     {
       
    66     DRM::CDrmQueue< T >* self( new( ELeave ) DRM::CDrmQueue< T >() );
       
    67     CleanupStack::PushL( self );
       
    68     self->ConstructL();
       
    69     return self;
       
    70     }
       
    71 
       
    72 
       
    73 // -----------------------------------------------------------------------------
       
    74 // CDrmQueue::NewL
       
    75 // (other items were commented in a header).
       
    76 // -----------------------------------------------------------------------------
       
    77 //
       
    78 template< class T > inline DRM::CDrmQueue< T >* DRM::CDrmQueue< T >::NewL()
       
    79     {
       
    80     DRM::CDrmQueue< T >* self( DRM::CDrmQueue< T >::NewLC() );
       
    81     CleanupStack::Pop( self );
       
    82     return self;
       
    83     }
       
    84 
       
    85 // -----------------------------------------------------------------------------
       
    86 // CDrmRightsInfoImpl::AppendToQueue
       
    87 // -----------------------------------------------------------------------------
       
    88 //
       
    89 template< class T > inline void DRM::CDrmQueue< T >::AppendToQueueL( T* aData )
       
    90     {
       
    91     CleanupStack::PushL( aData );
       
    92 
       
    93     QueueData* data( new (ELeave) QueueData );
       
    94     // insert the data into the queue object
       
    95     data->iData = aData;
       
    96     data->iNext = NULL;
       
    97 
       
    98     CleanupStack::Pop( aData );
       
    99 
       
   100     // Critical area start:
       
   101     iSemaphore.Wait();
       
   102 
       
   103     if( !iLast )
       
   104         {
       
   105         iFirst = data;
       
   106         iLast = iFirst;
       
   107         }
       
   108     else
       
   109         {
       
   110         iLast->iNext = data;
       
   111         iLast = data;
       
   112         }
       
   113 
       
   114     // Critical area end
       
   115     iSemaphore.Signal();
       
   116     }
       
   117 
       
   118 // -----------------------------------------------------------------------------
       
   119 // CDrmRightsInfoImpl::PopFront
       
   120 // -----------------------------------------------------------------------------
       
   121 //
       
   122 template< class T > inline T* DRM::CDrmQueue< T >::PopFront()
       
   123     {
       
   124     QueueData* data = NULL;
       
   125     T* payload = NULL;
       
   126 
       
   127     // If there is nothing in the queue return NULL
       
   128     if( !iFirst )
       
   129         {
       
   130         return payload;
       
   131         }
       
   132 
       
   133     // Critical area start:
       
   134     iSemaphore.Wait();
       
   135 
       
   136     data = iFirst;
       
   137 
       
   138     // Set the first and the next
       
   139     iFirst = iFirst->iNext;
       
   140 
       
   141     // reset the pointer on data
       
   142     data->iNext = NULL;
       
   143 
       
   144     // if there is no next in iFirst, this is the last or if there is no iFirst
       
   145     if( !iFirst || !iFirst->iNext )
       
   146         {
       
   147         iLast = iFirst;
       
   148         }
       
   149 
       
   150     // Critical Area End
       
   151     iSemaphore.Signal();
       
   152 
       
   153     payload = data->iData;
       
   154     delete data;
       
   155     data = NULL;
       
   156 
       
   157     return payload;
       
   158     }
       
   159 
       
   160 // -----------------------------------------------------------------------------
       
   161 // CDrmRightsInfoImpl::PopItem
       
   162 // -----------------------------------------------------------------------------
       
   163 //
       
   164 template< class T > inline T* DRM::CDrmQueue< T >::PopItem( TInt aId )
       
   165     {
       
   166     QueueData* current( iFirst );
       
   167     QueueData* previous( NULL );
       
   168     T* payload( NULL );
       
   169 
       
   170     // If there is nothing in the queue return NULL
       
   171     if( !iFirst )
       
   172         {
       
   173         return payload;
       
   174         }
       
   175     // Critical area start:
       
   176     iSemaphore.Wait();
       
   177     // Iterate over all items in the queue
       
   178     while ( current )
       
   179         {
       
   180         if ( aId == current->iData->iOperationId )
       
   181             {
       
   182             payload = current->iData;
       
   183             current->iData = NULL;
       
   184             if ( previous )
       
   185                 {
       
   186                 // Previous present. So its member iNext must be updated.
       
   187                 previous->iNext = current->iNext;
       
   188                 }
       
   189             else
       
   190                 {
       
   191                 // previous not present, so current was first,
       
   192                 // and iFirst must be updated.
       
   193                 iFirst = current->iNext;
       
   194                 }
       
   195 
       
   196             if ( !current->iNext )
       
   197                 {
       
   198                 // Current was last item. So iLast must be updated.
       
   199                 iLast = previous;
       
   200                 }
       
   201             current->iNext = NULL;
       
   202             delete current;
       
   203             break;
       
   204             }
       
   205         previous = current;
       
   206         current = current->iNext;
       
   207         }
       
   208     // Critical Area End
       
   209     iSemaphore.Signal();
       
   210     return payload;
       
   211     }
       
   212 
       
   213 //  End of File