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 2007/07/11 14:03:07 UTC
svn commit: r555252 - in /activemq/activemq-cpp/trunk/src/decaf/src/main:
Makefile.am decaf/util/Iterator.h decaf/util/Map.h decaf/util/Properties.h
decaf/util/Queue.h decaf/util/Set.h decaf/util/concurrent/Mutex.cpp
decaf/util/concurrent/Mutex.h
Author: tabish
Date: Wed Jul 11 05:03:06 2007
New Revision: 555252
URL: http://svn.apache.org/viewvc?view=rev&rev=555252
Log:
http://issues.apache.org/activemq/browse/AMQCPP-103
Added:
activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/Iterator.h
Modified:
activemq/activemq-cpp/trunk/src/decaf/src/main/Makefile.am
activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/Map.h
activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/Properties.h
activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/Queue.h
activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/Set.h
activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/concurrent/Mutex.cpp
activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/concurrent/Mutex.h
Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/Makefile.am
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/Makefile.am?view=diff&rev=555252&r1=555251&r2=555252
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/Makefile.am (original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/Makefile.am Wed Jul 11 05:03:06 2007
@@ -108,6 +108,7 @@
decaf/util/concurrent/ThreadPool.h \
decaf/util/Date.h \
decaf/util/Guid.h \
+ decaf/util/Iterator.h \
decaf/util/Map.h \
decaf/util/Properties.h \
decaf/util/Queue.h \
Added: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/Iterator.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/Iterator.h?view=auto&rev=555252
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/Iterator.h (added)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/Iterator.h Wed Jul 11 05:03:06 2007
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _DECAF_UTIL_ITERATOR_H_
+#define _DECAF_UTIL_ITERATOR_H_
+
+#include <decaf/lang/exceptions/NoSuchElementException.h>
+#include <decaf/lang/exceptions/IllegalStateException.h>
+#include <decaf/lang/exceptions/UnsupportedOperationException.h>
+
+namespace decaf{
+namespace util{
+
+ /**
+ * Defines an object that can be used to iterate over the elements of a
+ * collection. The iterator provides a way to access and remove elements
+ * with well defined semantics.
+ */
+ template< typename T>
+ class DECAF_API Iterator {
+ public:
+
+ virtual ~Iterator() {}
+
+ /**
+ * Returns the next element in the iteration. Calling this method
+ * repeatedly until the hasNext() method returns false will return
+ * each element in the underlying collection exactly once.
+ * @returns next element in the iteration of elements
+ * @throws NoSuchElementException - iteration has no more elements.
+ */
+ virtual T next() throw( lang::exceptions::NoSuchElementException ) = 0;
+
+ /**
+ * Returns true if the iteration has more elements. Returns false if
+ * the next call to next would result in an NoSuchElementException to
+ * be thrown.
+ */
+ virtual bool hasNext() const = 0;
+
+ /**
+ * Removes from the underlying collection the last element returned
+ * by the iterator (optional operation). This method can be called
+ * only once per call to next. The behavior of an iterator is
+ * unspecified if the underlying collection is modified while the
+ * iteration is in progress in any way other than by calling this
+ * method.
+ * @throws UnsupportedOperationException - if the remove operation
+ * is not supported by this Iterator.
+ * @throws IllegalStateException - if the next method has not yet been
+ * called, or the remove method has already been called after
+ * the last call to the next method.
+ */
+ virtual void remove() throw ( lang::exceptions::IllegalStateException,
+ lang::exceptions::UnsupportedOperationException ) = 0;
+
+ };
+
+}}
+
+#endif /*_DECAF_UTIL_ITERATOR_H_*/
Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/Map.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/Map.h?view=diff&rev=555252&r1=555251&r2=555252
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/Map.h (original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/Map.h Wed Jul 11 05:03:06 2007
@@ -32,8 +32,7 @@
* a more user-friendly interface and to provide common
* functions that do not exist in std::map.
*/
- template <typename K, typename V> class DECAF_API Map :
- public concurrent::Synchronizable
+ template <typename K, typename V> class DECAF_API Map : public concurrent::Synchronizable
{
private:
@@ -45,18 +44,18 @@
/**
* Default constructor - does nothing.
*/
- Map(){};
+ Map() {}
/**
* Copy constructor - copies the content of the given map into this
* one.
* @param source The source map.
*/
- Map( const Map& source ){
+ Map( const Map& source ) {
copy( source );
}
- virtual ~Map(){};
+ virtual ~Map() {}
/**
* Comparison, equality is dependant on the method of determining
@@ -64,19 +63,30 @@
* @param source - Map to compare to this one.
* @returns true if the Map passed is equal in value to this one.
*/
- virtual bool equals( const Map& source ) const;
+ virtual bool equals( const Map& source ) const {
+ return this->valueMap == source.valueMap;
+ }
/**
* Copies the content of the source map into this map. Erases
* all existing data in this map.
* @param source The source object to copy from.
*/
- virtual void copy( const Map& source );
+ virtual void copy( const Map& source ) {
+
+ // Erase the content of this object, and copy from the source
+ // all the elements. We access source's private map since we
+ // are also a Map, this saves us a lot of time.
+ valueMap.clear();
+ valueMap.insert( source.valueMap.begin(), source.valueMap.end() );
+ }
/**
* Removes all keys and values from this map.
*/
- virtual void clear();
+ virtual void clear() {
+ valueMap.clear();
+ }
/**
* Indicates whether or this map contains a value for the
@@ -84,7 +94,11 @@
* @param key The key to look up.
* @return true if this map contains the value, otherwise false.
*/
- virtual bool containsKey( const K& key ) const;
+ virtual bool containsKey( const K& key ) const {
+ typename std::map<K,V>::const_iterator iter;
+ iter = valueMap.find(key);
+ return iter != valueMap.end();
+ }
/**
* Indicates whether or this map contains a value for the
@@ -93,17 +107,35 @@
* @param value The Value to look up.
* @return true if this map contains the value, otherwise false.
*/
- virtual bool containsValue( const V& value ) const;
+ virtual bool containsValue( const V& value ) const {
+
+ if( valueMap.empty() ){
+ return false;
+ }
+
+ typename std::map<K,V>::const_iterator iter = valueMap.begin();
+ for( ; iter != valueMap.end(); ++iter ){
+ if( (*iter).second == value ) {
+ return true;
+ }
+ }
+
+ return false;
+ }
/**
* @return if the Map contains any element or not, TRUE or FALSE
*/
- virtual bool isEmpty() const;
+ virtual bool isEmpty() const {
+ return valueMap.empty();
+ }
/**
* @return The number of elements (key/value pairs) in this map.
*/
- virtual std::size_t size() const;
+ virtual std::size_t size() const {
+ return valueMap.size();
+ }
/**
* Gets the value for the specified key.
@@ -112,31 +144,66 @@
* @throws activemq::exceptions::NoSuchElementException
*/
virtual V getValue( const K& key ) const
- throw( lang::exceptions::NoSuchElementException );
+ throw( lang::exceptions::NoSuchElementException ) {
+
+ typename std::map<K,V>::const_iterator iter;
+ iter = valueMap.find(key);
+ if( iter == valueMap.end() ){
+ throw activemq::exceptions::NoSuchElementException( __FILE__,
+ __LINE__,
+ "Key does not exist in map" );
+ }
+
+ return iter->second;
+ }
/**
* Sets the value for the specified key.
* @param key The target key.
* @param value The value to be set.
*/
- virtual void setValue( const K& key, V value );
+ virtual void setValue( const K& key, V value ) {
+ valueMap[key] = value;
+ }
/**
* Removes the value (key/value pair) for the specified key from
* the map.
* @param key The search key.
*/
- virtual void remove( const K& key );
+ virtual void remove( const K& key ) {
+ valueMap.erase( key );
+ }
/**
* @return the entire set of keys in this map as a std::vector.
*/
- virtual std::vector<K> getKeys() const;
+ virtual std::vector<K> getKeys() const{
+ std::vector<K> keys( valueMap.size() );
+
+ typename std::map<K,V>::const_iterator iter;
+ iter=valueMap.begin();
+ for( int ix=0; iter != valueMap.end(); ++iter, ++ix ){
+ keys[ix] = iter->first;
+ }
+
+ return keys;
+ }
/**
* @return the entire set of values in this map as a std::vector.
*/
- virtual std::vector<V> getValues() const;
+ virtual std::vector<V> getValues() const {
+ std::vector<V> values( valueMap.size() );
+
+ typename std::map<K,V>::const_iterator iter;
+ iter=valueMap.begin();
+ for( int ix=0; iter != valueMap.end(); ++iter, ++ix ){
+ values[ix] = iter->second;
+ }
+
+ return values;
+ }
public: // Methods from Synchronizable
@@ -144,7 +211,7 @@
* Locks the object.
* @throws ActiveMQException
*/
- virtual void lock() throw(lang::Exception) {
+ virtual void lock() throw( lang::Exception ) {
mutex.lock();
}
@@ -152,7 +219,7 @@
* Unlocks the object.
* @throws ActiveMQException
*/
- virtual void unlock() throw(lang::Exception) {
+ virtual void unlock() throw( lang::Exception ) {
mutex.unlock();
}
@@ -162,7 +229,7 @@
* calling.
* @throws ActiveMQException
*/
- virtual void wait() throw(lang::Exception) {
+ virtual void wait() throw( lang::Exception ) {
mutex.wait();
}
@@ -175,8 +242,8 @@
* WAIT_INIFINITE
* @throws ActiveMQException
*/
- virtual void wait(unsigned long millisecs)
- throw(lang::Exception) {
+ virtual void wait( unsigned long millisecs )
+ throw( lang::Exception ) {
mutex.wait(millisecs);
}
@@ -200,130 +267,6 @@
mutex.notifyAll();
}
};
-
- ////////////////////////////////////////////////////////////////////////////
- template <typename K, typename V>
- bool Map<K,V>::equals( const Map& source ) const {
- return this->valueMap == source.valueMap;
- }
-
- ////////////////////////////////////////////////////////////////////////////
- template <typename K, typename V>
- void Map<K,V>::copy( const Map<K,V>& source ) {
-
- // Get an iterator to the beginning of the source map.
- typename std::map<K,V>::const_iterator iter;
- iter = source.valueMap.begin();
-
- // Erase the content of this object.
- clear();
-
- // Add all of the entries to this map.
- for( ; iter != source.valueMap.end(); iter++ ){
- setValue( iter->first, iter->second );
- }
-
- }
-
- ////////////////////////////////////////////////////////////////////////////
- template <typename K, typename V>
- void Map<K,V>::clear(){
- valueMap.clear();
- }
-
- ////////////////////////////////////////////////////////////////////////////
- template <typename K, typename V>
- bool Map<K,V>::containsKey(const K& key) const{
- typename std::map<K,V>::const_iterator iter;
- iter = valueMap.find(key);
- return iter != valueMap.end();
- }
-
- ////////////////////////////////////////////////////////////////////////////
- template <typename K, typename V>
- bool Map<K,V>::containsValue( const V& value ) const {
-
- if( valueMap.empty() ){
- return false;
- }
-
- typename std::map<K,V>::const_iterator iter = valueMap.begin();
- for( ; iter != valueMap.end(); ++iter ){
- if( (*iter).second == value ) {
- return true;
- }
- }
-
- return false;
- }
-
- ////////////////////////////////////////////////////////////////////////////
- template <typename K, typename V>
- bool Map<K,V>::isEmpty() const{
- return valueMap.empty();
- }
-
- ////////////////////////////////////////////////////////////////////////////
- template <typename K, typename V>
- std::size_t Map<K,V>::size() const{
- return valueMap.size();
- }
-
- ////////////////////////////////////////////////////////////////////////////
- template <typename K, typename V>
- V Map<K,V>::getValue( const K& key ) const
- throw(activemq::exceptions::NoSuchElementException){
-
- typename std::map<K,V>::const_iterator iter;
- iter = valueMap.find(key);
- if( iter == valueMap.end() ){
- throw activemq::exceptions::NoSuchElementException( __FILE__,
- __LINE__,
- "Key does not exist in map" );
- }
-
- return iter->second;
- }
-
- ////////////////////////////////////////////////////////////////////////////
- template <typename K, typename V>
- void Map<K,V>::setValue( const K& key, V value ){
- valueMap[key] = value;
- }
-
- ////////////////////////////////////////////////////////////////////////////
- template <typename K, typename V>
- void Map<K,V>::remove( const K& key ){
- valueMap.erase(key);
- }
-
- ////////////////////////////////////////////////////////////////////////////
- template <typename K, typename V>
- std::vector<K> Map<K,V>::getKeys() const{
- std::vector<K> keys(valueMap.size());
-
- typename std::map<K,V>::const_iterator iter;
- iter=valueMap.begin();
- for( int ix=0; iter != valueMap.end(); ++iter, ++ix ){
- keys[ix] = iter->first;
- }
-
- return keys;
- }
-
- ////////////////////////////////////////////////////////////////////////////
- template <typename K, typename V>
- std::vector<V> Map<K,V>::getValues() const{
- std::vector<V> values(valueMap.size());
-
- typename std::map<K,V>::const_iterator iter;
- iter=valueMap.begin();
- for( int ix=0; iter != valueMap.end(); ++iter, ++ix ){
- values[ix] = iter->second;
- }
-
- return values;
- }
}}
Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/Properties.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/Properties.h?view=diff&rev=555252&r1=555251&r2=555252
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/Properties.h (original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/Properties.h Wed Jul 11 05:03:06 2007
@@ -22,7 +22,6 @@
#include <vector>
#include <string>
#include <sstream>
-
#include <decaf/util/Config.h>
namespace decaf{
@@ -94,6 +93,7 @@
virtual void setProperty( const std::string& name,
const std::string& value ){
properties[name] = value;
+ //properties.insert( std::make_pair( name, value ) );
}
/**
@@ -128,15 +128,8 @@
virtual std::vector< std::pair< std::string, std::string > > toArray() const{
// Create a vector big enough to hold all the elements in the map.
- std::vector< std::pair<std::string, std::string> > vec( properties.size() );
-
- // Get an iterator at the beginning of the map.
- std::map< std::string, std::string >::const_iterator iter = properties.begin();
-
- // Copy all of the elements from the map to the vector.
- for( int ix=0; iter != properties.end(); ++iter, ++ix ){
- vec[ix] = *iter;
- }
+ std::vector< std::pair<std::string, std::string> > vec(
+ properties.begin(), properties.end() );
return vec;
}
@@ -148,12 +141,7 @@
virtual void copy( const Properties* source ){
clear();
-
- std::vector< std::pair< std::string, std::string > > vec =
- source->toArray();
- for( unsigned int ix=0; ix<vec.size(); ++ix ){
- properties[vec[ix].first] = vec[ix].second;
- }
+ this->properties = source->properties;
}
/**
@@ -186,14 +174,14 @@
std::ostringstream stream;
std::map< std::string, std::string >::const_iterator iter;
- stream << "Begin Class decaf::util::Properties:" << std::endl;
+ stream << "Begin Class activemq::util::Properties:" << std::endl;
for( iter = properties.begin(); iter != properties.end(); ++iter ){
stream << " properties[" << iter->first << "] = "
<< iter->second << std::endl;
}
- stream << "End Class decaf::util::Properties:" << std::endl;
+ stream << "End Class activemq::util::Properties:" << std::endl;
return stream.str();
}
Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/Queue.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/Queue.h?view=diff&rev=555252&r1=555251&r2=555252
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/Queue.h (original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/Queue.h Wed Jul 11 05:03:06 2007
@@ -14,8 +14,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-#ifndef _DECAF_UTIL_QUEUE_H
-#define _DECAF_UTIL_QUEUE_H
+#ifndef _DECAF_UTIL_QUEUE_H_
+#define _DECAF_UTIL_QUEUE_H_
#include <list>
#include <vector>
@@ -55,76 +55,123 @@
* polling loop to ensure that you don't get stuck there.
*/
- template <typename T> class DECAF_API Queue : public concurrent::Synchronizable
- {
+ template <typename T> class DECAF_API Queue : public concurrent::Synchronizable {
public:
- Queue();
- virtual ~Queue();
+ Queue() {}
+ virtual ~Queue() {}
/**
* Empties this queue.
*/
- void clear();
+ void clear() {
+ queue.clear();
+ }
/**
* Returns a Reference to the element at the head of the queue
* @return reference to a queue type object or (safe)
*/
- T& front();
+ T& front() {
+ if( queue.empty() ) {
+ return safe;
+ }
+
+ return queue.front();
+ }
/**
* Returns a Reference to the element at the head of the queue
* @return reference to a queue type object or (safe)
*/
- const T& front() const;
+ const T& front() const {
+ if( queue.empty() ) {
+ return safe;
+ }
+
+ return queue.front();
+ }
/**
* Returns a Reference to the element at the tail of the queue
* @return reference to a queue type object or (safe)
*/
- T& back();
+ T& back() {
+ if( queue.empty() ) {
+ return safe;
+ }
+
+ return queue.back();
+ }
/**
* Returns a Reference to the element at the tail of the queue
* @return reference to a queue type object or (safe)
*/
- const T& back() const;
+ const T& back() const {
+ if( queue.empty() ) {
+ return safe;
+ }
+
+ return queue.back();
+ }
/**
* Places a new Object at the Tail of the queue
* @param t - Queue Object Type reference.
*/
- void push( const T &t );
+ void push( const T &t ) {
+ queue.push_back( t );
+ }
/**
* Places a new Object at the front of the queue
* @param t - Queue Object Type reference.
*/
- void enqueueFront( const T &t );
+ void enqueueFront( const T &t ) {
+ queue.push_front( t );
+ }
/**
* Removes and returns the element that is at the Head of the queue
* @return reference to a queue type object or (safe)
*/
- T pop();
+ T pop() {
+ if( queue.empty() ) {
+ return safe;
+ }
+
+ // Pop the element into a temp, since we need to remain locked.
+ // this means getting front and then popping.
+ T temp = queue.front();
+ queue.pop_front();
+
+ return temp;
+ }
/**
* Gets the Number of elements currently in the Queue
* @return Queue Size
*/
- size_t size() const;
+ size_t size() const{
+ return queue.size();
+ }
/**
* Checks if this Queue is currently empty
* @return boolean indicating queue emptiness
*/
- bool empty() const;
+ bool empty() const {
+ return queue.empty();
+ }
/**
* @return the all values in this queue as a std::vector.
*/
- virtual std::vector<T> toArray() const;
+ virtual std::vector<T> toArray() const {
+ std::vector<T> valueArray( queue.begin(), queue.end() );
+ return valueArray;
+ }
/**
* Reverses the order of the contents of this queue and stores them
@@ -132,7 +179,9 @@
* @param target - The target queue that will receive the contents of
* this queue in reverse order.
*/
- void reverse( Queue<T>& target ) const;
+ void reverse( Queue<T>& target ) const {
+ target.queue.insert( target.queue.end(), queue.rbegin(), queue.rend() );
+ }
/**
* Locks the object.
@@ -204,7 +253,7 @@
std::list<T> queue;
// Object used for sync
- concurrent::Mutex mutex;
+ util::concurrent::Mutex mutex;
// Safe value used when pop, front or back are
// called and the queue is empty.
@@ -213,150 +262,8 @@
};
//-----{ Static Init }----------------------------------------------------//
- template <typename T>
- T Queue<T>::safe;
-
- //-----{ Retrieve current length of Queue }-------------------------------//
-
- template <typename T> inline size_t Queue<T>::size() const
- {
- return queue.size();
- }
-
- //-----{ Retrieve whether Queue is empty or not }-------------------------//
-
- template <typename T> inline bool Queue<T>::empty() const
- {
- return queue.empty();
- }
-
- //-----{ Defulat Constructor }--------------------------------------------//
-
- template <typename T>
- Queue<T>::Queue()
- {
- }
-
- //-----{ Default Destructor }---------------------------------------------//
-
- template <typename T> Queue<T>::~Queue()
- {
- }
-
- template <typename T>
- void Queue<T>::clear()
- {
- queue.clear();
- }
-
- //-----{ Add Elements to Back of Queue }----------------------------------//
-
- template <typename T>
- void Queue<T>::push( const T &t )
- {
- queue.push_back( t );
- }
-
- template <typename T>
- void Queue<T>::enqueueFront( const T &t )
- {
- queue.push_front( t );
- }
-
- //-----{ Remove Elements from Front of Queue }----------------------------//
-
- template <typename T>
- T Queue<T>::pop()
- {
- if(queue.empty())
- {
- return safe;
- }
-
- // Pop the element into a temp, since we need to remain locked.
- // this means getting front and then popping.
- T temp = queue.front();
- queue.pop_front();
-
- return temp;
- }
-
- //-----{ Returnreference to element at front of Queue }-------------------//
-
- template <typename T>
- T& Queue<T>::front()
- {
- if( queue.empty() )
- {
- return safe;
- }
-
- return queue.front();
- }
-
- //-----{ Returnreference to element at front of Queue }-------------------//
-
- template <typename T>
- const T& Queue<T>::front() const
- {
- if( queue.empty() )
- {
- return safe;
- }
-
- return queue.front();
- }
-
- //-----{ Returnreference to element at back of Queue }--------------------//
-
- template <typename T>
- T& Queue<T>::back()
- {
- if( queue.empty() )
- {
- return safe;
- }
-
- return queue.back();
- }
-
- //-----{ Returnreference to element at back of Queue }--------------------//
-
- template <typename T>
- const T& Queue<T>::back() const
- {
- if( queue.empty() )
- {
- return safe;
- }
-
- return queue.back();
- }
-
- template <typename T>
- void Queue<T>::reverse( Queue<T>& target ) const
- {
- typename std::list<T>::const_reverse_iterator iter;
- iter = queue.rbegin();
- for( ; iter != queue.rend(); ++iter ) {
- target.push( *iter );
- }
- }
-
- ////////////////////////////////////////////////////////////////////////////
- template <typename T>
- std::vector<T> Queue<T>::toArray() const{
- std::vector<T> valueArray(queue.size());
-
- typename std::list<T>::const_iterator iter;
- iter=queue.begin();
- for( int ix=0; iter != queue.end(); ++iter, ++ix ){
- valueArray[ix] = *iter;
- }
-
- return valueArray;
- }
+ template <typename T> T Queue<T>::safe;
}}
-#endif /* _DECAF_UTIL_QUEUE_H */
+#endif /*_DECAF_UTIL_QUEUE_H_*/
Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/Set.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/Set.h?view=diff&rev=555252&r1=555251&r2=555252
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/Set.h (original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/Set.h Wed Jul 11 05:03:06 2007
@@ -23,8 +23,9 @@
#include <decaf/lang/exceptions/NoSuchElementException.h>
#include <decaf/util/concurrent/Synchronizable.h>
#include <decaf/util/concurrent/Mutex.h>
+#include <decaf/util/Iterator.h>
-namespace decaf{
+namespace activemq{
namespace util{
/**
@@ -39,6 +40,52 @@
std::set<E> values;
concurrent::Mutex mutex;
+ private:
+
+ class SetIterator : public Iterator<E> {
+ private:
+
+ typename std::set<E>::iterator current;
+ typename std::set<E>::iterator previous;
+ typename std::set<E>* set;
+
+ public:
+
+ SetIterator( typename std::set<E>* set ) {
+ this->current = set->begin();
+ this->previous = set->end();
+ this->set = set;
+ }
+ virtual ~SetIterator() {}
+
+ virtual E next() throw( lang::exceptions::NoSuchElementException ){
+ if( this->current == set->end() ) {
+ throw exceptions::NoSuchElementException(
+ __FILE__, __LINE__,
+ "Set::Iterator::next - No more elements to return" );
+ }
+
+ this->previous = this->current;
+ return *( this->current++ );
+ }
+
+ virtual bool hasNext() const {
+ return ( this->current != set->end() );
+ }
+
+ virtual void remove() throw ( lang::exceptions::IllegalStateException,
+ lang::exceptions::UnsupportedOperationException ){
+ if( this->previous == set->end() ) {
+ throw lang::exceptions::IllegalStateException(
+ __FILE__, __LINE__,
+ "Set::Iterator::remove - Invalid State to call remove" );
+ }
+
+ this->set->erase( this->previous );
+ this->previous = this->set->end();
+ }
+ };
+
public:
/**
@@ -58,50 +105,87 @@
virtual ~Set(){}
/**
+ * Returns an iterator for this collection. The order of Iteration
+ * is in no particular order other than the natural ording of the
+ * elements in the Set class.
+ * @returns Iterator<E> for this collection
+ */
+ Iterator<E>* iterator() {
+ return new SetIterator( &values );
+ }
+
+ /**
* Copies the content of the source set into this set. Erases
* all existing data in this st.
* @param source The source object to copy from.
*/
- virtual void copy( const Set& source );
+ virtual void copy( const Set& source ) {
+ // Add all of the entries to this map.
+ values = source.values;
+ }
/**
* Removes all values from this set.
*/
- virtual void clear();
+ virtual void clear() {
+ values.clear();
+ }
/**
* Indicates whether or this set contains the given value.
* @param value The value to look up.
* @return true if this set contains the value, otherwise false.
*/
- virtual bool contains( const E& value ) const;
+ virtual bool contains( const E& value ) const {
+ typename std::set<E>::const_iterator iter;
+ iter = values.find( value );
+ return iter != values.end();
+ }
/**
* @return if the set contains any element or not, TRUE or FALSE
*/
- virtual bool isEmpty() const;
+ virtual bool isEmpty() const {
+ return values.empty();
+ }
/**
* @return The number of elements in this set.
*/
- virtual std::size_t size() const;
+ virtual std::size_t size() const {
+ return values.size();
+ }
/**
* Adds the given value to the set.
* @param value The value to add.
*/
- virtual void add( const E& value );
+ virtual void add( const E& value ) {
+ values.insert( value );
+ }
/**
* Removes the value from the set.
* @param value The value to be removed.
*/
- virtual void remove( const E& value );
+ virtual void remove( const E& value ) {
+ values.erase( value );
+ }
/**
* @return the all values in this set as a std::vector.
*/
- virtual std::vector<E> toArray() const;
+ virtual std::vector<E> toArray() const {
+ std::vector<E> valueArray( values.size() );
+
+ typename std::set<E>::const_iterator iter;
+ iter=values.begin();
+ for( int ix=0; iter != values.end(); ++iter, ++ix ){
+ valueArray[ix] = *iter;
+ }
+
+ return valueArray;
+ }
public: // Methods from Synchronizable
@@ -140,8 +224,9 @@
* WAIT_INIFINITE
* @throws ActiveMQException
*/
- virtual void wait(unsigned long millisecs) throw( lang::Exception ) {
- mutex.wait(millisecs);
+ virtual void wait( unsigned long millisecs )
+ throw( lang::Exception ) {
+ mutex.wait( millisecs );
}
/**
@@ -150,7 +235,7 @@
* calling.
* @throws ActiveMQException
*/
- virtual void notify() throw( lang::Exception ) {
+ virtual void notify() throw( lang::Exception ) {
mutex.notify();
}
@@ -160,70 +245,10 @@
* calling.
* @throws ActiveMQException
*/
- virtual void notifyAll() throw( lang::Exception ) {
+ virtual void notifyAll() throw( lang::Exception ) {
mutex.notifyAll();
}
};
-
- ////////////////////////////////////////////////////////////////////////////
- template <typename E>
- void Set<E>::copy( const Set<E>& source ) {
-
- // Add all of the entries to this map.
- values = source.values;
- }
-
- ////////////////////////////////////////////////////////////////////////////
- template <typename E>
- void Set<E>::clear(){
- values.clear();
- }
-
- ////////////////////////////////////////////////////////////////////////////
- template <typename E>
- bool Set<E>::contains(const E& value) const{
- typename std::set<E>::const_iterator iter;
- iter = values.find(value);
- return iter != values.end();
- }
-
- ////////////////////////////////////////////////////////////////////////////
- template <typename E>
- bool Set<E>::isEmpty() const{
- return values.empty();
- }
-
- ////////////////////////////////////////////////////////////////////////////
- template <typename E>
- std::size_t Set<E>::size() const{
- return values.size();
- }
-
- ////////////////////////////////////////////////////////////////////////////
- template <typename E>
- void Set<E>::add( const E& value ){
- values.insert(value);
- }
-
- ////////////////////////////////////////////////////////////////////////////
- template <typename E>
- void Set<E>::remove( const E& value ){
- values.erase(value);
- }
-
- ////////////////////////////////////////////////////////////////////////////
- template <typename E>
- std::vector<E> Set<E>::toArray() const{
- std::vector<E> valueArray(values.size());
-
- typename std::set<E>::const_iterator iter;
- iter=values.begin();
- for( int ix=0; iter != values.end(); ++iter, ++ix ){
- valueArray[ix] = *iter;
- }
-
- return valueArray;
- }
}}
Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/concurrent/Mutex.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/concurrent/Mutex.cpp?view=diff&rev=555252&r1=555251&r2=555252
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/concurrent/Mutex.cpp (original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/concurrent/Mutex.cpp Wed Jul 11 05:03:06 2007
@@ -26,11 +26,11 @@
{
#ifdef HAVE_PTHREAD_H
pthread_mutexattr_t attr;
- pthread_mutexattr_init(&attr);
- pthread_mutex_init(&mutex, &attr);
- pthread_mutexattr_destroy(&attr);
+ pthread_mutexattr_init( &attr );
+ pthread_mutex_init( &mutex, &attr );
+ pthread_mutexattr_destroy( &attr );
#else
- InitializeCriticalSection(&mutex);
+ InitializeCriticalSection( &mutex );
#endif
lock_owner = 0;
@@ -44,42 +44,40 @@
unlock();
#ifdef HAVE_PTHREAD_H
- pthread_mutex_destroy(&mutex);
+ pthread_mutex_destroy( &mutex );
#else
- DeleteCriticalSection(&mutex);
+ DeleteCriticalSection( &mutex );
#endif
}
////////////////////////////////////////////////////////////////////////////////
void Mutex::lock() throw( lang::Exception )
{
- if(isLockOwner())
- {
+ unsigned long threadId = lang::Thread::getId();
+
+ if( threadId == lock_owner ) {
lock_count++;
- }
- else
- {
+ } else {
+
#ifdef HAVE_PTHREAD_H
- pthread_mutex_lock(&mutex);
+ pthread_mutex_lock( &mutex );
#else
- EnterCriticalSection(&mutex);
+ EnterCriticalSection( &mutex );
#endif
+ lock_owner = threadId;
lock_count = 1;
- lock_owner = lang::Thread::getId();
}
}
////////////////////////////////////////////////////////////////////////////////
void Mutex::unlock() throw( lang::Exception )
{
- if(lock_owner == 0)
- {
+ if( lock_owner == 0 ) {
return;
}
- if(!isLockOwner())
- {
+ if( !isLockOwner() ) {
throw lang::Exception(
__FILE__, __LINE__,
"Mutex::unlock - Failed, not Lock Owner!" );
@@ -87,14 +85,13 @@
lock_count--;
- if(lock_count == 0)
- {
+ if(lock_count == 0) {
lock_owner = 0;
#ifdef HAVE_PTHREAD_H
- pthread_mutex_unlock(&mutex);
+ pthread_mutex_unlock( &mutex );
#else
- LeaveCriticalSection(&mutex);
+ LeaveCriticalSection( &mutex );
#endif
}
}
@@ -110,8 +107,7 @@
void Mutex::wait( unsigned long millisecs )
throw( lang::Exception )
{
- if(!isLockOwner())
- {
+ if( !isLockOwner() ) {
throw lang::Exception(
__FILE__, __LINE__,
"Mutex::wait - Failed, not Lock Owner!");
@@ -122,7 +118,7 @@
// When we come back and re-lock we want to restore to the
// state we were in before.
unsigned long lock_owner = this->lock_owner;
- int lock_count = this->lock_count;
+ int lock_count = this->lock_count;
this->lock_count = 0;
this->lock_owner = 0;
@@ -131,61 +127,47 @@
// Create this threads wait event
pthread_cond_t waitEvent;
- pthread_cond_init(&waitEvent, NULL);
+ pthread_cond_init( &waitEvent, NULL );
// Store the event in the queue so that a notify can
// call it and wake up the thread.
- eventQ.push_back(&waitEvent);
+ eventQ.push_back( &waitEvent );
int returnValue = 0;
- if(millisecs != WAIT_INFINITE)
- {
+ if( millisecs != WAIT_INFINITE ) {
timeval now = {0,0};
- gettimeofday(&now, NULL);
+ gettimeofday( &now, NULL );
timespec wait = {0,0};
wait.tv_sec = now.tv_sec + (millisecs / 1000);
wait.tv_nsec = (now.tv_usec * 1000) + ((millisecs % 1000) * 1000000);
- if(wait.tv_nsec > 1000000000)
- {
+ if( wait.tv_nsec > 1000000000 ) {
wait.tv_sec++;
wait.tv_nsec -= 1000000000;
}
- returnValue = pthread_cond_timedwait(&waitEvent, &mutex, &wait);
- }
- else
- {
- returnValue = pthread_cond_wait(&waitEvent, &mutex);
- }
-
- // If the wait did not succeed for any reason, remove it
- // from the queue.
- if( returnValue != 0 ){
- std::list<pthread_cond_t*>::iterator iter = eventQ.begin();
- for( ; iter != eventQ.end(); ++iter ){
- if( *iter == &waitEvent ){
- eventQ.erase(iter);
- break;
- }
- }
+ returnValue = pthread_cond_timedwait( &waitEvent, &mutex, &wait );
+ } else {
+ returnValue = pthread_cond_wait( &waitEvent, &mutex );
}
+ // Be Sure that the event is now removed
+ eventQ.remove( &waitEvent );
+
// Destroy our wait event now, the notify method will have removed it
// from the event queue.
- pthread_cond_destroy(&waitEvent);
+ pthread_cond_destroy( &waitEvent );
#else // !defined(HAVE_PTHREAD_H)
// Create the event to wait on
HANDLE waitEvent = CreateEvent( NULL, false, false, NULL );
- if(waitEvent == NULL)
- {
+ if( waitEvent == NULL ) {
throw lang::Exception(
__FILE__, __LINE__,
- "Mutex::Mutex - Failed Creating Event." );
+ "Mutex::wait - Failed Creating Event." );
}
eventQ.push_back( waitEvent );
@@ -194,11 +176,18 @@
LeaveCriticalSection( &mutex );
// Wait for a signal
- WaitForSingleObject( waitEvent, millisecs );
+ if( WaitForSingleObject( waitEvent, millisecs ) != WAIT_OBJECT_0 && millisecs == WAIT_INFINITE ) {
+ throw lang::Exception(
+ __FILE__, __LINE__,
+ "Mutex::wait - Infinite wait aborted for unknown reason." );
+ }
// Reaquire the Lock
EnterCriticalSection( &mutex );
+ // Remove the event no matter what
+ eventQ.remove( waitEvent );
+
// Clean up the event, the notif methods will have
// already poped it from the queue.
CloseHandle( waitEvent );
@@ -213,21 +202,21 @@
////////////////////////////////////////////////////////////////////////////////
void Mutex::notify() throw( lang::Exception )
{
- if( !isLockOwner() )
- {
+ if( !isLockOwner() ) {
throw lang::Exception(
__FILE__, __LINE__,
"Mutex::Notify - Failed, not Lock Owner!" );
}
- if( !eventQ.empty() )
- {
+ if( !eventQ.empty() ) {
#ifdef HAVE_PTHREAD_H
- pthread_cond_signal( eventQ.front() );
- eventQ.pop_front();
-#else
- SetEvent( eventQ.front() );
- eventQ.pop_front();
+ pthread_cond_t* event = eventQ.front();
+ eventQ.remove( event );
+ pthread_cond_signal( event );
+#else
+ HANDLE event = eventQ.front();
+ eventQ.remove( event );
+ SetEvent( event );
#endif
}
}
@@ -235,29 +224,22 @@
////////////////////////////////////////////////////////////////////////////////
void Mutex::notifyAll() throw( lang::Exception )
{
- if(!isLockOwner())
- {
+ if( !isLockOwner() ) {
throw lang::Exception(
__FILE__, __LINE__,
"Mutex::NotifyAll - Failed, not Lock Owner!" );
}
-#ifdef HAVE_PTHREAD_H
-
- while(!eventQ.empty())
- {
- pthread_cond_signal( eventQ.front() );
- eventQ.pop_front();
- }
-
-#else
-
- while(!eventQ.empty())
- {
- SetEvent( eventQ.front() );
- eventQ.pop_front();
- }
+ while( !eventQ.empty() ) {
+#ifdef HAVE_PTHREAD_H
+ pthread_cond_t* event = eventQ.front();
+ eventQ.remove( event );
+ pthread_cond_signal( event );
+#else
+ HANDLE event = eventQ.front();
+ eventQ.remove( event );
+ SetEvent( event );
#endif
+ }
}
-
Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/concurrent/Mutex.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/concurrent/Mutex.h?view=diff&rev=555252&r1=555251&r2=555252
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/concurrent/Mutex.h (original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/util/concurrent/Mutex.h Wed Jul 11 05:03:06 2007
@@ -15,8 +15,8 @@
* limitations under the License.
*/
-#ifndef _DECAF_UTIL_CONCURRENT_MUTEX_H_
-#define _DECAF_UTIL_CONCURRENT_MUTEX_H_
+#ifndef _DECAF_CONCURRENT_MUTEX_H_
+#define _DECAF_CONCURRENT_MUTEX_H_
// Includes.
#include <decaf/util/concurrent/Synchronizable.h>
@@ -72,8 +72,8 @@
#endif
// Lock Status Members
- int lock_count;
- unsigned long lock_owner;
+ volatile int lock_count;
+ volatile unsigned long lock_owner;
public:
@@ -135,10 +135,9 @@
/**
* Check if the calling thread is the Lock Owner
- * @retuns true if the caller is the lock owner
+ * @retun true if the caller is the lock owner
*/
- bool isLockOwner()
- {
+ bool isLockOwner(){
return lock_owner == lang::Thread::getId();
}
@@ -146,4 +145,4 @@
}}}
-#endif // _DECAF_UTIL_CONCURRENT_MUTEX_H_
+#endif /*_DECAF_CONCURRENT_MUTEX_H_*/