84 lines
2.2 KiB
C++
84 lines
2.2 KiB
C++
|
/***
|
||
|
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
||
|
|
||
|
File: ByteBufferStreamPair.cpp
|
||
|
Date: 2022-6-6
|
||
|
Author: Reece
|
||
|
***/
|
||
|
#include <Source/RuntimeInternal.hpp>
|
||
|
#include <Aurora/IO/IOExperimental.hpp>
|
||
|
#include "ByteBufferStreamPair.hpp"
|
||
|
|
||
|
namespace Aurora::IO
|
||
|
{
|
||
|
ByteBufferStreamPair::ByteBufferStreamPair(const AuSPtr<Memory::ByteBuffer> &buffer) :
|
||
|
buffer(buffer),
|
||
|
reader(buffer),
|
||
|
writer(buffer),
|
||
|
seeker(buffer)
|
||
|
{
|
||
|
|
||
|
}
|
||
|
|
||
|
AuSPtr<IStreamReader> ByteBufferStreamPair::ToStreamReader()
|
||
|
{
|
||
|
return AuSPtr<IStreamReader>(AuSharedFromThis(), AuStaticCast<IStreamReader>(&this->reader));
|
||
|
}
|
||
|
|
||
|
AuSPtr<ISeekingReader> ByteBufferStreamPair::ToSeekingReader()
|
||
|
{
|
||
|
if (this->buffer->flagCircular)
|
||
|
{
|
||
|
SysPushErrorIO("ring buffer must not be seekable");
|
||
|
return {};
|
||
|
}
|
||
|
|
||
|
return AuSPtr<ISeekingReader>(AuSharedFromThis(), AuStaticCast<ISeekingReader>(&this->seeker));
|
||
|
}
|
||
|
|
||
|
AuSPtr<IStreamWriter> ByteBufferStreamPair::ToStreamWriter()
|
||
|
{
|
||
|
return AuSPtr<IStreamWriter>(AuSharedFromThis(), AuStaticCast<IStreamWriter>(&this->writer));
|
||
|
}
|
||
|
|
||
|
AuSPtr<Memory::ByteBuffer> ByteBufferStreamPair::ToByteBuffer()
|
||
|
{
|
||
|
return this->buffer;
|
||
|
}
|
||
|
|
||
|
AUKN_SYM AuSPtr<IByteBufferStreamPair> NewByteBufferPair()
|
||
|
{
|
||
|
auto scalable = AuMakeShared<AuByteBuffer>();
|
||
|
if (!scalable)
|
||
|
{
|
||
|
SysPushErrorMem();
|
||
|
return {};
|
||
|
}
|
||
|
|
||
|
return AuMakeShared<ByteBufferStreamPair>(scalable);
|
||
|
}
|
||
|
|
||
|
AUKN_SYM AuSPtr<IByteBufferStreamPair> NewByteBufferPairEx(AuUInt length, bool permitResize)
|
||
|
{
|
||
|
auto buffer = AuMakeShared<AuByteBuffer>(length, false, permitResize);
|
||
|
if (!buffer)
|
||
|
{
|
||
|
SysPushErrorMem();
|
||
|
return {};
|
||
|
}
|
||
|
|
||
|
return AuMakeShared<ByteBufferStreamPair>(buffer);
|
||
|
}
|
||
|
|
||
|
AUKN_SYM AuSPtr<IByteBufferStreamPair> NewRingByteBuffer(AuUInt length)
|
||
|
{
|
||
|
auto buffer = AuMakeShared<AuByteBuffer>(length, false, false);
|
||
|
if (!buffer)
|
||
|
{
|
||
|
SysPushErrorMem();
|
||
|
return {};
|
||
|
}
|
||
|
|
||
|
return AuMakeShared<ByteBufferStreamPair>(buffer);
|
||
|
}
|
||
|
}
|