activemq-cpp-3.9.5
StlQueue.h
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17#ifndef _DECAF_UTIL_STLQUEUE_H_
18#define _DECAF_UTIL_STLQUEUE_H_
19
20#include <list>
21#include <vector>
22#include <decaf/util/Iterator.h>
25
26namespace decaf{
27namespace util{
28
58
59 template <typename T>
61 private:
62
63 // The real queue
64 std::list<T> queue;
65
66 // Object used for sync
67 mutable util::concurrent::Mutex mutex;
68
69 private:
70
71 class QueueIterator : public Iterator<T> {
72 private:
73
74 typename std::list<T>::iterator current;
75 typename std::list<T>::iterator previous;
76 typename std::list<T>* queue;
77
78 public:
79
80 QueueIterator( typename std::list<T>* queue ) :
81 current( queue->begin() ), previous( queue->end() ), queue( queue ) {
82 }
83
84 virtual ~QueueIterator() {}
85
86 virtual T next() {
87 if( this->current == queue->end() ) {
89 __FILE__, __LINE__,
90 "Queue::Iterator::next - No more elements to return" );
91 }
92
93 this->previous = this->current;
94 return *( this->current++ );
95 }
96
97 virtual bool hasNext() const {
98 return ( this->current != queue->end() );
99 }
100
101 virtual void remove() {
102 if( this->previous == queue->end() ) {
104 __FILE__, __LINE__,
105 "Queue::Iterator::remove - Invalid State to call remove" );
106 }
107
108 this->queue->erase( this->previous );
109 this->previous = this->queue->end();
110 }
111 };
112
113 public:
114
115 StlQueue() : queue(), mutex() {}
116
117 virtual ~StlQueue() {}
118
124 return new QueueIterator( &queue );
125 }
126
130 void clear() {
131 queue.clear();
132 }
133
138 T& front() {
139 if( queue.empty() ) {
140 return getSafeValue();
141 }
142
143 return queue.front();
144 }
145
150 const T& front() const {
151 if( queue.empty() ) {
152 return getSafeValue();
153 }
154
155 return queue.front();
156 }
157
162 T& back() {
163 if( queue.empty() ) {
164 return getSafeValue();
165 }
166
167 return queue.back();
168 }
169
174 const T& back() const {
175 if( queue.empty() ) {
176 return getSafeValue();
177 }
178
179 return queue.back();
180 }
181
186 void push( const T &t ) {
187 queue.push_back( t );
188 }
189
194 void enqueueFront( const T &t ) {
195 queue.push_front( t );
196 }
197
202 T pop() {
203 if( queue.empty() ) {
204 return getSafeValue();
205 }
206
207 // Pop the element into a temp, since we need to remain locked.
208 // this means getting front and then popping.
209 T temp = queue.front();
210 queue.pop_front();
211
212 return temp;
213 }
214
219 size_t size() const{
220 return queue.size();
221 }
222
227 bool empty() const {
228 return queue.empty();
229 }
230
234 virtual std::vector<T> toArray() const {
235 std::vector<T> valueArray( queue.begin(), queue.end() );
236 return valueArray;
237 }
238
245 void reverse( StlQueue<T>& target ) const {
246 target.queue.insert( target.queue.end(), queue.rbegin(), queue.rend() );
247 }
248
249 public: // Synchronizable
250
251 virtual void lock() {
252 mutex.lock();
253 }
254
255 virtual bool tryLock() {
256 return mutex.tryLock();
257 }
258
259 virtual void unlock() {
260 mutex.unlock();
261 }
262
263 virtual void wait() {
264 mutex.wait();
265 }
266
267 virtual void wait( long long millisecs ) {
268 mutex.wait( millisecs );
269 }
270
271 virtual void wait( long long millisecs, int nanos ) {
272 mutex.wait( millisecs, nanos );
273 }
274
275 virtual void notify() {
276 mutex.notify();
277 }
278
279 virtual void notifyAll() {
280 mutex.notifyAll();
281 }
282
283 public: // Statics
284
291 static T safe;
292 return safe;
293 }
294
295 };
296
297}}
298
299#endif /*_DECAF_UTIL_STLQUEUE_H_*/
Definition IllegalStateException.h:32
Defines an object that can be used to iterate over the elements of a collection.
Definition Iterator.h:34
Definition NoSuchElementException.h:31
size_t size() const
Gets the Number of elements currently in the Queue.
Definition StlQueue.h:219
virtual void wait(long long millisecs, int nanos)
Waits on a signal from this object, which is generated by a call to Notify.
Definition StlQueue.h:271
const T & back() const
Returns a Reference to the element at the tail of the queue.
Definition StlQueue.h:174
void enqueueFront(const T &t)
Places a new Object at the front of the queue.
Definition StlQueue.h:194
virtual void lock()
Locks the object.
Definition StlQueue.h:251
virtual ~StlQueue()
Definition StlQueue.h:117
bool empty() const
Checks if this Queue is currently empty.
Definition StlQueue.h:227
Iterator< T > * iterator()
Gets an Iterator over this Queue.
Definition StlQueue.h:123
virtual void wait()
Waits on a signal from this object, which is generated by a call to Notify.
Definition StlQueue.h:263
virtual void unlock()
Unlocks the object.
Definition StlQueue.h:259
StlQueue()
Definition StlQueue.h:115
const T & front() const
Returns a Reference to the element at the head of the queue.
Definition StlQueue.h:150
T & back()
Returns a Reference to the element at the tail of the queue.
Definition StlQueue.h:162
virtual void notifyAll()
Signals the waiters on this object that it can now wake up and continue.
Definition StlQueue.h:279
virtual std::vector< T > toArray() const
Definition StlQueue.h:234
virtual bool tryLock()
Attempts to Lock the object, if the lock is already held by another thread than this method returns f...
Definition StlQueue.h:255
virtual void wait(long long millisecs)
Waits on a signal from this object, which is generated by a call to Notify.
Definition StlQueue.h:267
T pop()
Removes and returns the element that is at the Head of the queue.
Definition StlQueue.h:202
void clear()
Empties this queue.
Definition StlQueue.h:130
void push(const T &t)
Places a new Object at the Tail of the queue.
Definition StlQueue.h:186
virtual void notify()
Signals a waiter on this object that it can now wake up and continue.
Definition StlQueue.h:275
void reverse(StlQueue< T > &target) const
Reverses the order of the contents of this queue and stores them in the target queue.
Definition StlQueue.h:245
T & front()
Returns a Reference to the element at the head of the queue.
Definition StlQueue.h:138
T & getSafeValue()
Fetch a reference to the safe value this object will return when there is nothing to fetch from the q...
Definition StlQueue.h:290
Mutex object that offers recursive support on all platforms as well as providing the ability to use t...
Definition Mutex.h:39
The interface for all synchronizable objects (that is, objects that can be locked and unlocked).
Definition Synchronizable.h:37
Definition AbstractCollection.h:33
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
Definition AprPool.h:25