Allows to add more data to the MessagePayload through append method.
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Fri, 26 Aug 2011 01:03:55 +0000 (22:03 -0300)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Fri, 26 Aug 2011 01:03:55 +0000 (22:03 -0300)
Also fixed the read and write documentation.

src/host/messagepayload.cpp
src/host/messagepayload.h

index d6cc358..f988ac6 100644 (file)
@@ -121,6 +121,48 @@ uint8_t* MessagePayload::get() const
 }
 
 /**
+ * @brief Adds extra bytes in the MessagePayload.
+ *
+ * @details If there is no more space available in the internal buffer, extra
+ * space is allocated to hold the additional bytes.
+ *
+ * @param extra_payload_data The data bytes to append at the end of the current
+ * payload.
+ * @param extra_payload_size_in_bytes The amount of bytes in @a extra_payload_data
+ * to store at the @c MessagePayload.
+ *
+ * @return void.
+ */
+void MessagePayload::append(
+        const uint8_t *extra_payload_data,
+        const size_t extra_payload_size_in_bytes
+)
+{
+    BOOST_ASSERT( extra_payload_data != NULL );
+
+    if ( 0 < extra_payload_size_in_bytes )
+    {
+        BOOST_ASSERT( 0 < extra_payload_size_in_bytes );
+
+        size_t old_payload_size_in_bytes = PayloadSizeInBytes;
+        uint8_t *old_payload_data = Payload.get();
+
+        size_t new_payload_size_in_bytes = old_payload_size_in_bytes + extra_payload_size_in_bytes;
+        uint8_t *new_payload_data = new uint8_t[ new_payload_size_in_bytes ];
+
+        copy( old_payload_data, old_payload_data + old_payload_size_in_bytes, new_payload_data );
+        copy( extra_payload_data, extra_payload_data + extra_payload_size_in_bytes, new_payload_data + old_payload_size_in_bytes );
+
+        Payload.reset( new_payload_data );
+        PayloadSizeInBytes = new_payload_size_in_bytes;
+
+        BOOST_ASSERT( PayloadSizeInBytes == ( old_payload_size_in_bytes + extra_payload_size_in_bytes ) );
+        BOOST_ASSERT( equal( Payload.get(), Payload.get() + old_payload_size_in_bytes, old_payload_data ) );
+        BOOST_ASSERT( equal( Payload.get() + old_payload_size_in_bytes, Payload.get() + extra_payload_size_in_bytes, extra_payload_data ) );
+    }
+}
+
+/**
  * @brief Retrieve 16 bits from the payload buffer.
  *
  * @param left_byte the index of the left byte
@@ -236,11 +278,17 @@ void MessagePayload::encode32(
 }
 
 /**
- * @brief Read all the data from the payload and stores in the istream.
+ * @brief Read/Extract all the data from the input stream @a is and stores it
+ * in the payload buffer.
+ *
+ * @param is The stream object on which the action is performed.
+ *
+ * @return The stream object passed as parameter.
  */
 istream& MessagePayload::read( istream &is )
 {
-    char *payload_data_array = reinterpret_cast<char *> ( Payload.get() );
+    char *payload_data_array = reinterpret_cast<char *>( Payload.get() );
+
     (void) is.read( payload_data_array, PayloadSizeInBytes );
 
     size_t data_received_in_bytes = static_cast<size_t>( is.gcount() );
@@ -254,10 +302,18 @@ istream& MessagePayload::read( istream &is )
 }
 
 /**
- * @brief Writes all the data from the ostream to the payload buffer.
+ * @brief Write/Insert all the data to the output stream @a os that is stored in
+ * the payload buffer.
+ *
+ * @param os The stream object on which the action is performed.
+ *
+ * @return The stream object passed as parameter.
  */
 ostream& MessagePayload::write( ostream &os ) const
 {
-    const char *data_array = reinterpret_cast<const char *> ( Payload.get() );
-    return os.write( data_array, PayloadSizeInBytes );
+    const char *data_array = reinterpret_cast<const char *>( Payload.get() );
+
+    (void) os.write( data_array, PayloadSizeInBytes );
+
+    return os;
 }
index 85a226b..e88cb9c 100644 (file)
@@ -33,11 +33,16 @@ public:
 
     MessagePayload& operator=( const MessagePayload &other );
 
-    const uint8_t& operator[]( std::size_t offset ) const;
-    uint8_t& operator[]( std::size_t offset );
+    const uint8_t& operator[]( const std::size_t offset ) const;
+    uint8_t& operator[]( const std::size_t offset );
 
     uint8_t* get() const;
 
+    void append(
+            const uint8_t *extra_payload_data,
+            const std::size_t extra_payload_size_in_bytes
+    );
+
     uint16_t decode16(
             const int left_byte,
             const int right_byte
@@ -63,7 +68,7 @@ public:
 
 private:
     /// The size of the payload buffer
-    const std::size_t PayloadSizeInBytes;
+    std::size_t PayloadSizeInBytes;
     /// The payload buffer
     boost::scoped_array<uint8_t> Payload;