mmresourcemgmt/mmresctrl/inc/mmrcutil.h
changeset 0 40261b775718
child 38 4269ca484c7b
equal deleted inserted replaced
-1:000000000000 0:40261b775718
       
     1 // Copyright (c) 2007-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 //
       
    15 
       
    16 #ifndef _MMRCUTIL_H_
       
    17 #define _MMRCUTIL_H_
       
    18 
       
    19 #include <e32base.h>
       
    20 
       
    21 template <class T>
       
    22 class RMMRCFifoQueue
       
    23 	{
       
    24 public:
       
    25 	RMMRCFifoQueue( );
       
    26 	~RMMRCFifoQueue( );
       
    27 
       
    28 	void Reset( );
       
    29 	void ResetAndDestroy();
       
    30 	TBool IsEmpty( ) const;
       
    31 	TInt Count() const;
       
    32 	T* PopAndRemove( );
       
    33 	T* PopAndRemove( TInt aIndex );
       
    34 	void PushL( T const* const  aElement );
       
    35 	T* Pop( ) const;
       
    36 	T* Pop( TInt aIndex ) const;
       
    37 	T* operator[] ( TInt aIndex ) const;
       
    38 
       
    39 private:
       
    40 	RMMRCFifoQueue( const RMMRCFifoQueue & aMMRCFifoQueue ); //not implemented
       
    41 	const RMMRCFifoQueue & operator= ( const RMMRCFifoQueue & aMMRCFifoQueue );  //not implemented
       
    42 
       
    43 private:
       
    44 	struct TListNode
       
    45 		{
       
    46 		T const*  iElement;
       
    47 		TListNode *iNext;
       
    48 
       
    49 		TListNode( T const* const aElement )
       
    50 		: iElement( aElement ), iNext(NULL) { }
       
    51 		};
       
    52 
       
    53 	TListNode *iFirst;
       
    54 	TListNode *iLast;
       
    55 	TInt iCount;
       
    56 	};
       
    57 	
       
    58 /**
       
    59  Construct the queue.
       
    60 */
       
    61 template <class T>
       
    62 RMMRCFifoQueue<T>::RMMRCFifoQueue( )
       
    63 	{
       
    64 	iFirst = iLast = NULL;
       
    65 	iCount = 0;
       
    66 	}
       
    67 
       
    68 /**
       
    69  Destructor.
       
    70 */
       
    71 template <class T>
       
    72 RMMRCFifoQueue<T>::~RMMRCFifoQueue( )
       
    73 	{
       
    74 	Reset( );
       
    75 	}
       
    76 
       
    77 
       
    78 /**
       
    79  Delete each node without deleting the elements.
       
    80 */
       
    81 template <class T>
       
    82 void RMMRCFifoQueue<T>::Reset( )
       
    83 	{
       
    84 	while( !IsEmpty( ) )
       
    85 		{
       
    86 		PopAndRemove( );
       
    87 		}
       
    88 	}
       
    89 
       
    90 /**
       
    91  Delete each node and each element.
       
    92 */
       
    93 template <class T>
       
    94 void RMMRCFifoQueue<T>::ResetAndDestroy( )
       
    95 	{
       
    96 	while( !IsEmpty( ) )
       
    97 		{
       
    98 		T* element = PopAndRemove( );
       
    99 		delete element;
       
   100 		}
       
   101 	}
       
   102 
       
   103 /**
       
   104  Return the number of elements.
       
   105 */
       
   106 template <class T>
       
   107 TInt RMMRCFifoQueue<T>::Count( ) const
       
   108 	{
       
   109 	return iCount;
       
   110 	}
       
   111 
       
   112 
       
   113 /**
       
   114  Test if the queue is logically empty.
       
   115  Return TTrue if empty, TFalse, otherwise.
       
   116 */
       
   117 template <class T>
       
   118 TBool RMMRCFifoQueue<T>::IsEmpty( ) const
       
   119 	{
       
   120 	return iFirst == NULL;
       
   121 	}
       
   122 
       
   123 
       
   124 /**
       
   125  Return and remove the least recently inserted item from the queue.
       
   126 */
       
   127 template <class T>
       
   128 T* RMMRCFifoQueue<T>::PopAndRemove( )
       
   129 	{
       
   130 	T* element = NULL;
       
   131 	if( !IsEmpty( ) )
       
   132 		{
       
   133 		TListNode* front = iFirst;
       
   134 		element = const_cast<T*>(iFirst->iElement);
       
   135 		iFirst = iFirst->iNext;
       
   136 		if( NULL == iFirst )
       
   137 			{
       
   138 			iLast = NULL;
       
   139 			}
       
   140 		iCount--;
       
   141 		delete front;
       
   142 		front = NULL;
       
   143 		}
       
   144 	return element;
       
   145 	}
       
   146 
       
   147 /**
       
   148  Return and remove the least recently inserted item from the queue.
       
   149 */
       
   150 template <class T>
       
   151 T* RMMRCFifoQueue<T>::PopAndRemove( TInt aIndex )
       
   152 	{
       
   153 	T* element = NULL;
       
   154 	TListNode* PreviousNode = NULL;
       
   155 	TListNode* node = iFirst;
       
   156 	TInt i = 0;
       
   157 	for( ; i<aIndex && i<iCount && node->iNext ; i++)
       
   158 		{
       
   159 		PreviousNode = node;
       
   160 		node = node->iNext;
       
   161 		}
       
   162 	
       
   163 	if(node && i < iCount)
       
   164 		{
       
   165 		if(PreviousNode)
       
   166 			{
       
   167 			PreviousNode->iNext = node->iNext;
       
   168 			if( NULL == node->iNext )
       
   169 				{
       
   170 				iLast = PreviousNode;
       
   171 				}
       
   172 			}
       
   173 		else 
       
   174 			{
       
   175 			iFirst = node->iNext;
       
   176 			if( NULL == iFirst )
       
   177 				{
       
   178 				iLast = NULL;
       
   179 				} 
       
   180 			}
       
   181 		element = const_cast<T*>(node->iElement);
       
   182 		iCount--;
       
   183 		delete node;
       
   184 		}
       
   185 	return element;
       
   186 	}
       
   187 
       
   188 /**
       
   189  Insert aElement into the queue.
       
   190 */
       
   191 template <class T>
       
   192 void RMMRCFifoQueue<T>::PushL( T const* const aElement )
       
   193 	{
       
   194 	if( IsEmpty( ) )
       
   195 		{
       
   196 		iLast = iFirst = new(ELeave) TListNode( aElement );
       
   197 		}
       
   198 	else
       
   199 		{
       
   200 		iLast = iLast->iNext = new(ELeave) TListNode( aElement );
       
   201 		}
       
   202 	iCount++;
       
   203 	}
       
   204 
       
   205 /**
       
   206  Pop a pointer of the aIndex elements.
       
   207 */
       
   208 template <class T>
       
   209 T* RMMRCFifoQueue<T>::Pop ( ) const
       
   210 	{
       
   211 	T* element = NULL;
       
   212 	TListNode* node = iFirst;
       
   213 	
       
   214 	if( node )
       
   215 		{
       
   216 		element = const_cast<T*>(node->iElement);
       
   217 		}
       
   218 	
       
   219 	return element;
       
   220 	}
       
   221 
       
   222 /**
       
   223  Pop a pointer of the aIndex elements.
       
   224 */
       
   225 template <class T>
       
   226 T* RMMRCFifoQueue<T>::Pop ( TInt aIndex ) const
       
   227 	{
       
   228 	T* element = NULL;
       
   229 	TListNode* node = iFirst;
       
   230 	
       
   231 	TInt i = 0;
       
   232 	for( ; (i < aIndex) && (i < iCount) && (node->iNext != NULL) ; i++)
       
   233 		{
       
   234 		node = node->iNext;
       
   235 		}
       
   236 	
       
   237 	if(  ( NULL != node ) 
       
   238 			&& ( i < iCount )  )
       
   239 		{
       
   240 		element = const_cast<T*>(node->iElement);
       
   241 		}
       
   242 	
       
   243 	return element;
       
   244 	}
       
   245 
       
   246 /**
       
   247  Pop a pointer of the aIndex elements.
       
   248 */
       
   249 template <class T>
       
   250 T* RMMRCFifoQueue<T>::operator[] ( TInt aIndex ) const
       
   251 	{
       
   252 	return Pop(aIndex);
       
   253 	}
       
   254 
       
   255 
       
   256 /**
       
   257  Class for manage the list of the sessions requesting pause.
       
   258 */
       
   259 template <class T>
       
   260 class RMMRCFifoOrderQueue : private RMMRCFifoQueue <T>
       
   261 	{
       
   262 
       
   263 public:
       
   264 	RMMRCFifoOrderQueue( );
       
   265 	~RMMRCFifoOrderQueue( );
       
   266 	void ResetAndDestroy( );
       
   267 	TInt Count() const;
       
   268 	void PushL( T const* const  aElement );
       
   269 	T* PopAndRemoveForPause( TInt aIndex );
       
   270 	T* operator[] ( TInt aIndex ) const;
       
   271 
       
   272 
       
   273 private:
       
   274 	RPointerArray< T >  todo;
       
   275 	};
       
   276 
       
   277 
       
   278 /**
       
   279  Construct the queue.
       
   280 */
       
   281 template <class T>
       
   282 RMMRCFifoOrderQueue<T>::RMMRCFifoOrderQueue( )
       
   283 	{
       
   284 	}
       
   285 
       
   286 /**
       
   287  Destructor.
       
   288 */
       
   289 template <class T>
       
   290 RMMRCFifoOrderQueue<T>::~RMMRCFifoOrderQueue( )
       
   291 	{
       
   292 	todo.Reset();
       
   293 	todo.Close();
       
   294 	}
       
   295 	
       
   296 /**
       
   297  Return the number of elements.
       
   298 */
       
   299 template <class T>
       
   300 TInt RMMRCFifoOrderQueue<T>::Count( ) const
       
   301 	{
       
   302 	
       
   303 	TInt iNum = todo.Count();
       
   304 	return iNum;
       
   305 	}	
       
   306 
       
   307 /**
       
   308  Delete each node and each element.
       
   309 */
       
   310 template <class T>
       
   311 void RMMRCFifoOrderQueue<T>::ResetAndDestroy( )
       
   312 	{
       
   313 	todo.ResetAndDestroy();
       
   314 	}
       
   315 
       
   316 /**
       
   317  Insert aElement into the queue and ordered.
       
   318 */
       
   319 template <class T>
       
   320 void RMMRCFifoOrderQueue<T>::PushL( T const* const aElement )
       
   321 	{
       
   322 	TInt numElements = todo.Count();
       
   323 	
       
   324 	if( numElements == 0 )
       
   325 		{
       
   326 		todo.Append(aElement); //TODO: We need to check the error here
       
   327 		return;
       
   328 		}
       
   329 	
       
   330 	for(TInt i(0); i<numElements ; ++i)
       
   331 		{
       
   332 		if(aElement->HasMultimediaCapability() && !(todo[i]->HasMultimediaCapability()))
       
   333 			{
       
   334 			todo.Insert(aElement, i);
       
   335 			return;
       
   336 			}
       
   337 		else if(aElement->HasMultimediaCapability() == todo[i]->HasMultimediaCapability())	
       
   338 			{
       
   339 			if (aElement->GetPriority() > todo[i]->GetPriority())
       
   340 			 	{
       
   341 				todo.Insert(aElement,i);
       
   342 				return;
       
   343 			 	}			
       
   344 			}
       
   345 		}
       
   346 	todo.Insert(aElement,numElements);
       
   347 	}
       
   348 	
       
   349 /**
       
   350  Return and remove the inserted item from the queue.
       
   351 */
       
   352 template <class T>
       
   353 T* RMMRCFifoOrderQueue<T>::PopAndRemoveForPause( TInt aIndex )
       
   354 	{
       
   355 	T* aux = NULL;
       
   356 	aux = todo[aIndex];
       
   357 	todo.Remove(aIndex);	
       
   358 	return aux;
       
   359 	}
       
   360 
       
   361 
       
   362 /**
       
   363  Pop a pointer of the aIndex elements.
       
   364 */
       
   365 template <class T>
       
   366 T* RMMRCFifoOrderQueue<T>::operator[] ( TInt aIndex ) const
       
   367 	{
       
   368 	if(todo.Count() != 0)
       
   369 		{
       
   370 		return todo[aIndex];
       
   371 		}
       
   372 	else
       
   373 		{
       
   374 		return NULL;
       
   375 		}
       
   376 	}
       
   377 
       
   378 #endif /*_MMRCUTIL_H_*/