1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 15:17:46 +00:00

LibIPC+Everywhere: Change IPC decoders to construct values in-place

Currently, the generated IPC decoders will default-construct the type to
be decoded, then pass that value by reference to the concrete decoder.
This, of course, requires that the type is default-constructible. This
was an issue for decoding Variants, which had to require the first type
in the Variant list is Empty, to ensure it is default constructible.

Further, this made it possible for values to become uninitialized in
user-defined decoders.

This patch makes the decoder interface such that the concrete decoders
themselves contruct the decoded type upon return from the decoder. To do
so, the default decoders in IPC::Decoder had to be moved to the IPC
namespace scope, as these decoders are now specializations instead of
overloaded methods (C++ requires specializations to be in a namespace
scope).
This commit is contained in:
Timothy Flynn 2022-12-22 20:40:33 -05:00 committed by Andreas Kling
parent 765c5b416f
commit 9b483625e6
31 changed files with 437 additions and 519 deletions

View file

@ -374,12 +374,10 @@ bool IPC::encode(Encoder& encoder, Color const& color)
}
template<>
ErrorOr<void> IPC::decode(Decoder& decoder, Color& color)
ErrorOr<Gfx::Color> IPC::decode(Decoder& decoder)
{
u32 rgba;
TRY(decoder.decode(rgba));
color = Color::from_argb(rgba);
return {};
auto rgba = TRY(decoder.decode<u32>());
return Gfx::Color::from_argb(rgba);
}
ErrorOr<void> AK::Formatter<Gfx::Color>::format(FormatBuilder& builder, Gfx::Color value)

View file

@ -577,6 +577,6 @@ template<>
bool encode(Encoder&, Gfx::Color const&);
template<>
ErrorOr<void> decode(Decoder&, Gfx::Color&);
ErrorOr<Gfx::Color> decode(Decoder&);
}

View file

@ -59,14 +59,11 @@ bool encode(Encoder& encoder, Gfx::IntPoint const& point)
}
template<>
ErrorOr<void> decode(Decoder& decoder, Gfx::IntPoint& point)
ErrorOr<Gfx::IntPoint> decode(Decoder& decoder)
{
int x = 0;
int y = 0;
TRY(decoder.decode(x));
TRY(decoder.decode(y));
point = { x, y };
return {};
auto x = TRY(decoder.decode<int>());
auto y = TRY(decoder.decode<int>());
return Gfx::IntPoint { x, y };
}
}

View file

@ -295,7 +295,7 @@ template<>
bool encode(Encoder&, Gfx::IntPoint const&);
template<>
ErrorOr<void> decode(Decoder&, Gfx::IntPoint&);
ErrorOr<Gfx::IntPoint> decode(Decoder&);
}

View file

@ -38,14 +38,11 @@ bool encode(Encoder& encoder, Gfx::IntRect const& rect)
}
template<>
ErrorOr<void> decode(Decoder& decoder, Gfx::IntRect& rect)
ErrorOr<Gfx::IntRect> decode(Decoder& decoder)
{
Gfx::IntPoint point;
Gfx::IntSize size;
TRY(decoder.decode(point));
TRY(decoder.decode(size));
rect = { point, size };
return {};
auto point = TRY(decoder.decode<Gfx::IntPoint>());
auto size = TRY(decoder.decode<Gfx::IntSize>());
return Gfx::IntRect { point, size };
}
}

View file

@ -1036,6 +1036,6 @@ template<>
bool encode(Encoder&, Gfx::IntRect const&);
template<>
ErrorOr<void> decode(Decoder&, Gfx::IntRect&);
ErrorOr<Gfx::IntRect> decode(Decoder&);
}

View file

