hti/PC_Tools/DataGateway/INC/safequeue.h
branchRCL_3
changeset 59 8ad140f3dd41
parent 0 a03f92240627
equal deleted inserted replaced
49:7fdc9a71d314 59:8ad140f3dd41
       
     1 /*
       
     2 * Copyright (c) 2009 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:
       
    15 *    This file contains headers of SafeQueue template class.
       
    16 */
       
    17 
       
    18 #ifndef SAFEQUEUE_H
       
    19 #define SAFEQUEUE_H
       
    20 
       
    21 #include "sync.h"
       
    22 
       
    23 //STL
       
    24 #pragma warning (push, 3)
       
    25 #pragma warning( disable : 4702 ) // Unreachable code warning
       
    26 #pragma warning( disable : 4290 ) // C++ exception specification ignored except to indicate a function is not __declspec(nothrow)
       
    27 #include <queue>
       
    28 
       
    29 
       
    30 
       
    31 
       
    32 using namespace std;
       
    33 
       
    34 //**********************************************************************************
       
    35 // Template Class SafeQueue
       
    36 // 
       
    37 // This template class implements a thread safe queue with generic types
       
    38 //**********************************************************************************
       
    39 
       
    40 template <class T>
       
    41 class SafeQueue : private queue<T>,
       
    42 				  private Mutex,
       
    43 				  private Semaphore
       
    44 {
       
    45 public:
       
    46     typedef typename queue<T>::size_type size_type;
       
    47     SafeQueue();
       
    48 	~SafeQueue();
       
    49 	/*
       
    50 	 * Adds a new element to the end of the queue
       
    51 	 */
       
    52 	void push(const T& val);
       
    53 	/*
       
    54 	 * Removes the next element in the queue
       
    55 	 */
       
    56 	void pop(void);
       
    57 	/*
       
    58 	 * Returns whether the queue is empty or not
       
    59 	 */
       
    60 	bool empty(void) const;
       
    61 	/*
       
    62 	 * Returns a reference to the last element in the queue
       
    63 	 */
       
    64 	T& back(DWORD timeout = INFINITE) throw (TimeoutException);
       
    65 	/*
       
    66 	 * Returns a constant reference to the last element in the queue
       
    67 	 */
       
    68 	const T& back(DWORD timeout = INFINITE) const throw (TimeoutException);
       
    69 	/*
       
    70 	 * Returns a reference to the next element in the queue
       
    71 	 */
       
    72 	T& front(DWORD timeout = INFINITE) throw (TimeoutException);
       
    73 	/*
       
    74 	 * Returns a constant reference to the next element in the queue
       
    75 	 */
       
    76 	const T& front(DWORD timeout = INFINITE) const throw (TimeoutException);
       
    77 	/*
       
    78 	 * Returns the number of elements in the queue
       
    79 	 */
       
    80 	typename SafeQueue<T>::size_type size(void) const;
       
    81 	/*
       
    82 	 * This method is used to pop all of the elements in queue and return them in single Data object
       
    83 	 */
       
    84 	T& popAll(DWORD timeout = INFINITE) throw (TimeoutException);
       
    85 };
       
    86 
       
    87 template <class T>
       
    88 SafeQueue<T>::SafeQueue()
       
    89 {
       
    90 }
       
    91 
       
    92 template <class T>
       
    93 SafeQueue<T>::~SafeQueue()
       
    94 {
       
    95 }
       
    96 
       
    97 /*
       
    98  * Adds a new element to the end of the queue
       
    99  */
       
   100 template <class T>
       
   101 void SafeQueue<T>::push(const T& val)
       
   102 {
       
   103 	Mutex::Lock();
       
   104 	queue<T>::push(val);
       
   105 	Mutex::Unlock();
       
   106 	Semaphore::Notify();
       
   107 }
       
   108 
       
   109 /*
       
   110  * Removes the next element in the queue
       
   111  */
       
   112 template <class T>
       
   113 void SafeQueue<T>::pop(void)
       
   114 {
       
   115 	Mutex::Lock();
       
   116 	queue<T>::pop();
       
   117 	Mutex::Unlock();
       
   118 }
       
   119 
       
   120 /*
       
   121  * Returns whether the queue is empty or not
       
   122  */
       
   123 template <class T>
       
   124 bool SafeQueue<T>::empty(void) const
       
   125 {
       
   126 	const_cast<SafeQueue<T>*>(this)->Lock();
       
   127 	bool value = queue<T>::empty();
       
   128 	const_cast<SafeQueue<T>*>(this)->Unlock();
       
   129 	return value;
       
   130 }
       
   131 
       
   132 /*
       
   133  * Returns a reference to the last element in the queue
       
   134  */
       
   135 template <class T>
       
   136 T& SafeQueue<T>::back(DWORD timeout)
       
   137 	throw (TimeoutException)
       
   138 {
       
   139 	if (Semaphore::Wait(timeout) == WAIT_TIMEOUT)
       
   140 	{
       
   141 		throw TimeoutException("queue back timed out");
       
   142 	}
       
   143 	Mutex::Lock();
       
   144 	T& value = queue<T>::back();
       
   145 	Mutex::Unlock();
       
   146 	return value;
       
   147 }
       
   148 
       
   149 /*
       
   150  * Returns a constant reference to the last element in the queue
       
   151  */
       
   152 template <class T>
       
   153 const T& SafeQueue<T>::back(DWORD timeout) const
       
   154 	throw (TimeoutException)
       
   155 {
       
   156 	if (const_cast<SafeQueue<T>*>(this)->Wait(timeout) == WAIT_TIMEOUT)
       
   157 	{
       
   158 		throw TimeoutException("queue back timed out");;
       
   159 	}
       
   160 	const_cast<SafeQueue<T>*>(this)->Lock();
       
   161 	const T& value = queue<T>::back();
       
   162 	const_cast<SafeQueue<T>*>(this)->Unlock();
       
   163 	return value;
       
   164 }
       
   165 
       
   166 /*
       
   167  * Returns a reference to the next element in the queue
       
   168  */
       
   169 template <class T>
       
   170 T& SafeQueue<T>::front(DWORD timeout)
       
   171 	throw (TimeoutException)
       
   172 {
       
   173 	if (Semaphore::Wait(timeout) == WAIT_TIMEOUT)
       
   174 	{
       
   175 		throw TimeoutException("queue front timed out");
       
   176 	}
       
   177 	Mutex::Lock();
       
   178 	T& value = queue<T>::front();
       
   179 	Mutex::Unlock();
       
   180 	return value;
       
   181 }
       
   182 
       
   183 /*
       
   184  * Returns a constant reference to the next element in the queue
       
   185  */
       
   186 template <class T>
       
   187 const T& SafeQueue<T>::front(DWORD timeout) const
       
   188 	throw (TimeoutException)
       
   189 {
       
   190 	if (const_cast<SafeQueue<T>*>(this)->Wait(timeout) == WAIT_TIMEOUT)
       
   191 	{
       
   192 		throw TimeoutException("queue front timed out");
       
   193 	}
       
   194 	const_cast<SafeQueue<T>*>(this)->Lock();
       
   195 	const T& value = queue<T>::front();
       
   196 	const_cast<SafeQueue<T>*>(this)->Unlock();
       
   197 	return value;
       
   198 }
       
   199 
       
   200 /*
       
   201  * Returns the number of elements in the queue
       
   202  */
       
   203 template <class T>
       
   204 typename SafeQueue<T>::size_type SafeQueue<T>::size(void) const
       
   205 {
       
   206 	const_cast<SafeQueue<T>*>(this)->Lock();
       
   207 	queue<T>::size_type value = queue<T>::size();
       
   208 	const_cast<SafeQueue<T>*>(this)->Unlock();
       
   209 	return value;
       
   210 }
       
   211 
       
   212 #pragma warning (pop)
       
   213 
       
   214 #endif
       
   215 
       
   216 // End of the file