diff --git a/Userland/Libraries/LibIPC/Decoder.cpp b/Userland/Libraries/LibIPC/Decoder.cpp index 3337aa012d..04ef566e99 100644 --- a/Userland/Libraries/LibIPC/Decoder.cpp +++ b/Userland/Libraries/LibIPC/Decoder.cpp @@ -208,4 +208,10 @@ ErrorOr decode(Decoder& decoder, Core::ProxyData& data) return {}; } +// No-op. +ErrorOr Decoder::decode(AK::Empty&) +{ + return {}; +} + } diff --git a/Userland/Libraries/LibIPC/Decoder.h b/Userland/Libraries/LibIPC/Decoder.h index a20efa38d6..1cc3902a58 100644 --- a/Userland/Libraries/LibIPC/Decoder.h +++ b/Userland/Libraries/LibIPC/Decoder.h @@ -12,6 +12,8 @@ #include #include #include +#include +#include #include #include #include @@ -53,6 +55,7 @@ public: ErrorOr decode(URL&); ErrorOr decode(Dictionary&); ErrorOr decode(File&); + ErrorOr decode(AK::Empty&); template ErrorOr decode(HashMap& hashmap) { @@ -133,6 +136,18 @@ public: return {}; } + template + ErrorOr decode(Variant& variant) + { + typename AK::Variant::IndexType type_index; + TRY(decode(type_index)); + if (type_index >= sizeof...(VariantTypes)) + return Error::from_string_literal("IPC: Invalid variant index"); + + TRY((decode_variant<0, sizeof...(VariantTypes), VariantTypes...>(type_index, variant))); + return {}; + } + template ErrorOr decode(Optional& optional) { @@ -149,6 +164,22 @@ public: } private: + template + ErrorOr decode_variant(size_t index, Variant& variant) + { + if constexpr (CurrentIndex < Max) { + if (index == CurrentIndex) { + typename TypeList::template Type element; + TRY(decode(element)); + variant.set(move(element)); + return {}; + } + return decode_variant(index, variant); + } else { + VERIFY_NOT_REACHED(); + } + } + InputMemoryStream& m_stream; Core::Stream::LocalSocket& m_socket; }; diff --git a/Userland/Libraries/LibIPC/Encoder.cpp b/Userland/Libraries/LibIPC/Encoder.cpp index 1794915cdb..28121ef519 100644 --- a/Userland/Libraries/LibIPC/Encoder.cpp +++ b/Userland/Libraries/LibIPC/Encoder.cpp @@ -196,6 +196,12 @@ Encoder& Encoder::operator<<(File const& file) return *this; } +// No-op. +Encoder& Encoder::operator<<(AK::Empty const&) +{ + return *this; +} + template<> bool encode(Encoder& encoder, Core::AnonymousBuffer const& buffer) { diff --git a/Userland/Libraries/LibIPC/Encoder.h b/Userland/Libraries/LibIPC/Encoder.h index e1531c33fa..1d1696a4a0 100644 --- a/Userland/Libraries/LibIPC/Encoder.h +++ b/Userland/Libraries/LibIPC/Encoder.h @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -49,6 +50,7 @@ public: Encoder& operator<<(URL const&); Encoder& operator<<(Dictionary const&); Encoder& operator<<(File const&); + Encoder& operator<<(AK::Empty const&); template Encoder& operator<<(HashMap const& hashmap) { @@ -87,6 +89,17 @@ public: return *this; } + // Note: We require any encodeable variant to have Empty as its first variant, as only possibly-empty variants can be default constructed. + // The default constructability is required by generated IPC message marshalling code. + template + Encoder& operator<<(AK::Variant const& variant) + { + // Note: This might be either u8 or size_t depending on the size of the variant; both are encodeable. + *this << variant.index(); + variant.visit([this](auto const& underlying_value) { *this << underlying_value; }); + return *this; + } + template Encoder& operator<<(T const& enum_value) {