#ifndef cmw_Formatting_hh #define cmw_Formatting_hh #include class SameFormat { public: template class B, typename U> void Read (B& buf, U& data); template class B, typename U> void ReadBlock (B& buf, U* data, unsigned int elements); }; class LeastSignificantFirst { public: template class B> void Read (B& buf, uint16_t&); template class B> void Read (B& buf, uint32_t&); template class B> void Read (B& buf, uint64_t&); template class B> void Read (B& buf, float&); template class B> void Read (B& buf, double&); template class B, typename U> void ReadBlock (B& buf , U* data , unsigned int elements ); template class B> void ReadBlock (B& buf , uint8_t* data , unsigned int elements ); template class B> void ReadBlock (B& buf , int8_t* data , unsigned int elements ); }; class MostSignificantFirst { public: template class B> void Read (B& buf, uint16_t&); template class B> void Read (B& buf, uint32_t&); template class B> void Read (B& buf, uint64_t&); template class B> void Read (B& buf, float&); template class B> void Read (B& buf, double&); template class B, typename U> void ReadBlock (B& buf , U* data , unsigned int elements ); template class B> void ReadBlock (B& buf , uint8_t* data , unsigned int elements ); template class B> void ReadBlock (B& buf , int8_t* data , unsigned int elements ); }; uint8_t const least_significant_first = 0; uint8_t const most_significant_first = 1; #include // memcpy template class B, typename U> void SameFormat::Read (B& buf, U& data) { buf.Give(&data, sizeof data); } template class B, typename U> void SameFormat::ReadBlock (B& buf , U* data , unsigned int elements ) { buf.Give(data, elements * sizeof(U)); } // ---------------------------------------------------- template class B> void LeastSignificantFirst::Read (B& buf , uint16_t& value ) { value = buf.Give(); value |= buf.Give() << 8; } template class B> void LeastSignificantFirst::Read (B& buf , uint32_t& value ) { value = buf.Give(); value |= buf.Give() << 8; value |= buf.Give() << 16; value |= buf.Give() << 24; } template class B> void LeastSignificantFirst::Read (B& buf , uint64_t& value ) { value = buf.Give(); value |= buf.Give() << 8; value |= buf.Give() << 16; value |= buf.Give() << 24; value |= (uint64_t)buf.Give() << 32; value |= (uint64_t)buf.Give() << 40; value |= (uint64_t)buf.Give() << 48; value |= (uint64_t)buf.Give() << 56; } template class B> void LeastSignificantFirst::Read (B& buf , float& value ) { uint32_t tmp; this->Read(buf, tmp); ::std::memcpy(&value, &tmp, sizeof(value)); } template class B> void LeastSignificantFirst::Read (B& buf , double& value ) { uint64_t tmp; this->Read(buf, tmp); ::std::memcpy(&value, &tmp, sizeof(value)); } template class B, typename U> void LeastSignificantFirst::ReadBlock (B& buf , U* data , unsigned int elements ) { for (unsigned int ii = 0; ii < elements; ++ii) { buf.Give(*(data + ii)); } } /* Overloads for when U is uint8_t or int8_t */ template class B> void LeastSignificantFirst::ReadBlock (B& buf , uint8_t* data , unsigned int elements ) { buf.Give(data, elements); } template class B> void LeastSignificantFirst::ReadBlock (B& buf , int8_t* data , unsigned int elements ) { buf.Give(data, elements); } // ---------------------------------------------------- template class B> void MostSignificantFirst::Read (B& buf , uint16_t& value ) { value = buf.Give() << 8; value |= buf.Give(); } template class B> void MostSignificantFirst::Read (B& buf , uint32_t& value ) { value = buf.Give() << 24; value |= buf.Give() << 16; value |= buf.Give() << 8; value |= buf.Give(); } template class B> void MostSignificantFirst::Read (B& buf , uint64_t& value ) { value = (uint64_t)buf.Give() << 56; value |= (uint64_t)buf.Give() << 48; value |= (uint64_t)buf.Give() << 40; value |= (uint64_t)buf.Give() << 32; value |= buf.Give() << 24; value |= buf.Give() << 16; value |= buf.Give() << 8; value |= buf.Give(); } template class B> void MostSignificantFirst::Read (B& buf , float& value ) { uint32_t tmp; this->Read(buf, tmp); ::std::memcpy(&value, &tmp, sizeof(value)); } template class B> void MostSignificantFirst::Read (B& buf , double& value ) { uint64_t tmp; this->Read(buf, tmp); ::std::memcpy(&value, &tmp, sizeof(value)); } template class B, typename U> void MostSignificantFirst::ReadBlock (B& buf , U* data , unsigned int elements ) { for (unsigned int ii = 0; ii < elements; ++ii) { buf.Give(*(data + ii)); } } template class B> void MostSignificantFirst::ReadBlock (B& buf , uint8_t* data , unsigned int elements ) { buf.Give(data, elements); } template class B> void MostSignificantFirst::ReadBlock (B& buf , int8_t* data , unsigned int elements ) { buf.Give(data, elements); } #endif