From b33921531d3ddd59d393d254441fbe16e93f9358 Mon Sep 17 00:00:00 2001 From: asynts Date: Sat, 3 Oct 2020 19:11:11 +0200 Subject: [PATCH] AK: Make Buffered non-copyable. --- AK/Buffered.h | 41 ++++++++++++++++++++++++++++++----------- AK/Result.h | 9 ++++++++- 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/AK/Buffered.h b/AK/Buffered.h index eb593214f25..4b0fe71c6ba 100644 --- a/AK/Buffered.h +++ b/AK/Buffered.h @@ -37,6 +37,8 @@ class Buffered; template class Buffered::value>::Type> final : public InputStream { + AK_MAKE_NONCOPYABLE(Buffered); + public: template 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 class Buffered::value>::Type> final : public OutputStream { + AK_MAKE_NONCOPYABLE(Buffered); + public: template 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(); } diff --git a/AK/Result.h b/AK/Result.h index 6c72163b197..64595f8b935 100644 --- a/AK/Result.h +++ b/AK/Result.h @@ -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)