Allowing to copy the MessagePayload object, through implementation of Copy Constructo...
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Tue, 16 Aug 2011 10:40:11 +0000 (07:40 -0300)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Tue, 16 Aug 2011 10:43:13 +0000 (07:43 -0300)
src/host/messagepayload.cpp
src/host/messagepayload.h

index 6c32e4e..bcb522d 100644 (file)
@@ -6,6 +6,7 @@
 //          http://www.boost.org/LICENSE_1_0.txt)
 #include "host/messagepayload.h"
 
+#include <algorithm>
 #include <limits>
 
 #include <boost/assert.hpp>
@@ -21,6 +22,8 @@ using I2n::Logger::GlobalLogger;
 //-----------------------------------------------------------------------------
 
 /**
+ * @brief Parameterized constructor.
+ *
  * @param payload_size_in_bytes the size of the payload (ie the internal buffer)
  */
 MessagePayload::MessagePayload(
@@ -34,9 +37,47 @@ MessagePayload::MessagePayload(
     fill( Payload.get(), Payload.get() + PayloadSizeInBytes, 0 );
 }
 
+/**
+ * @brief Copy constructor.
+ *
+ * @param other The object from where to get the message data.
+ */
+MessagePayload::MessagePayload(
+        const MessagePayload &other
+) :
+    PayloadSizeInBytes( other.PayloadSizeInBytes ),
+    Payload( new uint8_t[ other.PayloadSizeInBytes ] )
+{
+    BOOST_ASSERT( 0 < PayloadSizeInBytes );
+    BOOST_ASSERT( PayloadSizeInBytes == other.PayloadSizeInBytes );
+    BOOST_ASSERT( Payload.get() == NULL );
+    BOOST_ASSERT( other.Payload.get() != NULL );
+
+    copy( other.Payload.get(), other.Payload.get() + PayloadSizeInBytes, Payload.get() );
+}
+
 MessagePayload::~MessagePayload()
 {
-    // Payload automatically delete by smart pointer scope end
+    // The Payload is automatically deleted when the smart pointer's scope ends
+}
+
+/**
+ * @brief Assignment operator.
+ *
+ * @param other The object from where to get the message data.
+ *
+ * @return This object.
+ */
+MessagePayload& MessagePayload::operator=( const MessagePayload &other )
+{
+    BOOST_ASSERT( 0 < PayloadSizeInBytes );
+    BOOST_ASSERT( PayloadSizeInBytes == other.PayloadSizeInBytes );
+    BOOST_ASSERT( Payload.get() != NULL );
+    BOOST_ASSERT( other.Payload.get() != NULL );
+
+    copy( other.Payload.get(), other.Payload.get() + PayloadSizeInBytes, Payload.get() );
+
+    return *this;
 }
 
 /**
@@ -188,7 +229,7 @@ istream& MessagePayload::read( istream &is )
     if ( data_received_in_bytes != PayloadSizeInBytes )
     {
         GlobalLogger.error() << "Error: expecting " << PayloadSizeInBytes
-                << " bytes, but received " << is.gcount() << " bytes" << endl;
+                << " bytes, but received " << is.gcount() << " bytes." << endl;
     }
 
     return is;
index bbe40ed..5b66a0a 100644 (file)
 #include <istream>
 #include <ostream>
 
-#include <boost/noncopyable.hpp>
 #include <boost/scoped_array.hpp>
 
-#define NONCOPYABLE( Class ) \
-    private: \
-    Class( const Class & ); \
-    Class & operator= ( const Class & );
-
 //-----------------------------------------------------------------------------
 // MessagePayload
 //-----------------------------------------------------------------------------
 
 /**
- * @brief This class represents the contents of the network messages. It
- * provides means for encode and decode (i.e. deal with endianness), and also
- * can be treated like an ordinary array.
+ * @brief This class represents the contents of a network message. It provides
+ * means to encode and to decode data to network format (i.e. deal with
+ * endianness), and also can be treated like an ordinary array.
  */
 class MessagePayload
 {
 public:
     explicit MessagePayload( const std::size_t &payload_size_in_bytes );
+    MessagePayload( const MessagePayload& other );
     ~MessagePayload();
 
+    MessagePayload& operator=( const MessagePayload &other );
+
     const uint8_t& operator[]( std::size_t offset ) const;
     uint8_t& operator[]( std::size_t offset );
 
@@ -62,12 +59,11 @@ public:
     std::ostream& write( std::ostream &os ) const;
 
 private:
-    /// the size of the payload buffer
+    /// The size of the payload buffer
     const std::size_t PayloadSizeInBytes;
-    /// the payload buffer
+    /// The payload buffer
     boost::scoped_array<uint8_t> Payload;
 
-    NONCOPYABLE( MessagePayload )
 };
 
 #endif // MESSAGE_PAYLOAD_H