mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 00:50:22 +00:00
AK: Move Handle
from LibCore
and name it MaybeOwned
The new name should make it abundantly clear what it does.
This commit is contained in:
parent
5fa590de71
commit
5f2ea31816
Notes:
sideshowbarker
2024-07-17 04:49:48 +09:00
Author: https://github.com/timschumi Commit: https://github.com/SerenityOS/serenity/commit/5f2ea31816 Pull-request: https://github.com/SerenityOS/serenity/pull/17173 Reviewed-by: https://github.com/ADKaster ✅ Reviewed-by: https://github.com/alimpfard
16 changed files with 115 additions and 92 deletions
60
AK/MaybeOwned.h
Normal file
60
AK/MaybeOwned.h
Normal file
|
@ -0,0 +1,60 @@
|
|||
/*
|
||||
* Copyright (c) 2022, Tim Schumacher <timschumi@gmx.de>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/NonnullOwnPtr.h>
|
||||
#include <AK/Variant.h>
|
||||
|
||||
namespace AK {
|
||||
|
||||
template<typename T>
|
||||
class MaybeOwned {
|
||||
public:
|
||||
template<DerivedFrom<T> U>
|
||||
MaybeOwned(NonnullOwnPtr<U> handle)
|
||||
: m_handle(adopt_own<T>(*handle.leak_ptr()))
|
||||
{
|
||||
}
|
||||
|
||||
// This is made `explicit` to not accidentally create a non-owning MaybeOwned,
|
||||
// which may not always be intended.
|
||||
explicit MaybeOwned(T& handle)
|
||||
: m_handle(&handle)
|
||||
{
|
||||
}
|
||||
|
||||
T* ptr()
|
||||
{
|
||||
if (m_handle.template has<T*>())
|
||||
return m_handle.template get<T*>();
|
||||
else
|
||||
return m_handle.template get<NonnullOwnPtr<T>>();
|
||||
}
|
||||
|
||||
T const* ptr() const
|
||||
{
|
||||
if (m_handle.template has<T*>())
|
||||
return m_handle.template get<T*>();
|
||||
else
|
||||
return m_handle.template get<NonnullOwnPtr<T>>();
|
||||
}
|
||||
|
||||
T* operator->() { return ptr(); }
|
||||
T const* operator->() const { return ptr(); }
|
||||
|
||||
T& operator*() { return *ptr(); }
|
||||
T const& operator*() const { return *ptr(); }
|
||||
|
||||
private:
|
||||
Variant<NonnullOwnPtr<T>, T*> m_handle;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#if USING_AK_GLOBALLY
|
||||
using AK::MaybeOwned;
|
||||
#endif
|
|
@ -5,6 +5,7 @@
|
|||
*/
|
||||
|
||||
#include <AK/Format.h>
|
||||
#include <AK/MaybeOwned.h>
|
||||
#include <AK/String.h>
|
||||
#include <LibCore/BitStream.h>
|
||||
#include <LibCore/EventLoop.h>
|
||||
|
@ -726,8 +727,8 @@ TEST_CASE(little_endian_bit_stream_input_output_match)
|
|||
|
||||
// Note: The bit stream only ever reads from/writes to the underlying stream in one byte chunks,
|
||||
// so testing with sizes that will not trigger a write will yield unexpected results.
|
||||
auto bit_write_stream = MUST(Core::Stream::LittleEndianOutputBitStream::construct(Core::Stream::Handle<Core::Stream::Stream>(*memory_stream)));
|
||||
auto bit_read_stream = MUST(Core::Stream::LittleEndianInputBitStream::construct(Core::Stream::Handle<Core::Stream::Stream>(*memory_stream)));
|
||||
auto bit_write_stream = MUST(Core::Stream::LittleEndianOutputBitStream::construct(MaybeOwned<Core::Stream::Stream>(*memory_stream)));
|
||||
auto bit_read_stream = MUST(Core::Stream::LittleEndianInputBitStream::construct(MaybeOwned<Core::Stream::Stream>(*memory_stream)));
|
||||
|
||||
// Test two mirrored chunks of a fully mirrored pattern to check that we are not dropping bits.
|
||||
{
|
||||
|
@ -782,8 +783,8 @@ TEST_CASE(big_endian_bit_stream_input_output_match)
|
|||
|
||||
// Note: The bit stream only ever reads from/writes to the underlying stream in one byte chunks,
|
||||
// so testing with sizes that will not trigger a write will yield unexpected results.
|
||||
auto bit_write_stream = MUST(Core::Stream::BigEndianOutputBitStream::construct(Core::Stream::Handle<Core::Stream::Stream>(*memory_stream)));
|
||||
auto bit_read_stream = MUST(Core::Stream::BigEndianInputBitStream::construct(Core::Stream::Handle<Core::Stream::Stream>(*memory_stream)));
|
||||
auto bit_write_stream = MUST(Core::Stream::BigEndianOutputBitStream::construct(MaybeOwned<Core::Stream::Stream>(*memory_stream)));
|
||||
auto bit_read_stream = MUST(Core::Stream::BigEndianInputBitStream::construct(MaybeOwned<Core::Stream::Stream>(*memory_stream)));
|
||||
|
||||
// Test two mirrored chunks of a fully mirrored pattern to check that we are not dropping bits.
|
||||
{
|
||||
|
|
|
@ -136,7 +136,7 @@ TarFileStream TarInputStream::file_contents()
|
|||
return TarFileStream(*this);
|
||||
}
|
||||
|
||||
TarOutputStream::TarOutputStream(Core::Stream::Handle<Core::Stream::Stream> stream)
|
||||
TarOutputStream::TarOutputStream(MaybeOwned<Core::Stream::Stream> stream)
|
||||
: m_stream(move(stream))
|
||||
{
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <AK/MaybeOwned.h>
|
||||
#include <AK/Span.h>
|
||||
#include <LibArchive/Tar.h>
|
||||
#include <LibCore/Stream.h>
|
||||
|
@ -58,14 +59,14 @@ private:
|
|||
|
||||
class TarOutputStream {
|
||||
public:
|
||||
TarOutputStream(Core::Stream::Handle<Core::Stream::Stream>);
|
||||
TarOutputStream(MaybeOwned<Core::Stream::Stream>);
|
||||
ErrorOr<void> add_file(StringView path, mode_t, ReadonlyBytes);
|
||||
ErrorOr<void> add_link(StringView path, mode_t, StringView);
|
||||
ErrorOr<void> add_directory(StringView path, mode_t);
|
||||
ErrorOr<void> finish();
|
||||
|
||||
private:
|
||||
Core::Stream::Handle<Core::Stream::Stream> m_stream;
|
||||
MaybeOwned<Core::Stream::Stream> m_stream;
|
||||
bool m_finished { false };
|
||||
|
||||
friend class TarFileStream;
|
||||
|
|
|
@ -60,7 +60,7 @@ MaybeLoaderError FlacLoaderPlugin::initialize()
|
|||
// 11.5 STREAM
|
||||
MaybeLoaderError FlacLoaderPlugin::parse_header()
|
||||
{
|
||||
auto bit_input = LOADER_TRY(BigEndianInputBitStream::construct(Core::Stream::Handle<Core::Stream::Stream>(*m_stream)));
|
||||
auto bit_input = LOADER_TRY(BigEndianInputBitStream::construct(MaybeOwned<Core::Stream::Stream>(*m_stream)));
|
||||
|
||||
// A mixture of VERIFY and the non-crashing TRY().
|
||||
#define FLAC_VERIFY(check, category, msg) \
|
||||
|
@ -79,7 +79,7 @@ MaybeLoaderError FlacLoaderPlugin::parse_header()
|
|||
auto streaminfo = TRY(next_meta_block(*bit_input));
|
||||
FLAC_VERIFY(streaminfo.type == FlacMetadataBlockType::STREAMINFO, LoaderError::Category::Format, "First block must be STREAMINFO");
|
||||
auto streaminfo_data_memory = LOADER_TRY(Core::Stream::FixedMemoryStream::construct(streaminfo.data.bytes()));
|
||||
auto streaminfo_data = LOADER_TRY(BigEndianInputBitStream::construct(Core::Stream::Handle<Core::Stream::Stream>(*streaminfo_data_memory)));
|
||||
auto streaminfo_data = LOADER_TRY(BigEndianInputBitStream::construct(MaybeOwned<Core::Stream::Stream>(*streaminfo_data_memory)));
|
||||
|
||||
// 11.10 METADATA_BLOCK_STREAMINFO
|
||||
m_min_block_size = LOADER_TRY(streaminfo_data->read_bits<u16>(16));
|
||||
|
@ -150,7 +150,7 @@ MaybeLoaderError FlacLoaderPlugin::parse_header()
|
|||
MaybeLoaderError FlacLoaderPlugin::load_picture(FlacRawMetadataBlock& block)
|
||||
{
|
||||
auto memory_stream = LOADER_TRY(Core::Stream::FixedMemoryStream::construct(block.data.bytes()));
|
||||
auto picture_block_bytes = LOADER_TRY(BigEndianInputBitStream::construct(Core::Stream::Handle<Core::Stream::Stream>(*memory_stream)));
|
||||
auto picture_block_bytes = LOADER_TRY(BigEndianInputBitStream::construct(MaybeOwned<Core::Stream::Stream>(*memory_stream)));
|
||||
|
||||
PictureData picture {};
|
||||
|
||||
|
@ -187,7 +187,7 @@ MaybeLoaderError FlacLoaderPlugin::load_picture(FlacRawMetadataBlock& block)
|
|||
MaybeLoaderError FlacLoaderPlugin::load_seektable(FlacRawMetadataBlock& block)
|
||||
{
|
||||
auto memory_stream = LOADER_TRY(Core::Stream::FixedMemoryStream::construct(block.data.bytes()));
|
||||
auto seektable_bytes = LOADER_TRY(BigEndianInputBitStream::construct(Core::Stream::Handle<Core::Stream::Stream>(*memory_stream)));
|
||||
auto seektable_bytes = LOADER_TRY(BigEndianInputBitStream::construct(MaybeOwned<Core::Stream::Stream>(*memory_stream)));
|
||||
for (size_t i = 0; i < block.length / 18; ++i) {
|
||||
// 11.14. SEEKPOINT
|
||||
FlacSeekPoint seekpoint {
|
||||
|
@ -333,7 +333,7 @@ MaybeLoaderError FlacLoaderPlugin::next_frame(Span<Sample> target_vector)
|
|||
} \
|
||||
} while (0)
|
||||
|
||||
auto bit_stream = LOADER_TRY(BigEndianInputBitStream::construct(Core::Stream::Handle<Core::Stream::Stream>(*m_stream)));
|
||||
auto bit_stream = LOADER_TRY(BigEndianInputBitStream::construct(MaybeOwned<Core::Stream::Stream>(*m_stream)));
|
||||
|
||||
// TODO: Check the CRC-16 checksum (and others) by keeping track of read data
|
||||
|
||||
|
|
|
@ -41,7 +41,7 @@ Result<NonnullOwnPtr<MP3LoaderPlugin>, LoaderError> MP3LoaderPlugin::create(Byte
|
|||
|
||||
MaybeLoaderError MP3LoaderPlugin::initialize()
|
||||
{
|
||||
m_bitstream = LOADER_TRY(Core::Stream::BigEndianInputBitStream::construct(Core::Stream::Handle<Core::Stream::Stream>(*m_stream)));
|
||||
m_bitstream = LOADER_TRY(Core::Stream::BigEndianInputBitStream::construct(MaybeOwned<Core::Stream::Stream>(*m_stream)));
|
||||
|
||||
TRY(synchronize());
|
||||
|
||||
|
@ -242,7 +242,7 @@ ErrorOr<MP3::MP3Frame, LoaderError> MP3LoaderPlugin::read_frame_data(MP3::Header
|
|||
|
||||
TRY(m_bit_reservoir.discard(old_reservoir_size - frame.main_data_begin));
|
||||
|
||||
auto reservoir_stream = TRY(Core::Stream::BigEndianInputBitStream::construct(Core::Stream::Handle<Core::Stream::Stream>(m_bit_reservoir)));
|
||||
auto reservoir_stream = TRY(Core::Stream::BigEndianInputBitStream::construct(MaybeOwned<Core::Stream::Stream>(m_bit_reservoir)));
|
||||
|
||||
for (size_t granule_index = 0; granule_index < 2; granule_index++) {
|
||||
for (size_t channel_index = 0; channel_index < header.channel_count(); channel_index++) {
|
||||
|
|
|
@ -29,7 +29,7 @@ ErrorOr<size_t> BrotliDecompressionStream::CanonicalCode::read_symbol(LittleEndi
|
|||
}
|
||||
|
||||
BrotliDecompressionStream::BrotliDecompressionStream(Stream& stream)
|
||||
: m_input_stream(Core::Stream::Handle(stream))
|
||||
: m_input_stream(MaybeOwned(stream))
|
||||
{
|
||||
}
|
||||
|
||||
|
|
|
@ -188,13 +188,13 @@ ErrorOr<bool> DeflateDecompressor::UncompressedBlock::try_read_more()
|
|||
return true;
|
||||
}
|
||||
|
||||
ErrorOr<NonnullOwnPtr<DeflateDecompressor>> DeflateDecompressor::construct(Core::Stream::Handle<Core::Stream::Stream> stream)
|
||||
ErrorOr<NonnullOwnPtr<DeflateDecompressor>> DeflateDecompressor::construct(MaybeOwned<Core::Stream::Stream> stream)
|
||||
{
|
||||
auto output_buffer = TRY(CircularBuffer::create_empty(32 * KiB));
|
||||
return TRY(adopt_nonnull_own_or_enomem(new (nothrow) DeflateDecompressor(move(stream), move(output_buffer))));
|
||||
}
|
||||
|
||||
DeflateDecompressor::DeflateDecompressor(Core::Stream::Handle<Core::Stream::Stream> stream, CircularBuffer output_buffer)
|
||||
DeflateDecompressor::DeflateDecompressor(MaybeOwned<Core::Stream::Stream> stream, CircularBuffer output_buffer)
|
||||
: m_input_stream(make<Core::Stream::LittleEndianInputBitStream>(move(stream)))
|
||||
, m_output_buffer(move(output_buffer))
|
||||
{
|
||||
|
@ -446,7 +446,7 @@ ErrorOr<void> DeflateDecompressor::decode_codes(CanonicalCode& literal_code, Opt
|
|||
return {};
|
||||
}
|
||||
|
||||
ErrorOr<NonnullOwnPtr<DeflateCompressor>> DeflateCompressor::construct(Core::Stream::Handle<Core::Stream::Stream> stream, CompressionLevel compression_level)
|
||||
ErrorOr<NonnullOwnPtr<DeflateCompressor>> DeflateCompressor::construct(MaybeOwned<Core::Stream::Stream> stream, CompressionLevel compression_level)
|
||||
{
|
||||
auto bit_stream = TRY(Core::Stream::LittleEndianOutputBitStream::construct(move(stream)));
|
||||
auto deflate_compressor = TRY(adopt_nonnull_own_or_enomem(new (nothrow) DeflateCompressor(move(bit_stream), compression_level)));
|
||||
|
@ -1017,7 +1017,7 @@ ErrorOr<void> DeflateCompressor::final_flush()
|
|||
ErrorOr<ByteBuffer> DeflateCompressor::compress_all(ReadonlyBytes bytes, CompressionLevel compression_level)
|
||||
{
|
||||
auto output_stream = TRY(try_make<Core::Stream::AllocatingMemoryStream>());
|
||||
auto deflate_stream = TRY(DeflateCompressor::construct(Core::Stream::Handle<Core::Stream::Stream>(*output_stream), compression_level));
|
||||
auto deflate_stream = TRY(DeflateCompressor::construct(MaybeOwned<Core::Stream::Stream>(*output_stream), compression_level));
|
||||
|
||||
TRY(deflate_stream->write_entire_buffer(bytes));
|
||||
TRY(deflate_stream->final_flush());
|
||||
|
|
|
@ -74,7 +74,7 @@ public:
|
|||
friend CompressedBlock;
|
||||
friend UncompressedBlock;
|
||||
|
||||
static ErrorOr<NonnullOwnPtr<DeflateDecompressor>> construct(Core::Stream::Handle<Core::Stream::Stream> stream);
|
||||
static ErrorOr<NonnullOwnPtr<DeflateDecompressor>> construct(MaybeOwned<Core::Stream::Stream> stream);
|
||||
~DeflateDecompressor();
|
||||
|
||||
virtual ErrorOr<Bytes> read(Bytes) override;
|
||||
|
@ -86,7 +86,7 @@ public:
|
|||
static ErrorOr<ByteBuffer> decompress_all(ReadonlyBytes);
|
||||
|
||||
private:
|
||||
DeflateDecompressor(Core::Stream::Handle<Core::Stream::Stream> stream, CircularBuffer buffer);
|
||||
DeflateDecompressor(MaybeOwned<Core::Stream::Stream> stream, CircularBuffer buffer);
|
||||
|
||||
ErrorOr<u32> decode_length(u32);
|
||||
ErrorOr<u32> decode_distance(u32);
|
||||
|
@ -100,7 +100,7 @@ private:
|
|||
UncompressedBlock m_uncompressed_block;
|
||||
};
|
||||
|
||||
Core::Stream::Handle<Core::Stream::LittleEndianInputBitStream> m_input_stream;
|
||||
MaybeOwned<Core::Stream::LittleEndianInputBitStream> m_input_stream;
|
||||
CircularBuffer m_output_buffer;
|
||||
};
|
||||
|
||||
|
@ -139,7 +139,7 @@ public:
|
|||
BEST // WARNING: this one can take an unreasonable amount of time!
|
||||
};
|
||||
|
||||
static ErrorOr<NonnullOwnPtr<DeflateCompressor>> construct(Core::Stream::Handle<Core::Stream::Stream>, CompressionLevel = CompressionLevel::GOOD);
|
||||
static ErrorOr<NonnullOwnPtr<DeflateCompressor>> construct(MaybeOwned<Core::Stream::Stream>, CompressionLevel = CompressionLevel::GOOD);
|
||||
~DeflateCompressor();
|
||||
|
||||
virtual ErrorOr<Bytes> read(Bytes) override;
|
||||
|
|
|
@ -40,7 +40,7 @@ bool BlockHeader::supported_by_implementation() const
|
|||
|
||||
ErrorOr<NonnullOwnPtr<GzipDecompressor::Member>> GzipDecompressor::Member::construct(BlockHeader header, Core::Stream::Stream& stream)
|
||||
{
|
||||
auto deflate_stream = TRY(DeflateDecompressor::construct(Core::Stream::Handle<Core::Stream::Stream>(stream)));
|
||||
auto deflate_stream = TRY(DeflateDecompressor::construct(MaybeOwned<Core::Stream::Stream>(stream)));
|
||||
return TRY(adopt_nonnull_own_or_enomem(new (nothrow) Member(header, move(deflate_stream))));
|
||||
}
|
||||
|
||||
|
@ -186,7 +186,7 @@ ErrorOr<size_t> GzipDecompressor::write(ReadonlyBytes)
|
|||
return Error::from_errno(EBADF);
|
||||
}
|
||||
|
||||
GzipCompressor::GzipCompressor(Core::Stream::Handle<Core::Stream::Stream> stream)
|
||||
GzipCompressor::GzipCompressor(MaybeOwned<Core::Stream::Stream> stream)
|
||||
: m_output_stream(move(stream))
|
||||
{
|
||||
}
|
||||
|
@ -207,7 +207,7 @@ ErrorOr<size_t> GzipCompressor::write(ReadonlyBytes bytes)
|
|||
header.extra_flags = 3; // DEFLATE sets 2 for maximum compression and 4 for minimum compression
|
||||
header.operating_system = 3; // unix
|
||||
TRY(m_output_stream->write_entire_buffer({ &header, sizeof(header) }));
|
||||
auto compressed_stream = TRY(DeflateCompressor::construct(Core::Stream::Handle(*m_output_stream)));
|
||||
auto compressed_stream = TRY(DeflateCompressor::construct(MaybeOwned(*m_output_stream)));
|
||||
TRY(compressed_stream->write_entire_buffer(bytes));
|
||||
TRY(compressed_stream->final_flush());
|
||||
Crypto::Checksum::CRC32 crc32;
|
||||
|
@ -236,7 +236,7 @@ void GzipCompressor::close()
|
|||
ErrorOr<ByteBuffer> GzipCompressor::compress_all(ReadonlyBytes bytes)
|
||||
{
|
||||
auto output_stream = TRY(try_make<Core::Stream::AllocatingMemoryStream>());
|
||||
GzipCompressor gzip_stream { Core::Stream::Handle<Core::Stream::Stream>(*output_stream) };
|
||||
GzipCompressor gzip_stream { MaybeOwned<Core::Stream::Stream>(*output_stream) };
|
||||
|
||||
TRY(gzip_stream.write_entire_buffer(bytes));
|
||||
|
||||
|
|
|
@ -80,7 +80,7 @@ private:
|
|||
|
||||
class GzipCompressor final : public Core::Stream::Stream {
|
||||
public:
|
||||
GzipCompressor(Core::Stream::Handle<Core::Stream::Stream>);
|
||||
GzipCompressor(MaybeOwned<Core::Stream::Stream>);
|
||||
|
||||
virtual ErrorOr<Bytes> read(Bytes) override;
|
||||
virtual ErrorOr<size_t> write(ReadonlyBytes) override;
|
||||
|
@ -91,7 +91,7 @@ public:
|
|||
static ErrorOr<ByteBuffer> compress_all(ReadonlyBytes bytes);
|
||||
|
||||
private:
|
||||
Core::Stream::Handle<Core::Stream::Stream> m_output_stream;
|
||||
MaybeOwned<Core::Stream::Stream> m_output_stream;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -69,13 +69,13 @@ u32 ZlibDecompressor::checksum()
|
|||
return m_checksum;
|
||||
}
|
||||
|
||||
ErrorOr<NonnullOwnPtr<ZlibCompressor>> ZlibCompressor::construct(Core::Stream::Handle<Core::Stream::Stream> stream, ZlibCompressionLevel compression_level)
|
||||
ErrorOr<NonnullOwnPtr<ZlibCompressor>> ZlibCompressor::construct(MaybeOwned<Core::Stream::Stream> stream, ZlibCompressionLevel compression_level)
|
||||
{
|
||||
// Zlib only defines Deflate as a compression method.
|
||||
auto compression_method = ZlibCompressionMethod::Deflate;
|
||||
|
||||
// FIXME: Find a way to compress with Deflate's "Best" compression level.
|
||||
auto compressor_stream = TRY(DeflateCompressor::construct(Core::Stream::Handle(*stream), static_cast<DeflateCompressor::CompressionLevel>(compression_level)));
|
||||
auto compressor_stream = TRY(DeflateCompressor::construct(MaybeOwned(*stream), static_cast<DeflateCompressor::CompressionLevel>(compression_level)));
|
||||
|
||||
auto zlib_compressor = TRY(adopt_nonnull_own_or_enomem(new (nothrow) ZlibCompressor(move(stream), move(compressor_stream))));
|
||||
TRY(zlib_compressor->write_header(compression_method, compression_level));
|
||||
|
@ -83,7 +83,7 @@ ErrorOr<NonnullOwnPtr<ZlibCompressor>> ZlibCompressor::construct(Core::Stream::H
|
|||
return zlib_compressor;
|
||||
}
|
||||
|
||||
ZlibCompressor::ZlibCompressor(Core::Stream::Handle<Core::Stream::Stream> stream, NonnullOwnPtr<Core::Stream::Stream> compressor_stream)
|
||||
ZlibCompressor::ZlibCompressor(MaybeOwned<Core::Stream::Stream> stream, NonnullOwnPtr<Core::Stream::Stream> compressor_stream)
|
||||
: m_output_stream(move(stream))
|
||||
, m_compressor(move(compressor_stream))
|
||||
{
|
||||
|
@ -164,7 +164,7 @@ ErrorOr<void> ZlibCompressor::finish()
|
|||
ErrorOr<ByteBuffer> ZlibCompressor::compress_all(ReadonlyBytes bytes, ZlibCompressionLevel compression_level)
|
||||
{
|
||||
auto output_stream = TRY(try_make<Core::Stream::AllocatingMemoryStream>());
|
||||
auto zlib_stream = TRY(ZlibCompressor::construct(Core::Stream::Handle<Core::Stream::Stream>(*output_stream), compression_level));
|
||||
auto zlib_stream = TRY(ZlibCompressor::construct(MaybeOwned<Core::Stream::Stream>(*output_stream), compression_level));
|
||||
|
||||
TRY(zlib_stream->write_entire_buffer(bytes));
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/ByteBuffer.h>
|
||||
#include <AK/MaybeOwned.h>
|
||||
#include <AK/Optional.h>
|
||||
#include <AK/OwnPtr.h>
|
||||
#include <AK/Span.h>
|
||||
|
@ -62,7 +63,7 @@ private:
|
|||
|
||||
class ZlibCompressor : public Core::Stream::Stream {
|
||||
public:
|
||||
static ErrorOr<NonnullOwnPtr<ZlibCompressor>> construct(Core::Stream::Handle<Core::Stream::Stream>, ZlibCompressionLevel = ZlibCompressionLevel::Default);
|
||||
static ErrorOr<NonnullOwnPtr<ZlibCompressor>> construct(MaybeOwned<Core::Stream::Stream>, ZlibCompressionLevel = ZlibCompressionLevel::Default);
|
||||
~ZlibCompressor();
|
||||
|
||||
virtual ErrorOr<Bytes> read(Bytes) override;
|
||||
|
@ -75,11 +76,11 @@ public:
|
|||
static ErrorOr<ByteBuffer> compress_all(ReadonlyBytes bytes, ZlibCompressionLevel = ZlibCompressionLevel::Default);
|
||||
|
||||
private:
|
||||
ZlibCompressor(Core::Stream::Handle<Core::Stream::Stream> stream, NonnullOwnPtr<Core::Stream::Stream> compressor_stream);
|
||||
ZlibCompressor(MaybeOwned<Core::Stream::Stream> stream, NonnullOwnPtr<Core::Stream::Stream> compressor_stream);
|
||||
ErrorOr<void> write_header(ZlibCompressionMethod, ZlibCompressionLevel);
|
||||
|
||||
bool m_finished { false };
|
||||
Core::Stream::Handle<Core::Stream::Stream> m_output_stream;
|
||||
MaybeOwned<Core::Stream::Stream> m_output_stream;
|
||||
NonnullOwnPtr<Core::Stream::Stream> m_compressor;
|
||||
Crypto::Checksum::Adler32 m_adler32_checksum;
|
||||
};
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
#include <AK/ByteBuffer.h>
|
||||
#include <AK/Concepts.h>
|
||||
#include <AK/Error.h>
|
||||
#include <AK/MaybeOwned.h>
|
||||
#include <AK/NonnullOwnPtr.h>
|
||||
#include <AK/NonnullRefPtr.h>
|
||||
#include <AK/OwnPtr.h>
|
||||
|
@ -23,7 +24,7 @@ namespace Core::Stream {
|
|||
/// in big-endian order from another stream.
|
||||
class BigEndianInputBitStream : public Stream {
|
||||
public:
|
||||
static ErrorOr<NonnullOwnPtr<BigEndianInputBitStream>> construct(Handle<Stream> stream)
|
||||
static ErrorOr<NonnullOwnPtr<BigEndianInputBitStream>> construct(MaybeOwned<Stream> stream)
|
||||
{
|
||||
return adopt_nonnull_own_or_enomem<BigEndianInputBitStream>(new BigEndianInputBitStream(move(stream)));
|
||||
}
|
||||
|
@ -118,26 +119,26 @@ public:
|
|||
ALWAYS_INLINE bool is_aligned_to_byte_boundary() const { return m_bit_offset == 0; }
|
||||
|
||||
private:
|
||||
BigEndianInputBitStream(Handle<Stream> stream)
|
||||
BigEndianInputBitStream(MaybeOwned<Stream> stream)
|
||||
: m_stream(move(stream))
|
||||
{
|
||||
}
|
||||
|
||||
Optional<u8> m_current_byte;
|
||||
size_t m_bit_offset { 0 };
|
||||
Handle<Stream> m_stream;
|
||||
MaybeOwned<Stream> m_stream;
|
||||
};
|
||||
|
||||
/// A stream wrapper class that allows you to read arbitrary amounts of bits
|
||||
/// in little-endian order from another stream.
|
||||
class LittleEndianInputBitStream : public Stream {
|
||||
public:
|
||||
static ErrorOr<NonnullOwnPtr<LittleEndianInputBitStream>> construct(Handle<Stream> stream)
|
||||
static ErrorOr<NonnullOwnPtr<LittleEndianInputBitStream>> construct(MaybeOwned<Stream> stream)
|
||||
{
|
||||
return adopt_nonnull_own_or_enomem<LittleEndianInputBitStream>(new LittleEndianInputBitStream(move(stream)));
|
||||
}
|
||||
|
||||
LittleEndianInputBitStream(Handle<Stream> stream)
|
||||
LittleEndianInputBitStream(MaybeOwned<Stream> stream)
|
||||
: m_stream(move(stream))
|
||||
{
|
||||
}
|
||||
|
@ -234,14 +235,14 @@ public:
|
|||
private:
|
||||
Optional<u8> m_current_byte;
|
||||
size_t m_bit_offset { 0 };
|
||||
Handle<Stream> m_stream;
|
||||
MaybeOwned<Stream> m_stream;
|
||||
};
|
||||
|
||||
/// A stream wrapper class that allows you to write arbitrary amounts of bits
|
||||
/// in big-endian order to another stream.
|
||||
class BigEndianOutputBitStream : public Stream {
|
||||
public:
|
||||
static ErrorOr<NonnullOwnPtr<BigEndianOutputBitStream>> construct(Handle<Stream> stream)
|
||||
static ErrorOr<NonnullOwnPtr<BigEndianOutputBitStream>> construct(MaybeOwned<Stream> stream)
|
||||
{
|
||||
return adopt_nonnull_own_or_enomem<BigEndianOutputBitStream>(new BigEndianOutputBitStream(move(stream)));
|
||||
}
|
||||
|
@ -310,12 +311,12 @@ public:
|
|||
}
|
||||
|
||||
private:
|
||||
BigEndianOutputBitStream(Handle<Stream> stream)
|
||||
BigEndianOutputBitStream(MaybeOwned<Stream> stream)
|
||||
: m_stream(move(stream))
|
||||
{
|
||||
}
|
||||
|
||||
Handle<Stream> m_stream;
|
||||
MaybeOwned<Stream> m_stream;
|
||||
u8 m_current_byte { 0 };
|
||||
size_t m_bit_offset { 0 };
|
||||
};
|
||||
|
@ -324,7 +325,7 @@ private:
|
|||
/// in little-endian order to another stream.
|
||||
class LittleEndianOutputBitStream : public Stream {
|
||||
public:
|
||||
static ErrorOr<NonnullOwnPtr<LittleEndianOutputBitStream>> construct(Handle<Stream> stream)
|
||||
static ErrorOr<NonnullOwnPtr<LittleEndianOutputBitStream>> construct(MaybeOwned<Stream> stream)
|
||||
{
|
||||
return adopt_nonnull_own_or_enomem<LittleEndianOutputBitStream>(new LittleEndianOutputBitStream(move(stream)));
|
||||
}
|
||||
|
@ -393,12 +394,12 @@ public:
|
|||
}
|
||||
|
||||
private:
|
||||
LittleEndianOutputBitStream(Handle<Stream> stream)
|
||||
LittleEndianOutputBitStream(MaybeOwned<Stream> stream)
|
||||
: m_stream(move(stream))
|
||||
{
|
||||
}
|
||||
|
||||
Handle<Stream> m_stream;
|
||||
MaybeOwned<Stream> m_stream;
|
||||
u8 m_current_byte { 0 };
|
||||
size_t m_bit_offset { 0 };
|
||||
};
|
||||
|
|
|
@ -27,48 +27,6 @@
|
|||
|
||||
namespace Core::Stream {
|
||||
|
||||
template<DerivedFrom<Core::Stream::Stream> T>
|
||||
class Handle {
|
||||
public:
|
||||
template<DerivedFrom<T> U>
|
||||
Handle(NonnullOwnPtr<U> handle)
|
||||
: m_handle(adopt_own<T>(*handle.leak_ptr()))
|
||||
{
|
||||
}
|
||||
|
||||
// This is made `explicit` to not accidentally create a non-owning Handle,
|
||||
// which may not always be intended.
|
||||
explicit Handle(T& handle)
|
||||
: m_handle(&handle)
|
||||
{
|
||||
}
|
||||
|
||||
T* ptr()
|
||||
{
|
||||
if (m_handle.template has<T*>())
|
||||
return m_handle.template get<T*>();
|
||||
else
|
||||
return m_handle.template get<NonnullOwnPtr<T>>();
|
||||
}
|
||||
|
||||
T const* ptr() const
|
||||
{
|
||||
if (m_handle.template has<T*>())
|
||||
return m_handle.template get<T*>();
|
||||
else
|
||||
return m_handle.template get<NonnullOwnPtr<T>>();
|
||||
}
|
||||
|
||||
T* operator->() { return ptr(); }
|
||||
T const* operator->() const { return ptr(); }
|
||||
|
||||
T& operator*() { return *ptr(); }
|
||||
T const& operator*() const { return *ptr(); }
|
||||
|
||||
private:
|
||||
Variant<NonnullOwnPtr<T>, T*> m_handle;
|
||||
};
|
||||
|
||||
/// The base, abstract class for stream operations. This class defines the
|
||||
/// operations one can perform on every stream in LibCore.
|
||||
/// Operations without a sensible default that are unsupported by an implementation
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
#include <AK/Base64.h>
|
||||
#include <AK/GenericLexer.h>
|
||||
#include <AK/LexicalPath.h>
|
||||
#include <AK/MaybeOwned.h>
|
||||
#include <AK/NumberFormat.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/URL.h>
|
||||
|
@ -105,7 +106,7 @@ private:
|
|||
template<typename ConditionT>
|
||||
class ConditionalOutputStream final : public Core::Stream::Stream {
|
||||
public:
|
||||
ConditionalOutputStream(ConditionT&& condition, Core::Stream::Handle<Core::Stream::Stream> stream)
|
||||
ConditionalOutputStream(ConditionT&& condition, MaybeOwned<Core::Stream::Stream> stream)
|
||||
: m_stream(move(stream))
|
||||
, m_condition(condition)
|
||||
{
|
||||
|
@ -140,7 +141,7 @@ public:
|
|||
}
|
||||
|
||||
private:
|
||||
Core::Stream::Handle<Core::Stream::Stream> m_stream;
|
||||
MaybeOwned<Core::Stream::Stream> m_stream;
|
||||
ConditionT m_condition;
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue