AuroraRuntime/Include/Aurora/IO/FS/FileWriter.hpp

83 lines
2.1 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: FileWriter.hpp
Date: 2021-6-10
Author: Reece
***/
#pragma once
namespace Aurora::IO::FS
{
struct FileWriter : IStreamWriter
{
AU_NO_COPY_NO_MOVE(FileWriter)
inline FileWriter() {}
inline FileWriter(const AuSPtr<IFileStream> &stream) : stream_(stream) { }
inline ~FileWriter() {}
template<typename... T>
inline bool OpenFile(T... args)
{
this->stream_ = OpenWriteShared(args...);
return bool(this->stream_);
}
inline virtual EStreamError IsOpen() override
{
return this->stream_ ? EStreamError::eErrorNone : EStreamError::eErrorStreamNotOpen;
}
inline virtual EStreamError Write(const Memory::MemoryViewStreamRead &parameters) override
{
if (!this->stream_)
{
return EStreamError::eErrorStreamNotOpen;
}
if (!this->stream_->Write(parameters))
{
return EStreamError::eErrorStreamInterrupted;
}
if (parameters.outVariable == 0)
{
return EStreamError::eErrorEndOfStream;
}
if (parameters.outVariable != parameters.length)
{
return EStreamError::eErrorStreamInterrupted;
}
return EStreamError::eErrorNone;
}
inline virtual void Flush() override
{
if (this->stream_)
{
this->stream_->Flush();
}
}
inline virtual void Close() override
{
this->stream_.reset();
}
inline virtual AuUInt64 GetPosition()
{
return this->stream_ ? this->stream_->GetOffset() : 0;
}
inline virtual bool SetPosition(AuUInt64 pos)
{
return this->stream_ ? this->stream_->SetOffset(pos) : false;
}
private:
AuSPtr<IFileStream> stream_ {};
};
}