1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 06:37:35 +00:00

LibIPC+IPCCompiler+AK: Make IPC value decoders return ErrorOr<void>

This allows us to use TRY() in decoding helpers, leading to a nice
reduction in line count.
This commit is contained in:
Andreas Kling 2021-11-28 11:56:31 +01:00
parent 8d76eb773f
commit cb9cac4e40
21 changed files with 207 additions and 296 deletions

View file

@ -13,7 +13,6 @@
#include <LibIPC/Decoder.h>
#include <LibIPC/Encoder.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
namespace Gfx {
@ -343,13 +342,12 @@ bool IPC::encode(IPC::Encoder& encoder, Color const& color)
return true;
}
bool IPC::decode(IPC::Decoder& decoder, Color& color)
ErrorOr<void> IPC::decode(IPC::Decoder& decoder, Color& color)
{
u32 rgba = 0;
if (!decoder.decode(rgba))
return false;
u32 rgba;
TRY(decoder.decode(rgba));
color = Color::from_rgba(rgba);
return true;
return {};
}
ErrorOr<void> AK::Formatter<Gfx::Color>::format(FormatBuilder& builder, Gfx::Color const& value)

View file

@ -486,6 +486,6 @@ struct Formatter<Gfx::Color> : public Formatter<StringView> {
namespace IPC {
bool encode(Encoder&, Gfx::Color const&);
bool decode(Decoder&, Gfx::Color&);
ErrorOr<void> decode(Decoder&, Gfx::Color&);
}

View file

@ -57,16 +57,14 @@ bool encode(Encoder& encoder, Gfx::IntPoint const& point)
return true;
}
bool decode(Decoder& decoder, Gfx::IntPoint& point)
ErrorOr<void> decode(Decoder& decoder, Gfx::IntPoint& point)
{
int x = 0;
int y = 0;
if (!decoder.decode(x))
return false;
if (!decoder.decode(y))
return false;
TRY(decoder.decode(x));
TRY(decoder.decode(y));
point = { x, y };
return true;
return {};
}
}

View file

@ -291,7 +291,7 @@ struct Formatter<Gfx::Point<T>> : Formatter<StringView> {
namespace IPC {
bool encode(Encoder&, Gfx::IntPoint const&);
bool decode(Decoder&, Gfx::IntPoint&);
ErrorOr<void> decode(Decoder&, Gfx::IntPoint&);
}

View file

@ -328,16 +328,14 @@ bool encode(Encoder& encoder, Gfx::IntRect const& rect)
return true;
}
bool decode(Decoder& decoder, Gfx::IntRect& rect)
ErrorOr<void> decode(Decoder& decoder, Gfx::IntRect& rect)
{
Gfx::IntPoint point;
Gfx::IntSize size;
if (!decoder.decode(point))
return false;
if (!decoder.decode(size))
return false;
TRY(decoder.decode(point));
TRY(decoder.decode(size));
rect = { point, size };
return true;
return {};
}
}

View file

@ -736,7 +736,7 @@ struct Formatter<Gfx::Rect<T>> : Formatter<StringView> {
namespace IPC {
bool decode(Decoder&, Gfx::IntRect&);
bool encode(Encoder&, const Gfx::IntRect&);
ErrorOr<void> decode(Decoder&, Gfx::IntRect&);
}

View file

@ -39,43 +39,33 @@ bool encode(Encoder& encoder, const Gfx::ShareableBitmap& shareable_bitmap)
return true;
}
bool decode(Decoder& decoder, Gfx::ShareableBitmap& shareable_bitmap)
ErrorOr<void> decode(Decoder& decoder, Gfx::ShareableBitmap& shareable_bitmap)
{
bool valid = false;
if (!decoder.decode(valid))
return false;
TRY(decoder.decode(valid));
if (!valid) {
shareable_bitmap = {};
return true;
return {};
}
IPC::File anon_file;
if (!decoder.decode(anon_file))
return false;
TRY(decoder.decode(anon_file));
Gfx::IntSize size;
if (!decoder.decode(size))
return false;
TRY(decoder.decode(size));
u32 scale;
if (!decoder.decode(scale))
return false;
TRY(decoder.decode(scale));
u32 raw_bitmap_format;
if (!decoder.decode(raw_bitmap_format))
return false;
TRY(decoder.decode(raw_bitmap_format));
if (!Gfx::is_valid_bitmap_format(raw_bitmap_format))
return false;
return Error::from_string_literal("IPC: Invalid Gfx::ShareableBitmap format"sv);
auto bitmap_format = (Gfx::BitmapFormat)raw_bitmap_format;
Vector<Gfx::RGBA32> palette;
if (Gfx::Bitmap::is_indexed(bitmap_format)) {
if (!decoder.decode(palette))
return false;
TRY(decoder.decode(palette));
}
auto buffer_or_error = Core::AnonymousBuffer::create_from_anon_fd(anon_file.take_fd(), Gfx::Bitmap::size_in_bytes(Gfx::Bitmap::minimum_pitch(size.width(), bitmap_format), size.height()));
if (buffer_or_error.is_error())
return false;
auto bitmap_or_error = Gfx::Bitmap::try_create_with_anonymous_buffer(bitmap_format, buffer_or_error.release_value(), size, scale, palette);
if (bitmap_or_error.is_error())
return false;
shareable_bitmap = Gfx::ShareableBitmap { bitmap_or_error.release_value(), Gfx::ShareableBitmap::ConstructWithKnownGoodBitmap };
return true;
auto buffer = TRY(Core::AnonymousBuffer::create_from_anon_fd(anon_file.take_fd(), Gfx::Bitmap::size_in_bytes(Gfx::Bitmap::minimum_pitch(size.width(), bitmap_format), size.height())));
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 {};
}
}

View file

@ -35,6 +35,6 @@ private:
namespace IPC {
bool encode(Encoder&, const Gfx::ShareableBitmap&);
bool decode(Decoder&, Gfx::ShareableBitmap&);
ErrorOr<void> decode(Decoder&, Gfx::ShareableBitmap&);
}

View file

@ -33,16 +33,14 @@ bool encode(Encoder& encoder, Gfx::IntSize const& size)
return true;
}
bool decode(Decoder& decoder, Gfx::IntSize& size)
ErrorOr<void> decode(Decoder& decoder, Gfx::IntSize& size)
{
int width = 0;
int height = 0;
if (!decoder.decode(width))
return false;
if (!decoder.decode(height))
return false;
TRY(decoder.decode(width));
TRY(decoder.decode(height));
size = { width, height };
return true;
return {};
}
}

View file

@ -189,6 +189,6 @@ struct Formatter<Gfx::Size<T>> : Formatter<StringView> {
namespace IPC {
bool encode(Encoder&, Gfx::IntSize const&);
bool decode(Decoder&, Gfx::IntSize&);
ErrorOr<void> decode(Decoder&, Gfx::IntSize&);
}