From: Guilherme Maciel Ferreira Date: Tue, 16 Aug 2011 10:40:11 +0000 (-0300) Subject: Allowing to copy the MessagePayload object, through implementation of Copy Constructo... X-Git-Tag: v1.1^2~15 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=d45ab4e48e4b92961b196dd5ffe752a049cc7bba;p=pingcheck Allowing to copy the MessagePayload object, through implementation of Copy Constructor and Assignment Operator --- diff --git a/src/host/messagepayload.cpp b/src/host/messagepayload.cpp index 6c32e4e..bcb522d 100644 --- a/src/host/messagepayload.cpp +++ b/src/host/messagepayload.cpp @@ -6,6 +6,7 @@ // http://www.boost.org/LICENSE_1_0.txt) #include "host/messagepayload.h" +#include #include #include @@ -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; diff --git a/src/host/messagepayload.h b/src/host/messagepayload.h index bbe40ed..5b66a0a 100644 --- a/src/host/messagepayload.h +++ b/src/host/messagepayload.h @@ -12,29 +12,26 @@ #include #include -#include #include -#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 Payload; - NONCOPYABLE( MessagePayload ) }; #endif // MESSAGE_PAYLOAD_H