corelib/include/ebcl/inline/Buffers.hh

266 lines
6.5 KiB
C++
Raw Normal View History

/******************************************************************************/
/* BUFFERS - INLINE CODE ******************************************************/
/******************************************************************************/
#ifndef _H_EBCL_INLINE_BUFFERS
#define _H_EBCL_INLINE_BUFFERS
#include <ebcl/Buffers.hh>
namespace ebcl {
/*= T_BufferBase =============================================================*/
inline T_BufferBase::T_BufferBase( uint8_t* buffer ) noexcept
: data_( buffer )
{ }
inline T_BufferBase::T_BufferBase( size_t size )
: data_( ( uint8_t* ) malloc( size ) )
{
if ( size != 0 && data_ == nullptr ) {
throw std::bad_alloc( );
}
}
inline T_BufferBase::T_BufferBase( T_BufferBase&& source ) noexcept
: T_BufferBase( nullptr )
{
swap( *this , source );
}
inline T_BufferBase::~T_BufferBase( )
{
free( data_ );
}
/*----------------------------------------------------------------------------*/
inline void swap( T_BufferBase& lhs , T_BufferBase& rhs )
{
using std::swap;
swap( lhs.data_ , rhs.data_ );
}
/*----------------------------------------------------------------------------*/
inline uint8_t* T_BufferBase::data( ) const
{
return data_;
}
/*= T_FixedBuffer ============================================================*/
template< size_t S , typename T >
inline T_FixedBuffer< S , T >::T_FixedBuffer( )
: T_BufferBase( BYTES_ )
{ }
template< size_t S , typename T >
inline T_FixedBuffer< S , T >::T_FixedBuffer(
T_FixedBuffer< S , T >&& source ) noexcept
: T_BufferBase( std::move( source ) )
{ }
template< size_t S , typename T >
inline T_FixedBuffer< S , T >::T_FixedBuffer(
T_FixedBuffer< S , T > const& source )
: T_BufferBase( BYTES_ )
{
*this = source;
}
/*----------------------------------------------------------------------------*/
template< size_t S , typename T >
inline T_FixedBuffer< S , T >& T_FixedBuffer< S , T >::operator= ( T_FixedBuffer< S , T > const& other )
{
memcpy( data_ , other.data_ , BYTES_ );
return *this;
}
template< size_t S , typename T >
inline T_FixedBuffer< S , T >& T_FixedBuffer< S , T >::operator= ( T_FixedBuffer< S , T >&& other ) noexcept
{
swap( *this , other );
return *this;
}
/*----------------------------------------------------------------------------*/
template< size_t S , typename T >
inline void swap( T_FixedBuffer< S , T >& lhs , T_FixedBuffer< S , T >& rhs ) noexcept
{
swap( ( T_BufferBase& ) lhs , ( T_BufferBase& ) rhs );
}
/*----------------------------------------------------------------------------*/
template< size_t S , typename T >
inline size_t T_FixedBuffer< S , T >::bytes( ) const
{
return BYTES_;
}
template< size_t S , typename T >
inline T& T_FixedBuffer< S , T >::operator[] ( size_t index )
{
assert( index < S );
return *reinterpret_cast< T* >( data_ + index * sizeof( T ) );
}
template< size_t S , typename T >
inline T const& T_FixedBuffer< S , T >::operator[] ( size_t index ) const
{
assert( index < S );
return *reinterpret_cast< T* >( data_ + index * sizeof( T ) );
}
/*----------------------------------------------------------------------------*/
template< size_t S , typename T >
inline T_FixedBuffer< S , T >& T_FixedBuffer< S , T >::setAll( T const& value )
{
for ( size_t i = 0 ; i < S ; i ++ ) {
memcpy( &(*this)[ i ] , &value , sizeof( T ) );
}
return *this;
}
/*= T_Buffer =================================================================*/
template< typename T >
inline T_Buffer< T >::T_Buffer( ) noexcept
: T_BufferBase( nullptr ) , size_( 0 )
{ }
template< typename T >
inline T_Buffer< T >::T_Buffer( size_t size )
: T_BufferBase( size * sizeof( T ) ) , size_( size )
{ }
template< typename T >
inline T_Buffer< T >::T_Buffer( T const* data , size_t size )
: T_BufferBase( size * sizeof( T ) ) , size_( size )
{
memcpy( data_ , data , bytes( ) );
}
/*----------------------------------------------------------------------------*/
template< typename T >
inline T_Buffer< T >::T_Buffer( T_Buffer< T > const& other )
: T_Buffer( reinterpret_cast< T const* >( other.data_ ) , other.size( ) )
{ }
template< typename T >
inline T_Buffer< T >::T_Buffer( T_Buffer< T >&& other ) noexcept
: T_Buffer( )
{
swap( *this , other );
}
/*----------------------------------------------------------------------------*/
template< typename T >
inline T_Buffer< T >& T_Buffer< T >::operator= ( T_Buffer< T > const& other )
{
if ( size_ != other.size_ ) {
free( data_ );
size_ = other.size_;
if ( size_ == 0 ) {
data_ = nullptr;
} else {
data_ = ( uint8_t* ) malloc( bytes( ) );
if ( data_ == nullptr ) {
throw std::bad_alloc( );
}
}
}
memcpy( data_ , other.data_ , bytes( ) );
return *this;
}
template< typename T >
inline T_Buffer< T >& T_Buffer< T >::operator= ( T_Buffer< T >&& other )
{
swap( *this , other );
return *this;
}
/*----------------------------------------------------------------------------*/
template< typename T >
inline void swap( T_Buffer< T >& lhs , T_Buffer< T >& rhs ) noexcept
{
using std::swap;
swap( ( T_BufferBase& ) lhs , ( T_BufferBase& ) rhs );
swap( lhs.size_ , rhs.size_ );
}
/*----------------------------------------------------------------------------*/
template< typename T >
inline size_t T_Buffer< T >::size( ) const
{
return size_;
}
template< typename T >
inline size_t T_Buffer< T >::bytes( ) const
{
return size_ * sizeof( T );
}
/*----------------------------------------------------------------------------*/
template< typename T >
inline T& T_Buffer< T >::operator[] ( size_t index )
{
assert( index < size_ );
return *reinterpret_cast< T* >( data_ + index * sizeof( T ) );
}
template< typename T >
inline T const& T_Buffer< T >::operator[] ( size_t index ) const
{
assert( index < size_ );
return *reinterpret_cast< T* >( data_ + index * sizeof( T ) );
}
/*----------------------------------------------------------------------------*/
template< typename T >
inline T_Buffer< T >& T_Buffer< T >::resize( size_t newSize )
{
if ( newSize != size_ ) {
size_ = newSize;
data_ = ( uint8_t* ) realloc( data_ , bytes( ) );
if ( bytes( ) != 0 && data_ == nullptr ) {
throw std::bad_alloc( );
}
}
return *this;
}
template< typename T >
inline T_Buffer< T >& T_Buffer< T >::setAll( T const& value )
{
for ( size_t i = 0 ; i < size_ ; i ++ ) {
memcpy( &(*this)[ i ] , &value , sizeof( T ) );
}
return *this;
}
template< typename T >
inline T_Buffer< T >& T_Buffer< T >::copyAll( T const* data )
{
memcpy( data_ , data , bytes( ) );
return *this;
}
} // namespace
#endif // _H_EBCL_INLINE_BUFFERS