activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tab...@apache.org
Subject svn commit: r740875 - in /activemq/activemq-cpp/trunk/src: main/decaf/lang/Pointer.h main/decaf/util/Comparator.h main/decaf/util/Map.h test/activemq/commands/BrokerIdTest.cpp test/activemq/commands/BrokerIdTest.h test/testRegistry.cpp
Date Wed, 04 Feb 2009 20:54:26 GMT
Author: tabish
Date: Wed Feb  4 20:54:25 2009
New Revision: 740875

URL: http://svn.apache.org/viewvc?rev=740875&view=rev
Log:
Adds the ability to define what the comparison is for map keys, defaults to the standard std::less
comparator.  Completes the definition of the Comparator interface in Util, adds a PointerComparator
to the Pointer class so that the elements in a Pointer can be compared based on the actual
contained objects and not just the underlying pointer value.  Adds some test cases to validate
these classes.

Modified:
    activemq/activemq-cpp/trunk/src/main/decaf/lang/Pointer.h
    activemq/activemq-cpp/trunk/src/main/decaf/util/Comparator.h
    activemq/activemq-cpp/trunk/src/main/decaf/util/Map.h
    activemq/activemq-cpp/trunk/src/test/activemq/commands/BrokerIdTest.cpp
    activemq/activemq-cpp/trunk/src/test/activemq/commands/BrokerIdTest.h
    activemq/activemq-cpp/trunk/src/test/testRegistry.cpp

Modified: activemq/activemq-cpp/trunk/src/main/decaf/lang/Pointer.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/decaf/lang/Pointer.h?rev=740875&r1=740874&r2=740875&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/decaf/lang/Pointer.h (original)
+++ activemq/activemq-cpp/trunk/src/main/decaf/lang/Pointer.h Wed Feb  4 20:54:25 2009
@@ -21,6 +21,7 @@
 #include <decaf/util/Config.h>
 #include <decaf/lang/exceptions/NullPointerException.h>
 #include <decaf/util/concurrent/atomic/AtomicInteger.h>
+#include <decaf/util/Comparator.h>
 #include <memory>
 
 namespace decaf {
@@ -357,6 +358,35 @@
         return right.get() != left;
     }
 
+    /**
+     * This implementation of Comparator is designed to allows objects in a Collection
+     * to be sorted or tested for equality based on the value of the Object being Pointed
+     * to and not the value of the contained pointer in the Pointer instance.  This can
+     * be useful in the case where a series of values in a Collection is more efficiently
+     * accessed in the Objects Natural Order and not the underlying pointers location in
+     * memory.
+     * <p>
+     * Also this allows Pointer objects that Point to different instances of the same type
+     * to be compared based on the comparison of the object itself and not just the value
of
+     * the pointer.
+     */
+    template< typename T, typename R = AtomicRefCounter<T> >
+    class PointerComparator : public decaf::util::Comparator< Pointer<T,R> >
{
+    public:
+
+        // Allows for operator less on types that implement Comparable or provide
+        // a workable operator <
+        virtual bool operator() ( const Pointer<T,R>& left, const Pointer<T,R>&
right ) const {
+            return *left < *right;
+        }
+
+        // Requires that the type in the pointer is an instance of a Comparable.
+        virtual int compare( const Pointer<T,R>& left, const Pointer<T,R>&
right ) const {
+            return left->compareTo( *right );
+        }
+
+    };
+
 }}
 
 ////////////////////////////////////////////////////////////////////////////////

Modified: activemq/activemq-cpp/trunk/src/main/decaf/util/Comparator.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/decaf/util/Comparator.h?rev=740875&r1=740874&r2=740875&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/decaf/util/Comparator.h (original)
+++ activemq/activemq-cpp/trunk/src/main/decaf/util/Comparator.h Wed Feb  4 20:54:25 2009
@@ -19,6 +19,7 @@
 #define _DECAF_UTIL_COMPARATOR_H_
 
 #include <decaf/util/Config.h>
