AuroraRuntime/Source/IO/Character/BufferedCharacterConsumer.cpp
Reece cd8cfd4f1c [+] Shutdown runtime on unsafe main thread termination
[+] Grug will flush everything inline on deinit
[*] Fix bug in BufferedCharConsumer, didn't return char in peak (doh)
2022-02-21 03:40:29 +00:00

97 lines
2.1 KiB
C++

/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: BufferedCharacterConsumer.cpp
Date: 2022-1-29
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "BufferedCharacterConsumer.hpp"
namespace Aurora::IO::Character
{
BufferedCharacterConsumer::BufferedCharacterConsumer(const AuSPtr<ICharacterProvider> &provider) : provider_(provider)
{
}
bool BufferedCharacterConsumer::HasBufferedNext() const
{
return this->hasNext_;
}
AuUInt8 BufferedCharacterConsumer::NextBufferedByte() const
{
return this->peeked_;
}
bool BufferedCharacterConsumer::PeekNext(AuUInt8 &out)
{
AU_LOCK_GUARD(this->lock_);
if (!this->hasNext_)
{
this->hasNext_ = provider_->GetByte(this->peeked_);
}
if (!this->hasNext_)
{
this->hasFailed_ = true;
return false;
}
out = this->peeked_;
return this->hasNext_;
}
AuUInt8 BufferedCharacterConsumer::GetCurrent()
{
AU_LOCK_GUARD(this->lock_);
return this->cur_;
}
bool BufferedCharacterConsumer::Next(AuUInt8 &out)
{
AU_LOCK_GUARD(this->lock_);
// autism security
if (this->hasFailed_)
{
return false;
}
if (this->hasNext_)
{
out = this->peeked_;
this->cur_ = out;
this->hasNext_ = false;
return true;
}
if (this->provider_->GetByte(out))
{
this->cur_ = out;
return true;
}
this->hasFailed_ = true;
return false;
}
IBufferedCharacterConsumer *BufferConsumerFromProviderNew(const AuSPtr<ICharacterProvider> &provider)
{
if (!provider)
{
SysPushErrorArg("Missing ICharacterProvider");
return {};
}
return _new BufferedCharacterConsumer(provider);
}
void BufferConsumerFromProviderRelease(IBufferedCharacterConsumer *me)
{
AuSafeDelete<BufferedCharacterConsumer *>(me);
}
}