#include "boost-compat/static_assert.hpp" #include "boost-compat/remove_cv.hpp" #ifdef HAVE_BOOST #include #endif namespace byte_io { template inline T no_const( const volatile T v ) { return v; } template inline void write( unsigned char* out, const volatile T d ) { write( out, no_const( d ) ); } template inline T read( const unsigned char* out ) { //BOOST_STATIC_ASSERT( !( ::boost::is_same::type>::value ) ); return read::type>( out ); } template<> inline void write( unsigned char* out, uint8_t d ) { *out = d; } template<> inline uint8_t read( const unsigned char* in ) { return *in; } template<> struct byte_length_struct { static const int value = 1; }; template<> inline void write( unsigned char* out, uint16_t d ) { *out++ = ( ( d >> 0 ) & 0xff ); *out++ = ( ( d >> 8 ) & 0xff ); } template<> inline uint16_t read( const unsigned char* in ) { uint16_t res = 0; res |= ( ( *in++ & 0xff ) << 0 ); res |= ( ( *in++ & 0xff ) << 8 ); return res; } template<> struct byte_length_struct { static const int value = 2; }; template<> inline void write( unsigned char* out, uint32_t d ) { *out++ = ( ( d >> 0 ) & 0xff ); *out++ = ( ( d >> 8 ) & 0xff ); *out++ = ( ( d >> 16 ) & 0xff ); *out++ = ( ( d >> 24 ) & 0xff ); } template<> inline uint32_t read( const unsigned char* in ) { uint32_t res = 0; res |= ( ( *in++ & 0xff ) << 0 ); res |= ( ( *in++ & 0xff ) << 8 ); res |= ( ( *in++ & 0xff ) << 16 ); res |= ( ( *in++ & 0xff ) << 24 ); return res; } template<> struct byte_length_struct { static const int value = 4; }; }