// http://www.boost.org/LICENSE_1_0.txt)
#include "host/messagepayload.h"
+#include <algorithm>
#include <limits>
#include <boost/assert.hpp>
//-----------------------------------------------------------------------------
/**
+ * @brief Parameterized constructor.
+ *
* @param payload_size_in_bytes the size of the payload (ie the internal buffer)
*/
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;
}
/**
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;
#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 );
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