+#include <algorithm>
 
 namespace decaf{
 namespace util{
@@ -35,13 +36,23 @@
      * every e1 and e2 in S.
      */
     template<typename T>
-    class Comparator
-    {
+    class Comparator : public std::binary_function< T, T, bool > {
     public:
 
         virtual ~Comparator() {}
 
         /**
+         * Implementation of the Binary function interface as a means of allowing
+         * a Comparator to be passed to an STL Map for use as the sorting criteria.
+         *
+         * @param left - the Left hand side operand.
+         * @param right - the Right hand side operand.
+         *
+         * @return true if the vale of left is less than the value of right.
+         */
+        virtual bool operator() ( const T&, const T& ) const = 0;
+
+        /**
          * Compares its two arguments for order. Returns a negative integer, zero,
          * or a positive integer as the first argument is less than, equal to, or
          * greater than the second.
@@ -67,7 +78,7 @@
          * @returns a negative integer, zero, or a positive integer as the first
          * argument is less than, equal to, or greater than the second.
          */
-        virtual int compare( const E& o1, const E& o2 ) = 0;
+        virtual int compare( const T& o1, const T& o2 ) const = 0;
 
     };
 

Modified: activemq/activemq-cpp/trunk/src/main/decaf/util/Map.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/decaf/util/Map.h?rev=740875&r1=740874&r2=740875&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/decaf/util/Map.h (original)
+++ activemq/activemq-cpp/trunk/src/main/decaf/util/Map.h Wed Feb  4 20:54:25 2009
@@ -36,7 +36,7 @@
         public concurrent::Synchronizable {
     private:
 
-        std::map<K,V> valueMap;
+        std::map<K,V, COMPARE> valueMap;
         concurrent::Mutex mutex;
 
     public:

Modified: activemq/activemq-cpp/trunk/src/test/activemq/commands/BrokerIdTest.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/activemq/commands/BrokerIdTest.cpp?rev=740875&r1=740874&r2=740875&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/activemq/commands/BrokerIdTest.cpp (original)
+++ activemq/activemq-cpp/trunk/src/test/activemq/commands/BrokerIdTest.cpp Wed Feb  4 20:54:25
2009
@@ -18,15 +18,90 @@
 #include "BrokerIdTest.h"
 
 #include <activemq/commands/BrokerId.h>
+#include <decaf/util/Map.h>
+#include <decaf/lang/Pointer.h>
+#include <decaf/lang/Comparable.h>
+
+////////////////////////////////////////////////////////////////////////////////
+namespace activemq{
+namespace commands{
+
+    struct BrokerIdComparitor :
+        public std::binary_function< activemq::commands::BrokerId*,
+                                     activemq::commands::BrokerId*, bool>
+    {
+        bool operator() ( const activemq::commands::BrokerId* left,
+                          const activemq::commands::BrokerId* right ) const
+        {
+            if( left == NULL && right == NULL ) {
+                return false;
+            } else if( left == NULL && right != NULL ) {
+                return true;
+            } else if( left != NULL && right == NULL ) {
+                return false;
+            }
+
+            return left->compareTo( *right ) == -1;
+        }
+    };
+
+}}
 
 using namespace std;
 using namespace activemq;
 using namespace activemq::commands;
+using namespace decaf;
+using namespace decaf::lang;
+using namespace decaf::util;
+
+////////////////////////////////////////////////////////////////////////////////
+void BrokerIdTest::test() {
+
+    BrokerId myCommand2;
+    BrokerId myCommand3;
+    BrokerId myCommand1;
+    CPPUNIT_ASSERT( myCommand1.getDataStructureType() == BrokerId::ID_BROKERID );
+
+    myCommand1.setValue( "A" );
+    myCommand2.setValue( "B" );
+    myCommand3.setValue( "C" );
+
+    Map< BrokerId*, int, BrokerIdComparitor > testMap;
+
+    testMap.setValue( &myCommand1, 0 );
+    testMap.setValue( &myCommand3, 0 );
+    testMap.setValue( &myCommand2, 0 );
+
+    CPPUNIT_ASSERT( testMap.getKeys().at( 0 )->getValue() == "A" );
+    CPPUNIT_ASSERT( testMap.getKeys().at( 1 )->getValue() == "B" );
+    CPPUNIT_ASSERT( testMap.getKeys().at( 2 )->getValue() == "C" );
+}
 
 ////////////////////////////////////////////////////////////////////////////////
-void BrokerIdTest::test()
-{
-    BrokerId myCommand;
+void BrokerIdTest::test2() {
+
+    typedef PointerComparator< BrokerId > COMPARATOR;
+
+    Pointer<BrokerId> myCommand1( new BrokerId );
+    Pointer<BrokerId> myCommand2( new BrokerId );
+    Pointer<BrokerId> myCommand3( new BrokerId );
+
+    myCommand1->setValue( "A" );
+    myCommand2->setValue( "A" );
+    myCommand3->setValue( "C" );
+
+    CPPUNIT_ASSERT( myCommand1->compareTo( *myCommand2 ) == 0 );
+    CPPUNIT_ASSERT( myCommand1->compareTo( *myCommand3 ) == -1 );
+
+    Map< Pointer<BrokerId>, int, COMPARATOR > testMap;
+
+    testMap.setValue( myCommand3, 0 );
+    testMap.setValue( myCommand1, 0 );
+    CPPUNIT_ASSERT( testMap.size() == 2 );
+
+    testMap.setValue( myCommand2, 0 );
+    CPPUNIT_ASSERT( testMap.size() == 2 );
 
-    CPPUNIT_ASSERT( myCommand.getDataStructureType() == BrokerId::ID_BROKERID );
+    CPPUNIT_ASSERT( testMap.getKeys().at( 0 )->getValue() == "A" );
+    CPPUNIT_ASSERT( testMap.getKeys().at( 1 )->getValue() == "C" );
 }

Modified: activemq/activemq-cpp/trunk/src/test/activemq/commands/BrokerIdTest.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/activemq/commands/BrokerIdTest.h?rev=740875&r1=740874&r2=740875&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/activemq/commands/BrokerIdTest.h (original)
+++ activemq/activemq-cpp/trunk/src/test/activemq/commands/BrokerIdTest.h Wed Feb  4 20:54:25
2009
@@ -28,6 +28,7 @@
 
         CPPUNIT_TEST_SUITE( BrokerIdTest );
         CPPUNIT_TEST( test );
+        CPPUNIT_TEST( test2 );
         CPPUNIT_TEST_SUITE_END();
 
     public:
@@ -36,6 +37,7 @@
         virtual ~BrokerIdTest() {}
 
         virtual void test();
+        virtual void test2();
 
     };
 

Modified: activemq/activemq-cpp/trunk/src/test/testRegistry.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/testRegistry.cpp?rev=740875&r1=740874&r2=740875&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/testRegistry.cpp (original)
+++ activemq/activemq-cpp/trunk/src/test/testRegistry.cpp Wed Feb  4 20:54:25 2009
@@ -20,8 +20,8 @@
 
 //#include <activemq/commands/BrokerInfoTest.h>
 //CPPUNIT_TEST_SUITE_REGISTRATION( activemq::commands::BrokerInfoTest );
-//#include <activemq/commands/BrokerIdTest.h>
-//CPPUNIT_TEST_SUITE_REGISTRATION( activemq::commands::BrokerIdTest );
+#include <activemq/commands/BrokerIdTest.h>
+CPPUNIT_TEST_SUITE_REGISTRATION( activemq::commands::BrokerIdTest );
 //#include <activemq/commands/ActiveMQTopicTest.h>
 //CPPUNIT_TEST_SUITE_REGISTRATION( activemq::commands::ActiveMQTopicTest );
 //#include <activemq/commands/ActiveMQTextMessageTest.h>



Mime
View raw message