Added the 32 bits version of encode and decode methods
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Sat, 13 Aug 2011 23:13:13 +0000 (20:13 -0300)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Sat, 13 Aug 2011 23:13:13 +0000 (20:13 -0300)
src/host/messagepayload.cpp
src/host/messagepayload.h

index b857b75..6c32e4e 100644 (file)
@@ -80,11 +80,12 @@ uint16_t MessagePayload::decode16(
     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 );
 }
 
 /**
@@ -108,8 +109,71 @@ void MessagePayload::encode16(
     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 );
 }
 
 /**
index 5bc19e2..bbe40ed 100644 (file)
@@ -48,6 +48,16 @@ public:
             const uint16_t value
     );
 
+    uint32_t decode32(
+            const int first_byte,
+            const int last_byte
+    ) const;
+    void encode32(
+            const int first_byte,
+            const int last_byte,
+            const uint32_t value
+    );
+
     std::istream& read( std::istream &is );
     std::ostream& write( std::ostream &os ) const;