From 2ed854ab91e6a48481220416ad82a5d0e0121db9 Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Thu, 29 Mar 2012 00:22:49 -0300 Subject: [PATCH] Replaced the boost::Scoped_array by std::vector as the underlying message payload byte sequence. - This change simplifies many operations. --- src/host/messagepayload.cpp | 128 ++++++++++++++++++++++++++----------------- src/host/messagepayload.h | 13 ++-- 2 files changed, 84 insertions(+), 57 deletions(-) diff --git a/src/host/messagepayload.cpp b/src/host/messagepayload.cpp index 49c7c6b..ad9b442 100644 --- a/src/host/messagepayload.cpp +++ b/src/host/messagepayload.cpp @@ -14,7 +14,6 @@ #include using namespace std; -using boost::scoped_array; using I2n::Logger::GlobalLogger; //----------------------------------------------------------------------------- @@ -30,12 +29,12 @@ MessagePayload::MessagePayload( const std::size_t &payload_size_in_bytes ) : PayloadSizeInBytes( payload_size_in_bytes ), - Payload( new uint8_t[ payload_size_in_bytes ] ) + Payload( payload_size_in_bytes ) { BOOST_ASSERT( 0 < PayloadSizeInBytes ); - BOOST_ASSERT( Payload.get() != NULL ); + BOOST_ASSERT( Payload.capacity() == PayloadSizeInBytes ); - fill( Payload.get(), Payload.get() + PayloadSizeInBytes, 0 ); + fill( Payload.begin(), Payload.end(), 0 ); } /** @@ -47,15 +46,18 @@ MessagePayload::MessagePayload( const MessagePayload &other ) : PayloadSizeInBytes( other.PayloadSizeInBytes ), - Payload( new uint8_t[ other.PayloadSizeInBytes ] ) + Payload( other.PayloadSizeInBytes ) { BOOST_ASSERT( 0 < PayloadSizeInBytes ); - BOOST_ASSERT( Payload.get() != NULL ); - BOOST_ASSERT( other.Payload.get() != NULL ); + BOOST_ASSERT( Payload.capacity() == PayloadSizeInBytes ); + BOOST_ASSERT( other.Payload.capacity() == other.PayloadSizeInBytes ); - copy( other.Payload.get(), other.Payload.get() + PayloadSizeInBytes, Payload.get() ); + copy( other.Payload.begin(), other.Payload.end(), Payload.begin() ); BOOST_ASSERT( PayloadSizeInBytes == other.PayloadSizeInBytes ); + BOOST_ASSERT( Payload.capacity() >= other.Payload.size() ); + BOOST_ASSERT( equal( Payload.begin(), Payload.end(), other.Payload.begin() ) ); + BOOST_ASSERT( equal( other.Payload.begin(), other.Payload.end(), Payload.begin() ) ); } /** @@ -76,22 +78,30 @@ MessagePayload::~MessagePayload() MessagePayload& MessagePayload::operator=( const MessagePayload &other ) { BOOST_ASSERT( 0 < PayloadSizeInBytes ); - BOOST_ASSERT( Payload.get() != NULL ); - BOOST_ASSERT( other.Payload.get() != NULL ); + BOOST_ASSERT( Payload.capacity() == PayloadSizeInBytes ); + BOOST_ASSERT( other.Payload.capacity() == other.PayloadSizeInBytes ); + BOOST_ASSERT( !other.Payload.empty() ); // checking for self-assignment (i.e. object1 = object1) - if ( &other == this ) { + if ( &other == this ) + { return *this; } size_t new_payload_size_in_bytes = other.PayloadSizeInBytes; - uint8_t *new_payload_data = new uint8_t[ new_payload_size_in_bytes ]; - Payload.reset( new_payload_data ); - PayloadSizeInBytes = new_payload_size_in_bytes; + size_t old_payload_size_in_bytes = PayloadSizeInBytes; + if ( new_payload_size_in_bytes > old_payload_size_in_bytes ) + { + Payload.reserve( new_payload_size_in_bytes ); + PayloadSizeInBytes = new_payload_size_in_bytes; + } - copy( other.Payload.get(), other.Payload.get() + other.PayloadSizeInBytes, Payload.get() ); + copy( other.Payload.begin(), other.Payload.end(), Payload.begin() ); BOOST_ASSERT( PayloadSizeInBytes == other.PayloadSizeInBytes ); + BOOST_ASSERT( Payload.capacity() >= other.Payload.size() ); + BOOST_ASSERT( equal( Payload.begin(), Payload.end(), other.Payload.begin() ) ); + BOOST_ASSERT( equal( other.Payload.begin(), other.Payload.end(), Payload.begin() ) ); return *this; //lint !e429 } @@ -106,9 +116,11 @@ MessagePayload& MessagePayload::operator=( const MessagePayload &other ) */ const uint8_t& MessagePayload::operator[]( size_t offset ) const { + BOOST_ASSERT( 0 < PayloadSizeInBytes ); BOOST_ASSERT( offset < PayloadSizeInBytes ); + BOOST_ASSERT( Payload.capacity() == PayloadSizeInBytes ); - ptrdiff_t index = static_cast(offset); + ptrdiff_t index = static_cast( offset ); return Payload[ index ]; } @@ -122,9 +134,11 @@ const uint8_t& MessagePayload::operator[]( size_t offset ) const */ uint8_t& MessagePayload::operator[]( size_t offset ) { + BOOST_ASSERT( 0 < PayloadSizeInBytes ); BOOST_ASSERT( offset < PayloadSizeInBytes ); + BOOST_ASSERT( Payload.capacity() == PayloadSizeInBytes ); - ptrdiff_t index = static_cast(offset); + ptrdiff_t index = static_cast( offset ); return Payload[ index ]; } //lint !e1762 @@ -133,9 +147,15 @@ uint8_t& MessagePayload::operator[]( size_t offset ) * * @return a pointer to the array encapsulated by this class. */ -uint8_t* MessagePayload::get() const +uint8_t* MessagePayload::get() { - return Payload.get(); + BOOST_ASSERT( 0 < PayloadSizeInBytes ); + BOOST_ASSERT( Payload.capacity() == PayloadSizeInBytes ); + + // Vector containers have their elements stored in contiguous storage + // locations, which means that their elements can be accessed not only + // using iterators but also using offsets on regular pointers to elements. + return static_cast( &Payload[ 0 ] ); } /** @@ -156,42 +176,28 @@ void MessagePayload::append( const size_t extra_payload_size_in_bytes ) { + BOOST_ASSERT( 0 < PayloadSizeInBytes ); + BOOST_ASSERT( Payload.capacity() == PayloadSizeInBytes ); BOOST_ASSERT( extra_payload_data != NULL ); if ( 0 < extra_payload_size_in_bytes ) { BOOST_ASSERT( 0 < extra_payload_size_in_bytes ); - // as Payload.reset() deletes the array previously held by Payload, we - // can't reference that array by old_payload_data pointer, thus it is - // required to copy the old array to another memory location in order to - // keep the original data intact size_t old_payload_size_in_bytes = PayloadSizeInBytes; - uint8_t *old_payload_data = new uint8_t[ old_payload_size_in_bytes ]; - copy( Payload.get(), Payload.get() + PayloadSizeInBytes, old_payload_data ); - scoped_array old_payload( old_payload_data ); - 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 both data, the original and the one to be appended, to the new - // buffer, in contiguous memory. - copy( &old_payload_data[0], &old_payload_data[old_payload_size_in_bytes], &new_payload_data[0] ); - copy( &extra_payload_data[0], &extra_payload_data[extra_payload_size_in_bytes], &new_payload_data[old_payload_size_in_bytes] ); - - // the reset() deletes the old array held by Payload - Payload.reset( new_payload_data ); PayloadSizeInBytes = new_payload_size_in_bytes; + Payload.insert( Payload.end(), + &extra_payload_data[ 0 ], + &extra_payload_data[ extra_payload_size_in_bytes ] + ); + #ifndef BOOST_DISABLE_ASSERTS BOOST_ASSERT( PayloadSizeInBytes == ( old_payload_size_in_bytes + extra_payload_size_in_bytes ) ); - uint8_t *original_data_begin = Payload.get(); - uint8_t *original_data_end = Payload.get() + old_payload_size_in_bytes; - BOOST_ASSERT( equal( original_data_begin, original_data_end, old_payload_data ) ); - - uint8_t *extra_data_begin = Payload.get() + old_payload_size_in_bytes; - uint8_t *extra_data_end = Payload.get() + old_payload_size_in_bytes + extra_payload_size_in_bytes; + uint8_t *extra_data_begin = &Payload[ old_payload_size_in_bytes ]; + uint8_t *extra_data_end = &Payload[ old_payload_size_in_bytes + extra_payload_size_in_bytes ]; BOOST_ASSERT( equal( extra_data_begin, extra_data_end, extra_payload_data ) ); #endif } @@ -211,6 +217,8 @@ bool MessagePayload::decode1( const int bit_index ) const { + BOOST_ASSERT( 0 < PayloadSizeInBytes ); + BOOST_ASSERT( Payload.capacity() == PayloadSizeInBytes ); const int max_bit_index = 7; BOOST_ASSERT( ( 0 <= byte_index ) && ( byte_index < static_cast(PayloadSizeInBytes) ) ); BOOST_ASSERT( ( 0 <= bit_index ) && ( bit_index <= max_bit_index ) ); @@ -235,8 +243,10 @@ void MessagePayload::encode1( const int byte_index, const int bit_index, const bool value -) const +) { + BOOST_ASSERT( 0 < PayloadSizeInBytes ); + BOOST_ASSERT( Payload.capacity() == PayloadSizeInBytes ); const int max_bit_index = 7; BOOST_ASSERT( ( 0 <= byte_index ) && ( byte_index < static_cast(PayloadSizeInBytes) ) ); BOOST_ASSERT( ( 0 <= bit_index ) && ( bit_index <= max_bit_index ) ); @@ -269,6 +279,8 @@ uint16_t MessagePayload::decode16( const int right_byte_index ) const { + BOOST_ASSERT( 0 < PayloadSizeInBytes ); + BOOST_ASSERT( Payload.capacity() == PayloadSizeInBytes ); BOOST_ASSERT( ( 0 <= left_byte_index ) && ( left_byte_index < static_cast(PayloadSizeInBytes) ) ); BOOST_ASSERT( ( 0 <= right_byte_index ) && ( right_byte_index < static_cast(PayloadSizeInBytes) ) ); BOOST_ASSERT( left_byte_index < right_byte_index ); @@ -296,8 +308,10 @@ void MessagePayload::encode16( const int left_byte_index, const int right_byte_index, const uint16_t value -) const +) { + BOOST_ASSERT( 0 < PayloadSizeInBytes ); + BOOST_ASSERT( Payload.capacity() == PayloadSizeInBytes ); BOOST_ASSERT( ( 0 <= left_byte_index ) && ( left_byte_index < static_cast(PayloadSizeInBytes) ) ); BOOST_ASSERT( ( 0 <= right_byte_index ) && ( right_byte_index < static_cast(PayloadSizeInBytes) ) ); BOOST_ASSERT( left_byte_index < right_byte_index ); @@ -321,6 +335,8 @@ uint32_t MessagePayload::decode32( const int last_byte_index ) const { + BOOST_ASSERT( 0 < PayloadSizeInBytes ); + BOOST_ASSERT( Payload.capacity() == PayloadSizeInBytes ); BOOST_ASSERT( ( 0 <= first_byte_index ) && ( first_byte_index < static_cast(PayloadSizeInBytes) ) ); BOOST_ASSERT( ( 0 <= last_byte_index ) && ( last_byte_index < static_cast(PayloadSizeInBytes) ) ); BOOST_ASSERT( first_byte_index < last_byte_index ); @@ -353,8 +369,10 @@ void MessagePayload::encode32( const int first_byte_index, const int last_byte_index, const uint32_t value -) const +) { + BOOST_ASSERT( 0 < PayloadSizeInBytes ); + BOOST_ASSERT( Payload.capacity() == PayloadSizeInBytes ); BOOST_ASSERT( ( 0 <= first_byte_index ) && ( first_byte_index < static_cast(PayloadSizeInBytes) ) ); BOOST_ASSERT( ( 0 <= last_byte_index ) && ( last_byte_index < static_cast(PayloadSizeInBytes) ) ); BOOST_ASSERT( first_byte_index < last_byte_index ); @@ -380,9 +398,10 @@ void MessagePayload::encode32( */ istream& MessagePayload::read( istream &is ) { - BOOST_ASSERT( Payload.get() != NULL ); + BOOST_ASSERT( 0 < PayloadSizeInBytes ); + BOOST_ASSERT( Payload.capacity() == PayloadSizeInBytes ); - char *payload_data_array = reinterpret_cast( Payload.get() ); + char *payload_data_array = reinterpret_cast( &Payload[ 0 ] ); (void) is.read( payload_data_array, static_cast( PayloadSizeInBytes ) ); @@ -393,6 +412,9 @@ istream& MessagePayload::read( istream &is ) << " bytes, but received " << is.gcount() << " bytes." << endl; } + BOOST_ASSERT( Payload.size() == PayloadSizeInBytes ); + BOOST_ASSERT( !Payload.empty() ); + return is; } @@ -406,11 +428,17 @@ istream& MessagePayload::read( istream &is ) */ ostream& MessagePayload::write( ostream &os ) const { - BOOST_ASSERT( Payload.get() != NULL ); + BOOST_ASSERT( 0 < PayloadSizeInBytes ); + BOOST_ASSERT( Payload.capacity() == PayloadSizeInBytes ); + BOOST_ASSERT( Payload.size() == PayloadSizeInBytes ); + BOOST_ASSERT( !Payload.empty() ); - const char *data_array = reinterpret_cast( Payload.get() ); + // Vector containers have their elements stored in contiguous storage + // locations, which means that their elements can be accessed not only + // using iterators but also using offsets on regular pointers to elements. + const char *payload_data_array = reinterpret_cast( &Payload[ 0 ] ); - (void) os.write( data_array, static_cast( PayloadSizeInBytes ) ); + (void) os.write( payload_data_array, static_cast( PayloadSizeInBytes ) ); return os; } diff --git a/src/host/messagepayload.h b/src/host/messagepayload.h index 772ff0a..c94f419 100644 --- a/src/host/messagepayload.h +++ b/src/host/messagepayload.h @@ -11,8 +11,7 @@ #include #include - -#include +#include //----------------------------------------------------------------------------- // MessagePayload @@ -36,7 +35,7 @@ public: const uint8_t& operator[]( const std::size_t offset ) const; uint8_t& operator[]( const std::size_t offset ); - uint8_t* get() const; + uint8_t* get(); void append( const uint8_t *extra_payload_data, @@ -51,7 +50,7 @@ public: const int byte_index, const int bit_index, const bool value - ) const; + ); uint16_t decode16( const int left_byte_index, @@ -61,7 +60,7 @@ public: const int left_byte_index, const int right_byte_index, const uint16_t value - ) const; + ); uint32_t decode32( const int first_byte, @@ -71,7 +70,7 @@ public: const int first_byte, const int last_byte, const uint32_t value - ) const; + ); std::istream& read( std::istream &is ); std::ostream& write( std::ostream &os ) const; @@ -80,7 +79,7 @@ private: /// The size of the payload buffer std::size_t PayloadSizeInBytes; /// The payload buffer - boost::scoped_array Payload; + std::vector Payload; }; -- 1.7.1