@ -32,7 +32,7 @@ bool encode(Encoder& encoder, Gfx::ShareableBitmap const& shareable_bitmap)
encoder << IPC::File(bitmap.anonymous_buffer().fd());
encoder << bitmap.size();
encoder << static_cast<u32>(bitmap.scale());
encoder << (u32)bitmap.format();
encoder << static_cast<u32>(bitmap.format());
if (bitmap.is_indexed()) {
auto palette = bitmap.palette_to_vector();
encoder << palette;
@ -41,33 +41,28 @@ bool encode(Encoder& encoder, Gfx::ShareableBitmap const& shareable_bitmap)
}
template<>
ErrorOr<void> decode(Decoder& decoder, Gfx::ShareableBitmap& shareable_bitmap)
ErrorOr<Gfx::ShareableBitmap> decode(Decoder& decoder)
{
bool valid = false;
TRY(decoder.decode(valid));
if (!valid) {
shareable_bitmap = {};
return {};
}
IPC::File anon_file;
TRY(decoder.decode(anon_file));
Gfx::IntSize size;
TRY(decoder.decode(size));
u32 scale;
TRY(decoder.decode(scale));
u32 raw_bitmap_format;
TRY(decoder.decode(raw_bitmap_format));
if (auto valid = TRY(decoder.decode<bool>()); !valid)
return Gfx::ShareableBitmap {};
auto anon_file = TRY(decoder.decode<IPC::File>());
auto size = TRY(decoder.decode<Gfx::IntSize>());
auto scale = TRY(decoder.decode<u32>());
auto raw_bitmap_format = TRY(decoder.decode<u32>());
if (!Gfx::is_valid_bitmap_format(raw_bitmap_format))
return Error::from_string_literal("IPC: Invalid Gfx::ShareableBitmap format");
auto bitmap_format = (Gfx::BitmapFormat)raw_bitmap_format;
auto bitmap_format = static_cast<Gfx::BitmapFormat>(raw_bitmap_format);
Vector<Gfx::ARGB32> palette;
if (Gfx::Bitmap::is_indexed(bitmap_format)) {
TRY(decoder.decode(palette));
}
if (Gfx::Bitmap::is_indexed(bitmap_format))
palette = TRY(decoder.decode<decltype(palette)>());
auto buffer = TRY(Core::AnonymousBuffer::create_from_anon_fd(anon_file.take_fd(), Gfx::Bitmap::size_in_bytes(Gfx::Bitmap::minimum_pitch(size.width() * scale, bitmap_format), size.height() * scale)));
auto bitmap = TRY(Gfx::Bitmap::try_create_with_anonymous_buffer(bitmap_format, move(buffer), size, scale, palette));
shareable_bitmap = Gfx::ShareableBitmap { move(bitmap), Gfx::ShareableBitmap::ConstructWithKnownGoodBitmap };
return {};
return Gfx::ShareableBitmap { move(bitmap), Gfx::ShareableBitmap::ConstructWithKnownGoodBitmap };
}
}

View file

@ -9,6 +9,7 @@
#include <AK/RefPtr.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/Size.h>
#include <LibIPC/Forward.h>
namespace Gfx {
@ -38,6 +39,6 @@ template<>
bool encode(Encoder&, Gfx::ShareableBitmap const&);
template<>
ErrorOr<void> decode(Decoder&, Gfx::ShareableBitmap&);
ErrorOr<Gfx::ShareableBitmap> decode(Decoder&);
}

View file

@ -35,14 +35,11 @@ bool encode(Encoder& encoder, Gfx::IntSize const& size)
}
template<>
ErrorOr<void> decode(Decoder& decoder, Gfx::IntSize& size)
ErrorOr<Gfx::IntSize> decode(Decoder& decoder)
{
int width = 0;
int height = 0;
TRY(decoder.decode(width));
TRY(decoder.decode(height));
size = { width, height };
return {};
auto width = TRY(decoder.decode<int>());
auto height = TRY(decoder.decode<int>());
return Gfx::IntSize { width, height };
}
}

View file

@ -218,6 +218,6 @@ template<>
bool encode(Encoder&, Gfx::IntSize const&);
template<>
ErrorOr<void> decode(Decoder&, Gfx::IntSize&);
ErrorOr<Gfx::IntSize> decode(Decoder&);
}