diff options
author | Timothy Flynn <trflynn89@pm.me> | 2022-12-22 20:40:33 -0500 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-12-26 09:36:16 +0100 |
commit | 9b483625e653d2ea89533c99b501a2e296c4d366 (patch) | |
tree | a84ddbd6a8bae04104dd9617e3b99e5e1f8e9c4a /Userland/Libraries/LibGfx | |
parent | 765c5b416f61f08dd68ea4b77a39e9380f2c8f9d (diff) | |
download | serenity-9b483625e653d2ea89533c99b501a2e296c4d366.zip |
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).
Diffstat (limited to 'Userland/Libraries/LibGfx')
-rw-r--r-- | Userland/Libraries/LibGfx/Color.cpp | 8 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/Color.h | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/Point.cpp | 11 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/Point.h | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/Rect.cpp | 11 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/Rect.h | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/ShareableBitmap.cpp | 39 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/ShareableBitmap.h | 3 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/Size.cpp | 11 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/Size.h | 2 |
10 files changed, 38 insertions, 53 deletions
diff --git a/Userland/Libraries/LibGfx/Color.cpp b/Userland/Libraries/LibGfx/Color.cpp index 2438ae3c7f..b8e8da487b 100644 --- a/Userland/Libraries/LibGfx/Color.cpp +++ b/Userland/Libraries/LibGfx/Color.cpp @@ -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) diff --git a/Userland/Libraries/LibGfx/Color.h b/Userland/Libraries/LibGfx/Color.h index 9458d97036..9fe5b7e5ab 100644 --- a/Userland/Libraries/LibGfx/Color.h +++ b/Userland/Libraries/LibGfx/Color.h @@ -577,6 +577,6 @@ template<> bool encode(Encoder&, Gfx::Color const&); template<> -ErrorOr<void> decode(Decoder&, Gfx::Color&); +ErrorOr<Gfx::Color> decode(Decoder&); } diff --git a/Userland/Libraries/LibGfx/Point.cpp b/Userland/Libraries/LibGfx/Point.cpp index f5a99bf82e..8dd484ca19 100644 --- a/Userland/Libraries/LibGfx/Point.cpp +++ b/Userland/Libraries/LibGfx/Point.cpp @@ -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 }; } } diff --git a/Userland/Libraries/LibGfx/Point.h b/Userland/Libraries/LibGfx/Point.h index 452bc605dc..ea601edc5e 100644 --- a/Userland/Libraries/LibGfx/Point.h +++ b/Userland/Libraries/LibGfx/Point.h @@ -295,7 +295,7 @@ template<> bool encode(Encoder&, Gfx::IntPoint const&); template<> -ErrorOr<void> decode(Decoder&, Gfx::IntPoint&); +ErrorOr<Gfx::IntPoint> decode(Decoder&); } diff --git a/Userland/Libraries/LibGfx/Rect.cpp b/Userland/Libraries/LibGfx/Rect.cpp index 6c85b50fac..68bbfd4ead 100644 --- a/Userland/Libraries/LibGfx/Rect.cpp +++ b/Userland/Libraries/LibGfx/Rect.cpp @@ -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 }; } } diff --git a/Userland/Libraries/LibGfx/Rect.h b/Userland/Libraries/LibGfx/Rect.h index a848b73edc..440f4bb885 100644 --- a/Userland/Libraries/LibGfx/Rect.h +++ b/Userland/Libraries/LibGfx/Rect.h @@ -1036,6 +1036,6 @@ template<> bool encode(Encoder&, Gfx::IntRect const&); template<> -ErrorOr<void> decode(Decoder&, Gfx::IntRect&); +ErrorOr<Gfx::IntRect> decode(Decoder&); } diff --git a/Userland/Libraries/LibGfx/ShareableBitmap.cpp b/Userland/Libraries/LibGfx/ShareableBitmap.cpp index 97bc55fd94..a741c3d7e5 100644 --- a/Userland/Libraries/LibGfx/ShareableBitmap.cpp +++ b/Userland/Libraries/LibGfx/ShareableBitmap.cpp @@ -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 }; } } diff --git a/Userland/Libraries/LibGfx/ShareableBitmap.h b/Userland/Libraries/LibGfx/ShareableBitmap.h index 26c074a297..16e8df9c13 100644 --- a/Userland/Libraries/LibGfx/ShareableBitmap.h +++ b/Userland/Libraries/LibGfx/ShareableBitmap.h @@ -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&); } diff --git a/Userland/Libraries/LibGfx/Size.cpp b/Userland/Libraries/LibGfx/Size.cpp index 718312d052..b737e7af15 100644 --- a/Userland/Libraries/LibGfx/Size.cpp +++ b/Userland/Libraries/LibGfx/Size.cpp @@ -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 }; } } diff --git a/Userland/Libraries/LibGfx/Size.h b/Userland/Libraries/LibGfx/Size.h index ec461de9c2..ff71e7d16a 100644 --- a/Userland/Libraries/LibGfx/Size.h +++ b/Userland/Libraries/LibGfx/Size.h @@ -218,6 +218,6 @@ template<> bool encode(Encoder&, Gfx::IntSize const&); template<> -ErrorOr<void> decode(Decoder&, Gfx::IntSize&); +ErrorOr<Gfx::IntSize> decode(Decoder&); } |