Made TypelessBuffer allocator-aware and split some of its functionality into MemoryView type.
This commit is contained in:
@@ -132,8 +132,9 @@ public:
|
||||
const std::size_t bytes = std::distance(range.begin(), range.end()) * sizeof(std::ranges::range_value_t<TRange>);
|
||||
return readRaw(&*range.begin(), bytes, {.partial = partial}, outBytesRead);
|
||||
}
|
||||
|
||||
StreamError readRaw(TypelessBuffer& buffer, const ReadOptions& options = {})
|
||||
|
||||
template<template<typename> typename TAllocator>
|
||||
StreamError readRaw(BaseTypelessBuffer<TAllocator>& buffer, const ReadOptions& options = {})
|
||||
{
|
||||
return readRaw(buffer.data(), buffer.byteSize(), options);
|
||||
}
|
||||
@@ -144,8 +145,9 @@ public:
|
||||
const std::size_t bytes = std::distance(range.begin(), range.end()) * sizeof(std::ranges::range_value_t<TRange>);
|
||||
return c_readRaw(&*range.begin(), bytes, options, outBytesRead);
|
||||
}
|
||||
|
||||
mijin::Task<StreamError> c_readRaw(TypelessBuffer& buffer, const ReadOptions& options = {})
|
||||
|
||||
template<template<typename> typename TAllocator>
|
||||
mijin::Task<StreamError> c_readRaw(BaseTypelessBuffer<TAllocator>& buffer, const ReadOptions& options = {})
|
||||
{
|
||||
return c_readRaw(buffer.data(), buffer.byteSize(), options);
|
||||
}
|
||||
@@ -269,8 +271,12 @@ public:
|
||||
inline StreamError writeString(std::string_view str) { return writeBinaryString(str); }
|
||||
|
||||
StreamError getTotalLength(std::size_t& outLength);
|
||||
StreamError readRest(TypelessBuffer& outBuffer);
|
||||
mijin::Task<StreamError> c_readRest(TypelessBuffer& outBuffer);
|
||||
|
||||
template<template<typename> typename TAllocator>
|
||||
StreamError readRest(BaseTypelessBuffer<TAllocator>& outBuffer);
|
||||
|
||||
template<template<typename> typename TAllocator>
|
||||
mijin::Task<StreamError> c_readRest(BaseTypelessBuffer<TAllocator>& outBuffer);
|
||||
|
||||
StreamError readLine(std::string& outString);
|
||||
mijin::Task<StreamError> c_readLine(std::string& outString);
|
||||
@@ -360,6 +366,78 @@ using StreamResult = ResultBase<TSuccess, StreamError>;
|
||||
// public functions
|
||||
//
|
||||
|
||||
template<template<typename> typename TAllocator>
|
||||
StreamError Stream::readRest(BaseTypelessBuffer<TAllocator>& outBuffer)
|
||||
{
|
||||
// first try to allocate everything at once
|
||||
std::size_t length = 0;
|
||||
if (const StreamError lengthError = getTotalLength(length); lengthError == StreamError::SUCCESS)
|
||||
{
|
||||
MIJIN_ASSERT(getFeatures().tell, "How did you find the length if you cannot tell()?");
|
||||
length -= tell();
|
||||
outBuffer.resize(length);
|
||||
if (const StreamError error = readRaw(outBuffer.data(), length); error != StreamError::SUCCESS)
|
||||
{
|
||||
return error;
|
||||
}
|
||||
return StreamError::SUCCESS;
|
||||
}
|
||||
|
||||
// could not determine the size, read chunk-wise
|
||||
static constexpr std::size_t CHUNK_SIZE = 4096;
|
||||
std::array<std::byte, CHUNK_SIZE> chunk = {};
|
||||
|
||||
while (!isAtEnd())
|
||||
{
|
||||
std::size_t bytesRead = 0;
|
||||
if (const StreamError error = readRaw(chunk, {.partial = true}, &bytesRead); error != StreamError::SUCCESS)
|
||||
{
|
||||
return error;
|
||||
}
|
||||
|
||||
outBuffer.resize(outBuffer.byteSize() + bytesRead);
|
||||
const std::span<std::byte> bufferBytes = outBuffer.template makeSpan<std::byte>();
|
||||
std::copy_n(chunk.begin(), bytesRead, bufferBytes.end() - static_cast<long>(bytesRead));
|
||||
}
|
||||
return StreamError::SUCCESS;
|
||||
}
|
||||
|
||||
template<template<typename> typename TAllocator>
|
||||
mijin::Task<StreamError> Stream::c_readRest(BaseTypelessBuffer<TAllocator>& outBuffer)
|
||||
{
|
||||
// first try to allocate everything at once
|
||||
std::size_t length = 0;
|
||||
if (StreamError lengthError = getTotalLength(length); lengthError == StreamError::SUCCESS)
|
||||
{
|
||||
MIJIN_ASSERT(getFeatures().tell, "How did you find the length if you cannot tell()?");
|
||||
length -= tell();
|
||||
outBuffer.resize(length);
|
||||
if (StreamError error = co_await c_readRaw(outBuffer.data(), length); error != StreamError::SUCCESS)
|
||||
{
|
||||
co_return error;
|
||||
}
|
||||
co_return StreamError::SUCCESS;
|
||||
}
|
||||
|
||||
// could not determine the size, read chunk-wise
|
||||
static constexpr std::size_t CHUNK_SIZE = 4096;
|
||||
std::array<std::byte, CHUNK_SIZE> chunk = {};
|
||||
|
||||
while (!isAtEnd())
|
||||
{
|
||||
std::size_t bytesRead = 0;
|
||||
if (StreamError error = co_await c_readRaw(chunk, {.partial = true}, &bytesRead); error != StreamError::SUCCESS)
|
||||
{
|
||||
co_return error;
|
||||
}
|
||||
|
||||
outBuffer.resize(outBuffer.byteSize() + bytesRead);
|
||||
std::span<std::byte> bufferBytes = outBuffer.template makeSpan<std::byte>();
|
||||
std::copy_n(chunk.begin(), bytesRead, bufferBytes.end() - static_cast<long>(bytesRead));
|
||||
}
|
||||
co_return StreamError::SUCCESS;
|
||||
}
|
||||
|
||||
template<typename TChar>
|
||||
StreamError Stream::readAsString(std::basic_string<TChar>& outString)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user