AK: Make Buffered<T> non-copyable.

This commit is contained in:
asynts 2020-10-03 19:11:11 +02:00 committed by Andreas Kling
parent e64f43c3a7
commit b33921531d
Notes: sideshowbarker 2024-07-19 02:05:09 +09:00
2 changed files with 38 additions and 12 deletions

View file

@ -37,6 +37,8 @@ class Buffered;
template<typename StreamType, size_t Size>
class Buffered<StreamType, Size, typename EnableIf<IsBaseOf<InputStream, StreamType>::value>::Type> final : public InputStream {
AK_MAKE_NONCOPYABLE(Buffered);
public:
template<typename... Parameters>
explicit Buffered(Parameters&&... parameters)
@ -44,6 +46,13 @@ public:
{
}
Buffered(Buffered&& other)
: m_stream(move(other.m_stream))
{
other.buffer().copy_to(buffer());
m_buffered = exchange(other.m_buffered, 0);
}
bool has_recoverable_error() const override { return m_stream.has_recoverable_error(); }
bool has_fatal_error() const override { return m_stream.has_fatal_error(); }
bool has_any_error() const override { return m_stream.has_any_error(); }
@ -60,15 +69,15 @@ public:
if (has_any_error())
return 0;
auto nread = buffer().trim(m_buffer_remaining).copy_trimmed_to(bytes);
auto nread = buffer().trim(m_buffered).copy_trimmed_to(bytes);
m_buffer_remaining -= nread;
buffer().slice(nread, m_buffer_remaining).copy_to(buffer());
m_buffered -= nread;
buffer().slice(nread, m_buffered).copy_to(buffer());
if (nread < bytes.size()) {
m_buffer_remaining = m_stream.read(buffer());
m_buffered = m_stream.read(buffer());
if (m_buffer_remaining == 0)
if (m_buffered == 0)
return nread;
nread += read(bytes.slice(nread));
@ -87,16 +96,16 @@ public:
return true;
}
bool unreliable_eof() const override { return m_buffer_remaining == 0 && m_stream.unreliable_eof(); }
bool unreliable_eof() const override { return m_buffered == 0 && m_stream.unreliable_eof(); }
bool eof() const
{
if (m_buffer_remaining > 0)
if (m_buffered > 0)
return false;
m_buffer_remaining = m_stream.read(buffer());
m_buffered = m_stream.read(buffer());
return m_buffer_remaining == 0;
return m_buffered == 0;
}
bool discard_or_error(size_t count) override
@ -119,11 +128,13 @@ private:
mutable StreamType m_stream;
mutable u8 m_buffer[Size];
mutable size_t m_buffer_remaining { 0 };
mutable size_t m_buffered { 0 };
};
template<typename StreamType, size_t Size>
class Buffered<StreamType, Size, typename EnableIf<IsBaseOf<OutputStream, StreamType>::value>::Type> final : public OutputStream {
AK_MAKE_NONCOPYABLE(Buffered);
public:
template<typename... Parameters>
explicit Buffered(Parameters&&... parameters)
@ -131,9 +142,17 @@ public:
{
}
Buffered(Buffered&& other)
: m_stream(move(other.m_stream))
{
other.buffer().copy_to(buffer());
m_buffered = exchange(other.m_buffered, 0);
}
~Buffered()
{
flush();
if (m_buffered > 0)
flush();
}
bool has_recoverable_error() const override { return m_stream.has_recoverable_error(); }

View file

@ -39,11 +39,18 @@ public:
: m_result(res)
{
}
Result(ValueType && res)
: m_result(move(res))
{
}
Result(const ErrorType& error)
: m_error(error)
{
}
Result(ErrorType && error)
: m_error(move(error))
{
}
Result(const ValueType& res, const ErrorType& error)
: m_result(res)