AK: Remove the deprecated Stream implementation :^)

This commit is contained in:
Tim Schumacher 2023-01-30 09:57:02 +01:00 committed by Linus Groh
parent 63b11030f0
commit e8d5e938de
11 changed files with 1 additions and 708 deletions

View file

@ -1,139 +0,0 @@
/*
* Copyright (c) 2020, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/ByteBuffer.h>
#include <AK/DeprecatedStream.h>
#include <AK/MemMem.h>
#include <AK/Vector.h>
namespace AK {
class DeprecatedInputMemoryStream final : public DeprecatedInputStream {
public:
explicit DeprecatedInputMemoryStream(ReadonlyBytes bytes)
: m_bytes(bytes)
{
}
bool unreliable_eof() const override { return eof(); }
bool eof() const { return m_offset >= m_bytes.size(); }
size_t read(Bytes bytes) override
{
if (has_any_error())
return 0;
auto const count = min(bytes.size(), remaining());
__builtin_memcpy(bytes.data(), m_bytes.data() + m_offset, count);
m_offset += count;
return count;
}
bool read_or_error(Bytes bytes) override
{
if (remaining() < bytes.size()) {
set_recoverable_error();
return false;
}
__builtin_memcpy(bytes.data(), m_bytes.data() + m_offset, bytes.size());
m_offset += bytes.size();
return true;
}
bool discard_or_error(size_t count) override
{
if (remaining() < count) {
set_recoverable_error();
return false;
}
m_offset += count;
return true;
}
void seek(size_t offset)
{
VERIFY(offset < m_bytes.size());
m_offset = offset;
}
u8 peek_or_error() const
{
if (remaining() == 0) {
set_recoverable_error();
return 0;
}
return m_bytes[m_offset];
}
ReadonlyBytes bytes() const { return m_bytes; }
size_t offset() const { return m_offset; }
size_t remaining() const { return m_bytes.size() - m_offset; }
private:
ReadonlyBytes m_bytes;
size_t m_offset { 0 };
};
class DeprecatedOutputMemoryStream final : public DeprecatedOutputStream {
public:
explicit DeprecatedOutputMemoryStream(Bytes bytes)
: m_bytes(bytes)
{
}
size_t write(ReadonlyBytes bytes) override
{
auto const nwritten = bytes.copy_trimmed_to(m_bytes.slice(m_offset));
m_offset += nwritten;
return nwritten;
}
bool write_or_error(ReadonlyBytes bytes) override
{
if (remaining() < bytes.size()) {
set_recoverable_error();
return false;
}
write(bytes);
return true;
}
size_t fill_to_end(u8 value)
{
auto const nwritten = m_bytes.slice(m_offset).fill(value);
m_offset += nwritten;
return nwritten;
}
bool is_end() const { return remaining() == 0; }
ReadonlyBytes bytes() const { return { data(), size() }; }
Bytes bytes() { return { data(), size() }; }
u8 const* data() const { return m_bytes.data(); }
u8* data() { return m_bytes.data(); }
size_t size() const { return m_offset; }
size_t remaining() const { return m_bytes.size() - m_offset; }
void reset() { m_offset = 0; }
private:
size_t m_offset { 0 };
Bytes m_bytes;
};
}
#if USING_AK_GLOBALLY
using AK::DeprecatedInputMemoryStream;
using AK::DeprecatedOutputMemoryStream;
#endif

View file

@ -1,167 +0,0 @@
/*
* Copyright (c) 2020, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Concepts.h>
#include <AK/Endian.h>
#include <AK/Forward.h>
#include <AK/Optional.h>
#include <AK/Span.h>
#include <AK/StdLibExtras.h>
namespace AK::Detail {
class DeprecatedStream {
public:
virtual ~DeprecatedStream() { VERIFY(!has_any_error()); }
virtual bool has_recoverable_error() const { return m_recoverable_error; }
virtual bool has_fatal_error() const { return m_fatal_error; }
virtual bool has_any_error() const { return has_recoverable_error() || has_fatal_error(); }
virtual bool handle_recoverable_error()
{
VERIFY(!has_fatal_error());
return exchange(m_recoverable_error, false);
}
virtual bool handle_fatal_error() { return exchange(m_fatal_error, false); }
virtual bool handle_any_error()
{
if (has_any_error()) {
m_recoverable_error = false;
m_fatal_error = false;
return true;
}
return false;
}
ErrorOr<void> try_handle_any_error()
{
if (!handle_any_error())
return {};
return Error::from_string_literal("Stream error");
}
virtual void set_recoverable_error() const { m_recoverable_error = true; }
virtual void set_fatal_error() const { m_fatal_error = true; }
private:
mutable bool m_recoverable_error { false };
mutable bool m_fatal_error { false };
};
}
namespace AK {
class DeprecatedInputStream : public virtual Detail::DeprecatedStream {
public:
// Reads at least one byte unless none are requested or none are available. Does nothing
// and returns zero if there is already an error.
virtual size_t read(Bytes) = 0;
// If this function returns true, then no more data can be read. If read(Bytes) previously
// returned zero even though bytes were requested, then the inverse is true as well.
virtual bool unreliable_eof() const = 0;
// Some streams additionally define a method with the signature:
//
// bool eof() const;
//
// This method has the same semantics as unreliable_eof() but returns true if and only if no
// more data can be read. (A failed read is not necessary.)
virtual bool read_or_error(Bytes) = 0;
virtual bool discard_or_error(size_t count) = 0;
};
class DeprecatedOutputStream : public virtual Detail::DeprecatedStream {
public:
virtual size_t write(ReadonlyBytes) = 0;
virtual bool write_or_error(ReadonlyBytes) = 0;
};
class DeprecatedDuplexStream
: public DeprecatedInputStream
, public DeprecatedOutputStream {
};
inline DeprecatedInputStream& operator>>(DeprecatedInputStream& stream, Bytes bytes)
{
stream.read_or_error(bytes);
return stream;
}
inline DeprecatedOutputStream& operator<<(DeprecatedOutputStream& stream, ReadonlyBytes bytes)
{
stream.write_or_error(bytes);
return stream;
}
template<typename T>
DeprecatedInputStream& operator>>(DeprecatedInputStream& stream, LittleEndian<T>& value)
{
return stream >> Bytes { &value.m_value, sizeof(value.m_value) };
}
template<typename T>
DeprecatedOutputStream& operator<<(DeprecatedOutputStream& stream, LittleEndian<T> value)
{
return stream << ReadonlyBytes { &value.m_value, sizeof(value.m_value) };
}
template<typename T>
DeprecatedInputStream& operator>>(DeprecatedInputStream& stream, BigEndian<T>& value)
{
return stream >> Bytes { &value.m_value, sizeof(value.m_value) };
}
template<typename T>
DeprecatedOutputStream& operator<<(DeprecatedOutputStream& stream, BigEndian<T> value)
{
return stream << ReadonlyBytes { &value.m_value, sizeof(value.m_value) };
}
template<typename T>
DeprecatedInputStream& operator>>(DeprecatedInputStream& stream, Optional<T>& value)
{
T temporary;
stream >> temporary;
value = temporary;
return stream;
}
template<Integral I>
DeprecatedInputStream& operator>>(DeprecatedInputStream& stream, I& value)
{
stream.read_or_error({ &value, sizeof(value) });
return stream;
}
template<Integral I>
DeprecatedOutputStream& operator<<(DeprecatedOutputStream& stream, I value)
{
stream.write_or_error({ &value, sizeof(value) });
return stream;
}
#ifndef KERNEL
template<FloatingPoint F>
DeprecatedInputStream& operator>>(DeprecatedInputStream& stream, F& value)
{
stream.read_or_error({ &value, sizeof(value) });
return stream;
}
template<FloatingPoint F>
DeprecatedOutputStream& operator<<(DeprecatedOutputStream& stream, F value)
{
stream.write_or_error({ &value, sizeof(value) });
return stream;
}
#endif
}

View file

@ -6,7 +6,6 @@
#include <AK/ByteBuffer.h>
#include <AK/DeprecatedFlyString.h>
#include <AK/DeprecatedStream.h>
#include <AK/DeprecatedString.h>
#include <AK/Format.h>
#include <AK/Function.h>
@ -416,29 +415,6 @@ bool DeprecatedString::operator==(char const* cstring) const
return view() == cstring;
}
DeprecatedInputStream& operator>>(DeprecatedInputStream& stream, DeprecatedString& string)
{
StringBuilder builder;
for (;;) {
char next_char;
stream >> next_char;
if (stream.has_any_error()) {
stream.set_fatal_error();
string = nullptr;
return stream;
}
if (next_char) {
builder.append(next_char);
} else {
string = builder.to_deprecated_string();
return stream;
}
}
}
DeprecatedString DeprecatedString::vformatted(StringView fmtstr, TypeErasedFormatParams& params)
{
StringBuilder builder;

View file

@ -338,8 +338,6 @@ struct CaseInsensitiveStringTraits : public Traits<DeprecatedString> {
DeprecatedString escape_html_entities(StringView html);
DeprecatedInputStream& operator>>(DeprecatedInputStream& stream, DeprecatedString& string);
}
#if USING_AK_GLOBALLY

View file

@ -76,21 +76,9 @@ ALWAYS_INLINE T convert_between_host_and_network_endian(T value)
return convert_between_host_and_big_endian(value);
}
template<typename T>
class LittleEndian;
template<typename T>
DeprecatedInputStream& operator>>(DeprecatedInputStream&, LittleEndian<T>&);
template<typename T>
DeprecatedOutputStream& operator<<(DeprecatedOutputStream&, LittleEndian<T>);
template<typename T>
class [[gnu::packed]] LittleEndian {
public:
friend DeprecatedInputStream& operator>><T>(DeprecatedInputStream&, LittleEndian<T>&);
friend DeprecatedOutputStream& operator<< <T>(DeprecatedOutputStream&, LittleEndian<T>);
constexpr LittleEndian() = default;
constexpr LittleEndian(T value)
@ -108,21 +96,9 @@ private:
T m_value { 0 };
};
template<typename T>
class BigEndian;
template<typename T>
DeprecatedInputStream& operator>>(DeprecatedInputStream&, BigEndian<T>&);
template<typename T>
DeprecatedOutputStream& operator<<(DeprecatedOutputStream&, BigEndian<T>);
template<typename T>
class [[gnu::packed]] BigEndian {
public:
friend DeprecatedInputStream& operator>><T>(DeprecatedInputStream&, BigEndian<T>&);
friend DeprecatedOutputStream& operator<< <T>(DeprecatedOutputStream&, BigEndian<T>);
constexpr BigEndian() = default;
constexpr BigEndian(T value)

View file

@ -22,10 +22,6 @@ class BigEndianInputBitStream;
class BigEndianOutputBitStream;
using ByteBuffer = Detail::ByteBuffer<32>;
class CircularBuffer;
class DeprecatedInputStream;
class DeprecatedInputMemoryStream;
class DeprecatedOutputStream;
class DeprecatedOutputMemoryStream;
class Error;
class FlyString;
class GenericLexer;
@ -167,10 +163,6 @@ using AK::Bytes;
using AK::CircularBuffer;
using AK::CircularQueue;
using AK::DeprecatedFlyString;
using AK::DeprecatedInputMemoryStream;
using AK::DeprecatedInputStream;
using AK::DeprecatedOutputMemoryStream;
using AK::DeprecatedOutputStream;
using AK::DeprecatedString;
using AK::DeprecatedStringCodePointIterator;
using AK::DoublyLinkedList;

View file

@ -19,7 +19,6 @@ set(AK_TEST_SOURCES
TestCircularDeque.cpp
TestCircularQueue.cpp
TestComplex.cpp
TestDeprecatedMemoryStream.cpp
TestDeprecatedString.cpp
TestDisjointChunks.cpp
TestDistinctNumeric.cpp

View file

@ -1,137 +0,0 @@
/*
* Copyright (c) 2020, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibTest/TestCase.h>
#include <AK/Array.h>
#include <AK/DeprecatedMemoryStream.h>
TEST_CASE(read_an_integer)
{
u32 expected = 0x01020304, actual;
DeprecatedInputMemoryStream stream { { &expected, sizeof(expected) } };
stream >> actual;
EXPECT(!stream.has_any_error() && stream.eof());
EXPECT_EQ(expected, actual);
}
TEST_CASE(read_a_bool)
{
bool expected = true, actual;
DeprecatedInputMemoryStream stream { { &expected, sizeof(expected) } };
stream >> actual;
EXPECT(!stream.has_any_error() && stream.eof());
EXPECT_EQ(expected, actual);
}
TEST_CASE(read_a_double)
{
double expected = 3.141592653589793, actual;
DeprecatedInputMemoryStream stream { { &expected, sizeof(expected) } };
stream >> actual;
EXPECT(!stream.has_any_error() && stream.eof());
EXPECT_EQ(expected, actual);
}
TEST_CASE(recoverable_error)
{
u32 expected = 0x01020304, actual = 0;
u64 to_large_value = 0;
DeprecatedInputMemoryStream stream { { &expected, sizeof(expected) } };
EXPECT(!stream.has_any_error() && !stream.eof());
stream >> to_large_value;
EXPECT(stream.has_recoverable_error() && !stream.eof());
EXPECT(stream.handle_recoverable_error());
EXPECT(!stream.has_any_error() && !stream.eof());
stream >> actual;
EXPECT(!stream.has_any_error() && stream.eof());
EXPECT_EQ(expected, actual);
}
TEST_CASE(chain_stream_operator)
{
Array<u8, 4> const expected { 0, 1, 2, 3 };
Array<u8, 4> actual;
DeprecatedInputMemoryStream stream { expected };
stream >> actual[0] >> actual[1] >> actual[2] >> actual[3];
EXPECT(!stream.has_any_error() && stream.eof());
EXPECT_EQ(expected, actual);
}
TEST_CASE(seeking_slicing_offset)
{
Array<u8, 8> const input { 0, 1, 2, 3, 4, 5, 6, 7 };
Array<u8, 4> const expected0 { 0, 1, 2, 3 };
Array<u8, 4> const expected1 { 4, 5, 6, 7 };
Array<u8, 4> const expected2 { 1, 2, 3, 4 };
Array<u8, 4> actual0 {}, actual1 {}, actual2 {};
DeprecatedInputMemoryStream stream { input };
stream >> actual0;
EXPECT(!stream.has_any_error() && !stream.eof());
EXPECT_EQ(expected0, actual0);
stream.seek(4);
stream >> actual1;
EXPECT(!stream.has_any_error() && stream.eof());
EXPECT_EQ(expected1, actual1);
stream.seek(1);
stream >> actual2;
EXPECT(!stream.has_any_error() && !stream.eof());
EXPECT_EQ(expected2, actual2);
}
TEST_CASE(read_endian_values)
{
Array<u8, 8> const input { 0, 1, 2, 3, 4, 5, 6, 7 };
DeprecatedInputMemoryStream stream { input };
LittleEndian<u32> value1;
BigEndian<u32> value2;
stream >> value1 >> value2;
EXPECT_EQ(value1, 0x03020100u);
EXPECT_EQ(value2, 0x04050607u);
}
TEST_CASE(new_output_memory_stream)
{
Array<u8, 16> buffer;
DeprecatedOutputMemoryStream stream { buffer };
EXPECT_EQ(stream.size(), 0u);
EXPECT_EQ(stream.remaining(), 16u);
stream << LittleEndian<u16>(0x12'87);
EXPECT_EQ(stream.size(), 2u);
EXPECT_EQ(stream.remaining(), 14u);
stream << buffer;
EXPECT(stream.handle_recoverable_error());
EXPECT_EQ(stream.size(), 2u);
EXPECT_EQ(stream.remaining(), 14u);
EXPECT_EQ(stream.bytes().data(), buffer.data());
EXPECT_EQ(stream.bytes().size(), 2u);
}

View file

@ -589,155 +589,4 @@ ErrorOr<int> LocalSocket::release_fd()
return fd;
}
WrappedAKInputStream::WrappedAKInputStream(NonnullOwnPtr<DeprecatedInputStream> stream)
: m_stream(move(stream))
{
}
ErrorOr<Bytes> WrappedAKInputStream::read(Bytes bytes)
{
auto bytes_read = m_stream->read(bytes);
if (m_stream->has_any_error())
return Error::from_string_literal("Underlying InputStream indicated an error");
return bytes.slice(0, bytes_read);
}
ErrorOr<void> WrappedAKInputStream::discard(size_t discarded_bytes)
{
if (!m_stream->discard_or_error(discarded_bytes))
return Error::from_string_literal("Underlying InputStream indicated an error");
return {};
}
ErrorOr<size_t> WrappedAKInputStream::write(ReadonlyBytes)
{
return Error::from_errno(EBADF);
}
bool WrappedAKInputStream::is_eof() const
{
return m_stream->unreliable_eof();
}
bool WrappedAKInputStream::is_open() const
{
return true;
}
void WrappedAKInputStream::close()
{
}
WrappedAKOutputStream::WrappedAKOutputStream(NonnullOwnPtr<DeprecatedOutputStream> stream)
: m_stream(move(stream))
{
}
ErrorOr<Bytes> WrappedAKOutputStream::read(Bytes)
{
return Error::from_errno(EBADF);
}
ErrorOr<size_t> WrappedAKOutputStream::write(ReadonlyBytes bytes)
{
auto bytes_written = m_stream->write(bytes);
if (m_stream->has_any_error())
return Error::from_string_literal("Underlying OutputStream indicated an error");
return bytes_written;
}
bool WrappedAKOutputStream::is_eof() const
{
return true;
}
bool WrappedAKOutputStream::is_open() const
{
return true;
}
void WrappedAKOutputStream::close()
{
}
WrapInAKInputStream::WrapInAKInputStream(AK::Stream& stream)
: m_stream(stream)
{
}
size_t WrapInAKInputStream::read(Bytes bytes)
{
if (has_any_error())
return 0;
auto data_or_error = m_stream.read(bytes);
if (data_or_error.is_error()) {
set_fatal_error();
return 0;
}
return data_or_error.value().size();
}
bool WrapInAKInputStream::unreliable_eof() const
{
return m_stream.is_eof();
}
bool WrapInAKInputStream::read_or_error(Bytes bytes)
{
if (read(bytes) < bytes.size()) {
set_fatal_error();
return false;
}
return true;
}
bool WrapInAKInputStream::discard_or_error(size_t count)
{
auto maybe_error = m_stream.discard(count);
if (maybe_error.is_error()) {
set_fatal_error();
return false;
}
return true;
}
WrapInAKOutputStream::WrapInAKOutputStream(AK::Stream& stream)
: m_stream(stream)
{
}
size_t WrapInAKOutputStream::write(ReadonlyBytes bytes)
{
if (has_any_error())
return 0;
auto length_or_error = m_stream.write(bytes);
if (length_or_error.is_error()) {
set_fatal_error();
return 0;
}
return length_or_error.value();
}
bool WrapInAKOutputStream::write_or_error(ReadonlyBytes bytes)
{
if (write(bytes) < bytes.size()) {
set_fatal_error();
return false;
}
return true;
}
}

View file

@ -10,7 +10,6 @@
#include <AK/Badge.h>
#include <AK/BufferedStream.h>
#include <AK/CircularBuffer.h>
#include <AK/DeprecatedStream.h>
#include <AK/DeprecatedString.h>
#include <AK/EnumBits.h>
#include <AK/Function.h>
@ -608,57 +607,4 @@ private:
using ReusableTCPSocket = BasicReusableSocket<TCPSocket>;
using ReusableUDPSocket = BasicReusableSocket<UDPSocket>;
// Note: This is only a temporary hack, to break up the task of moving away from AK::Stream into smaller parts.
class WrappedAKInputStream final : public AK::Stream {
public:
WrappedAKInputStream(NonnullOwnPtr<DeprecatedInputStream> stream);
virtual ErrorOr<Bytes> read(Bytes) override;
virtual ErrorOr<void> discard(size_t discarded_bytes) override;
virtual ErrorOr<size_t> write(ReadonlyBytes) override;
virtual bool is_eof() const override;
virtual bool is_open() const override;
virtual void close() override;
private:
NonnullOwnPtr<DeprecatedInputStream> m_stream;
};
// Note: This is only a temporary hack, to break up the task of moving away from AK::Stream into smaller parts.
class WrappedAKOutputStream final : public AK::Stream {
public:
WrappedAKOutputStream(NonnullOwnPtr<DeprecatedOutputStream> stream);
virtual ErrorOr<Bytes> read(Bytes) override;
virtual ErrorOr<size_t> write(ReadonlyBytes) override;
virtual bool is_eof() const override;
virtual bool is_open() const override;
virtual void close() override;
private:
NonnullOwnPtr<DeprecatedOutputStream> m_stream;
};
// Note: This is only a temporary hack, to break up the task of moving away from AK::Stream into smaller parts.
class WrapInAKInputStream final : public DeprecatedInputStream {
public:
WrapInAKInputStream(AK::Stream& stream);
virtual size_t read(Bytes) override;
virtual bool unreliable_eof() const override;
virtual bool read_or_error(Bytes) override;
virtual bool discard_or_error(size_t count) override;
private:
AK::Stream& m_stream;
};
// Note: This is only a temporary hack, to break up the task of moving away from AK::Stream into smaller parts.
class WrapInAKOutputStream final : public DeprecatedOutputStream {
public:
WrapInAKOutputStream(AK::Stream& stream);
virtual size_t write(ReadonlyBytes) override;
virtual bool write_or_error(ReadonlyBytes) override;
private:
AK::Stream& m_stream;
};
}

View file

@ -7,7 +7,7 @@
#include <AK/Array.h>
#include <AK/Debug.h>
#include <AK/DeprecatedMemoryStream.h>
#include <AK/Endian.h>
#include <AK/Error.h>
#include <AK/IntegralMath.h>
#include <AK/Memory.h>