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