You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ta...@apache.org on 2009/04/01 14:54:53 UTC
svn commit: r760872 -
/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/StlQueue.h
Author: tabish
Date: Wed Apr 1 12:54:50 2009
New Revision: 760872
URL: http://svn.apache.org/viewvc?rev=760872&view=rev
Log:
Add an Iterator to the StlQueue class
Modified:
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/StlQueue.h
Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/StlQueue.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/StlQueue.h?rev=760872&r1=760871&r2=760872&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/StlQueue.h (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/util/StlQueue.h Wed Apr 1 12:54:50 2009
@@ -19,6 +19,7 @@
#include <list>
#include <vector>
+#include <decaf/util/Iterator.h>
#include <decaf/util/concurrent/Mutex.h>
#include <decaf/lang/Exception.h>
@@ -56,12 +57,74 @@
*/
template <typename T> class StlQueue : public concurrent::Synchronizable {
+ private:
+
+ // The real queue
+ std::list<T> queue;
+
+ // Object used for sync
+ mutable util::concurrent::Mutex mutex;
+
+ private:
+
+ class QueueIterator : public Iterator<T> {
+ private:
+
+ typename std::list<T>::iterator current;
+ typename std::list<T>::iterator previous;
+ typename std::list<T>* queue;
+
+ public:
+
+ QueueIterator( typename std::list<T>* queue ) {
+ this->current = queue->begin();
+ this->previous = queue->end();
+ this->queue = queue;
+ }
+ virtual ~QueueIterator() {}
+
+ virtual T next() throw( lang::exceptions::NoSuchElementException ){
+ if( this->current == queue->end() ) {
+ throw lang::exceptions::NoSuchElementException(
+ __FILE__, __LINE__,
+ "Queue::Iterator::next - No more elements to return" );
+ }
+
+ this->previous = this->current;
+ return *( this->current++ );
+ }
+
+ virtual bool hasNext() const {
+ return ( this->current != queue->end() );
+ }
+
+ virtual void remove() throw ( lang::exceptions::IllegalStateException,
+ lang::exceptions::UnsupportedOperationException ){
+ if( this->previous == queue->end() ) {
+ throw lang::exceptions::IllegalStateException(
+ __FILE__, __LINE__,
+ "Queue::Iterator::remove - Invalid State to call remove" );
+ }
+
+ this->queue->erase( this->previous );
+ this->previous = this->queue->end();
+ }
+ };
+
public:
StlQueue() {}
virtual ~StlQueue() {}
/**
+ * Gets an Iterator over this Queue.
+ * @return new iterator pointer that is owned by the caller.
+ */
+ Iterator<T>* iterator() {
+ return new QueueIterator( &queue );
+ }
+
+ /**
* Empties this queue.
*/
void clear() {
@@ -250,14 +313,6 @@
return safe;
}
- private:
-
- // The real queue
- std::list<T> queue;
-
- // Object used for sync
- mutable util::concurrent::Mutex mutex;
-
};
}}