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 2010/03/11 23:34:43 UTC

svn commit: r922046 - in /activemq/activemq-cpp/trunk/activemq-cpp/src: main/decaf/lang/String.cpp main/decaf/lang/String.h test/decaf/lang/StringTest.cpp test/decaf/lang/StringTest.h

Author: tabish
Date: Thu Mar 11 22:34:42 2010
New Revision: 922046

URL: http://svn.apache.org/viewvc?rev=922046&view=rev
Log:
Implement some more of the String methods, and test.

Modified:
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/String.cpp
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/String.h
    activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/lang/StringTest.cpp
    activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/lang/StringTest.h

Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/String.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/String.cpp?rev=922046&r1=922045&r2=922046&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/String.cpp (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/String.cpp Thu Mar 11 22:34:42 2010
@@ -17,6 +17,7 @@
 
 #include "String.h"
 
+#include <decaf/lang/ArrayPointer.h>
 #include <decaf/lang/System.h>
 #include <decaf/lang/exceptions/NullPointerException.h>
 
@@ -32,20 +33,16 @@ namespace lang{
     class Contents {
     public:
 
-        unsigned char* value;
+        ArrayPointer<unsigned char> value;
         int length;
         int offset;
 
     public:
 
-        Contents() : value( NULL ), length( 0 ), offset( 0 ) {
+        Contents() : value(), length( 0 ), offset( 0 ) {
         }
 
-        Contents( int length ) : value( new unsigned char[length] ), length( length ), offset( 0 ) {
-        }
-
-        ~Contents() {
-            delete [] value;
+        Contents( int length ) : value( length ), length( length ), offset( 0 ) {
         }
 
     };
@@ -54,23 +51,68 @@ namespace lang{
 
 ////////////////////////////////////////////////////////////////////////////////
 String::String() {
-    this->contents.reset( new Contents() );
+    this->contents = new Contents();
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 String::String( const std::string& source ) {
 
     // Initialize the contents object.
-    this->contents.reset( new Contents( source.length() ) );
+    this->contents = new Contents( source.length() );
 
     // load the passed string into the contents value.
-    System::arraycopy( (unsigned char*)source.c_str(), 0, contents->value, 0, source.length() );
+    System::arraycopy( (unsigned char*)source.c_str(), 0, contents->value.get(), 0, source.length() );
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 String::~String() {
     try{
+        delete this->contents;
     }
     DECAF_CATCH_NOTHROW( Exception )
     DECAF_CATCHALL_NOTHROW()
 }
+
+////////////////////////////////////////////////////////////////////////////////
+std::size_t String::length() const {
+    return this->contents->offset + this->contents->length;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+bool String::isEmpty() const {
+    return ( this->contents->offset + this->contents->length ) == 0;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+char String::charAt( std::size_t index ) const
+    throw( lang::exceptions::IndexOutOfBoundsException ) {
+
+    try{
+
+        if( index >= this->length() ) {
+            throw IndexOutOfBoundsException(
+                __FILE__, __LINE__, "Index given exceeds length of this String." );
+        }
+
+        return this->contents->value[this->contents->offset + index];
+    }
+    DECAF_CATCH_RETHROW( IndexOutOfBoundsException )
+    DECAF_CATCHALL_THROW( IndexOutOfBoundsException )
+}
+
+////////////////////////////////////////////////////////////////////////////////
+CharSequence* String::subSequence( std::size_t start DECAF_UNUSED, std::size_t end DECAF_UNUSED ) const
+    throw( lang::exceptions::IndexOutOfBoundsException ) {
+
+    return NULL;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+std::string String::toString() const {
+
+    if( this->contents->value == NULL ) {
+        return "null";
+    }
+
+    return std::string( (const char*)this->contents->value.get(), this->length() );
+}

Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/String.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/String.h?rev=922046&r1=922045&r2=922046&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/String.h (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/lang/String.h Thu Mar 11 22:34:42 2010
@@ -22,7 +22,6 @@
 
 #include <decaf/lang/CharSequence.h>
 #include <decaf/lang/Comparable.h>
-#include <decaf/lang/Pointer.h>
 
 #include <string>
 
@@ -36,12 +35,12 @@ namespace lang {
      *
      * @since 1.0
      */
-    class DECAF_API String {
+    class DECAF_API String : public CharSequence {
     private:
 
-        decaf::lang::Pointer<Contents> contents;
+        Contents* contents;
 
-    public:
+    public:  // Constructors
 
         /**
          * Creates a new empty String object.
@@ -58,6 +57,37 @@ namespace lang {
 
         virtual ~String();
 
+    public:  // String API
+
+        /**
+         * @returns true if the length of this String is zero.
+         */
+        bool isEmpty() const;
+
+    public:  // CharSequence Implementation
+
+        /**
+         * {@inheritDoc}
+         */
+        virtual std::size_t length() const;
+
+        /**
+         * {@inheritDoc}
+         */
+        virtual char charAt( std::size_t index ) const
+            throw( lang::exceptions::IndexOutOfBoundsException );
+
+        /**
+         * {@inheritDoc}
+         */
+        virtual CharSequence* subSequence( std::size_t start, std::size_t end ) const
+            throw( lang::exceptions::IndexOutOfBoundsException );
+
+        /**
+         * {@inheritDoc}
+         */
+        virtual std::string toString() const;
+
     };
 
 }}

Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/lang/StringTest.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/lang/StringTest.cpp?rev=922046&r1=922045&r2=922046&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/lang/StringTest.cpp (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/lang/StringTest.cpp Thu Mar 11 22:34:42 2010
@@ -18,10 +18,12 @@
 #include "StringTest.h"
 
 #include <decaf/lang/String.h>
+#include <decaf/lang/exceptions/IndexOutOfBoundsException.h>
 
 using namespace std;
 using namespace decaf;
 using namespace decaf::lang;
+using namespace decaf::lang::exceptions;
 
 ////////////////////////////////////////////////////////////////////////////////
 StringTest::StringTest() {
@@ -32,5 +34,15 @@ StringTest::~StringTest() {
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-void StringTest::test() {
+void StringTest::testConstructor1() {
+
+    String test;
+
+    CPPUNIT_ASSERT( test.length() == 0 );
+    CPPUNIT_ASSERT( test.isEmpty() == true );
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+         "Should have thrown an IndexOutOfBoundsException",
+         test.charAt( 1 ),
+         IndexOutOfBoundsException );
 }

Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/lang/StringTest.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/lang/StringTest.h?rev=922046&r1=922045&r2=922046&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/lang/StringTest.h (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/lang/StringTest.h Thu Mar 11 22:34:42 2010
@@ -27,7 +27,7 @@ namespace lang {
     class StringTest : public CppUnit::TestFixture
     {
         CPPUNIT_TEST_SUITE( StringTest );
-        CPPUNIT_TEST( test );
+        CPPUNIT_TEST( testConstructor1 );
         CPPUNIT_TEST_SUITE_END();
 
     public:
@@ -36,7 +36,7 @@ namespace lang {
 
         virtual ~StringTest();
 
-        void test();
+        void testConstructor1();
 
     };