LBPM/IO/PackData.cpp

104 lines
2.2 KiB
C++
Raw Permalink Normal View History

2021-01-04 22:37:44 -06:00
#include "IO/PackData.h"
#include <string>
2021-01-04 22:37:44 -06:00
/********************************************************
2021-03-17 09:24:14 -05:00
* Concrete implimentations for packing/unpacking *
********************************************************/
2021-01-04 22:37:44 -06:00
// unsigned char
template<>
2021-03-17 09:24:14 -05:00
size_t packsize<unsigned char>( const unsigned char &rhs )
2021-01-04 22:37:44 -06:00
{
2021-03-17 09:24:14 -05:00
return sizeof( unsigned char );
2021-01-04 22:37:44 -06:00
}
template<>
2021-03-17 09:24:14 -05:00
void pack<unsigned char>( const unsigned char &rhs, char *buffer )
2021-01-04 22:37:44 -06:00
{
2021-03-17 09:24:14 -05:00
memcpy( buffer, &rhs, sizeof( unsigned char ) );
2021-01-04 22:37:44 -06:00
}
template<>
2021-03-17 09:24:14 -05:00
void unpack<unsigned char>( unsigned char &data, const char *buffer )
2021-01-04 22:37:44 -06:00
{
2021-03-17 09:24:14 -05:00
memcpy( &data, buffer, sizeof( unsigned char ) );
2021-01-04 22:37:44 -06:00
}
// char
template<>
2021-03-17 09:24:14 -05:00
size_t packsize<char>( const char &rhs )
2021-01-04 22:37:44 -06:00
{
2021-03-17 09:24:14 -05:00
return sizeof( char );
2021-01-04 22:37:44 -06:00
}
template<>
2021-03-17 09:24:14 -05:00
void pack<char>( const char &rhs, char *buffer )
2021-01-04 22:37:44 -06:00
{
2021-03-17 09:24:14 -05:00
memcpy( buffer, &rhs, sizeof( char ) );
2021-01-04 22:37:44 -06:00
}
template<>
2021-03-17 09:24:14 -05:00
void unpack<char>( char &data, const char *buffer )
2021-01-04 22:37:44 -06:00
{
2021-03-17 09:24:14 -05:00
memcpy( &data, buffer, sizeof( char ) );
2021-01-04 22:37:44 -06:00
}
// int
template<>
2021-03-17 09:24:14 -05:00
size_t packsize<int>( const int &rhs )
2021-01-04 22:37:44 -06:00
{
2021-03-17 09:24:14 -05:00
return sizeof( int );
2021-01-04 22:37:44 -06:00
}
template<>
2021-03-17 09:24:14 -05:00
void pack<int>( const int &rhs, char *buffer )
2021-01-04 22:37:44 -06:00
{
2021-03-17 09:24:14 -05:00
memcpy( buffer, &rhs, sizeof( int ) );
2021-01-04 22:37:44 -06:00
}
template<>
2021-03-17 09:24:14 -05:00
void unpack<int>( int &data, const char *buffer )
2021-01-04 22:37:44 -06:00
{
2021-03-17 09:24:14 -05:00
memcpy( &data, buffer, sizeof( int ) );
2021-01-04 22:37:44 -06:00
}
// unsigned int
template<>
2021-03-17 09:24:14 -05:00
size_t packsize<unsigned int>( const unsigned int &rhs )
2021-01-04 22:37:44 -06:00
{
2021-03-17 09:24:14 -05:00
return sizeof( unsigned int );
2021-01-04 22:37:44 -06:00
}
template<>
2021-03-17 09:24:14 -05:00
void pack<unsigned int>( const unsigned int &rhs, char *buffer )
2021-01-04 22:37:44 -06:00
{
2021-03-17 09:24:14 -05:00
memcpy( buffer, &rhs, sizeof( int ) );
2021-01-04 22:37:44 -06:00
}
template<>
2021-03-17 09:24:14 -05:00
void unpack<unsigned int>( unsigned int &data, const char *buffer )
2021-01-04 22:37:44 -06:00
{
2021-03-17 09:24:14 -05:00
memcpy( &data, buffer, sizeof( int ) );
2021-01-04 22:37:44 -06:00
}
// size_t
template<>
2021-03-17 09:24:14 -05:00
size_t packsize<size_t>( const size_t &rhs )
2021-01-04 22:37:44 -06:00
{
2021-03-17 09:24:14 -05:00
return sizeof( size_t );
2021-01-04 22:37:44 -06:00
}
template<>
2021-03-17 09:24:14 -05:00
void pack<size_t>( const size_t &rhs, char *buffer )
2021-01-04 22:37:44 -06:00
{
2021-03-17 09:24:14 -05:00
memcpy( buffer, &rhs, sizeof( size_t ) );
2021-01-04 22:37:44 -06:00
}
template<>
2021-03-17 09:24:14 -05:00
void unpack<size_t>( size_t &data, const char *buffer )
2021-01-04 22:37:44 -06:00
{
2021-03-17 09:24:14 -05:00
memcpy( &data, buffer, sizeof( size_t ) );
2021-01-04 22:37:44 -06:00
}
// std::string
template<>
2021-03-17 09:24:14 -05:00
size_t packsize<std::string>( const std::string &rhs )
2021-01-04 22:37:44 -06:00
{
2021-03-17 09:24:14 -05:00
return rhs.size() + 1;
2021-01-04 22:37:44 -06:00
}
template<>
2021-03-17 09:24:14 -05:00
void pack<std::string>( const std::string &rhs, char *buffer )
2021-01-04 22:37:44 -06:00
{
2021-03-17 09:24:14 -05:00
memcpy( buffer, rhs.c_str(), rhs.size() + 1 );
2021-01-04 22:37:44 -06:00
}
template<>
2021-03-17 09:24:14 -05:00
void unpack<std::string>( std::string &data, const char *buffer )
2021-01-04 22:37:44 -06:00
{
2021-03-17 09:24:14 -05:00
data = std::string( buffer );
2021-01-04 22:37:44 -06:00
}