BOOST_ASSERT( left_byte < right_byte );
BOOST_ASSERT( ( right_byte - left_byte ) == sizeof(uint16_t) );
- uint32_t value = ( Payload[ left_byte ] << 8 ) + Payload[ right_byte ];
+ uint32_t value = static_cast<uint16_t>( Payload[ left_byte ] << 8 );
+ value += static_cast<uint16_t>( Payload[ right_byte ] );
BOOST_ASSERT( value <= numeric_limits<uint16_t>::max() );
- return static_cast<uint16_t> ( value );
+ return static_cast<uint16_t>( value );
}
/**
BOOST_ASSERT( left_byte < right_byte );
BOOST_ASSERT( ( right_byte - left_byte ) == sizeof(uint16_t) );
- Payload[ left_byte ] = static_cast<uint8_t> ( value >> 8 );
- Payload[ right_byte ] = static_cast<uint8_t> ( value & 0xFF );
+ Payload[ left_byte ] = static_cast<uint8_t>( value >> 8 );
+ Payload[ right_byte ] = static_cast<uint8_t>( value & 0xFF );
+}
+
+/**
+ * @brief Retrieve 32 bits from the payload buffer.
+ *
+ * @param first_byte The index of the first byte out of 4
+ * @param last_byte The index of the last byte out of 4
+ *
+ * @return a concatenation of 4 bytes, from the byte indexed by first_byte to
+ * the byte indexed by last_byte.
+ */
+uint32_t MessagePayload::decode32(
+ const int first_byte,
+ const int last_byte
+) const
+{
+ BOOST_ASSERT( ( 0 <= first_byte ) && ( first_byte < static_cast<int>(PayloadSizeInBytes) ) );
+ BOOST_ASSERT( ( 0 <= last_byte ) && ( last_byte < static_cast<int>(PayloadSizeInBytes) ) );
+ BOOST_ASSERT( first_byte < last_byte );
+ BOOST_ASSERT( ( last_byte - first_byte ) == sizeof(uint32_t) );
+
+ int current_byte = first_byte;
+
+ uint64_t value = static_cast<uint32_t>( Payload[ current_byte ] << 24 );
+ value += static_cast<uint32_t>( Payload[ ++current_byte ] << 16 );
+ value += static_cast<uint32_t>( Payload[ ++current_byte ] << 8 );
+ value += static_cast<uint32_t>( Payload[ ++current_byte ] );
+
+ BOOST_ASSERT( value <= numeric_limits<uint32_t>::max() );
+ BOOST_ASSERT( current_byte == last_byte );
+
+ return static_cast<uint32_t>( value );
+}
+
+/**
+ * @brief Store 32 bits in the payload buffer.
+ *
+ * @param first_byte The index of the first byte out of 4
+ * @param last_byte The index of the last byte out of 4
+ * @param value a 32 bits data be saved in the bytes indexed from the first_byte
+ * to the last_byte.
+ *
+ * @return void
+ */
+void MessagePayload::encode32(
+ const int first_byte,
+ const int last_byte,
+ const uint32_t value
+)
+{
+ BOOST_ASSERT( ( 0 < first_byte ) && ( first_byte < static_cast<int>(PayloadSizeInBytes) ) );
+ BOOST_ASSERT( ( 0 < last_byte ) && ( last_byte < static_cast<int>(PayloadSizeInBytes) ) );
+ BOOST_ASSERT( first_byte < last_byte );
+ BOOST_ASSERT( ( last_byte - first_byte ) == sizeof(uint32_t) );
+
+ int current_byte = first_byte;
+
+ Payload[ current_byte ] = static_cast<uint8_t>( ( value >> 24 ) & 0xFF );
+ Payload[ ++current_byte ] = static_cast<uint8_t>( ( value >> 16 ) & 0xFF );
+ Payload[ ++current_byte ] = static_cast<uint8_t>( ( value >> 8 ) & 0xFF );
+ Payload[ ++current_byte ] = static_cast<uint8_t>( value & 0xFF );
+
+ BOOST_ASSERT( current_byte == last_byte );
}
/**