Skip to content

Commit

Permalink
Userland: Properly define IPC::encode and IPC::decode specializations
Browse files Browse the repository at this point in the history
In order to avoid the base encode/decode methods from being used (and
failing a static assertion), we must be sure to declare/define the
custom type implementations as template specializations.

After this, LibIPC is no longer sensitive to include order.
  • Loading branch information
trflynn89 committed Nov 15, 2022
1 parent b1ea418 commit 05f4138
Show file tree
Hide file tree
Showing 25 changed files with 75 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Userland/Libraries/LibCore/AnonymousBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ class AnonymousBuffer {

namespace IPC {

template<>
bool encode(Encoder&, Core::AnonymousBuffer const&);

template<>
ErrorOr<void> decode(Decoder&, Core::AnonymousBuffer&);

}
3 changes: 3 additions & 0 deletions Userland/Libraries/LibCore/DateTime.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ class DateTime {

namespace IPC {

template<>
bool encode(Encoder&, Core::DateTime const&);

template<>
ErrorOr<void> decode(Decoder&, Core::DateTime&);

}
5 changes: 5 additions & 0 deletions Userland/Libraries/LibCore/Proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ struct ProxyData {
}

namespace IPC {

template<>
bool encode(Encoder&, Core::ProxyData const&);

template<>
ErrorOr<void> decode(Decoder&, Core::ProxyData&);

}
2 changes: 2 additions & 0 deletions Userland/Libraries/LibDNS/Answer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,14 @@ ErrorOr<void> AK::Formatter<DNS::RecordClass>::format(AK::FormatBuilder& builder

namespace IPC {

template<>
bool encode(Encoder& encoder, DNS::Answer const& answer)
{
encoder << answer.name().as_string() << (u16)answer.type() << (u16)answer.class_code() << answer.ttl() << answer.record_data() << answer.mdns_cache_flush();
return true;
}

template<>
ErrorOr<void> decode(Decoder& decoder, DNS::Answer& answer)
{
String name;
Expand Down
3 changes: 3 additions & 0 deletions Userland/Libraries/LibDNS/Answer.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ struct AK::Formatter<DNS::RecordClass> : StandardFormatter {

namespace IPC {

template<>
bool encode(Encoder&, DNS::Answer const&);

template<>
ErrorOr<void> decode(Decoder&, DNS::Answer&);

}
2 changes: 2 additions & 0 deletions Userland/Libraries/LibGfx/Color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,12 +366,14 @@ Vector<Color> Color::tints(u32 steps, float max) const

}

template<>
bool IPC::encode(Encoder& encoder, Color const& color)
{
encoder << color.value();
return true;
}

template<>
ErrorOr<void> IPC::decode(Decoder& decoder, Color& color)
{
u32 rgba;
Expand Down
3 changes: 3 additions & 0 deletions Userland/Libraries/LibGfx/Color.h
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,10 @@ struct Formatter<Gfx::Color> : public Formatter<StringView> {

namespace IPC {

template<>
bool encode(Encoder&, Gfx::Color const&);

template<>
ErrorOr<void> decode(Decoder&, Gfx::Color&);

}
2 changes: 2 additions & 0 deletions Userland/Libraries/LibGfx/Point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ String FloatPoint::to_string() const

namespace IPC {

template<>
bool encode(Encoder& encoder, Gfx::IntPoint const& point)
{
encoder << point.x() << point.y();
return true;
}

template<>
ErrorOr<void> decode(Decoder& decoder, Gfx::IntPoint& point)
{
int x = 0;
Expand Down
3 changes: 3 additions & 0 deletions Userland/Libraries/LibGfx/Point.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,10 @@ struct Formatter<Gfx::Point<T>> : Formatter<FormatString> {

namespace IPC {

template<>
bool encode(Encoder&, Gfx::IntPoint const&);

template<>
ErrorOr<void> decode(Decoder&, Gfx::IntPoint&);

}
Expand Down
2 changes: 2 additions & 0 deletions Userland/Libraries/LibGfx/Rect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ String FloatRect::to_string() const

namespace IPC {

template<>
bool encode(Encoder& encoder, Gfx::IntRect const& rect)
{
encoder << rect.location() << rect.size();
return true;
}

template<>
ErrorOr<void> decode(Decoder& decoder, Gfx::IntRect& rect)
{
Gfx::IntPoint point;
Expand Down
3 changes: 3 additions & 0 deletions Userland/Libraries/LibGfx/Rect.h
Original file line number Diff line number Diff line change
Expand Up @@ -1031,7 +1031,10 @@ struct Formatter<Gfx::Rect<T>> : Formatter<FormatString> {

namespace IPC {

template<>
bool encode(Encoder&, Gfx::IntRect const&);

template<>
ErrorOr<void> decode(Decoder&, Gfx::IntRect&);

}
2 changes: 2 additions & 0 deletions Userland/Libraries/LibGfx/ShareableBitmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ ShareableBitmap::ShareableBitmap(NonnullRefPtr<Bitmap> bitmap, Tag)

namespace IPC {

template<>
bool encode(Encoder& encoder, Gfx::ShareableBitmap const& shareable_bitmap)
{
encoder << shareable_bitmap.is_valid();
Expand All @@ -39,6 +40,7 @@ bool encode(Encoder& encoder, Gfx::ShareableBitmap const& shareable_bitmap)
return true;
}

template<>
ErrorOr<void> decode(Decoder& decoder, Gfx::ShareableBitmap& shareable_bitmap)
{
bool valid = false;
Expand Down
3 changes: 3 additions & 0 deletions Userland/Libraries/LibGfx/ShareableBitmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ class ShareableBitmap {

namespace IPC {

template<>
bool encode(Encoder&, Gfx::ShareableBitmap const&);

template<>
ErrorOr<void> decode(Decoder&, Gfx::ShareableBitmap&);

}
2 changes: 2 additions & 0 deletions Userland/Libraries/LibGfx/Size.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ String FloatSize::to_string() const

namespace IPC {

template<>
bool encode(Encoder& encoder, Gfx::IntSize const& size)
{
encoder << size.width() << size.height();
return true;
}

template<>
ErrorOr<void> decode(Decoder& decoder, Gfx::IntSize& size)
{
int width = 0;
Expand Down
3 changes: 3 additions & 0 deletions Userland/Libraries/LibGfx/Size.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,10 @@ struct Formatter<Gfx::Size<T>> : Formatter<FormatString> {

namespace IPC {

template<>
bool encode(Encoder&, Gfx::IntSize const&);

template<>
ErrorOr<void> decode(Decoder&, Gfx::IntSize&);

}
3 changes: 3 additions & 0 deletions Userland/Libraries/LibIPC/Decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ ErrorOr<void> Decoder::decode([[maybe_unused]] File& file)
return {};
}

template<>
ErrorOr<void> decode(Decoder& decoder, Core::AnonymousBuffer& buffer)
{
bool valid;
Expand All @@ -187,6 +188,7 @@ ErrorOr<void> decode(Decoder& decoder, Core::AnonymousBuffer& buffer)
return {};
}

template<>
ErrorOr<void> decode(Decoder& decoder, Core::DateTime& datetime)
{
i64 timestamp;
Expand All @@ -195,6 +197,7 @@ ErrorOr<void> decode(Decoder& decoder, Core::DateTime& datetime)
return {};
}

template<>
ErrorOr<void> decode(Decoder& decoder, Core::ProxyData& data)
{
UnderlyingType<decltype(data.type)> type;
Expand Down
3 changes: 3 additions & 0 deletions Userland/Libraries/LibIPC/Encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ Encoder& Encoder::operator<<(File const& file)
return *this;
}

template<>
bool encode(Encoder& encoder, Core::AnonymousBuffer const& buffer)
{
encoder << buffer.is_valid();
Expand All @@ -206,12 +207,14 @@ bool encode(Encoder& encoder, Core::AnonymousBuffer const& buffer)
return true;
}

template<>
bool encode(Encoder& encoder, Core::DateTime const& datetime)
{
encoder << static_cast<i64>(datetime.timestamp());
return true;
}

template<>
bool encode(Encoder& encoder, Core::ProxyData const& proxy)
{
encoder << to_underlying(proxy.type);
Expand Down
2 changes: 2 additions & 0 deletions Userland/Libraries/LibWeb/Cookie/Cookie.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ SameSite same_site_from_string(StringView same_site_mode)

}

template<>
bool IPC::encode(Encoder& encoder, Web::Cookie::Cookie const& cookie)
{
encoder << cookie.name;
Expand All @@ -56,6 +57,7 @@ bool IPC::encode(Encoder& encoder, Web::Cookie::Cookie const& cookie)
return true;
}

template<>
ErrorOr<void> IPC::decode(Decoder& decoder, Web::Cookie::Cookie& cookie)
{
TRY(decoder.decode(cookie.name));
Expand Down
3 changes: 3 additions & 0 deletions Userland/Libraries/LibWeb/Cookie/Cookie.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ SameSite same_site_from_string(StringView same_site_mode);

namespace IPC {

template<>
bool encode(Encoder&, Web::Cookie::Cookie const&);

template<>
ErrorOr<void> decode(Decoder&, Web::Cookie::Cookie&);

}
2 changes: 2 additions & 0 deletions Userland/Libraries/LibWeb/Cookie/ParsedCookie.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ Optional<Core::DateTime> parse_date_time(StringView date_string)

}

template<>
bool IPC::encode(Encoder& encoder, Web::Cookie::ParsedCookie const& cookie)
{
encoder << cookie.name;
Expand All @@ -362,6 +363,7 @@ bool IPC::encode(Encoder& encoder, Web::Cookie::ParsedCookie const& cookie)
return true;
}

template<>
ErrorOr<void> IPC::decode(Decoder& decoder, Web::Cookie::ParsedCookie& cookie)
{
TRY(decoder.decode(cookie.name));
Expand Down
3 changes: 3 additions & 0 deletions Userland/Libraries/LibWeb/Cookie/ParsedCookie.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ Optional<ParsedCookie> parse_cookie(String const& cookie_string);

namespace IPC {

template<>
bool encode(Encoder&, Web::Cookie::ParsedCookie const&);

template<>
ErrorOr<void> decode(Decoder&, Web::Cookie::ParsedCookie&);

}
2 changes: 2 additions & 0 deletions Userland/Libraries/LibWeb/WebDriver/Response.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Response::Response(Error&& error)

}

template<>
bool IPC::encode(Encoder& encoder, Web::WebDriver::Response const& response)
{
response.visit(
Expand All @@ -46,6 +47,7 @@ bool IPC::encode(Encoder& encoder, Web::WebDriver::Response const& response)
return true;
}

template<>
ErrorOr<void> IPC::decode(Decoder& decoder, Web::WebDriver::Response& response)
{
ResponseType type {};
Expand Down
3 changes: 3 additions & 0 deletions Userland/Libraries/LibWeb/WebDriver/Response.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ struct Response {

namespace IPC {

template<>
bool encode(Encoder&, Web::WebDriver::Response const&);

template<>
ErrorOr<void> decode(Decoder&, Web::WebDriver::Response&);

}
7 changes: 7 additions & 0 deletions Userland/Services/WindowServer/ScreenLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,16 @@ class ScreenLayout {

namespace IPC {

template<>
bool encode(Encoder&, WindowServer::ScreenLayout::Screen const&);

template<>
ErrorOr<void> decode(Decoder&, WindowServer::ScreenLayout::Screen&);

template<>
bool encode(Encoder&, WindowServer::ScreenLayout const&);

template<>
ErrorOr<void> decode(Decoder&, WindowServer::ScreenLayout&);

}
8 changes: 6 additions & 2 deletions Userland/Services/WindowServer/ScreenLayout.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -394,12 +394,14 @@ bool ScreenLayout::try_auto_add_display_connector(String const& device_path)

namespace IPC {

bool encode(Encoder& encoder, const WindowServer::ScreenLayout::Screen& screen)
template<>
bool encode(Encoder& encoder, WindowServer::ScreenLayout::Screen const& screen)
{
encoder << screen.mode << screen.device << screen.location << screen.resolution << screen.scale_factor;
return true;
}

template<>
ErrorOr<void> decode(Decoder& decoder, WindowServer::ScreenLayout::Screen& screen)
{
WindowServer::ScreenLayout::Screen::Mode mode;
Expand All @@ -416,12 +418,14 @@ ErrorOr<void> decode(Decoder& decoder, WindowServer::ScreenLayout::Screen& scree
return {};
}

bool encode(Encoder& encoder, const WindowServer::ScreenLayout& screen_layout)
template<>
bool encode(Encoder& encoder, WindowServer::ScreenLayout const& screen_layout)
{
encoder << screen_layout.screens << screen_layout.main_screen_index;
return true;
}

template<>
ErrorOr<void> decode(Decoder& decoder, WindowServer::ScreenLayout& screen_layout)
{
Vector<WindowServer::ScreenLayout::Screen> screens;
Expand Down

0 comments on commit 05f4138

Please sign in to comment.