diff -r 7fdc9a71d314 -r 8ad140f3dd41 hti/PC_Tools/HTIGateway/HtiGateway/inc/safequeue.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/hti/PC_Tools/HTIGateway/HtiGateway/inc/safequeue.h Wed Oct 13 16:17:58 2010 +0300 @@ -0,0 +1,218 @@ +/* +* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). +* All rights reserved. +* This component and the accompanying materials are made available +* under the terms of "Eclipse Public License v1.0" +* which accompanies this distribution, and is available +* at the URL "http://www.eclipse.org/legal/epl-v10.html". +* +* Initial Contributors: +* Nokia Corporation - initial contribution. +* +* Contributors: +* +* Description: +* This file contains headers of SafeQueue template class. +*/ + +#ifndef SAFEQUEUE_H +#define SAFEQUEUE_H + +#include "sync.h" + +//STL +#pragma warning (push, 3) +#pragma warning( disable : 4702 ) // Unreachable code warning +#pragma warning( disable : 4290 ) // C++ exception specification ignored except to indicate a function is not __declspec(nothrow) +#include + + + + +using namespace std; +class Data; + +//********************************************************************************** +// Template Class SafeQueue +// +// This template class implements a thread safe queue with generic types +//********************************************************************************** + +template +class SafeQueue : private queue, + private Mutex, + private Semaphore +{ +public: + typedef typename queue::size_type size_type; + SafeQueue(); + ~SafeQueue(); + /* + * Adds a new element to the end of the queue + */ + void push(const T& val); + /* + * Removes the next element in the queue + */ + void pop(void); + /* + * Returns whether the queue is empty or not + */ + bool empty(void) const; + /* + * Returns a reference to the last element in the queue + */ + T& back(DWORD timeout = INFINITE) throw (TimeoutException); + /* + * Returns a constant reference to the last element in the queue + */ + const T& back(DWORD timeout = INFINITE) const throw (TimeoutException); + /* + * Returns a reference to the next element in the queue + */ + T& front(DWORD timeout = INFINITE) throw (TimeoutException); + /* + * Returns a constant reference to the next element in the queue + */ + const T& front(DWORD timeout = INFINITE) const throw (TimeoutException); + /* + * Returns the number of elements in the queue + */ + typename SafeQueue::size_type size(void) const; + /* + * This method is used to pop all of the elements in queue and return them in single Data object + */ + T& popAll(DWORD timeout = INFINITE) throw (TimeoutException); +}; + +template +SafeQueue::SafeQueue() +{ +} + +template +SafeQueue::~SafeQueue() +{ +} + +/* + * Adds a new element to the end of the queue + */ +template +void SafeQueue::push(const T& val) +{ + Mutex::Lock(); + queue::push(val); + Mutex::Unlock(); + Semaphore::Notify(); +} + +/* + * Removes the next element in the queue + */ +template +void SafeQueue::pop(void) +{ + Mutex::Lock(); + queue::pop(); + Mutex::Unlock(); +} + +/* + * Returns whether the queue is empty or not + */ +template +bool SafeQueue::empty(void) const +{ + const_cast*>(this)->Lock(); + bool value = queue::empty(); + const_cast*>(this)->Unlock(); + return value; +} + +/* + * Returns a reference to the last element in the queue + */ +template +T& SafeQueue::back(DWORD timeout) + throw (TimeoutException) +{ + if (Semaphore::Wait(timeout) == WAIT_TIMEOUT) + { + throw TimeoutException("queue back timed out"); + } + Mutex::Lock(); + T& value = queue::back(); + Mutex::Unlock(); + return value; +} + +/* + * Returns a constant reference to the last element in the queue + */ +template +const T& SafeQueue::back(DWORD timeout) const + throw (TimeoutException) +{ + if (const_cast*>(this)->Wait(timeout) == WAIT_TIMEOUT) + { + throw TimeoutException("queue back timed out");; + } + const_cast*>(this)->Lock(); + const T& value = queue::back(); + const_cast*>(this)->Unlock(); + return value; +} + +/* + * Returns a reference to the next element in the queue + */ +template +T& SafeQueue::front(DWORD timeout) + throw (TimeoutException) +{ + if (Semaphore::Wait(timeout) == WAIT_TIMEOUT) + { + throw TimeoutException("queue front timed out"); + } + Mutex::Lock(); + T& value = queue::front(); + Mutex::Unlock(); + return value; +} + +/* + * Returns a constant reference to the next element in the queue + */ +template +const T& SafeQueue::front(DWORD timeout) const + throw (TimeoutException) +{ + if (const_cast*>(this)->Wait(timeout) == WAIT_TIMEOUT) + { + throw TimeoutException("queue front timed out"); + } + const_cast*>(this)->Lock(); + const T& value = queue::front(); + const_cast*>(this)->Unlock(); + return value; +} + +/* + * Returns the number of elements in the queue + */ +template +typename SafeQueue::size_type SafeQueue::size(void) const +{ + const_cast*>(this)->Lock(); + queue::size_type value = queue::size(); + const_cast*>(this)->Unlock(); + return value; +} + + +#pragma warning (pop) + +#endif + +// End of the file \ No newline at end of file