forked from Alepha/Alepha
Get Blob and Buffer building and a bit tested
This commit is contained in:
82
Buffer.h
82
Buffer.h
@ -2,6 +2,11 @@ static_assert( __cplusplus > 2020'99 );
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Alepha.h>
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <array>
|
||||
@ -10,19 +15,22 @@ static_assert( __cplusplus > 2020'99 );
|
||||
#include <exception>
|
||||
#include <stdexcept>
|
||||
|
||||
#include <boost/lexical_cast.hpp>
|
||||
#include <Alepha/Constness.h>
|
||||
#include <Alepha/lifetime.h>
|
||||
|
||||
#include <Alepha/IOStreams/String.h>
|
||||
|
||||
#include "Concepts.h"
|
||||
#include "assertion.h"
|
||||
#include "Capabilities.h"
|
||||
#include "lifetime.h"
|
||||
|
||||
|
||||
namespace Alepha::inline Cavorite ::detail:: buffer
|
||||
namespace Alepha::Hydrogen ::detail:: Buffer_m
|
||||
{
|
||||
inline namespace exports {}
|
||||
|
||||
using namespace std::literals::string_literals;
|
||||
using IOStreams::stringify;
|
||||
|
||||
namespace exports
|
||||
{
|
||||
@ -42,7 +50,7 @@ namespace Alepha::inline Cavorite ::detail:: buffer
|
||||
: baseAddress( address ), requestedSize( requestedSize ), availableSize( availableSize )
|
||||
{}
|
||||
|
||||
public
|
||||
public:
|
||||
const void *getAddress() const noexcept { return baseAddress; }
|
||||
const std::size_t getRequestedSize() const noexcept { return requestedSize; }
|
||||
const std::size_t getAvailableSize() const noexcept { return availableSize; }
|
||||
@ -62,20 +70,20 @@ namespace Alepha::inline Cavorite ::detail:: buffer
|
||||
: std::out_of_range( "Tried to access an object of type "s + type.name() + " which is " + stringify( requestedSize ) + " bytes in size. "
|
||||
+ "The request was at location " + stringify( location ) + " which only has " + stringify( availableSize )
|
||||
+ " bytes allocated" ),
|
||||
OutOfRangeError( location, requestedSIze, availableSize ),
|
||||
OutOfRangeError( location, requestedSize, availableSize ),
|
||||
typeID( type )
|
||||
{}
|
||||
};
|
||||
|
||||
class OutOfRangeSizeError
|
||||
: virtual public OutOfRangeException
|
||||
: virtual public OutOfRangeError
|
||||
{
|
||||
public:
|
||||
explicit
|
||||
OutOfRangeSizeException( const void *const location, const std::ptrdiff_t requestedOffset, const std::size_t availableSpace )
|
||||
OutOfRangeSizeError( const void *const location, const std::ptrdiff_t requestedOffset, const std::size_t availableSpace )
|
||||
: std::out_of_range( "Tried to view a byte offset of " + stringify( requestedOffset ) + " into location " + stringify( location )
|
||||
+ " which is " + stringify( availableSpace ) + " bytes in size." ),
|
||||
OutOfRangeException( location, requestedOffset, availableSpace )
|
||||
OutOfRangeError( location, requestedOffset, availableSpace )
|
||||
{}
|
||||
};
|
||||
|
||||
@ -106,26 +114,31 @@ namespace Alepha::inline Cavorite ::detail:: buffer
|
||||
|
||||
constexpr
|
||||
Buffer( const pointer_type ptr, const std::size_t bytes ) noexcept
|
||||
: ptr( static_cast< byte_pointer_type >( ptr ), bytes( bytes )
|
||||
: ptr( static_cast< byte_pointer_type >( ptr ) ), bytes( bytes )
|
||||
{}
|
||||
|
||||
constexpr
|
||||
Buffer( const Buffer< Mutable > © ) noexcept
|
||||
: ptr( copy.byte_data() ), bytes( copy.size() ) {}
|
||||
|
||||
template< Constness constness_= constness >
|
||||
requires( constness_ == Mutable )
|
||||
constexpr
|
||||
Buffer( const Buffer< Const > © ) noexcept requires( Constness == Mutable )= delete;
|
||||
Buffer( const Buffer< Const > © ) noexcept = delete;
|
||||
|
||||
|
||||
constexpr byte_pointer_type byte_data() const noexcept { return ptr; }
|
||||
constexpr pointer_type data() const noexcept { return ptr; }
|
||||
|
||||
constexpr const_byte_pointer_type byte_data() const noexcept { return ptr; }
|
||||
constexpr const_pointer_type data() const noexcept { return ptr; }
|
||||
constexpr const_byte_pointer_type const_byte_data() const noexcept { return ptr; }
|
||||
constexpr const_pointer_type const_data() const noexcept { return ptr; }
|
||||
|
||||
constexpr std::size_t size() const noexcept { return bytes; }
|
||||
constexpr bool empty() const noexcept { return size() == 0; }
|
||||
|
||||
constexpr byte_pointer_type begin() const noexcept { return byte_data(); }
|
||||
constexpr byte_pointer_type end() const noexcept { return begin() + size(); }
|
||||
|
||||
constexpr const_byte_pointer_type cbegin() const noexcept { return begin(); }
|
||||
constexpr const_byte_pointer_type cend() const noexcept { return end(); }
|
||||
|
||||
@ -137,7 +150,7 @@ namespace Alepha::inline Cavorite ::detail:: buffer
|
||||
as( std::nothrow_t ) const noexcept
|
||||
{
|
||||
assertion( sizeof( T ) <= bytes );
|
||||
return *start_lifetime_as< std::add_lvalue_reference_t< maybe_const_t< T, constness > > >( ptr );
|
||||
return *Alepha::start_lifetime_as< std::add_lvalue_reference_t< maybe_const_t< T, constness > > >( ptr );
|
||||
}
|
||||
|
||||
template< typename T >
|
||||
@ -153,7 +166,7 @@ namespace Alepha::inline Cavorite ::detail:: buffer
|
||||
const_as( std::nothrow_t ) const noexcept
|
||||
{
|
||||
assertion( sizeof( T ) <= bytes );
|
||||
return *start_lifetime_as< std::add_const_t< T > >( ptr );
|
||||
return *Alepha::start_lifetime_as< std::add_const_t< T > >( ptr );
|
||||
}
|
||||
|
||||
template< typename T >
|
||||
@ -222,7 +235,7 @@ namespace Alepha::inline Cavorite ::detail:: buffer
|
||||
struct BufferModel_capability {};
|
||||
|
||||
template< typename T >
|
||||
concept UndecayedBufferModelable= HasCapability< T, BufferModel_capability >;
|
||||
concept UndecayedBufferModelable= Capability< T, BufferModel_capability >;
|
||||
|
||||
template< typename T >
|
||||
concept BufferModelable= UndecayedBufferModelable< std::decay_t< T > >;
|
||||
@ -234,8 +247,8 @@ namespace Alepha::inline Cavorite ::detail:: buffer
|
||||
constexpr auto &crtp() noexcept { return static_cast< Derived & >( *this ); }
|
||||
constexpr const auto &crtp() const noexcept { return static_cast< const Derived & >( *this ); }
|
||||
|
||||
constexpr auto &buffer() { return static_cast< Buffer< Mutable > >( crtp() ); }
|
||||
constexpr const auto &buffer() const { return static_cast< Buffer< Const > >( crtp() ); }
|
||||
constexpr auto buffer() { return static_cast< Buffer< Mutable > >( crtp() ); }
|
||||
constexpr auto buffer() const { return static_cast< Buffer< Const > >( crtp() ); }
|
||||
|
||||
public:
|
||||
constexpr auto byte_data() { return buffer().byte_data(); }
|
||||
@ -273,8 +286,8 @@ namespace Alepha::inline Cavorite ::detail:: buffer
|
||||
template< typename T >
|
||||
extern Constness constness_of_v;
|
||||
|
||||
template< UndecayedBufferModelble T >
|
||||
constexpr Constness constness_of_v= std::is_const_v< T >;
|
||||
template< UndecayedBufferModelable T >
|
||||
constexpr Constness constness_of_v< T >{ std::is_const_v< T > };
|
||||
|
||||
template< Constness constness >
|
||||
constexpr Constness constness_of_v< Buffer< constness > >{ constness };
|
||||
@ -283,7 +296,7 @@ namespace Alepha::inline Cavorite ::detail:: buffer
|
||||
constexpr auto
|
||||
operator + ( const Buffer< constness > buffer, const std::size_t offset )
|
||||
{
|
||||
if( offset > buffer.size() ) throw OutOfRangeError{ buffer.data(), offset, buffer.size() };
|
||||
if( offset > buffer.size() ) throw OutOfRangeSizeError{ buffer.data(), std::ptrdiff_t( offset ), buffer.size() };
|
||||
return Buffer< constness >{ buffer.byte_data() + offset, buffer.size() - offset };
|
||||
}
|
||||
|
||||
@ -372,23 +385,6 @@ namespace Alepha::inline Cavorite ::detail:: buffer
|
||||
}
|
||||
|
||||
|
||||
template< Concepts::StandardLayout T, std::size_t size >
|
||||
constexpr Buffer< Mutable >
|
||||
make_buffer( T array[ size ] ) noexcept
|
||||
{
|
||||
// TODO: Do we need to consider overflow here?
|
||||
return { array, sizeof( array ) };
|
||||
}
|
||||
|
||||
template< Concepts::StandardLayout T, std::size_t size >
|
||||
constexpr Buffer< Const >
|
||||
make_buffer( const T array[ size ] ) noexcept
|
||||
{
|
||||
// TODO: Do we need to consider overflow here?
|
||||
return { array, sizeof( array ) };
|
||||
}
|
||||
|
||||
|
||||
inline Buffer< Mutable >
|
||||
make_buffer( std::string &string ) noexcept
|
||||
{
|
||||
@ -407,7 +403,7 @@ namespace Alepha::inline Cavorite ::detail:: buffer
|
||||
{
|
||||
if( source.size() > destination.size() ) throw InsufficientSizeError{ destination.data(), source.size(), destination.size(), typeid( std::byte ) };
|
||||
|
||||
::memcpy( destination, source, source.size() );
|
||||
std::memcpy( destination, source, source.size() );
|
||||
return { destination, source.size() };
|
||||
}
|
||||
|
||||
@ -419,13 +415,13 @@ namespace Alepha::inline Cavorite ::detail:: buffer
|
||||
|
||||
namespace exports
|
||||
{
|
||||
using detail::buffer::make_buffer;
|
||||
using detail::Buffer_m::make_buffer;
|
||||
}
|
||||
}
|
||||
|
||||
namespace Alepha::Cavorite::inline exports::inline buffer
|
||||
namespace Alepha::Hydrogen::inline exports::inline Buffer_m
|
||||
{
|
||||
using namespace detail::buffer::exports;
|
||||
using namespace detail::Buffer_m::exports;
|
||||
}
|
||||
|
||||
/*
|
||||
@ -453,8 +449,8 @@ namespace Alepha::Cavorite::inline exports::inline buffer
|
||||
|
||||
template<>
|
||||
constexpr auto
|
||||
std::cbegin( const ::Alepha::Cavorite::Buffer< Alepha::Cavorite::Mutable > &range ) -> decltype( range.begin() )= delete;
|
||||
std::cbegin( const ::Alepha::Hydrogen::Buffer< Alepha::Hydrogen::Mutable > &range ) -> decltype( range.begin() )= delete;
|
||||
|
||||
template<>
|
||||
constexpr auto
|
||||
std::cend( const ::Alepha::Cavorite::Buffer< Alepha::Cavorite::Mutable > &range ) -> decltype( range.end() )= delete;
|
||||
std::cend( const ::Alepha::Hydrogen::Buffer< Alepha::Hydrogen::Mutable > &range ) -> decltype( range.end() )= delete;
|
||||
|
||||
Reference in New Issue
Block a user